autogen_agentchat.messages#
该模块定义了用于 agent 之间通信的各种消息类型。 每种消息类型都继承自 BaseChatMessage 类或 BaseAgentEvent 类,并且包含与发送的消息类型相关的特定字段。
- AgentEvent#
BaseAgentEvent
的所有内置具体子类的联合类型。别名为
Annotated
[ToolCallRequestEvent
|ToolCallExecutionEvent
|MemoryQueryEvent
|UserInputRequestedEvent
|ModelClientStreamingChunkEvent
|ThoughtEvent
|SelectSpeakerEvent
|CodeGenerationEvent
|CodeExecutionEvent
, FieldInfo(annotation=NoneType, required=True, discriminator='type')]
- pydantic 模型 BaseAgentEvent[源代码]#
基类:
BaseMessage
,ABC
agent 事件的基类。
注意
如果您想创建一个新的消息类型来向用户和应用程序发送可观察事件的信号,请从此类继承。
Agent 事件用于向用户和应用程序发送 agent 和团队产生的操作和想法的信号。 它们不用于 agent 之间的通信,也不期望被其他 agent 处理。
如果您想提供内容的自定义呈现,您应该覆盖
to_text()
方法。显示 JSON 模式
{ "title": "BaseAgentEvent", "description": "Base class for agent events.\n\n.. note::\n\n If you want to create a new message type for signaling observable events\n to user and application, inherit from this class.\n\nAgent events are used to signal actions and thoughts produced by agents\nand teams to user and applications. They are not used for agent-to-agent\ncommunication and are not expected to be processed by other agents.\n\nYou should override the :meth:`to_text` method if you want to provide\na custom rendering of the content.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source" ] }
- 字段:
- 字段 models_usage: RequestUsage | None = None#
生成此消息时产生的模型客户端使用情况。
- pydantic 模型 BaseChatMessage[源代码]#
基类:
BaseMessage
,ABC
聊天消息的抽象基类。
注意
如果您想创建一个用于 agent 之间通信的新消息类型,请从此类继承,或者如果您的内容类型是 Pydantic BaseModel 的子类,则只需使用
StructuredMessage
。此类用于在聊天对话中的 agent 之间发送的消息。 代理应使用模型处理消息的内容,并将响应作为另一个
BaseChatMessage
返回。显示 JSON 模式
{ "title": "BaseChatMessage", "description": "Abstract base class for chat messages.\n\n.. note::\n\n If you want to create a new message type that is used for agent-to-agent\n communication, inherit from this class, or simply use\n :class:`StructuredMessage` if your content type is a subclass of\n Pydantic BaseModel.\n\nThis class is used for messages that are sent between agents in a chat\nconversation. Agents are expected to process the content of the\nmessage using models and return a response as another :class:`BaseChatMessage`.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source" ] }
- 字段:
- 字段 models_usage: RequestUsage | None = None#
生成此消息时产生的模型客户端使用情况。
- abstract to_model_message() UserMessage [源代码]#
将消息内容转换为
UserMessage
以便与模型客户端一起使用,例如,ChatCompletionClient
。
- abstract to_model_text() str [source]#
将消息的内容转换为纯文本表示形式。 这用于为模型创建纯文本内容。
这不用于在控制台中呈现消息。为此,请使用
to_text()
。此方法与
to_model_message()
之间的区别在于,此方法用于构造模型客户端的消息的一部分,而to_model_message()
用于为模型客户端创建完整的消息。
- pydantic model BaseMessage[source]#
基类:
BaseModel
,ABC
AgentChat中所有消息类型的抽象基类。
警告
如果要创建新的消息类型,请不要从此类继承。相反,请从
BaseChatMessage
或BaseAgentEvent
继承,以明确消息类型的目的。显示 JSON 模式
{ "title": "BaseMessage", "description": "Abstract base class for all message types in AgentChat.\n\n.. warning::\n\n If you want to create a new message type, do not inherit from this class.\n Instead, inherit from :class:`BaseChatMessage` or :class:`BaseAgentEvent`\n to clarify the purpose of the message type.", "type": "object", "properties": {} }
- dump() Mapping[str, Any] [source]#
将消息转换为JSON可序列化的字典。
默认实现使用Pydantic模型的
model_dump()
方法将消息转换为字典。 如果要自定义序列化过程或向输出添加其他字段,请覆盖此方法。
- classmethod load(data: Mapping[str, Any]) Self [source]#
从JSON可序列化数据的字典创建消息。
默认实现使用Pydantic模型的
model_validate()
方法从数据创建消息。 如果要自定义反序列化过程或向输入数据添加其他字段,请覆盖此方法。
- abstract to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic model BaseTextChatMessage[source]#
基类:
BaseChatMessage
,ABC
所有纯文本
BaseChatMessage
类型的基类。它具有to_text()
,to_model_text()
和to_model_message()
方法的实现。如果您的消息内容类型是字符串,请从此类继承。
显示 JSON 模式
{ "title": "BaseTextChatMessage", "description": "Base class for all text-only :class:`BaseChatMessage` types.\nIt has implementations for :meth:`to_text`, :meth:`to_model_text`,\nand :meth:`to_model_message` methods.\n\nInherit from this class if your message content type is a string.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- 字段:
- to_model_message() UserMessage [source]#
将消息内容转换为
UserMessage
以便与模型客户端一起使用,例如,ChatCompletionClient
。
- to_model_text() str [source]#
将消息的内容转换为纯文本表示形式。 这用于为模型创建纯文本内容。
这不用于在控制台中呈现消息。为此,请使用
to_text()
。此方法与
to_model_message()
之间的区别在于,此方法用于构造模型客户端的消息的一部分,而to_model_message()
用于为模型客户端创建完整的消息。
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- ChatMessage#
所有内置
BaseChatMessage
的具体子类的联合类型。 它不包括StructuredMessage
类型。是
Annotated
的别名,类型为 [TextMessage
|MultiModalMessage
|StopMessage
|ToolCallSummaryMessage
|HandoffMessage
, FieldInfo(annotation=NoneType, required=True, discriminator='type')]
- pydantic 模型 CodeExecutionEvent[源代码]#
基类:
BaseAgentEvent
表示代码执行事件的事件。
显示 JSON 模式
{ "title": "CodeExecutionEvent", "description": "An event signaling code execution event.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "retry_attempt": { "title": "Retry Attempt", "type": "integer" }, "result": { "$ref": "#/$defs/CodeResult" }, "type": { "const": "CodeExecutionEvent", "default": "CodeExecutionEvent", "title": "Type", "type": "string" } }, "$defs": { "CodeResult": { "properties": { "exit_code": { "title": "Exit Code", "type": "integer" }, "output": { "title": "Output", "type": "string" } }, "required": [ "exit_code", "output" ], "title": "CodeResult", "type": "object" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "retry_attempt", "result" ] }
- 字段:
- field result: CodeResult [必需]#
代码执行结果
- to_text() str [源代码]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic 模型 CodeGenerationEvent[源代码]#
基类:
BaseAgentEvent
表示代码生成事件的事件。
显示 JSON 模式
{ "title": "CodeGenerationEvent", "description": "An event signaling code generation event.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "retry_attempt": { "title": "Retry Attempt", "type": "integer" }, "content": { "title": "Content", "type": "string" }, "code_blocks": { "items": { "$ref": "#/$defs/CodeBlock" }, "title": "Code Blocks", "type": "array" }, "type": { "const": "CodeGenerationEvent", "default": "CodeGenerationEvent", "title": "Type", "type": "string" } }, "$defs": { "CodeBlock": { "properties": { "code": { "title": "Code", "type": "string" }, "language": { "title": "Language", "type": "string" } }, "required": [ "code", "language" ], "title": "CodeBlock", "type": "object" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "retry_attempt", "content", "code_blocks" ] }
- 字段:
- to_text() str [源代码]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic 模型 HandoffMessage[源代码]#
-
一个请求将对话移交给另一个代理的消息。
显示 JSON 模式
{ "title": "HandoffMessage", "description": "A message requesting handoff of a conversation to another agent.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" }, "target": { "title": "Target", "type": "string" }, "context": { "default": [], "items": { "discriminator": { "mapping": { "AssistantMessage": "#/$defs/AssistantMessage", "FunctionExecutionResultMessage": "#/$defs/FunctionExecutionResultMessage", "SystemMessage": "#/$defs/SystemMessage", "UserMessage": "#/$defs/UserMessage" }, "propertyName": "type" }, "oneOf": [ { "$ref": "#/$defs/SystemMessage" }, { "$ref": "#/$defs/UserMessage" }, { "$ref": "#/$defs/AssistantMessage" }, { "$ref": "#/$defs/FunctionExecutionResultMessage" } ] }, "title": "Context", "type": "array" }, "type": { "const": "HandoffMessage", "default": "HandoffMessage", "title": "Type", "type": "string" } }, "$defs": { "AssistantMessage": { "description": "Assistant message are sampled from the language model.", "properties": { "content": { "anyOf": [ { "type": "string" }, { "items": { "$ref": "#/$defs/FunctionCall" }, "type": "array" } ], "title": "Content" }, "thought": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "title": "Thought" }, "source": { "title": "Source", "type": "string" }, "type": { "const": "AssistantMessage", "default": "AssistantMessage", "title": "Type", "type": "string" } }, "required": [ "content", "source" ], "title": "AssistantMessage", "type": "object" }, "FunctionCall": { "properties": { "id": { "title": "Id", "type": "string" }, "arguments": { "title": "Arguments", "type": "string" }, "name": { "title": "Name", "type": "string" } }, "required": [ "id", "arguments", "name" ], "title": "FunctionCall", "type": "object" }, "FunctionExecutionResult": { "description": "Function execution result contains the output of a function call.", "properties": { "content": { "title": "Content", "type": "string" }, "name": { "title": "Name", "type": "string" }, "call_id": { "title": "Call Id", "type": "string" }, "is_error": { "anyOf": [ { "type": "boolean" }, { "type": "null" } ], "default": null, "title": "Is Error" } }, "required": [ "content", "name", "call_id" ], "title": "FunctionExecutionResult", "type": "object" }, "FunctionExecutionResultMessage": { "description": "Function execution result message contains the output of multiple function calls.", "properties": { "content": { "items": { "$ref": "#/$defs/FunctionExecutionResult" }, "title": "Content", "type": "array" }, "type": { "const": "FunctionExecutionResultMessage", "default": "FunctionExecutionResultMessage", "title": "Type", "type": "string" } }, "required": [ "content" ], "title": "FunctionExecutionResultMessage", "type": "object" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" }, "SystemMessage": { "description": "System message contains instructions for the model coming from the developer.\n\n.. note::\n\n Open AI is moving away from using 'system' role in favor of 'developer' role.\n See `Model Spec <https://cdn.openai.com/spec/model-spec-2024-05-08.html#definitions>`_ for more details.\n However, the 'system' role is still allowed in their API and will be automatically converted to 'developer' role\n on the server side.\n So, you can use `SystemMessage` for developer messages.", "properties": { "content": { "title": "Content", "type": "string" }, "type": { "const": "SystemMessage", "default": "SystemMessage", "title": "Type", "type": "string" } }, "required": [ "content" ], "title": "SystemMessage", "type": "object" }, "UserMessage": { "description": "User message contains input from end users, or a catch-all for data provided to the model.", "properties": { "content": { "anyOf": [ { "type": "string" }, { "items": { "anyOf": [ { "type": "string" }, {} ] }, "type": "array" } ], "title": "Content" }, "source": { "title": "Source", "type": "string" }, "type": { "const": "UserMessage", "default": "UserMessage", "title": "Type", "type": "string" } }, "required": [ "content", "source" ], "title": "UserMessage", "type": "object" } }, "required": [ "source", "content", "target" ] }
- 字段:
- field context: List[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] = []#
要传递给目标代理的模型上下文。
- pydantic 模型 MemoryQueryEvent[源代码]#
基类:
BaseAgentEvent
表示内存查询结果的事件。
显示 JSON 模式
{ "title": "MemoryQueryEvent", "description": "An event signaling the results of memory queries.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "items": { "$ref": "#/$defs/MemoryContent" }, "title": "Content", "type": "array" }, "type": { "const": "MemoryQueryEvent", "default": "MemoryQueryEvent", "title": "Type", "type": "string" } }, "$defs": { "MemoryContent": { "description": "A memory content item.", "properties": { "content": { "anyOf": [ { "type": "string" }, { "format": "binary", "type": "string" }, { "type": "object" }, {} ], "title": "Content" }, "mime_type": { "anyOf": [ { "$ref": "#/$defs/MemoryMimeType" }, { "type": "string" } ], "title": "Mime Type" }, "metadata": { "anyOf": [ { "type": "object" }, { "type": "null" } ], "default": null, "title": "Metadata" } }, "required": [ "content", "mime_type" ], "title": "MemoryContent", "type": "object" }, "MemoryMimeType": { "description": "Supported MIME types for memory content.", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- 字段:
- field content: List[MemoryContent] [必需]#
内存查询结果。
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic 模型 ModelClientStreamingChunkEvent[source]#
基类:
BaseAgentEvent
一个事件,表示来自模型客户端在流式模式下的文本输出块。
显示 JSON 模式
{ "title": "ModelClientStreamingChunkEvent", "description": "An event signaling a text output chunk from a model client in streaming mode.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" }, "type": { "const": "ModelClientStreamingChunkEvent", "default": "ModelClientStreamingChunkEvent", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic 模型 MultiModalMessage[source]#
基类:
BaseChatMessage
一个多模态消息。
显示 JSON 模式
{ "title": "MultiModalMessage", "description": "A multimodal message.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "items": { "anyOf": [ { "type": "string" }, {} ] }, "title": "Content", "type": "array" }, "type": { "const": "MultiModalMessage", "default": "MultiModalMessage", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- to_model_message() UserMessage [source]#
将消息内容转换为
UserMessage
以便与模型客户端一起使用,例如,ChatCompletionClient
。
- to_model_text(image_placeholder: str | None = '[image]') str [source]#
将消息的内容转换为纯字符串表示形式。 如果存在图像,默认情况下将替换为图像占位符,否则设置为 None 时将是 base64 字符串。
- to_text(iterm: bool = False) str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请使用to_model_text()
代替。
- pydantic 模型 SelectSpeakerEvent[source]#
基类:
BaseAgentEvent
一个事件,表示选择对话的发言者。
显示 JSON 模式
{ "title": "SelectSpeakerEvent", "description": "An event signaling the selection of speakers for a conversation.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "items": { "type": "string" }, "title": "Content", "type": "array" }, "type": { "const": "SelectSpeakerEvent", "default": "SelectSpeakerEvent", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic 模型 StopMessage[source]#
-
一个请求停止对话的消息。
显示 JSON 模式
{ "title": "StopMessage", "description": "A message requesting stop of a conversation.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" }, "type": { "const": "StopMessage", "default": "StopMessage", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- class StructuredContentType#
用于结构化内容类型的类型变量。
TypeVar 的别名('StructuredContentType',bound=
BaseModel
, covariant=True)
- pydantic 模型 StructuredMessage[source]#
基类:
BaseChatMessage
,Generic
[StructuredContentType
]一种具有未指定内容类型的
BaseChatMessage
类型。要创建新的结构化消息类型,请将内容类型指定为 Pydantic BaseModel 的子类。
from pydantic import BaseModel from autogen_agentchat.messages import StructuredMessage class MyMessageContent(BaseModel): text: str number: int message = StructuredMessage[MyMessageContent]( content=MyMessageContent(text="Hello", number=42), source="agent1", ) print(message.to_text()) # {"text": "Hello", "number": 42}
from pydantic import BaseModel from autogen_agentchat.messages import StructuredMessage class MyMessageContent(BaseModel): text: str number: int message = StructuredMessage[MyMessageContent]( content=MyMessageContent(text="Hello", number=42), source="agent", format_string="Hello, {text} {number}!", ) print(message.to_text()) # Hello, agent 42!
显示 JSON 模式
{ "title": "StructuredMessage", "description": "A :class:`BaseChatMessage` type with an unspecified content type.\n\nTo create a new structured message type, specify the content type\nas a subclass of `Pydantic BaseModel <https://docs.pydantic.org.cn/latest/concepts/models/>`_.\n\n.. code-block:: python\n\n from pydantic import BaseModel\n from autogen_agentchat.messages import StructuredMessage\n\n\n class MyMessageContent(BaseModel):\n text: str\n number: int\n\n\n message = StructuredMessage[MyMessageContent](\n content=MyMessageContent(text=\"Hello\", number=42),\n source=\"agent1\",\n )\n\n print(message.to_text()) # {\"text\": \"Hello\", \"number\": 42}\n\n.. code-block:: python\n\n from pydantic import BaseModel\n from autogen_agentchat.messages import StructuredMessage\n\n\n class MyMessageContent(BaseModel):\n text: str\n number: int\n\n\n message = StructuredMessage[MyMessageContent](\n content=MyMessageContent(text=\"Hello\", number=42),\n source=\"agent\",\n format_string=\"Hello, {text} {number}!\",\n )\n\n print(message.to_text()) # Hello, agent 42!", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "$ref": "#/$defs/BaseModel" }, "format_string": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "title": "Format String" } }, "$defs": { "BaseModel": { "properties": {}, "title": "BaseModel", "type": "object" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- field content: StructuredContentType [Required]#
消息的内容。必须是 Pydantic BaseModel 的子类。
- field format_string: str | None = None#
(实验性)一个可选的格式字符串,用于将内容渲染为人类可读的格式。该格式字符串可以使用内容模型的字段作为占位符。例如,如果内容模型有一个名为 name 的字段,则可以在格式字符串中使用 {name} 来包含该字段的值。该格式字符串在
to_text()
方法中使用,以创建消息的人类可读表示形式。此设置是实验性的,将来会发生变化。
- to_model_message() UserMessage [source]#
将消息内容转换为
UserMessage
以便与模型客户端一起使用,例如,ChatCompletionClient
。
- to_model_text() str [source]#
将消息的内容转换为纯文本表示形式。 这用于为模型创建纯文本内容。
这不用于在控制台中呈现消息。为此,请使用
to_text()
。此方法与
to_model_message()
的区别在于,此方法用于构造模型客户端的消息部分,而to_model_message()
用于为模型客户端创建完整的消息。
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件进行检查。这不用于为模型创建纯文本内容。对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic model TextMessage[source]#
-
仅包含字符串内容的文本消息。
显示 JSON 模式
{ "title": "TextMessage", "description": "A text message with string-only content.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" }, "type": { "const": "TextMessage", "default": "TextMessage", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- pydantic model ThoughtEvent[source]#
基类:
BaseAgentEvent
一个表示模型思维过程的事件。它用于传达推理模型生成的推理 token,或函数调用生成的额外文本内容。
显示 JSON 模式
{ "title": "ThoughtEvent", "description": "An event signaling the thought process of a model.\nIt is used to communicate the reasoning tokens generated by a reasoning model,\nor the extra text content generated by a function call.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" }, "type": { "const": "ThoughtEvent", "default": "ThoughtEvent", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic model ToolCallExecutionEvent[source]#
基类:
BaseAgentEvent
一个表示工具调用执行的事件。
显示 JSON 模式
{ "title": "ToolCallExecutionEvent", "description": "An event signaling the execution of tool calls.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "items": { "$ref": "#/$defs/FunctionExecutionResult" }, "title": "Content", "type": "array" }, "type": { "const": "ToolCallExecutionEvent", "default": "ToolCallExecutionEvent", "title": "Type", "type": "string" } }, "$defs": { "FunctionExecutionResult": { "description": "Function execution result contains the output of a function call.", "properties": { "content": { "title": "Content", "type": "string" }, "name": { "title": "Name", "type": "string" }, "call_id": { "title": "Call Id", "type": "string" }, "is_error": { "anyOf": [ { "type": "boolean" }, { "type": "null" } ], "default": null, "title": "Is Error" } }, "required": [ "content", "name", "call_id" ], "title": "FunctionExecutionResult", "type": "object" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- 字段:
- field content: List[FunctionExecutionResult] [Required]#
工具调用结果。
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic model ToolCallRequestEvent[source]#
基类:
BaseAgentEvent
一个表示请求使用工具的事件。
显示 JSON 模式
{ "title": "ToolCallRequestEvent", "description": "An event signaling a request to use tools.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "items": { "$ref": "#/$defs/FunctionCall" }, "title": "Content", "type": "array" }, "type": { "const": "ToolCallRequestEvent", "default": "ToolCallRequestEvent", "title": "Type", "type": "string" } }, "$defs": { "FunctionCall": { "properties": { "id": { "title": "Id", "type": "string" }, "arguments": { "title": "Arguments", "type": "string" }, "name": { "title": "Name", "type": "string" } }, "required": [ "id", "arguments", "name" ], "title": "FunctionCall", "type": "object" }, "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- field content: List[FunctionCall] [Required]#
工具调用。
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。
- pydantic 模型 ToolCallSummaryMessage[source]#
-
表示工具调用结果摘要的消息。
显示 JSON 模式
{ "title": "ToolCallSummaryMessage", "description": "A message signaling the summary of tool call results.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "content": { "title": "Content", "type": "string" }, "type": { "const": "ToolCallSummaryMessage", "default": "ToolCallSummaryMessage", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "content" ] }
- pydantic 模型 UserInputRequestedEvent[source]#
基类:
BaseAgentEvent
一个事件,表示用户代理已请求用户输入。 在调用输入回调之前发布。
显示 JSON 模式
{ "title": "UserInputRequestedEvent", "description": "An event signaling a that the user proxy has requested user input. Published prior to invoking the input callback.", "type": "object", "properties": { "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "request_id": { "title": "Request Id", "type": "string" }, "content": { "const": "", "default": "", "title": "Content", "type": "string" }, "type": { "const": "UserInputRequestedEvent", "default": "UserInputRequestedEvent", "title": "Type", "type": "string" } }, "$defs": { "RequestUsage": { "properties": { "prompt_tokens": { "title": "Prompt Tokens", "type": "integer" }, "completion_tokens": { "title": "Completion Tokens", "type": "integer" } }, "required": [ "prompt_tokens", "completion_tokens" ], "title": "RequestUsage", "type": "object" } }, "required": [ "source", "request_id" ] }
- to_text() str [source]#
将消息内容转换为纯字符串表示形式,该表示形式可以在控制台中呈现,并由用户或条件检查。 这不用于为模型创建纯文本内容。 对于
BaseChatMessage
类型,请改用to_model_text()
。