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

相关推荐
天行无忌1 小时前
2025:AI炒作巅峰,2026:价值回归与实用落地之年
agent
龙腾亚太3 小时前
大模型在工业物流领域有哪些应用
人工智能·具身智能·智能体·世界模型·智能体培训·具身智能培训
装不满的克莱因瓶3 小时前
【Coze智能体实战二】一键生成儿歌背单词视频
人工智能·ai·实战·agent·工作流·智能体·coze
沛沛老爹5 小时前
Web开发者实战RAG评估:从指标到工程化验证体系
前端·人工智能·llm·agent·rag·评估
荣江6 小时前
从无状态到有记忆:AI Agent 记忆系统的演进与 Cortex Memory 的实践
agent
loong_XL10 小时前
elastic kibana可视化数据统计案例
运维·ai·智能体
小白跃升坊10 小时前
基于AI+企微的智能报销系统
ai·企业微信·agent·智能体·效率提升·日常报销·报销流程
AlanHou12 小时前
Dify、n8n 还是 Coze?万字长文解析三大主流 AI Agent 平台
人工智能·agent
龙腾亚太14 小时前
如何有效整合文本、图像等不同模态信息,提升模型跨模态理解与生成能力
langchain·多模态·dify·具身智能·智能体·vla
大模型真好玩1 天前
LangGraph智能体开发设计模式(一)——提示链模式、路由模式、并行化模式
人工智能·langchain·agent