OpenAI多智能体框架Swarm实测—基于Qwen开源模型

1、基础配置

底层模型:Qwen开源模型

swarm:0.1.0

2、agent设置

python 复制代码
agent_a = Agent(name="Agent A", instructions="你是Agent A,是一个有用的助手", functions=[transfer_to_agent_b], model=model_name,
                tool_choice='auto')
agent_b = Agent(name="Agent B", instructions="你是Agent B,仅仅使用繁体字说话", functions=[transfer_to_agent_a],model=model_name, tool_choice='auto')

agent之间的跳转函数

python 复制代码
def transfer_to_agent_b():
    return agent_b
def transfer_to_agent_a():
    return agent_a

3、单智能体示例

输入示例:

python 复制代码
msg = [{"role": "user", "content": "你好,小明的年龄是12岁。"},
       {"role": "user", "content": "我的年龄是小明的2倍"},
       {"role": "user", "content": "请问我的年龄是多少"}]

4、多智能体示例

输入示例

python 复制代码
msg = [{"role": "user", "content": "你好,你是谁?"},
       {"role": "user", "content": "我想和Agent B说话"},
       {"role": "user", "content": "你好,你是谁?由哪家公司训练出来的?"},
       {"role": "user", "content": "写一副春节对联"},
       {"role": "user", "content": "帮我转到agent a"},
       {"role": "user", "content": "你好,你是哪家公司训练出来的?"},
       {"role": "user", "content": "写一首七言绝句,有关爱情的。"}]

初始的agent设置为Agent A,上面问答的基本流程是: Agent A >>>Agent B >>>Agent A,具体输出如下

可以看到,开源模型+Swarm框架,可以完成Agent之间的跳转,回复效果还不错。

5、测试脚本

python 复制代码
agent = agent_a
history = []
history_dict = {'history_agent_a':[], 'history_agent_b':[]}
for m in msg:
    print('{}: {}'.format(m['role'], m['content']))
    history = history + [m]
    str_name_forward = agent.name.lower().replace(' ','_')
    history_dict['history_{}'.format(str_name_forward)] += [m]

    response = client.run(agent=agent, messages=history_dict['history_{}'.format(str_name_forward)], history_messages=history_dict)

    agent = response.agent
    res = response.messages
    str_name_backward = agent.name.lower().replace(' ', '_')
    history_dict['history_{}'.format(str_name_backward)] += res
    history = history + res

    print("{}:{}".format(response.agent.name, response.messages[-1]["content"]))

history保存全部会话记录,history_agent_a和history_agent_b分别保存子Agent的会话记录

swarm git链接:swarm

相关推荐
Yeliang Wu3 分钟前
基于LangGraph的聊天机器人模拟评估系统(Ubuntu 2204 + Conda + vLLM)
智能体·vllm·langgraph
后端小肥肠21 分钟前
通吃网文投稿+AI漫剧版权!我用 n8n+飞书搭了个“万字爆款小说流水线”
人工智能·aigc·agent
七夜zippoe1 小时前
基于ReAct框架的智能体构建实战 - 从原理到企业级应用
前端·javascript·react.js·llm·agent·react
是ADI呀1 小时前
关于 RAG,我的学习路径与调优心得
agent
无垠的广袤2 小时前
【工业树莓派 CM0 NANO 单板计算机】小智语音聊天
人工智能·python·嵌入式硬件·语言模型·树莓派·智能体·小智
研华科技Advantech17 小时前
储能AI化的数据瓶颈与破解路径:研华全栈方案实践分析
数据库·人工智能·储能·智能体
大模型教程19 小时前
构建自己的Agent——最佳开源 RAG 框架选型指南
程序员·llm·agent
大模型教程19 小时前
小猫都能懂的大模型原理 1 - 深度学习基础
程序员·llm·agent
胡耀超20 小时前
AI的记忆革命:从Titans架构到长时运行智能体,谷歌Google,Anthropic,NeurIPS 2025
人工智能·python·ai·架构·智能体·上下文·titans
AI大模型21 小时前
大模型从入门到实践,最被读者认可的几本书就在这里!
程序员·llm·agent