从单 Agent 到 Workflow,现在已经进入了多 Agent 时代,多 Agent 本质上是在大模型的指导下,多个 Agent 共同协作完成用户的任务。在这个背景下衍生出了多种 Agent 架构,比如中心化、去中心化、层次化等。本文基于开源的 langgraph-supervisor-py 项目,分析 supervisor 架构下的多 Agent 设计,supervisor 架构是有一个 supervisor 和多个 Agent,supervisor 负责管理多个 Agent,Agent 负责具体的工作,开始执行时,从 supervisor 开始,然后根据大模型的返回不断调各个 Agent,每个 Agent 执行完再次回到 supervisor,supervisor 再次调大模型,如此反复,直到得到结果。
使用例子
下面从官方的一个例子开始。
复制from langchain_openai import ChatOpenAI from langgraph_supervisor import create_supervisor from langgraph.prebuilt import create_react_agent from langchain_core.runnables.graph import MermaidDrawMethod # 工具 def add(a: float, b: float) -> float: """Add two numbers.""" return a + b def multiply(a: float, b: float) -> float: """Multiply two numbers.""" return a * b def web_search(query: str) -> str: """Search the web for information.""" return ( "Here are the headcounts for each of the FAANG companies in 2024:\n" "1. **Facebook (Meta)**: 67,317 employees.\n" "2. **Apple**: 164,000 employees.\n" "3. **Amazon**: 1,551,000 employees.\n" "4. **Netflix**: 14,000 employees.\n" "5. **Google (Alphabet)**: 181,269 employees." ) # 数学计算 Agent math_agent = create_react_agent( model=model, tools=[add, multiply], name="math_expert", prompt="You are a math expert. Always use one tool at a time." ) # 搜索 Agent research_agent = create_react_agent( model=model, tools=[web_search], name="research_expert", prompt="You are a world class researcher with access to web search. Do not do any math." ) workflow = create_supervisor( [research_agent, math_agent], model=model, prompt=( "You are a team supervisor managing a research expert and a math expert. " "For current events, use research_agent. " "For math problems, use math_agent." ) ) app = workflow.compile() # 画图 img = app.get_graph().draw_mermaid_png( draw_method=MermaidDrawMethod.API, ) with open("graph.png", "wb") as f: f.write(img) f.close() # 执行 result = app.invoke({ "messages": [ { "role": "user", "content": "what's the combined headcount of the FAANG companies in 2024?" } ] })
上面的例子中创建了一个 supervisor 和两个 Agent(supervisor 其实也是一个 Agent),一个是用于搜索的 Agent,一个是用于做数学计算的 Agent,每个 Agent 拥有自己的工具集。三个节点形成以下结构的图。
接着详细分析上面的例子。
create_react_agent
create_react_agent 可以创建一个 react Agent,其工作原理
- Agent 调用大模型获取工具调用情况。
- 大模型返回调用哪个工具。
- Agent 调用工具,拿到结果,回到 1,除非返回不需要再调工具。 其实现如下。
def create_react_agent( model: Union[str, LanguageModelLike], tools: Union[Sequence[Union[BaseTool, Callable]], ToolNode], *, prompt: Optional[Prompt] = None, ... ) -> CompiledGraph: # 创建一个 Tool 节点统一处理 tool tool_node = ToolNode(tools) tool_classes = list(tool_node.tools_by_name.values()) tool_calling_enabled = len(tool_classes) > 0 # 绑定到 model,调用大模型时把工具信息传过去 if _should_bind_tools(model, tool_classes) and tool_calling_enabled: model = cast(BaseChatModel, model).bind_tools(tool_classes) model_runnable = _get_prompt_runnable(prompt) | model # Agent 节点的处理函数 def call_model(state: StateSchema, config: RunnableConfig) -> StateSchema: state = _get_model_input_state(state) response = cast(AIMessage, model_runnable.invoke(state, config)) # add agent name to the AIMessage response.name = name return ... # 执行完 Agent 后执行该判断函数 def should_continue(state: StateSchema) -> Union[str, list]: messages = _get_state_value(state, "messages") last_message = messages[-1] # 不需要执行工具调用则返回结束 if not isinstance(last_message, AIMessage) or not last_message.tool_calls: return END if response_format is None else "generate_structured_response" # 跳到 Tool 节点处理 else: if version == "v1": return "tools" elif version == "v2": tool_calls = [ tool_node.inject_tool_args(call, state, store) # type: ignore[arg-type] for call in last_message.tool_calls ] return [Send("tools", [tool_call]) for tool_call in tool_calls] # 开始构建执行流程图 workflow = StateGraph(state_schema or AgentState, config_schema=config_schema) # 添加 Agent 节点 workflow.add_node( "agent", RunnableCallable(call_model, acall_model), input=input_schema ) # 添加 Tool 节点 workflow.add_node("tools", tool_node) # 设置开始执行节点 entrypoint = "agent" workflow.set_entry_point(entrypoint) # 设置条件判断节点 workflow.add_conditional_edges( "agent", should_continue, path_map=should_continue_destinations, ) # 设置 Tool 节点到 Agent 的表,Tool 执行完后自动跳到 Agent 节点 workflow.add_edge("tools", entrypoint) return workflow.compile( checkpointer=checkpointer, store=store, interrupt_before=interrupt_before, interrupt_after=interrupt_after, debug=debug, name=name, )
create_react_agent 内部是使用 langgraph 构建了一个执行图,并封装了大模型和工具的调用逻辑。当开始执行 Agent 时,会从图起点开始执行,也就是调用大模型拿到需要调用的工具(也可能不需要执行工具,这里需要动态判断),然后执行到 ToolNode 节点,ToolNode 节点内部会根据大模型的返回信息调用具体的工具,调用完工具后回到 Agent,Agent 再次调大模型,如此反复,工具的处理函数如下。
复制def _run_one( self, call: ToolCall, input_type: Literal["list", "dict", "tool_calls"], config: RunnableConfig, ) -> ToolMessage: input = {**call, **{"type": "tool_call"}} response = self.tools_by_name[call["name"]].invoke(input, config) if isinstance(response, Command): return self._validate_tool_command(response, call, input_type) elif isinstance(response, ToolMessage): response.content = cast( Union[str, list], msg_content_output(response.content) ) return response else: raise TypeError( f"Tool {call['name']} returned unexpected type: {type(response)}" )
可以看到 ToolNode 内部会根据工具名称调用具体的工具,拿到结果后再跳回 Agent。结构图如下。。
create_supervisor
接着从 create_supervisor 开始分析 supervisor 内部的架构。
复制def create_supervisor( agents: list[Pregel], *, model: LanguageModelLike, tools: list[BaseTool | Callable] | None = None, prompt: Prompt | None = None, state_schema: StateSchemaType = AgentState, config_schema: Type[Any] | None = None, add_handoff_messages: bool = True, handoff_tool_prefix: Optional[str] = None, add_handoff_back_messages: Optional[bool] = None, supervisor_name: str = "supervisor", ) -> StateGraph: # 默认把 handoff 信息记录到 messages 中 if add_handoff_back_messages is None: add_handoff_back_messages = add_handoff_messages # 创建 handoff 工具,用于转移处理权 all_tools = [ create_handoff_tool( agent_name=agent.name, name=(None), add_handoff_messages=add_handoff_messages, ) for agent in agents ] # 绑定工具,调用大模型时会传过去 model = model.bind_tools(all_tools) # 创建一个角色为 supervisor 的 Agent supervisor_agent = create_react_agent( name=supervisor_name, model=model, tools=all_tools, prompt=prompt, state_schema=state_schema, # AgentState, reducer 为 add_messages response_format=response_format, ) builder = StateGraph(state_schema, config_schema=config_schema) # 添加 supervisor 到图中,下游是各个 Agent 和 END builder.add_node(supervisor_agent, destinatinotallow=tuple(agent_names) + (END,)) # 设置执行起点是 supervisor builder.add_edge(START, supervisor_agent.name) for agent in agents: # 把每个 Agent 加入图中 builder.add_node( agent.name, _make_call_agent( agent, output_mode, add_handoff_back_messages=add_handoff_back_messages, supervisor_name=supervisor_name, ), ) # 每个 Agent 都有一条指向 supervisor 的边 builder.add_edge(agent.name, supervisor_agent.name) return builder
create_supervisor 的大概逻辑是创建了一个 supervisor Agent,然后构造一个执行图。有两个地方需要分析。分别是 create_handoff_tool 和 _make_call_agent。先看 create_handoff_tool。
复制def create_handoff_tool( *, agent_name: str, name: str | None = None, description: str | None = None, add_handoff_messages: bool = True, ) -> BaseTool: # 工具的名称和描述 if name is None: name = f"transfer_to_{_normalize_agent_name(agent_name)}" if description is None: description = f"Ask agent '{agent_name}' for help" @tool(name, descriptinotallow=description) def handoff_to_agent( state: Annotated[dict, InjectedState], tool_call_id: Annotated[str, InjectedToolCallId], ): tool_message = ToolMessage( cnotallow=f"Successfully transferred to {agent_name}", name=name, tool_call_id=tool_call_id, response_metadata={METADATA_KEY_HANDOFF_DESTINATION: agent_name}, ) # 追加 handoff message handoff_messages = state["messages"] + [tool_message] # 跳到具体的 Agent return Command( goto=agent_name, graph=Command.PARENT, update={**state, "messages": handoff_messages}, ) handoff_to_agent.metadata = {METADATA_KEY_HANDOFF_DESTINATION: agent_name} return handoff_to_agent
create_handoff_tool 用于创建一个工具,这些工具信息会发送给大模型,大模型会根据用户的问题和工具信息(名称和描述),并告诉 supervisor 应该执行哪一个工具,supervisor 拿到大模型的响应后,会执行 create_handoff_tool 函数,并最终跳转到具体的 Agent。Agent 的执行由 _make_call_agent 进行了封装。
复制def _make_call_agent( agent: Pregel, output_mode: OutputMode, add_handoff_back_messages: bool, supervisor_name: str, ) -> Callable[[dict], dict] | RunnableCallable: def _process_output(output: dict) -> dict: messages = output["messages"] # 追加 handoff message,这里是追加 handoff 到 supervisor 的信息 if add_handoff_back_messages: messages.extend(create_handoff_back_messages(agent.name, supervisor_name)) # Agent 有指向 supervisor 的边,执行完后会自动跳到 supervisor return { **output, "messages": messages, } def call_agent(state: dict, config: RunnableConfig) -> dict: # 调大模型 output = agent.invoke(state, config) return _process_output(output) return RunnableCallable(call_agent, ...)
_make_call_agent 里做了一些处理并追加 handleoff message 后最终跳转到 supervisor,因为 Agent 有指向 supervisor 的边,所以会自动跳转,而不像 supervisor 跳到 Agent 时需要手动控制。从上面的分析可以知道,supervisor 架构中,处理流程如下。
- supervisor 传入 messages 和工具列表,然后调用大模型,大模型返回调用哪个工具。
- handoff 到具体的 Agent。
- Agent 传入 messages 和自己的工具列表,然后调用大模型,大模型返回调用哪个工具。
- Agent 调用工具,拿到工具的返回,再次调用大模型,拿到最终结果。
- handoff 到 supervisor,回到 1,直到调用大模型时,直到返回最终结果。
调用日志
下面是前面例子调用大模型的日志。
- supervisor
# supervisor 调用大模型,并传入工具列表,即有哪些 Agent request: { "tools": [ { "type": "function", "function": { "name": "transfer_to_research_expert", "description": "Ask agent 'research_expert' for help", "parameters": { "properties": {}, "type": "object" } } }, { "type": "function", "function": { "name": "transfer_to_math_expert", "description": "Ask agent 'math_expert' for help", "parameters": { "properties": {}, "type": "object" } } } ], "parallel_tool_calls": false, "messages": [ { "content": "You are a team supervisor managing a research expert and a math expert. For current events, use research_agent. For math problems, use math_agent.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" } ] } # 大模型返回调用 transfer_to_research_expert Agent response: { "id": "02174685630333191645265e627e21d5dcc187f9552fedf6fda0b", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "arguments": " {}", "name": "transfer_to_research_expert" }, "type": "function" } ] } } ], } }
- handoff 到搜索 Agent
# 搜索 Agent 调用大模型,并传入工具列表 request: { "tools": [ { "type": "function", "function": { "name": "web_search", "description": "Search the web for information.", "parameters": { "properties": { "query": { "type": "string" } }, "required": [ "query" ], "type": "object" } } } ], "messages": [ { "content": "You are a world class researcher with access to web search. Do not do any math.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" } ] } # 大模型返回调用搜索工具,并提供了参数 response: { "id": "02174685630508391645265e627e21d5dcc187f9552fedf724cf2", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_t52sv5g0zm4x1tft5hcnwjjs", "function": { "arguments": " {\n \"query\": \"Combined headcount of FAANG companies in 2024\"\n }\n", "name": "web_search" }, "type": "function" } ] } } ], } # 搜索 Agent 拿到了搜索工具的结果,并再次调用大模型 request: { "tools": [ { "type": "function", "function": { "name": "web_search", "description": "Search the web for information.", "parameters": { "properties": { "query": { "type": "string" } }, "required": [ "query" ], "type": "object" } } } ], "messages": [ { "content": "You are a world class researcher with access to web search. Do not do any math.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": null, "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_t52sv5g0zm4x1tft5hcnwjjs", "function": { "name": "web_search", "arguments": "{\"query\": \"Combined headcount of FAANG companies in 2024\"}" } } ] }, { "content": "Here are the headcounts for each of the FAANG companies in 2024:\n1. **Facebook (Meta)**: 67,317 employees.\n2. **Apple**: 164,000 employees.\n3. **Amazon**: 1,551,000 employees.\n4. **Netflix**: 14,000 employees.\n5. **Google (Alphabet)**: 181,269 employees.", "role": "tool", "tool_call_id": "call_t52sv5g0zm4x1tft5hcnwjjs" } ] } # 大模型觉得这个信息够了,不需要再搜索 response: { "id": "02174685630701191645265e627e21d5dcc187f9552fedf6f3f21", "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "message": { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "role": "assistant" } } ], }
- handoff 到 supervisor
# handoff 回 supervisor,supervisor 再次调用大模型 request: { "tools": [ { "type": "function", "function": { "name": "transfer_to_research_expert", "description": "Ask agent 'research_expert' for help", "parameters": { "properties": {}, "type": "object" } } }, { "type": "function", "function": { "name": "transfer_to_math_expert", "description": "Ask agent 'math_expert' for help", "parameters": { "properties": {}, "type": "object" } } } ], "parallel_tool_calls": false, "messages": [ { "content": "You are a team supervisor managing a research expert and a math expert. For current events, use research_agent. For math problems, use math_agent.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" } ] } # 大模型返回调用数学计算 Agent response: { "id": "02174685631050391645265e627e21d5dcc187f9552fedf85877e", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "arguments": " {}", "name": "transfer_to_math_expert" }, "type": "function" } ] } } ], }
- handoff 到数学计算 Agent
# 数学计算 Agent 调用大模型,并传入工具列表 request: { "tools": [ { "type": "function", "function": { "name": "add", "description": "Add two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } }, { "type": "function", "function": { "name": "multiply", "description": "Multiply two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } } ], "messages": [ { "content": "You are a math expert. Always use one tool at a time.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "name": "transfer_to_math_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to math_expert", "role": "tool", "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn" } ] } # 大模型返回调用数学计算工具 response: { "id": "02174685631226191645265e627e21d5dcc187f9552fedf93e39a", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_jd5esfvy2qsh9tdz23361pha", "function": { "arguments": " {\n \"a\": 67317, \n \"b\": 164000\n }\n", "name": "add" }, "type": "function" } ] } } ], } # 数学计算 Agent 调用大模型,并传入工具列表 request: { "tools": [ { "type": "function", "function": { "name": "add", "description": "Add two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } }, { "type": "function", "function": { "name": "multiply", "description": "Multiply two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } } ], "messages": [ { "content": "You are a math expert. Always use one tool at a time.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "name": "transfer_to_math_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to math_expert", "role": "tool", "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_jd5esfvy2qsh9tdz23361pha", "function": { "name": "add", "arguments": "{\"a\": 67317, \"b\": 164000}" } } ] }, { "content": "231317.0", "role": "tool", "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha" } ] } # 大模型返回了调用加法工具,并提供了参数 response: { "id": "02174685631455691645265e627e21d5dcc187f9552fedf732682", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_owov4sx70lmdpz7zooldnue3", "function": { "arguments": " {\"a\": 231317, \"b\": 1551000}", "name": "add" }, "type": "function" } ] } } ], } # 数学计算 Agent 拿到加法工具的调用结果后,再次调大模型 request: { "tools": [ { "type": "function", "function": { "name": "add", "description": "Add two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } }, { "type": "function", "function": { "name": "multiply", "description": "Multiply two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } } ], "messages": [ { "content": "You are a math expert. Always use one tool at a time.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "name": "transfer_to_math_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to math_expert", "role": "tool", "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_jd5esfvy2qsh9tdz23361pha", "function": { "name": "add", "arguments": "{\"a\": 67317, \"b\": 164000}" } } ] }, { "content": "231317.0", "role": "tool", "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_owov4sx70lmdpz7zooldnue3", "function": { "name": "add", "arguments": "{\"a\": 231317, \"b\": 1551000}" } } ] }, { "content": "1782317.0", "role": "tool", "tool_call_id": "call_owov4sx70lmdpz7zooldnue3" } ] } # 大模型再次返回了调用加法工具,并提供了参数,如此循环多次 response: { "id": "02174685631635591645265e627e21d5dcc187f9552fedf2db39e", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_w2tp827rhg290ekal228ei1c", "function": { "arguments": " {\"a\": 1782317, \"b\": 14000}", "name": "add" }, "type": "function" } ] } } ], } request: { "tools": [ { "type": "function", "function": { "name": "add", "description": "Add two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } }, { "type": "function", "function": { "name": "multiply", "description": "Multiply two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } } ], "messages": [ { "content": "You are a math expert. Always use one tool at a time.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "name": "transfer_to_math_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to math_expert", "role": "tool", "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_jd5esfvy2qsh9tdz23361pha", "function": { "name": "add", "arguments": "{\"a\": 67317, \"b\": 164000}" } } ] }, { "content": "231317.0", "role": "tool", "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_owov4sx70lmdpz7zooldnue3", "function": { "name": "add", "arguments": "{\"a\": 231317, \"b\": 1551000}" } } ] }, { "content": "1782317.0", "role": "tool", "tool_call_id": "call_owov4sx70lmdpz7zooldnue3" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_w2tp827rhg290ekal228ei1c", "function": { "name": "add", "arguments": "{\"a\": 1782317, \"b\": 14000}" } } ] }, { "content": "1796317.0", "role": "tool", "tool_call_id": "call_w2tp827rhg290ekal228ei1c" } ] } response: { "id": "02174685631866091645265e627e21d5dcc187f9552fedf7df673", "choices": [ { "finish_reason": "tool_calls", "index": 0, "logprobs": null, "message": { "content": "", "role": "assistant", "tool_calls": [ { "id": "call_rvoyhqsoj5sz1g0a9tz28qy9", "function": { "arguments": " {\"a\": 1796317, \"b\": 181269}", "name": "add" }, "type": "function" } ] } } ], } # 数学计算 Agent 最后一次调用大模型 request: { "tools": [ { "type": "function", "function": { "name": "add", "description": "Add two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } }, { "type": "function", "function": { "name": "multiply", "description": "Multiply two numbers.", "parameters": { "properties": { "a": { "type": "number" }, "b": { "type": "number" } }, "required": [ "a", "b" ], "type": "object" } } } ], "messages": [ { "content": "You are a math expert. Always use one tool at a time.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "name": "transfer_to_math_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to math_expert", "role": "tool", "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_jd5esfvy2qsh9tdz23361pha", "function": { "name": "add", "arguments": "{\"a\": 67317, \"b\": 164000}" } } ] }, { "content": "231317.0", "role": "tool", "tool_call_id": "call_jd5esfvy2qsh9tdz23361pha" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_owov4sx70lmdpz7zooldnue3", "function": { "name": "add", "arguments": "{\"a\": 231317, \"b\": 1551000}" } } ] }, { "content": "1782317.0", "role": "tool", "tool_call_id": "call_owov4sx70lmdpz7zooldnue3" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_w2tp827rhg290ekal228ei1c", "function": { "name": "add", "arguments": "{\"a\": 1782317, \"b\": 14000}" } } ] }, { "content": "1796317.0", "role": "tool", "tool_call_id": "call_w2tp827rhg290ekal228ei1c" }, { "content": null, "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_rvoyhqsoj5sz1g0a9tz28qy9", "function": { "name": "add", "arguments": "{\"a\": 1796317, \"b\": 181269}" } } ] }, { "content": "1977586.0", "role": "tool", "tool_call_id": "call_rvoyhqsoj5sz1g0a9tz28qy9" } ] } # 大模型返回结果,计算完毕 response: { "id": "02174685632091091645265e627e21d5dcc187f9552fedfe2a3f5", "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "message": { "content": "The combined headcount of the FAANG companies in 2024 is 1,977,586. ", "role": "assistant" } } ], }
- handoff 到 supervisor
# handoff 到 supervisor,supervisor 再次调大模型 request: { "tools": [ { "type": "function", "function": { "name": "transfer_to_research_expert", "description": "Ask agent 'research_expert' for help", "parameters": { "properties": {}, "type": "object" } } }, { "type": "function", "function": { "name": "transfer_to_math_expert", "description": "Ask agent 'math_expert' for help", "parameters": { "properties": {}, "type": "object" } } } ], "parallel_tool_calls": false, "messages": [ { "content": "You are a team supervisor managing a research expert and a math expert. For current events, use research_agent. For math problems, use math_agent.", "role": "system" }, { "content": "what's the combined headcount of the FAANG companies in 2024?", "role": "user" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_fyew4c06vn9ide7nhk6sijws", "function": { "name": "transfer_to_research_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to research_expert", "role": "tool", "tool_call_id": "call_fyew4c06vn9ide7nhk6sijws" }, { "content": "The individual headcounts of FAANG companies in 2024 are as follows: Meta has 67,317 employees, Apple has 164,000 employees, Amazon has 1,551,000 employees, Netflix has 14,000 employees, and Alphabet has 181,269 employees. You can sum these figures to get the combined head - count. ", "name": "research_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "research_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "267ff0b0-d27b-4764-a68b-71eda788379a", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "267ff0b0-d27b-4764-a68b-71eda788379a" }, { "content": null, "name": "supervisor", "role": "assistant", "tool_calls": [ { "type": "function", "id": "call_z9x3nnqjvnp9hxuehejkivgn", "function": { "name": "transfer_to_math_expert", "arguments": "{}" } } ] }, { "content": "Successfully transferred to math_expert", "role": "tool", "tool_call_id": "call_z9x3nnqjvnp9hxuehejkivgn" }, { "content": "The combined headcount of the FAANG companies in 2024 is 1,977,586. ", "name": "math_expert", "role": "assistant" }, { "content": "Transferring back to supervisor", "name": "math_expert", "role": "assistant", "tool_calls": [ { "type": "function", "id": "dff2e74c-2997-4acc-b86d-9d702f933dcb", "function": { "name": "transfer_back_to_supervisor", "arguments": "{}" } } ] }, { "content": "Successfully transferred back to supervisor", "role": "tool", "tool_call_id": "dff2e74c-2997-4acc-b86d-9d702f933dcb" } ] } # 大模型返回最终结果 response: { "id": "02174685632249991645265e627e21d5dcc187f9552fedf38b54d", "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "message": { "content": "The combined headcount of the FAANG companies in 2024 is 1,977,586. ", "role": "assistant" } } ], }
- 拿到最终结果。