项目地址:https://github.com/openai/swarm
本文简单介绍一下如何使用Swarm:
一、首先需要安装环境(python3.10+)
# 创建python虚拟环境(可选)conda create -n python=3.10
pip install git+ssh://git@github.com/openai/swarm.git
二、设置 OpenAI API
import osos.environ['OPENAI_API_KEY'] =''
三、下面是一个 handoff的例子
from swarm import Swarm, Agent
client = Swarm()
english_agent = Agent( name="English Agent", instructions="You only speak English.",)
spanish_agent = Agent( name="Spanish Agent", instructions="You only speak Spanish.",)
def transfer_to_spanish_agent(): """Transfer spanish speaking users immediately.""" return spanish_agent
english_agent.functions.append(transfer_to_spanish_agent)
messages = [{"role": "user", "content": "Hola. ¿Como estás?"}]response = client.run(agent=english_agent, messages=messages)
print(response.messages[-1]["content"])
输出:
¡Hola! Estoy bien, gracias. ¿Y tú, cómo estás?
参考文献:https://cookbook.openai.com/examples/orchestrating_agents