OpenAI发布了多智能体框架Swarm,它采用轻量化的设计和可定制的原语,使多智能体协作和执行变得更加容易和灵活。
原文标题:OpenAI今天Open了一下:开源多智能体框架Swarm
原文作者:机器之心
冷月清谈:
- Swarm采用两种原语抽象:智能体(agent)和交接(handoff),可简化多智能体协作和执行。
- 与为生产目的开发的Assistants API不同,Swarm更适合处理独立功能和指令较多的场景,并提供更细粒度的控制。
- Swarm核心组件包括客户端(client)、智能体(Agent)、函数(Function),用户可以通过调用client.run()函数与智能体交互。
- Swarm智能体可通过返回其他智能体实现交接,并通过返回Result对象更新上下文变量。
- Swarm支持流式处理,方便及时获取智能体处理信息。
怜星夜思:
2、Swarm框架有哪些潜在的应用场景?
3、Swarm框架的未来发展方向是什么?
原文内容
编辑:Panda
毫无疑问,多智能体肯定是 OpenAI 未来重要的研究方向之一,前些天 OpenAI 著名研究科学家 还在 X 上为 OpenAI 正在组建的一个新的多智能体研究团队招募机器学习工程师。
-
项目地址:https://github.com/openai/swarm
pip install git+ssh://git@github.com/openai/swarm.git
from swarm import Swarm, Agentclient = Swarm()
def transfer_to_agent_b():
return agent_bagent_a = Agent(
name=“Agent A”,
instructions=“You are a helpful agent.”,
functions=[transfer_to_agent_b],
)agent_b = Agent(
name=“Agent B”,
instructions=“Only speak in Haikus.”,
)response = client.run(
agent=agent_a,
messages=[{“role”: “user”, “content”: “I want to talk to agent B.”}],
)
print(response.messages[-1][“content”])
Hope glimmers brightly,
New paths converge gracefully,
What can I assist?
from swarm import Swarm
client = Swarm()
-
先让当前智能体完成一个结果
-
执行工具调用并附加结果
-
如有必要,切换智能体
-
如有必要,更新上下文变量
-
如果没有新的函数调用,则返回
agent = Agent(
instructions="You are a helpful agent."
)
def instructions(context_variables): user_name = context_variables["user_name"] return f"Help the user, {user_name}, do whatever they want."
agent = Agent(
instructions=instructions
)
response = client.run(
agent=agent,
messages=[{“role”:“user”, “content”: “Hi!”}],
context_variables={“user_name”:“John”}
)
print(response.messages[-1][“content”])
Hi John, how can I assist you today?
-
Swarm Agent 可以直接调用 Python 函数。
-
函数通常应返回一个字符串(数值会被转换为字符串)。
-
如果一个函数返回了一个 Agent,则执行过程将转交给该 Agent。
-
如果函数定义了 context_variables 参数,则它将由传递到 client.run() 的 context_variables 填充。
def greet(context_variables, language): user_name = context_variables["user_name"] greeting = "Hola" if language.lower() == "spanish" else "Hello" print(f"{greeting}, {user_name}!") return "Done"agent = Agent(
functions=[print_hello]
)
client.run(
agent=agent,
messages=[{“role”: “user”, “content”: “Usa greet() por favor.”}],
context_variables={“user_name”: “John”}
)
Hola, John!
sales_agent = Agent(name="Sales Agent")def transfer_to_sales():
return sales_agentagent = Agent(functions=[transfer_to_sales])
response = client.run(agent, [{“role”:“user”, “content”:“Transfer me to sales.”}])
print(response.agent.name)
Sales Agent
sales_agent = Agent(name="Sales Agent")def talk_to_sales():
print(“Hello, World!”)
return Result(
value=“Done”,
agent=sales_agent,
context_variables={“department”: “sales”}
)agent = Agent(functions=[talk_to_sales])
response = client.run(
agent=agent,
messages=[{“role”: “user”, “content”: “Transfer me to sales”}],
context_variables={“user_name”: “John”}
)
print(response.agent.name)
print(response.context_variables)
Sales Agent
{'department': 'sales', 'user_name': 'John'}
-
文档字符串会转换为函数 description。
-
没有默认值的参数会设置为 required。
-
类型提示会映射到参数的 type(默认为 string)。
-
不明确支持对每个参数进行描述,但如果只是在文档字符串中添加,应该能以相似的方式工作。
def greet(name, age: int, location: str = "New York"): """Greets the user. Make sure to get their name and age before calling.
Args:
name: Name of the user.
age: Age of the user.
location: Best place on earth.
“”"
print(f"Hello {name}, glad you are {age} in {location}!")
{
“type”: “function”,
“function”: {
“name”: “greet”,
“description”: “Greets the user. Make sure to get their name and age before calling.\n\nArgs:\n name: Name of the user.\n age: Age of the user.\n location: Best place on earth.”,
“parameters”: {
“type”: “object”,
“properties”: {
“name”: {“type”: “string”},
“age”: {“type”: “integer”},
“location”: {“type”: “string”}
},
“required”: [“name”, “age”]
}
}
}
stream = client.run(agent, messages, stream=True)
for chunk in stream:
print(chunk)
-
{"delim":"start"} 和 {"delim":"start"},用于在 Agent 每次处理单个消息(响应或函数调用)时发出信号。这有助于识别 Agent 之间的切换。
-
为方便起见,{"response": Response} 将在流的末尾返回带有已聚合的(完整)响应的 Response 对象。
-
Ilan Bigio - ibigio
-
James Hills - jhills20
-
Shyamal Anadkat - shyamal-anadkat
-
Charu Jaiswal - charuj
-
Colin Jarvis - colin-openai
© THE END
转载请联系本公众号获得授权
投稿或寻求报道:content@jiqizhixin.com







