autogen_agentchat.base#
- class ChatAgent(*args, **kwargs)[source]#
基类:
ABC,TaskRunner,ComponentBase[BaseModel]聊天代理协议。
- component_type: ClassVar[ComponentType] = 'agent'#
组件的逻辑类型。
- abstract property produced_message_types: Sequence[type[BaseChatMessage]]#
代理在
Response.chat_message字段中生成的邮件类型。它们必须是BaseChatMessage类型。
- abstract async on_messages(messages: Sequence[BaseChatMessage], cancellation_token: CancellationToken) Response[source]#
处理传入消息并返回响应。
- abstract async on_messages_stream(messages: Sequence[BaseChatMessage], cancellation_token: CancellationToken) AsyncGenerator[BaseAgentEvent | BaseChatMessage | Response, None][source]#
处理传入消息并返回内部消息流,最终项是响应。
- abstract async on_reset(cancellation_token: CancellationToken) None[source]#
将代理重置为其初始化状态。
- abstract async on_pause(cancellation_token: CancellationToken) None[source]#
当代理暂停时调用。当调用此方法时,代理可能正在
on_messages()或on_messages_stream()中运行。
- abstract async on_resume(cancellation_token: CancellationToken) None[source]#
当代理恢复时调用。当调用此方法时,代理可能正在
on_messages()或on_messages_stream()中运行。
- class Response(*, chat_message: Annotated[BaseChatMessage, SerializeAsAny()], inner_messages: Sequence[Annotated[BaseAgentEvent | BaseChatMessage, SerializeAsAny()]] | None = None)[source]#
基类:
object调用
ChatAgent.on_messages()后的响应。- chat_message: Annotated[BaseChatMessage, SerializeAsAny()]#
代理生成的作为响应的聊天消息。
- inner_messages: Sequence[Annotated[BaseAgentEvent | BaseChatMessage, SerializeAsAny()]] | None = None#
代理生成的内部消息,可以是
BaseAgentEvent或BaseChatMessage。
- class Team(*args, **kwargs)[source]#
基类:
ABC,TaskRunner,ComponentBase[BaseModel]- component_type: ClassVar[ComponentType] = 'team'#
组件的逻辑类型。
- abstract async pause() None[source]#
暂停团队及其所有参与者。这对于暂停
autogen_agentchat.base.TaskRunner.run()或autogen_agentchat.base.TaskRunner.run_stream()方法以防止它们并发运行很有用,同时保持它们处于活动状态。
- abstract async resume(cancellation_token: CancellationToken) None[source]#
在调用
pause()之后,从暂停中恢复团队及其所有参与者。
- class TerminationCondition[source]#
基类:
ABC,ComponentBase[BaseModel]一个有状态的条件,用于确定何时终止对话。
终止条件是一个可调用对象,它接收自上次调用该条件以来的一系列 BaseChatMessage 对象,如果对话应终止,则返回 StopMessage,否则返回 None。一旦达到终止条件,必须将其重置才能再次使用。
终止条件可以使用 AND 和 OR 运算符进行组合。
示例
import asyncio from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination async def main() -> None: # Terminate the conversation after 10 turns or if the text "TERMINATE" is mentioned. cond1 = MaxMessageTermination(10) | TextMentionTermination("TERMINATE") # Terminate the conversation after 10 turns and if the text "TERMINATE" is mentioned. cond2 = MaxMessageTermination(10) & TextMentionTermination("TERMINATE") # ... # Reset the termination condition. await cond1.reset() await cond2.reset() asyncio.run(main())
- component_type: ClassVar[ComponentType] = 'termination'#
组件的逻辑类型。
- class AndTerminationCondition(*conditions: TerminationCondition)[source]#
基类:
TerminationCondition,Component[AndTerminationConditionConfig]- component_config_schema#
别名
AndTerminationConditionConfig
- component_type: ClassVar[ComponentType] = 'termination'#
组件的逻辑类型。
- class OrTerminationCondition(*conditions: TerminationCondition)[source]#
基类:
TerminationCondition,Component[OrTerminationConditionConfig]- component_config_schema#
别名
OrTerminationConditionConfig
- component_type: ClassVar[ComponentType] = 'termination'#
组件的逻辑类型。
- pydantic model TaskResult[source]#
基类:
BaseModel运行任务的结果。
显示 JSON 模式
{ "title": "TaskResult", "description": "Result of running a task.", "type": "object", "properties": { "messages": { "items": { "anyOf": [ { "$ref": "#/$defs/BaseAgentEvent" }, { "$ref": "#/$defs/BaseChatMessage" } ] }, "title": "Messages", "type": "array" }, "stop_reason": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "title": "Stop Reason" } }, "$defs": { "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.", "properties": { "id": { "title": "Id", "type": "string" }, "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "created_at": { "format": "date-time", "title": "Created At", "type": "string" } }, "required": [ "source" ], "title": "BaseAgentEvent", "type": "object" }, "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`.", "properties": { "id": { "title": "Id", "type": "string" }, "source": { "title": "Source", "type": "string" }, "models_usage": { "anyOf": [ { "$ref": "#/$defs/RequestUsage" }, { "type": "null" } ], "default": null }, "metadata": { "additionalProperties": { "type": "string" }, "default": {}, "title": "Metadata", "type": "object" }, "created_at": { "format": "date-time", "title": "Created At", "type": "string" } }, "required": [ "source" ], "title": "BaseChatMessage", "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": [ "messages" ] }
- 字段:
messages (Sequence[autogen_agentchat.messages.BaseAgentEvent | autogen_agentchat.messages.BaseChatMessage])stop_reason (str | None)
- field messages: Sequence[Annotated[BaseAgentEvent | BaseChatMessage, SerializeAsAny()]] [Required]#
任务产生的消息。
- class TaskRunner(*args, **kwargs)[source]#
基类:
Protocol任务运行器。
- async run(*, task: str | BaseChatMessage | Sequence[BaseChatMessage] | None = None, cancellation_token: CancellationToken | None = None, output_task_messages: bool = True) TaskResult[source]#
运行任务并返回结果。
任务可以是字符串、单个消息或消息序列。
运行器是有状态的,后续调用此方法将从上次调用停止的地方继续。如果未指定任务,运行器将继续当前任务。
- 参数:
task – 要运行的任务。可以是字符串、单个消息或消息序列。
cancellation_token – 用于立即终止任务的取消令牌。
output_task_messages – 是否在
TaskResult.messages中包含任务消息。为了向后兼容,默认为 True。
- run_stream(*, task: str | BaseChatMessage | Sequence[BaseChatMessage] | None = None, cancellation_token: CancellationToken | None = None, output_task_messages: bool = True) AsyncGenerator[BaseAgentEvent | BaseChatMessage | TaskResult, None][source]#
运行任务并生成消息流,最终结果
TaskResult作为流中的最后一项。任务可以是字符串、单个消息或消息序列。
运行器是有状态的,后续调用此方法将从上次调用停止的地方继续。如果未指定任务,运行器将继续当前任务。
- 参数:
task – 要运行的任务。可以是字符串、单个消息或消息序列。
cancellation_token – 用于立即终止任务的取消令牌。
output_task_messages – 是否在输出流中包含任务消息。为了向后兼容,默认为 True。
- pydantic model Handoff[source]#
基类:
BaseModel交接配置。
显示 JSON 模式
{ "title": "Handoff", "description": "Handoff configuration.", "type": "object", "properties": { "target": { "title": "Target", "type": "string" }, "description": { "default": "", "title": "Description", "type": "string" }, "name": { "default": "", "title": "Name", "type": "string" }, "message": { "default": "", "title": "Message", "type": "string" } }, "required": [ "target" ] }
- 字段:
description (str)message (str)name (str)target (str)
- 验证器:
set_defaults»所有 字段