class AgentTool(agent: BaseChatAgentreturn_value_as_last_message: bool = False)[来源]

基类:TaskRunnerToolComponent[AgentToolConfig]

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

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

重要

使用 AgentTool 时,您必须禁用模型客户端配置中的并行工具调用以避免并发问题。代理无法并发运行,因为它们维护的内部状态会与并行执行冲突。例如,对于 OpenAIChatCompletionClient 和 AzureOpenAIChatCompletionClient,请设置 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: BaseGroupChatname: strdescription: strreturn_value_as_last_message: bool = False)[来源]

基类:TaskRunnerToolComponent[TeamToolConfig]

可用于运行任务的工具。

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

重要

使用 TeamTool 时,您必须禁用模型客户端配置中的并行工具调用以避免并发问题。团队无法并发运行,因为它们维护的内部状态会与并行执行冲突。例如,对于 OpenAIChatCompletionClient 和 AzureOpenAIChatCompletionClient,请设置 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 – 组件的新实例。

《AI提示工程必知必会》为读者提供了丰富的AI提示工程知识与实战技能。《AI提示工程必知必会》主要内容包括各类提示词的应用,如问答式、指令式、状态类、建议式、安全类和感谢类提示词,以及如何通过实战演练掌握提示词的使用技巧;使用提示词进行文本摘要、改写重述、语法纠错、机器翻译等语言处理任务,以及在数据挖掘、程序开发等领域的应用;AI在绘画创作上的应用,百度文心一言和阿里通义大模型这两大智能平台的特性与功能,以及市场调研中提示词的实战应用。通过阅读《AI提示工程必知必会》,读者可掌握如何有效利用AI提示工程提升工作效率,创新工作流程,并在职场中脱颖而出。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐