autogen_agentchat.tools#

class AgentTool(agent: BaseChatAgent, return_value_as_last_message: bool = False)[来源]#

基类:TaskRunnerToolComponent[AgentToolConfig]

可用于使用代理运行任务的工具。

该工具将任务执行结果作为 TaskResult 对象返回。

重要

使用 AgentTool 时,您必须禁用模型客户端配置中的并行工具调用以避免并发问题。代理无法并发运行,因为它们维护的内部状态会与并行执行冲突。例如,对于 OpenAIChatCompletionClientAzureOpenAIChatCompletionClient,请设置 parallel_tool_calls=False

参数:
  • agent (BaseChatAgent) – 用于运行任务的代理。

  • return_value_as_last_message (bool) – 是否将任务结果的最后一条消息内容作为 return_value_as_string() 中工具的返回值。如果设置为 True,则将最后一条消息内容作为字符串返回。如果设置为 False,该工具将把任务结果中的所有消息连接成一个字符串返回,每条消息前面都带有其来源(例如,“writer: …”,“assistant: …”)。

示例

import asyncio

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.tools import AgentTool
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient


async def main() -> None:
    model_client = OpenAIChatCompletionClient(model="gpt-4.1")
    writer = AssistantAgent(
        name="writer",
        description="A writer agent for generating text.",
        model_client=model_client,
        system_message="Write well.",
    )
    writer_tool = AgentTool(agent=writer)

    # Create model client with parallel tool calls disabled for the main agent
    main_model_client = OpenAIChatCompletionClient(model="gpt-4.1", parallel_tool_calls=False)
    assistant = AssistantAgent(
        name="assistant",
        model_client=main_model_client,
        tools=[writer_tool],
        system_message="You are a helpful assistant.",
    )
    await Console(assistant.run_stream(task="Write a poem about the sea."))


asyncio.run(main())
component_config_schema#

别名 AgentToolConfig

component_provider_override: ClassVar[str | None] = 'autogen_agentchat.tools.AgentTool'#

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

_to_config() AgentToolConfig[来源]#

转储创建与此实例配置匹配的组件新实例所需的配置。

返回:

T – 组件的配置。

classmethod _from_config(config: AgentToolConfig) Self[来源]#

从配置对象创建组件的新实例。

参数:

config (T) – 配置对象。

返回:

Self – 组件的新实例。

class TeamTool(team: BaseGroupChat, name: str, description: str, return_value_as_last_message: bool = False)[来源]#

基类:TaskRunnerToolComponent[TeamToolConfig]

可用于运行任务的工具。

该工具将任务执行结果作为 TaskResult 对象返回。

重要

使用 TeamTool 时,您必须禁用模型客户端配置中的并行工具调用以避免并发问题。团队无法并发运行,因为它们维护的内部状态会与并行执行冲突。例如,对于 OpenAIChatCompletionClientAzureOpenAIChatCompletionClient,请设置 parallel_tool_calls=False

参数:
  • team (BaseGroupChat) – 用于运行任务的团队。

  • name (str) – 工具的名称。

  • description (str) – 工具的描述。

  • return_value_as_last_message (bool) – 是否将任务结果的最后一条消息内容作为 return_value_as_string() 中工具的返回值。如果设置为 True,则将最后一条消息内容作为字符串返回。如果设置为 False,该工具将把任务结果中的所有消息连接成一个字符串返回,每条消息前面都带有其来源(例如,“writer: …”,“assistant: …”)。

示例

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import SourceMatchTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.tools import TeamTool
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient


async def main() -> None:
    # Disable parallel tool calls when using TeamTool
    model_client = OpenAIChatCompletionClient(model="gpt-4.1")

    writer = AssistantAgent(name="writer", model_client=model_client, system_message="You are a helpful assistant.")
    reviewer = AssistantAgent(
        name="reviewer", model_client=model_client, system_message="You are a critical reviewer."
    )
    summarizer = AssistantAgent(
        name="summarizer",
        model_client=model_client,
        system_message="You combine the review and produce a revised response.",
    )
    team = RoundRobinGroupChat(
        [writer, reviewer, summarizer], termination_condition=SourceMatchTermination(sources=["summarizer"])
    )

    # Create a TeamTool that uses the team to run tasks, returning the last message as the result.
    tool = TeamTool(
        team=team,
        name="writing_team",
        description="A tool for writing tasks.",
        return_value_as_last_message=True,
    )

    # Create model client with parallel tool calls disabled for the main agent
    main_model_client = OpenAIChatCompletionClient(model="gpt-4.1", parallel_tool_calls=False)
    main_agent = AssistantAgent(
        name="main_agent",
        model_client=main_model_client,
        system_message="You are a helpful assistant that can use the writing tool.",
        tools=[tool],
    )
    # For handling each events manually.
    # async for message in main_agent.run_stream(
    #     task="Write a short story about a robot learning to love.",
    # ):
    #     print(message)
    # Use Console to display the messages in a more readable format.
    await Console(
        main_agent.run_stream(
            task="Write a short story about a robot learning to love.",
        )
    )


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())
component_config_schema#

别名 TeamToolConfig

component_provider_override: ClassVar[str | None] = 'autogen_agentchat.tools.TeamTool'#

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

_to_config() TeamToolConfig[来源]#

转储创建与此实例配置匹配的组件新实例所需的配置。

返回:

T – 组件的配置。

classmethod _from_config(config: TeamToolConfig) Self[来源]#

从配置对象创建组件的新实例。

参数:

config (T) – 配置对象。

返回:

Self – 组件的新实例。