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

相关推荐
xiaoming-wu9 小时前
数据分析Agent构建
数据分析·大语言模型·agent
ZHOU_WUYI21 小时前
Dify源码教程:账户和密码传递分析
agent·dify
程序员鱼皮3 天前
全球首个无限执行的 AI 出现!给我玩爽了
计算机·ai·互联网·agent·代码
小阿鑫3 天前
记录第一次公司内部分享:如何基于大模型搭建企业+AI业务
大模型·llm·agent·大模型落地·ai落地·mcp·mcpserver
攻城狮7号3 天前
AI浪潮下的思辨:傅盛访谈之我见
人工智能·深度学习·agent
ZHOU_WUYI3 天前
吴恩达MCP课程(5):research_server_prompt_resource.py
agent·mcp
sg_knight3 天前
大模型连接万物的“万能插座”:深度解析模型上下文协议MCP
人工智能·ai·大模型·agent·ai大模型·mcp·模型上下文协议
Json____3 天前
服装产品属性描述数据集(19197条),AI智能体知识库收集~
人工智能·数据集·知识库·ai训练·智能体·ai智能体·ai模型
audyxiao0014 天前
数据挖掘顶刊《IEEE Transactions on Knowledge and Data Engineering》2025年5月研究热点都有些什么?
大数据·人工智能·数据挖掘·大模型·图技术·智能体·时序动态建模