OpenAI Swarm 多智能体框架介绍

目录

[一、什么是 Swarm ?](#一、什么是 Swarm ?)

[二、Swarn 使用方法](#二、Swarn 使用方法)

[2.1 安装依赖](#2.1 安装依赖)

[2.2 实例](#2.2 实例)


本篇文章主要介绍下 OpenAI Swarm 多智能体框架。

一、什么是 Swarm ?

Swarm 是 OpenAI 开源的一款多智能体框架,但 OpenAI 对 Swarm 的定位是教育性、实验性的框架,用于轻量级多智能体编排。

Educational framework exploring ergonomic, lightweight multi-agent orchestration. Managed by OpenAI Solution team.

下面就详细介绍下 Swarm 的使用。

二、Swarn 使用方法

2.1 安装依赖

需要 Python 3.10+,Python 的安装方法不多介绍,执行如下命令安装框架。

bash 复制代码
pip install git+ssh://git@github.com/openai/swarm.git

2.2 实例

下面通过一个实例来介绍如下使用 swarm 框架。

python 复制代码
from openai import OpenAI
from swarm import Swarm, Agent

Qwen_client = OpenAI(
    base_url = 'https://dashscope.aliyuncs.com/compatible-mode/v1',
    api_key='XXXXXXX'
)

# 在swarm构建时,指定与ollama连接的客户端
client = Swarm(client=Qwen_client)

agent_b = Agent(
    name="Agent B",
    model="qwen2-72b-instruct",
    instructions="Only speak in Haikus.",
)

def transfer_to_agent_b(**kwargs):
    return agent_b


agent_a = Agent(
    name="Agent A",
    model="qwen2-72b-instruct",
    instructions="你是一个智能助手",
    functions=[transfer_to_agent_b]
)



response = client.run(
    agent=agent_a,
    messages=[{"role": "user", "content": "I want to talk to agent B."}],
)

print(response.messages[-1]["content"])

其中,Swarm() 函数内部实例了一个OpenAI 客户端,所以默认使用 OpenAI 模型,如果想使用其他模型,重新初始化 OpenAI 客户端即可,上面代码重新创建了一个阿里的客户端,使用了Qwen 72B 模型。

更多实例请查看 examples 目录下的代码。

相关推荐
王小酱2 小时前
Claude Code 创始人 Boris 揭秘:团队 10 倍效率技巧
openai·ai编程·aiops
王小酱4 小时前
结合OpenSpec 与 Everything-Claude-Code (ECC) 的构建团队工作流程
openai·ai编程·aiops
王小酱9 小时前
AI 编程实战指南:核心概念梳理与 Claude Code 高效技巧
aigc·openai·ai编程
Mr_凌宇11 小时前
个人向:本机MAC部署OpenClaw过程记录
openai·ai编程
吴佳浩17 小时前
OpenClaw Windows 完整安装与本地模型配置教程(实战版)
llm·openai
用户47949283569151 天前
[开源分享] Agent 指挥 Agent,我做了一个让 Claude Code / Codex / Gemini/... 组成"军团"并行干活的工具
aigc·openai·claude
王小酱1 天前
Everything Claude Code 完全长篇指南
openai·ai编程·aiops
用户47949283569151 天前
像 Tech Lead 一样管理 AI Agent:一条命令,并行执行,交叉验证
aigc·openai·agent
树獭叔叔1 天前
06-大模型如何"学习":从梯度下降到AdamW优化器
后端·aigc·openai
王小酱1 天前
Everything Claude Code 速查指南
openai·ai编程·aiops