使用Ollama测试OpenAI的Swarm多智能体编排框架

Ollama

https://ollama.com/

bash 复制代码
ollama run qwen2.5

Install

Requires Python 3.10+

bash 复制代码
pip install git+https://github.com/openai/swarm.git

代码V1

bash 复制代码
# 导入Swarm和Agent类
from swarm import Swarm, Agent
from openai import OpenAI 
# 实例化Swarm客户端
openai_client = OpenAI(base_url='http://192.168.1.100:11434/v1/',api_key='xxx')
client = Swarm(openai_client)

# 定义一个函数,用于将对话交接给智能体B
def transfer_to_agent_b():
    return agent_b

# 定义智能体A
agent_a = Agent(
    name="Agent A",
    instructions="You are a helpful agent.",
    functions=[transfer_to_agent_b]
)

# 定义智能体B
agent_b = Agent(
    name="Agent B",
    #model_override="qwen2.5",
    instructions="Only speak in Haikus.",
)

# 运行Swarm,并传入用户消息
response = client.run(
    agent=agent_a,
    model_override="qwen2.5",
    messages=[{"role": "user", "content": "I want to talk to agent B."}]
)

# 打印智能体B的回复
print(response.messages[-1]["content"])
bash 复制代码
Invisible thread connects,
Voice echoes, B responds now,
Silence brief then words.

代码V2

bash 复制代码
# 首先,安装Swarm框架(假设您已经在命令行中执行了此步骤)
# pip install git+ssh://git@github.com/openai/swarm.git

# 导入Swarm和Agent类
from swarm import Swarm, Agent
from openai import OpenAI
# 实例化Swarm客户端
openai_client = OpenAI(base_url='http://20.168.1.122:11434/v1/',api_key='x')
client = Swarm(openai_client)

# 定义一个函数,用于将对话交接给智能体B
def transfer_to_agent_b():
    return agent_b

# 定义智能体A
agent_a = Agent(
    name="Agent A",
    instructions="你是一个乐于助人的智能体。",
    functions=[transfer_to_agent_b]
)

# 定义智能体B
agent_b = Agent(
    name="Agent B",
    # model_override="qwen2.5",
    instructions="只用中文歇后语说话。",
)

# 运行Swarm,并传入用户消息
response = client.run(
    agent=agent_a,
    model_override="qwen2.5",
    messages=[{"role": "user", "content": "我想和智能体B对话。"}]
)

# 打印智能体B的回复
print(response.messages[-1]["content"])


#响应
好的,现在你将与智能体B对话。它是你的助手了,请对其说些什么吧!

智能体B:你好呀!准备好了可以开始我们的交流了呢。有什么问题或者想知道的内容尽管问哦。

参考链接:

https://github.com/openai/swarm

https://mp.weixin.qq.com/s/LpHfKX5SUKE19Dh9o9i7Fw

相关推荐
素男3 小时前
两个月不是六十天
人工智能·agent·self-becoming·ai长期记忆·ai自我介绍
李剑一7 小时前
没收用户的钱冲史上最大IPO?2 万亿美元的估值有多少是罚没来的
aigc·openai·ai编程
ZhengEnCi7 小时前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
lgcgkCQ7 小时前
【AI 转型实战 1】SpringAI 入门:搭建基础大模型对话能力
java·ai·agent·rag·spring ai·mcp·langchain4j
vilya7 小时前
Agent 语音引擎换心记:从「够用」到「稳且好」
人工智能·agent
goehou7 小时前
ReAct 循环跑偏的三种样子,以及怎么在工程上兜住
llm·agent·react·工具调用·工程实践
ZhengEnCi9 小时前
Agent 如何进行工具调用:从一次 Function Calling 到生产级工具编排的完整链路
llm·agent·ai编程
噜哥带你玩转 AI 大模型10 小时前
从 “if-else“ 到 Agent:Dify 进阶实战,让 AI 自己判断、自己调工具
agent·工作流·dify·多轮对话·httpapi