Team.run() 函数会运行团队并生成响应,响应形式可以是一个 TeamRunResponse 对象,也可以是一系列的 TeamRunResponseEvent 对象。

我们的许多示例都使用了 team.print_response(),这是一个在终端打印响应的辅助工具。它底层调用的是 team.run()

以下是运行团队的方法。响应会被捕获在 responseresponse_stream 变量中。

from agno.team import Team
from agno.models.openai import OpenAIChat

agent_1 = Agent(name="News Agent", role="Get the latest news")

agent_2 = Agent(name="Weather Agent", role="Get the weather for the next 7 days")

team = Team(name="News and Weather Team", mode="coordinate", members=[agent_1, agent_2])

response = team.run("What is the weather in Tokyo?")

# Synchronous execution
result = team.run("What is the weather in Tokyo?")

# Asynchronous execution
result = await team.arun("What is the weather in Tokyo?")

# Streaming responses
for chunk in team.run("What is the weather in Tokyo?", stream=True):
    print(chunk.content, end="", flush=True)

# Asynchronous streaming
async for chunk in await team.arun("What is the weather in Tokyo?", stream=True):
    print(chunk.content, end="", flush=True)

流式传输中间步骤

在团队执行过程中,会发生多个事件,我们实时提供这些事件以增强团队的透明度。

您可以通过设置 stream_intermediate_steps=True 来启用中间步骤的流式传输。

# Stream with intermediate steps
response_stream = team.run(
    "What is the weather in Tokyo?",
    stream=True,
    stream_intermediate_steps=True
)

处理事件

您可以通过迭代响应流来处理到达的事件:

response_stream = team.run("Your prompt", stream=True, stream_intermediate_steps=True)

for event in response_stream:
    if event.event == "TeamRunResponseContent":
        print(f"Content: {event.content}")
    elif event.event == "TeamToolCallStarted":
        print(f"Tool call started: {event.tool}")
    elif event.event == "ToolCallStarted":
        print(f"Member tool call started: {event.tool}")
    elif event.event == "ToolCallCompleted":
        print(f"Member tool call completed: {event.tool}")
    elif event.event == "TeamReasoningStep":
        print(f"Reasoning step: {event.content}")
    ...

当团队成员正在执行时,团队成员事件会在团队执行期间产生。您可以通过设置 stream_member_events=False 来禁用此功能。

存储事件

您可以将运行期间发生的所有事件存储在 RunResponse 对象上。

from agno.team import Team
from agno.models.openai import OpenAIChat
from agno.utils.pprint import pprint_run_response

team = Team(model=OpenAIChat(id="gpt-4o-mini"), members=[], store_events=True)

response = team.run("Tell me a 5 second short story about a lion", stream=True, stream_intermediate_steps=True)
pprint_run_response(response)

for event in agent.run_response.events:
    print(event.event)

默认情况下,TeamRunResponseContentEventRunResponseContentEvent 事件不会被存储。您可以通过设置 events_to_skip 参数来修改要跳过的事件。

例如:

team = Team(model=OpenAIChat(id="gpt-4o-mini"), members=[], store_events=True, events_to_skip=[TeamRunEvent.run_started.value])

事件类型

根据团队的配置,Team.run()Team.arun() 函数会发送以下事件:

核心事件

Event TypeDescription
TeamRunStarted指示运行开始
TeamRunResponseContent以单个块形式包含模型的响应文本
TeamRunCompleted表示运行成功完成
TeamRunError指示在运行期间发生错误
TeamRunCancelled表示运行已被取消

工具事件

Event TypeDescription
TeamToolCallStarted指示工具调用开始
TeamToolCallCompleted表示工具调用完成,包括工具调用结果

推理事件

Event TypeDescription
TeamReasoningStarted指示代理的推理过程开始
TeamReasoningStep包含推理过程中的单个步骤
TeamReasoningCompleted表示推理过程完成

内存事件

Event TypeDescription
TeamMemoryUpdateStarted指示代理正在更新其内存
TeamMemoryUpdateCompleted表示内存更新完成

有关详细文档,请参阅 TeamRunResponse 文档。