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

相关推荐
king of code porter9 小时前
百宝箱企业版搭建智能体应用-平台概述
人工智能·大模型·智能体
bylander12 小时前
【AI学习】几分钟了解一下Clawdbot
人工智能·智能体·智能体应用
king of code porter13 小时前
百宝箱企业版搭建智能体应用-创建应用
人工智能·大模型·智能体
阿杰学AI15 小时前
AI核心知识75——大语言模型之MAS (简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·agent·多智能体协作·mas
AI周红伟17 小时前
周红伟:OpenClaw 智能体技术原理和技术原理,企业级部署和应用
智能体·openclaw
laplace012317 小时前
Clawdbot 部署到飞书(飞连)使用教程(完整版)
人工智能·笔记·agent·rag·clawdbot
weixin_5091383420 小时前
专业书评 | 《智能体认知动力学导论》:当思维有了形状
人工智能·智能体·认知动力学
GuoDongOrange20 小时前
智能体来了从 0 到 1:工作流在智能体系统中的真实作用
ai·智能体·从0到1·智能体来了·智能体来了从0到1
山顶夕景21 小时前
【LLM】多模态智能体Kimi-K2.5模型
llm·agent·多模态
yuanlulu21 小时前
Agent_Skills_完全教程「AI生成」
人工智能·大语言模型·agent·智能体·skill·claude code·opencode