毫无疑问,多智能体肯定是 openai 未来重要的研究方向之一,前些天 openai 著名研究科学家 noam brown 还在 x 上为 openai 正在组建的一个新的多智能体研究团队招募机器学习工程师。
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 Swarmclient=Swarm()
登录后复制
-
先让当前智能} E : { q ^ F体完成一个结果 -
执行工具调用% [ #并附加结果 -
如有必要,切换智0 h T c能体 -
如有必要,更新上下文变量 -
如果没有新的函数调用,则返回
响应字段
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?
登录后复制
-
Sr , Rwj _ & a I Harm Agent 可以直接调用 Python 函数。 -
函数通常应返回一个字符串(数值会被转换为字符l ` t ^ /串)。 -
如果一个函数返回了一个 Ag# D T / f d \ W tent,则执行过程将转交给该 Agent。 -
如s b `果函数定义了 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! 如果某个 Agent 函数调用出错(缺少函数、参数错误等),则会在聊天之中附加一条报错响应,以便 Agent 恢复正常。 如果 Ageny 调用多个函数,则按顺序执行它们。 交接和更新上下文变量 通过在返回的函数中包含一个 Agent,可将执行过程交接给这个 Agent。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'}
登录后复制n T $ w /
-
文档字符串会转换为函数 description。 -
没有默认值的参数会设置为 requi5 B } H R * # Ored。 -
类型提示会映射到参数的 t6 z h ] Pype(默认为 string)。 -
不明确支持对每个参数进行描述,但如果只是在文档字符B W S ? s h Z B串中添加,应该能以相似的方式工作。
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. Args: name: Name of the user. age: Age of the user. 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)
登录b Z b h Q U D后复制
-
{“delim”:”start”} 和 {“delim”:”start”},用于在 Agent 每次处理单个消息(响应或函数调用)时发出信号。这有助于识别 Agent 之间的切换。 -
为方便起见,{“response”: Response} 将在流的末尾返回E m x I # 2 u * =带有已聚合的(完整)响应的 Resp_ s 4 w @ R sonse 对象。
-
Ilan Bigio – ibigio -
James Hills – jhills20 -
Shyamal Anadkat – shyamal-anadkat -
Charu Jaiswal – charuj -
Colin Jarvis – colin-opena& 0 A b Ui
以上就是OpenAI今天Open了一下:开源多智能体框架Swarm的详细内容!