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

相关推荐
Linux猿2 天前
OpenAI Swarm 多智能体框架介绍
openai·多智能体·swarm·智能体·openai swarm·多智能体框架
leijiwen3 天前
基于智能体和RWA的分布式商业生态商业模型架构设计
分布式·智能体·rwa·商业模型
源大模型4 天前
OS-Genesis:基于逆向任务合成的 GUI 代理轨迹自动化生成
人工智能·gpt·智能体
虎鲸不是鱼9 天前
Win10环境借助DockerDesktop部署Open web UI集成DeepSeek
人工智能·ai·chatgpt·agent·deep seek
每天看一遍,防止恋爱&&堕落11 天前
5、大模型的记忆与缓存
缓存·langchain·大模型·llm·agent·大模型缓存
实在智能RPA13 天前
实在RPA案例|视源股份:驱动20+核心场景数字化升级,组织效能提升超80%
人工智能·microsoft·自动化·大模型·agent·rpa
您好啊数模君15 天前
2025.2.6 数模AI智能体大更新,更专业的比赛辅导,同提示词效果优于gpt-o1/o3mini、deepseek-r1满血
数学建模·ai·智能体·豆包·gpt-o1·deepseek
cxr82819 天前
构建由局部观测、分布式决策与全局奖励协同作用的多智能体强化学习系统
分布式·强化学习·智能体
山顶夕景20 天前
【LLM-agent】(task2)用llama-index搭建AI Agent
大模型·llm·agent·智能体·llama-index
产品媛Gloria Deng21 天前
分享| RL-GPT 框架通过慢agent和快agent结合提高AI解决复杂任务的能力-Arxiv
人工智能·gpt·ai·agent·ai智能体