在使用语言模型时,通常只需一个优质的提示词和合适的工具就能获得稳定表现。但当处理多个独特流程时,情况可能变得复杂。本指南将介绍一种应对方法。
我们将介绍例行程序和交接的概念,然后逐步讲解实现过程,并展示如何以简单、强大且可控的方式来协调多个智能体。
最后,我们提供了一个示例仓库Swarm,其中实现了这些理念并附有示例。
在使用语言模型时,通常只需一个优质的提示词和合适的工具就能获得稳定表现。但当处理多个独特流程时,情况可能变得复杂。本指南将介绍一种应对方法。
我们将介绍例行程序和交接的概念,然后逐步讲解实现过程,并展示如何以简单、强大且可控的方式来协调多个智能体。
最后,我们提供了一个示例仓库Swarm,其中实现了这些理念并附有示例。
让我们从设置导入开始。
from openai import OpenAI
from pydantic import BaseModel
from typing import Optional
import json
client = OpenAI()"例行程序"的概念并没有严格的定义,而是用来描述一系列步骤的集合。具体来说,我们可以将例行程序定义为用自然语言编写的指令列表(我们将用系统提示来表示),以及完成这些指令所需的工具。
让我们来看一个示例。下面我们定义了一个客户服务智能体的流程,指示其对用户问题进行分级处理,然后要么建议解决方案,要么提供退款。我们还定义了必要的函数execute_refund和look_up_item。我们可以将其称为客户服务流程、智能体、助手等——但核心概念是相同的:一系列步骤及执行这些步骤所需的工具。
# Customer Service Routine
system_message = (
"You are a customer support agent for ACME Inc."
"Always answer in a sentence or less."
"Follow the following routine with the user:"
"1. First, ask probing questions and understand the user's problem deeper.\n"
" - unless the user has already provided a reason.\n"
"2. Propose a fix (make one up).\n"
"3. ONLY if not satisfied, offer a refund.\n"
"4. If accepted, search for the ID and then execute refund."
""
)
def look_up_item(search_query):
"""Use to find item ID.
Search query can be a description or keywords."""
# return hard-coded item ID - in reality would be a lookup
return "item_132612938"
def execute_refund(item_id, reason="not provided"):
print("Summary:", item_id, reason) # lazy summary
return "success"
日常流程的主要优势在于其简单性和鲁棒性。请注意,这些指令包含条件判断,类似于状态机或代码中的分支结构。对于中小型日常流程,LLM实际上可以相当稳健地处理这些情况,并具有"柔性"遵循的额外优势——LLM能够自然地引导对话而不会陷入死胡同。
要执行一个例程,让我们实现一个简单的循环:
messages。messages。def run_full_turn(system_message, messages):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": system_message}] + messages,
)
message = response.choices[0].message
messages.append(message)
if message.content: print("Assistant:", message.content)
return message
messages = []
while True:
user = input("User: ")
messages.append({"role": "user", "content": user})
run_full_turn(system_message, messages)如你所见,当前忽略了函数调用,让我们添加这部分功能。
模型需要将函数格式化为函数模式。为了方便起见,我们可以定义一个辅助函数,将python函数转换为相应的函数模式。
import inspect
def function_to_schema(func) -> dict:
type_map = {
str: "string",
int: "integer",
float: "number",
bool: "boolean",
list: "array",
dict: "object",
type(None): "null",
}
try:
signature = inspect.signature(func)
except ValueError as e:
raise ValueError(
f"Failed to get signature for function {func.__name__}: {str(e)}"
)
parameters = {}
for param in signature.parameters.values():
try:
param_type = type_map.get(param.annotation, "string")
except KeyError as e:
raise KeyError(
f"Unknown type annotation {param.annotation} for parameter {param.name}: {str(e)}"
)
parameters[param.name] = {"type": param_type}
required = [
param.name
for param in signature.parameters.values()
if param.default == inspect._empty
]
return {
"type": "function",
"function": {
"name": func.__name__,
"description": (func.__doc__ or "").strip(),
"parameters": {
"type": "object",
"properties": parameters,
"required": required,
},
},
}例如:
def sample_function(param_1, param_2, the_third_one: int, some_optional="John Doe"):
"""
This is my docstring. Call this function when you want.
"""
print("Hello, world")
schema = function_to_schema(sample_function)
print(json.dumps(schema, indent=2)){
"type": "function",
"function": {
"name": "sample_function",
"description": "This is my docstring. Call this function when you want.",
"parameters": {
"type": "object",
"properties": {
"param_1": {
"type": "string"
},
"param_2": {
"type": "string"
},
"the_third_one": {
"type": "integer"
},
"some_optional": {
"type": "string"
}
},
"required": [
"param_1",
"param_2",
"the_third_one"
]
}
}
}
现在,我们可以在调用模型时使用此函数来传递工具。
messages = []
tools = [execute_refund, look_up_item]
tool_schemas = [function_to_schema(tool) for tool in tools]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Look up the black boot."}],
tools=tool_schemas,
)
message = response.choices[0].message
message.tool_calls[0].functionFunction(arguments='{"search_query":"black boot"}', name='look_up_item')最后,当模型调用工具时,我们需要执行相应的函数并将结果返回给模型。
我们可以通过在tool_map中将工具名称映射到对应的python函数,然后在execute_tool_call中查找并调用它。最后将结果添加到对话中。
tools_map = {tool.__name__: tool for tool in tools}
def execute_tool_call(tool_call, tools_map):
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"Assistant: {name}({args})")
# call corresponding function with provided arguments
return tools_map[name](**args)
for tool_call in message.tool_calls:
result = execute_tool_call(tool_call, tools_map)
# add result back to conversation
result_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
messages.append(result_message)Assistant: look_up_item({'search_query': 'black boot'})
在实践中,我们还会希望让模型利用该结果生成另一个响应。该响应可能也包含工具调用,因此我们可以直接在循环中运行此过程,直到不再有工具调用为止。
如果我们将所有内容整合在一起,看起来会是这样:
tools = [execute_refund, look_up_item]
def run_full_turn(system_message, tools, messages):
num_init_messages = len(messages)
messages = messages.copy()
while True:
# turn python functions into tools and save a reverse map
tool_schemas = [function_to_schema(tool) for tool in tools]
tools_map = {tool.__name__: tool for tool in tools}
# === 1. get openai completion ===
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": system_message}] + messages,
tools=tool_schemas or None,
)
message = response.choices[0].message
messages.append(message)
if message.content: # print assistant response
print("Assistant:", message.content)
if not message.tool_calls: # if finished handling tool calls, break
break
# === 2. handle tool calls ===
for tool_call in message.tool_calls:
result = execute_tool_call(tool_call, tools_map)
result_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
messages.append(result_message)
# ==== 3. return new messages =====
return messages[num_init_messages:]
def execute_tool_call(tool_call, tools_map):
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"Assistant: {name}({args})")
# call corresponding function with provided arguments
return tools_map[name](**args)
messages = []
while True:
user = input("User: ")
messages.append({"role": "user", "content": user})
new_messages = run_full_turn(system_message, tools, messages)
messages.extend(new_messages)现在我们有了一个例行程序,假设我们想添加更多步骤和工具。在一定程度上是可以的,但最终如果我们尝试用太多不同的任务扩展这个例行程序,它可能会开始出现问题。这时我们可以利用多个例行程序的概念——根据用户请求,我们可以加载具有适当步骤和工具的正确的例行程序来解决它。
动态切换系统指令和工具可能看起来令人望而生畏。然而,如果我们把"例行程序"视为"智能体",那么这种交接的概念就能让我们简单地表示这些切换——就像一个智能体将对话移交给另一个智能体。
让我们将转接定义为智能体(或例程)将活跃对话转交给另一个智能体的过程,就像电话通话中被转接给其他人一样。不同之处在于,这里的智能体完全知晓您之前的对话内容!
要查看交接的实际效果,我们首先定义一个基本的智能体类。
class Agent(BaseModel):
name: str = "Agent"
model: str = "gpt-4o-mini"
instructions: str = "You are a helpful Agent"
tools: list = []现在要让我们的代码支持它,我们可以将run_full_turn改为接收一个Agent而不是单独的system_message和tools:
def run_full_turn(agent, messages):
num_init_messages = len(messages)
messages = messages.copy()
while True:
# turn python functions into tools and save a reverse map
tool_schemas = [function_to_schema(tool) for tool in agent.tools]
tools_map = {tool.__name__: tool for tool in agent.tools}
# === 1. get openai completion ===
response = client.chat.completions.create(
model=agent.model,
messages=[{"role": "system", "content": agent.instructions}] + messages,
tools=tool_schemas or None,
)
message = response.choices[0].message
messages.append(message)
if message.content: # print assistant response
print("Assistant:", message.content)
if not message.tool_calls: # if finished handling tool calls, break
break
# === 2. handle tool calls ===
for tool_call in message.tool_calls:
result = execute_tool_call(tool_call, tools_map)
result_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
messages.append(result_message)
# ==== 3. return new messages =====
return messages[num_init_messages:]
def execute_tool_call(tool_call, tools_map):
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"Assistant: {name}({args})")
# call corresponding function with provided arguments
return tools_map[name](**args)我们现在可以轻松运行多个智能体:
def execute_refund(item_name):
return "success"
refund_agent = Agent(
name="Refund Agent",
instructions="You are a refund agent. Help the user with refunds.",
tools=[execute_refund],
)
def place_order(item_name):
return "success"
sales_assistant = Agent(
name="Sales Assistant",
instructions="You are a sales assistant. Sell the user a product.",
tools=[place_order],
)
messages = []
user_query = "Place an order for a black boot."
print("User:", user_query)
messages.append({"role": "user", "content": user_query})
response = run_full_turn(sales_assistant, messages) # sales assistant
messages.extend(response)
user_query = "Actually, I want a refund." # implicitly refers to the last item
print("User:", user_query)
messages.append({"role": "user", "content": user_query})
response = run_full_turn(refund_agent, messages) # refund agentUser: Place an order for a black boot.
Assistant: place_order({'item_name': 'black boot'})
Assistant: Your order for a black boot has been successfully placed! If you need anything else, feel free to ask!
User: Actually, I want a refund.
Assistant: execute_refund({'item_name': 'black boot'})
Assistant: Your refund for the black boot has been successfully processed. If you need further assistance, just let me know!
太好了!不过这里我们是手动进行交接的——我们希望智能体能够自行决定何时执行交接。一个简单但出奇有效的方法是给它们一个transfer_to_XXX函数,其中XXX代表某个智能体。模型足够智能,知道在需要交接时调用这个函数!
既然智能体能够表达移交的意图,我们就必须让它真正实现。实现方式有很多种,但有一种特别简洁的方法。
对于我们目前定义的智能体函数,比如execute_refund或place_order,它们会返回一个字符串,该字符串将提供给模型。如果我们改为返回一个Agent对象来指示要转移到哪个智能体会怎样?就像这样:
refund_agent = Agent(
name="Refund Agent",
instructions="You are a refund agent. Help the user with refunds.",
tools=[execute_refund],
)
def transfer_to_refunds():
return refund_agent
sales_assistant = Agent(
name="Sales Assistant",
instructions="You are a sales assistant. Sell the user a product.",
tools=[place_order],
)然后我们可以更新代码来检查函数响应的返回类型,如果是Agent,就更新当前使用的智能体!此外,现在run_full_turn需要返回最新使用的智能体以防发生交接。(我们可以通过Response类来实现这一点以保持整洁。)
class Response(BaseModel):
agent: Optional[Agent]
messages: list现在来看更新后的 run_full_turn:
def run_full_turn(agent, messages):
current_agent = agent
num_init_messages = len(messages)
messages = messages.copy()
while True:
# turn python functions into tools and save a reverse map
tool_schemas = [function_to_schema(tool) for tool in current_agent.tools]
tools = {tool.__name__: tool for tool in current_agent.tools}
# === 1. get openai completion ===
response = client.chat.completions.create(
model=agent.model,
messages=[{"role": "system", "content": current_agent.instructions}]
+ messages,
tools=tool_schemas or None,
)
message = response.choices[0].message
messages.append(message)
if message.content: # print agent response
print(f"{current_agent.name}:", message.content)
if not message.tool_calls: # if finished handling tool calls, break
break
# === 2. handle tool calls ===
for tool_call in message.tool_calls:
result = execute_tool_call(tool_call, tools, current_agent.name)
if type(result) is Agent: # if agent transfer, update current agent
current_agent = result
result = (
f"Transfered to {current_agent.name}. Adopt persona immediately."
)
result_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
messages.append(result_message)
# ==== 3. return last agent used and new messages =====
return Response(agent=current_agent, messages=messages[num_init_messages:])
def execute_tool_call(tool_call, tools, agent_name):
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"{agent_name}:", f"{name}({args})")
return tools[name](**args) # call corresponding function with provided arguments让我们看一个包含更多智能体的示例。
def escalate_to_human(summary):
"""Only call this if explicitly asked to."""
print("Escalating to human agent...")
print("\n=== Escalation Report ===")
print(f"Summary: {summary}")
print("=========================\n")
exit()
def transfer_to_sales_agent():
"""User for anything sales or buying related."""
return sales_agent
def transfer_to_issues_and_repairs():
"""User for issues, repairs, or refunds."""
return issues_and_repairs_agent
def transfer_back_to_triage():
"""Call this if the user brings up a topic outside of your purview,
including escalating to human."""
return triage_agent
triage_agent = Agent(
name="Triage Agent",
instructions=(
"You are a customer service bot for ACME Inc. "
"Introduce yourself. Always be very brief. "
"Gather information to direct the customer to the right department. "
"But make your questions subtle and natural."
),
tools=[transfer_to_sales_agent, transfer_to_issues_and_repairs, escalate_to_human],
)
def execute_order(product, price: int):
"""Price should be in USD."""
print("\n\n=== Order Summary ===")
print(f"Product: {product}")
print(f"Price: ${price}")
print("=================\n")
confirm = input("Confirm order? y/n: ").strip().lower()
if confirm == "y":
print("Order execution successful!")
return "Success"
else:
print("Order cancelled!")
return "User cancelled order."
sales_agent = Agent(
name="Sales Agent",
instructions=(
"You are a sales agent for ACME Inc."
"Always answer in a sentence or less."
"Follow the following routine with the user:"
"1. Ask them about any problems in their life related to catching roadrunners.\n"
"2. Casually mention one of ACME's crazy made-up products can help.\n"
" - Don't mention price.\n"
"3. Once the user is bought in, drop a ridiculous price.\n"
"4. Only after everything, and if the user says yes, "
"tell them a crazy caveat and execute their order.\n"
""
),
tools=[execute_order, transfer_back_to_triage],
)
def look_up_item(search_query):
"""Use to find item ID.
Search query can be a description or keywords."""
item_id = "item_132612938"
print("Found item:", item_id)
return item_id
def execute_refund(item_id, reason="not provided"):
print("\n\n=== Refund Summary ===")
print(f"Item ID: {item_id}")
print(f"Reason: {reason}")
print("=================\n")
print("Refund execution successful!")
return "success"
issues_and_repairs_agent = Agent(
name="Issues and Repairs Agent",
instructions=(
"You are a customer support agent for ACME Inc."
"Always answer in a sentence or less."
"Follow the following routine with the user:"
"1. First, ask probing questions and understand the user's problem deeper.\n"
" - unless the user has already provided a reason.\n"
"2. Propose a fix (make one up).\n"
"3. ONLY if not satesfied, offer a refund.\n"
"4. If accepted, search for the ID and then execute refund."
""
),
tools=[execute_refund, look_up_item, transfer_back_to_triage],
)最后,我们可以在循环中运行这段代码(这不会在Python笔记本中运行,因此你可以在单独的Python文件中尝试):
agent = triage_agent
messages = []
while True:
user = input("User: ")
messages.append({"role": "user", "content": user})
response = run_full_turn(agent, messages)
agent = response.agent
messages.extend(response.messages)作为概念验证,我们已将这些想法打包到一个名为Swarm的示例库中。这仅作为示例用途,不应直接在生产环境中使用。不过,欢迎借鉴这些想法和代码来构建您自己的版本!