一条对话消息,就像聊天记录里的一条。
| 30 | # - content(内容): 说了什么? |
| 31 | |
| 32 | class Message: |
| 33 | """一条对话消息,就像聊天记录里的一条。""" |
| 34 | |
| 35 | def __init__(self, role: str, content: str): |
| 36 | """ |
| 37 | 参数: |
| 38 | role: 谁说的。可选值: |
| 39 | "user" = 用户(你) |
| 40 | "assistant" = AI 助手(Claude) |
| 41 | "tool" = 工具返回的结果 |
| 42 | content: 说了什么(文字内容) |
| 43 | """ |
| 44 | self.role = role |
| 45 | self.content = content |
| 46 | |
| 47 | def __repr__(self): |
| 48 | # 方便打印查看 |
| 49 | preview = self.content[:50] + "..." if len(self.content) > 50 else self.content |
| 50 | return f"Message(role={self.role!r}, content={preview!r})" |
| 51 | |
| 52 | |
| 53 | # ============================================================ |