autogen_agentchat.base#

class ChatAgent(*args, **kwargs)[source]#

基类:ABC, TaskRunner, ComponentBase[BaseModel]

聊天代理协议。

component_type: ClassVar[ComponentType] = 'agent'#

组件的逻辑类型。

abstract property name: str#

代理的名称。团队使用此名称来唯一标识代理。它在团队中应该是唯一的。

abstract property description: str#

代理的描述。团队使用此描述来决定使用哪些代理。描述应说明代理的功能以及如何与其交互。

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() 中运行。

abstract async save_state() Mapping[str, Any][source]#

保存代理状态以供以后恢复

abstract async load_state(state: Mapping[str, Any]) None[source]#

从保存状态恢复代理

abstract async close() None[source]#

释放代理持有的任何资源。

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#

代理生成的内部消息,可以是 BaseAgentEventBaseChatMessage

class Team(*args, **kwargs)[source]#

基类:ABC, TaskRunner, ComponentBase[BaseModel]

component_type: ClassVar[ComponentType] = 'team'#

组件的逻辑类型。

abstract property name: str#

团队的名称。团队使用此名称在更大的团队集合中唯一标识自身。

abstract property description: str#

团队的描述。这用于为其父编排器提供关于团队及其目的的上下文。

abstract async reset() None[source]#

将团队及其所有参与者重置为初始状态。

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() 之后,从暂停中恢复团队及其所有参与者。

abstract async save_state() Mapping[str, Any][source]#

保存团队当前状态。

abstract async load_state(state: Mapping[str, Any]) None[source]#

加载团队状态。

exception TerminatedException[source]#

基类:BaseException

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'#

组件的逻辑类型。

abstract property terminated: bool#

检查终止条件是否已达到

abstract async reset() None#

重置终止条件。

class AndTerminationCondition(*conditions: TerminationCondition)[source]#

基类:TerminationCondition, Component[AndTerminationConditionConfig]

component_config_schema#

别名 AndTerminationConditionConfig

component_type: ClassVar[ComponentType] = 'termination'#

组件的逻辑类型。

component_provider_override: ClassVar[str | None] = 'autogen_agentchat.base.AndTerminationCondition'#

覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。

property terminated: bool#

检查终止条件是否已达到

async reset() None[source]#

重置终止条件。

class OrTerminationCondition(*conditions: TerminationCondition)[source]#

基类:TerminationCondition, Component[OrTerminationConditionConfig]

component_config_schema#

别名 OrTerminationConditionConfig

component_type: ClassVar[ComponentType] = 'termination'#

组件的逻辑类型。

component_provider_override: ClassVar[str | None] = 'autogen_agentchat.base.OrTerminationCondition'#

覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。

property terminated: bool#

检查终止条件是否已达到

async reset() None[source]#

重置终止条件。

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]#

任务产生的消息。

field stop_reason: str | None = None#

任务停止的原因。

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 » 所有 字段

field target: str [Required]#

要交接到的目标代理的名称。

由以下验证:
  • set_defaults

field description: str = ''#

交接的描述,例如应该发生的条件和目标代理的能力。如果未提供,则根据目标代理的名称生成。

由以下验证:
  • set_defaults

field name: str = ''#

此交接配置的名称。如果未提供,则根据目标代理的名称生成。

由以下验证:
  • set_defaults

field message: str = ''#

发送给目标代理的消息。默认情况下,它将是交接工具的结果。如果未提供,则根据目标代理的名称生成。

由以下验证:
  • set_defaults

validator set_defaults  »  所有 字段[source]#
property handoff_tool: BaseTool[BaseModel, BaseModel]#

从此交接配置创建交接工具。