swarm
安装方法
需要 Python 3.10+
方法一:
pip install git+ssh://git@github.com/openai/swarm.git
or
pip install git+https://github.com/openai/swarm.git
方法二:
在openai 官网下载swarm (https://github.com/openai/swarm)放在本地文件夹中,在工程中直接引用。
# import os
# 更换为你的本地路径
# sys.path.append('/root/swarm)
初始化
### 头文件引入
import os
from openai import OpenAI
from swarm import Swarm, Agent
环境变量设置
#临时设置环境变量
os.environ["OPENAI_API_KEY"] = 'your-openai-key'
os.environ["OPENAI_BASE_URL"] = 'https://ai.newone.tech/proxy/v1'
client = OpenAI (api_key=api_key,base_url="https://ai.newone.tech/proxy/v1")
使用方法介绍
单个Agent使用
from swarm import Swarm, Agent
swarm_client = Swarm(client)
agent_A = Agent(
name="英文问答机器人",
model = 'gpt-4o',
instructions="无论用户发送的消息是什么语言,请用英文回答.",
functions=[transfer_to_agent_b],
)
agent_B = Agent(
name="中文问答机器人",
model = 'gpt-4o',
instructions="无论用户发送的消息是什么语言,请用中文回答.",
)
test_message1 = {"role" : "user","content":"你好,好久不见,请介绍下你自己。" }
response_A = swarm_client.run(
agent = agent_A,
messages = [test_message1],
)
print(response.messages[-1]["content"])
多个Agent使用
from swarm import Swarm, Agent
swarm_client = Swarm(client)
def transfer_to_agent_b():
return agent_b
agent_a = Agent(
name = "Agent A",
instructions = "你是一个乐于助人的智能体.",
functions=[transfer_to_agent_b],
)
agent_b = Agent(
name = "Agent B",
instructions="只用美好的语句回答.",
)
test_message1 = {"role" : "user","content":"我想与智能体B对话。" }
response = swarm_client.run(
agent = agent_a,
messages = [test_message1],
)
print(response.messages[-1]["content"])