Multi-Agent 架構之 Supervisor
從單 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"
}
]
}
}
],
}
}
2.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"
}
}
],
}
3.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"
}
]
}
}
],
}
4.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"
}
}
],
}
5.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"
}
}
],
}
6.拿到最終結果。