autogen_agentchat.base#

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

基类: TerminationCondition, Component[AndTerminationConditionConfig]

component_config_schema#

别名:AndTerminationConditionConfig

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

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

component_type: ClassVar[ComponentType] = 'termination'#

组件的逻辑类型。

async reset() None[source]#

重置终止条件。

property terminated: bool#

检查是否已达到终止条件

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

基类: ABC, TaskRunner, ComponentBase[BaseModel]

聊天代理的协议。

abstract async close() None[source]#

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

component_type: ClassVar[ComponentType] = 'agent'#

组件的逻辑类型。

abstract property description: str#

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

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

从保存的状态恢复代理

abstract property name: str#

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

abstract async on_messages(messages: Sequence[BaseChatMessage], cancellation_token: CancellationToken) Response[source]#

处理传入的消息并返回响应。

abstract on_messages_stream(messages: Sequence[BaseChatMessage], cancellation_token: CancellationToken) AsyncGenerator[BaseAgentEvent | BaseChatMessage | Response, None][source]#

处理传入的消息并返回内部消息流,最后一个条目是响应。

abstract async on_pause(cancellation_token: CancellationToken) None[source]#

当代理暂停时调用。 当调用此方法时,代理可能正在 on_messages()on_messages_stream() 中运行。

abstract async on_reset(cancellation_token: CancellationToken) None[source]#

将代理重置为其初始化状态。

abstract async on_resume(cancellation_token: CancellationToken) None[source]#

当代理恢复时调用。 当调用此方法时,代理可能正在 on_messages()on_messages_stream() 中运行。

abstract property produced_message_types: Sequence[type[BaseChatMessage]]#

代理在 Response.chat_message 字段中生成的消息类型。 它们必须是 BaseChatMessage 类型。

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

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

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 » all fields

field description: str = ''#

切换的描述,例如应该发生切换的条件以及目标代理的能力。 如果未提供,则从目标代理的名称生成。

由以下项验证:
  • set_defaults

field message: str = ''#

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

由以下项验证:
  • set_defaults

field name: str = ''#

此切换配置的名称。 如果未提供,则从目标代理的名称生成。

由以下项验证:
  • set_defaults

field target: str [Required]#

要切换到的目标代理的名称。

由以下项验证:
  • set_defaults

validator set_defaults  »  all fields[source]#
property handoff_tool: BaseTool[BaseModel, BaseModel]#

从此切换配置创建切换工具。

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

基类:TerminationCondition, Component[OrTerminationConditionConfig]

component_config_schema#

OrTerminationConditionConfig 的别名

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

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

component_type: ClassVar[ComponentType] = 'termination'#

组件的逻辑类型。

async reset() None[源代码]#

重置终止条件。

property terminated: bool#

检查是否已达到终止条件

class Response(*, chat_message: BaseChatMessage, inner_messages: Sequence[BaseAgentEvent | BaseChatMessage] | None = None)[源代码]#

基类: object

调用 ChatAgent.on_messages() 的响应。

chat_message: BaseChatMessage#

代理作为响应产生的聊天消息。

inner_messages: Sequence[BaseAgentEvent | BaseChatMessage] | None = None#

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

pydantic 模型 TaskResult[源代码]#

基类: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": {
            "source": {
               "title": "Source",
               "type": "string"
            },
            "models_usage": {
               "anyOf": [
                  {
                     "$ref": "#/$defs/RequestUsage"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null
            },
            "metadata": {
               "additionalProperties": {
                  "type": "string"
               },
               "default": {},
               "title": "Metadata",
               "type": "object"
            }
         },
         "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": {
            "source": {
               "title": "Source",
               "type": "string"
            },
            "models_usage": {
               "anyOf": [
                  {
                     "$ref": "#/$defs/RequestUsage"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null
            },
            "metadata": {
               "additionalProperties": {
                  "type": "string"
               },
               "default": {},
               "title": "Metadata",
               "type": "object"
            }
         },
         "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[BaseAgentEvent | BaseChatMessage] [必需]#

任务产生的消息。

field stop_reason: str | None = None#

任务停止的原因。

class TaskRunner(*args, **kwargs)[源代码]#

基类: Protocol

任务运行器。

async run(*, task: str | BaseChatMessage | Sequence[BaseChatMessage] | None = None, cancellation_token: CancellationToken | None = None) TaskResult[source]#

运行任务并返回结果。

任务可以是一个字符串、一条消息或一系列消息。

运行器是有状态的,后续对此方法的调用将从上次调用停止的地方继续。 如果未指定任务,则运行器将继续执行当前任务。

run_stream(*, task: str | BaseChatMessage | Sequence[BaseChatMessage] | None = None, cancellation_token: CancellationToken | None = None) AsyncGenerator[BaseAgentEvent | BaseChatMessage | TaskResult, None][source]#

运行任务并生成消息流以及最终结果 TaskResult 作为流中的最后一个项目。

任务可以是一个字符串、一条消息或一系列消息。

运行器是有状态的,后续对此方法的调用将从上次调用停止的地方继续。 如果未指定任务,则运行器将继续执行当前任务。

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

基类: ABC, TaskRunner, ComponentBase[BaseModel]

component_type: ClassVar[ComponentType] = 'team'#

组件的逻辑类型。

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

加载团队的状态。

abstract async pause() None[source]#

暂停团队及其所有参与者。 这对于并发暂停 autogen_agentchat.base.TaskRunner.run()autogen_agentchat.base.TaskRunner.run_stream() 方法非常有用,同时保持它们处于活动状态。

abstract async reset() None[source]#

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

abstract async resume() None[source]#

在调用 pause() 后,从暂停状态恢复团队及其所有参与者。

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

保存团队的当前状态。

异常 TerminatedException[源代码]#

基类: BaseException

TerminationCondition[源代码]#

基类: 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 async reset() None[源代码]#

重置终止条件。

abstract property terminated: bool#

检查是否已达到终止条件