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

相关推荐
MonkeyKing_sunyuhua6 小时前
5.6 Microsoft Semantic Kernel:专注于将LLM集成到现有应用中的框架
人工智能·microsoft·agent
古希腊掌管学习的神19 小时前
[LangGraph教程]LangGraph04——支持人机协作的聊天机器人
人工智能·语言模型·chatgpt·机器人·agent
MonkeyKing_sunyuhua1 天前
4.4 记忆机制与上下文管理:短期与长期记忆的设计与应用
大数据·人工智能·agent
AI蜗牛车1 天前
【LLM+Code】Github Copilot Agent/VsCode Agent 模式Prompt&Tools详细解读
人工智能·语言模型·github·copilot·agent
阿桨2 天前
【(保姆级教程)Ubuntu24.10下部署Dify】
ubuntu·dify·智能体
亚里随笔2 天前
TORL:解锁大模型推理新境界,强化学习与工具融合的创新变革
人工智能·llm·agent·agentic rl
梦想就是摸鱼3 天前
利用COZE搭建工作流,实现自动爬取头条数据录入飞书表格
workflow·智能体
KuaFuAI3 天前
DeepSeek开源引爆AI Agent革命:应用生态迎来“安卓时刻”
人工智能·开源·agent·deepseek
MCP星球4 天前
我们离Agent终极形态又近了一步:全球首个支持谷歌A2A(Agent-to-Agent)协议的智能体注册网站上线Product Hunt,未来智能体协作网络
ai·agent·智能体·智能体协作·a2a·a2a协议·agent network·智能体网络·谷歌a2a·agent2agent
(initial)5 天前
第八章:探索新兴趋势:Agent 框架、产品与开源力量
人工智能·agent