包package
agentscope.messageagentscope.message
module agentscope.message
agentscope.message 属于消息·事件·格式化,统一消息模型与协议转换。 该包共收录 17 个公开类型。
The message module in agentscope.
源码Source
agentscope/message/__init__.py:1类Classes
Base64Source类class | base64 来源。 The base64 source. |
DataBlock类class | 数据块,在智能体、模型与前端之间传递结构化消息和事件。 The data block for binary content (images, audio, video, etc.). |
HintBlock类class | Hint 块,在智能体、模型与前端之间传递结构化消息和事件。 A block used to provide instructions or hints to the LLM during the reasoning-acting loop. When passed to the LLM API, the hint block is converted into a user message. |
Msg类class | 统一消息对象 The message class in AgentScope, responsible for information storage and transmission among different agents. |
TextBlock类class | 文本块。 The text block. |
ThinkingBlock类class | Thinking 块,在智能体、模型与前端之间传递结构化消息和事件。 The thinking block. |
ToolCallBlock类class | 工具调用块。 The tool call block. |
ToolCallState类class | 工具调用的状态。 The state of the tool call. |
ToolResultBlock类class | 工具结果块。 The tool result block. |
ToolResultState类class | 工具结果状态。 The tool result state. |
URLSource类class | URL 来源。 The URL source. |
Usage类class | 用量,在智能体、模型与前端之间传递结构化消息和事件。 The token usage information of a message. |
函数Functions
AssistantMsg函数function | 构造助手消息 Create an assistant message with role ``"assistant"``. |
SystemMsg函数function | 构造系统消息 Create a system message with role ``"system"``. |
UserMsg函数function | 构造用户消息 Create a user message with role ``"user"``. |
属性Attributes
ContentBlock字段attribute | — |
ContentBlockTypes字段attribute | — |
函数详情Function Details
AssistantMsg
def AssistantMsg(name: str, content: str | list[ContentBlock], metadata: dict | None = None, created_at: str | None = None, finished_at: str | None = None, finished_reason: ReplyFinishedReason | None = None, structured_output: dict | None = None, id: str | None = None, usage: Usage | None = None) -> Msg
创建角色为 assistant 的消息,内容可以是文本或多模态内容块。
Create an assistant message with role "assistant".
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | str | 名称(类型 `str`)。The name of the sender. |
content | str | list[ContentBlock] | 内容(类型 `str | list[ContentBlock]`)。The message content. A plain string will be automatically wrapped in a :class:`TextBlock`. Any :class:`ContentBlock` subtype is permitted for assistant messages. |
metadata默认default None | dict | None | 元数据(类型 `dict | None`)。Arbitrary key-value metadata attached to the message. Defaults to an empty dict when not provided. |
created_at默认default None | str | None | 已创建 at(类型 `str | None`)。ISO-format timestamp for when the message was created. Defaults to the current time when not provided. |
finished_at默认default None | str | None | finished at(类型 `str | None`)。ISO-format timestamp for when the message was finished. Not set by default for assistant messages. |
finished_reason默认default None | ReplyFinishedReason | None | finished 推理(类型 `ReplyFinishedReason | None`)。The finished reason for the assistant message. |
structured_output默认default None | dict | None | 结构化输出(类型 `dict | None`)。The structured output carried by the assistant message. |
id默认default None | str | None | 标识(类型 `str | None`)。A unique identifier for the message. A random UUID hex string is generated when not provided. |
usage默认default None | Usage | None | 用量(类型 `Usage | None`)。The token usage information of the message. |
Msg
返回消息。`Msg`:
源码Source
agentscope/message/_base.py:622–676SystemMsg
def SystemMsg(name: str, content: str | list[TextBlock], metadata: dict | None = None, created_at: str | None = None, finished_at: str | None = None, finished_reason: ReplyFinishedReason | None = None, id: str | None = None) -> Msg
创建角色为 system 的消息,用于注入系统提示词与全局约束。
Create a system message with role "system".
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | str | 名称(类型 `str`)。The name of the sender. |
content | str | list[TextBlock] | 内容(类型 `str | list[TextBlock]`)。The message content. A plain string will be automatically wrapped in a :class:`TextBlock`. Only :class:`TextBlock` is allowed for system messages. |
metadata默认default None | dict | None | 元数据(类型 `dict | None`)。Arbitrary key-value metadata attached to the message. Defaults to an empty dict when not provided. |
created_at默认default None | str | None | 已创建 at(类型 `str | None`)。ISO-format timestamp for when the message was created. Defaults to the current time when not provided. |
finished_at默认default None | str | None | finished at(类型 `str | None`)。ISO-format timestamp for when the message was finished. Defaults to the same value as `created_at` when not provided. |
finished_reason默认default None | ReplyFinishedReason | None | finished 推理(类型 `ReplyFinishedReason | None`)。The finished reason for the system message. |
id默认default None | str | None | 标识(类型 `str | None`)。A unique identifier for the message. A random UUID hex string is generated when not provided. |
Msg
返回消息。`Msg`:
源码Source
agentscope/message/_base.py:679–728UserMsg
def UserMsg(name: str, content: str | list[TextBlock | DataBlock], metadata: dict | None = None, created_at: str | None = None, finished_at: str | None = None, finished_reason: ReplyFinishedReason | None = None, id: str | None = None) -> Msg
创建角色为 user 的消息,用于承载用户输入与多模态内容。
Create a user message with role "user".
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | str | 名称(类型 `str`)。The name of the sender. |
content | str | list[TextBlock | DataBlock] | 内容(类型 `str | list[TextBlock | DataBlock]`)。The message content. A plain string will be automatically wrapped in a :class:`TextBlock`. Only :class:`TextBlock` and :class:`DataBlock` are allowed for user messages. |
metadata默认default None | dict | None | 元数据(类型 `dict | None`)。Arbitrary key-value metadata attached to the message. Defaults to an empty dict when not provided. |
created_at默认default None | str | None | 已创建 at(类型 `str | None`)。ISO-format timestamp for when the message was created. Defaults to the current time when not provided. |
finished_at默认default None | str | None | finished at(类型 `str | None`)。ISO-format timestamp for when the message was finished. Defaults to the same value as `created_at` when not provided. |
finished_reason默认default None | ReplyFinishedReason | None | finished 推理(类型 `ReplyFinishedReason | None`)。The reason the message was finished. Defaults to `None` when not provided. |
id默认default None | str | None | 标识(类型 `str | None`)。A unique identifier for the message. A random UUID hex string is generated when not provided. |
Msg
返回消息。`Msg`:
源码Source
agentscope/message/_base.py:569–619属性详情Attribute Details
ContentBlock
TypeAlias = TextBlock | ThinkingBlock | HintBlock | ToolCallBlock | ToolResultBlock | DataBlock内容块字段。声明为 TypeAlias。
The content block field.
源码Source
agentscope/message/_block.py:219ContentBlockTypes
TypeAlias = Literal['text', 'thinking', 'hint', 'tool_call', 'tool_result', 'data']内容块类型字段。声明为 TypeAlias。
The content block types field.
源码Source
agentscope/message/_block.py:228