Swarms 框架 Examples 目录全览:从单 Agent 到企业级多智能体编排的实战示例导航
2026/9/17 2:36:12 网站建设 项目流程

Swarms 框架 Examples 目录全览:从单 Agent 到企业级多智能体编排的实战示例导航

【免费下载链接】swarmsThe Enterprise-Grade Multi-Agent Orchestration Framework. Website: https://swarms.ai项目地址: https://gitcode.com/GitHub_Trending/swar/swarms

导读

本文以 Swarms 开源仓库中的 examples/README.md 为骨架,系统梳理examples/目录的完整结构,覆盖多智能体系统、单 Agent 能力、工具集成、MCP 协议、语音 Agent、CLI 命令与部署方案等核心主题,并结合仓库源码与测试对关键示例做纵深解读。读完本文,你将掌握如何根据业务场景在 Swarms 框架中快速定位并运行对应的示例代码,理解各模块间的调用关系与实现原理。


一、目录总览:Swarms 示例体系的组织方式

Swarms 的examples/目录是框架能力的"百科全书",每个子目录聚焦多智能体系统、单 Agent、工具与集成的特定方面。顶层布局如下:

examples/ ├── README.md # 本文所基于的总索引文档 ├── changelogs/ # 各版本(v9~v14)更新示例与变更日志 ├── cli/ # CLI 命令示例 ├── guides/ # 综合指南:demos、部署、graph workflow、x402 等 ├── mcp/ # Model Context Protocol 集成 ├── models/ # 各模型供应商(OpenAI、Anthropic、DeepSeek…)示例 ├── multi_agent/ # 多智能体架构与模式 ├── reasoning_agents/ # 推理增强 Agent(judge、MOA、router) ├── single_agent/ # 单 Agent 能力(工具、技能、RAG、视觉…) ├── tools/ # 工具集成示例 └── utils/ # 工具函数与辅助实现

注意:原文档中提到的voice_agents/marketplace/ui/swarms_api/等目录在仓库实际布局中位于 guides/voice_agents/、single_agent/integrations/marketplace/ 等位置,阅读时请以实际路径为准。

二、多智能体系统:multi_agent/核心模式速查

2.1 目录构成

multi_agent/README.md 是完整的多智能体示例文档,其子目录覆盖了从基础协作到研究论文复现的丰富模式:

子目录核心内容
agent_rearrange_examples/Agent 重新编排(fan-out / fan-in / multi-stage)
asb/Auto Swarm Builder(ASB)自动构建 swarm
batched_grid_workflow/批量网格工作流
caching_examples/Agent 缓存
council/council_of_judges/委员会模式与裁判评估
debate_examples/alternate_debates/多 Agent 辩论与变体
election_swarm_examples/选举与投票 swarm
forest_swarm_examples/森林/树状 swarm
graphworkflow_examples/图工作流(graph_workflow_example.py)
groupchat/异步自选择群聊:Agent 自行决定是否回复
heavy_swarm_examples/面向复杂任务的 HeavySwarm
hierarchical_swarm/层级结构(hierarchical_swarm_example.py)
majority_voting/多数投票与共识
moa_examples/Mixture of Agents(MoA)
sequential_workflow/顺序工作流
simulations/多 Agent 仿真
social_algorithms_examples/社交算法模式
spreadsheet_examples/基于表格的 Agent
swarm_router/通用 swarm 路由编排
orchestration_examples/协调模式
paper_implementations/研究论文复现

2.2 关键模式的源码佐证

以 Graph Workflow 为例,核心实现在 swarms/structs/graph_workflow.py,对应的测试位于 tests/structs/test_graph_workflow.py,示例则聚合在 graphworkflow_examples/。再如 Agent Rearrange 的实现位于 swarms/structs/agent_rearrange.py,其测试 tests/structs/test_agent_rearrange.py 验证了多种重排流程。

从源码结构看,这些模式共享swarms/structs/下的同一套编排基元(如run()arun()、回调与流式接口),因此示例间可以互相组合。

三、单 Agent 系统:single_agent/能力地图

3.1 目录构成

single_agent/README.md 提供完整的单 Agent 示例索引,能力覆盖:

类别说明
getting_started/首次运行:最小 Agent、交互模式、onboarding
capabilities/tools/工具集成:MCP、Exa search、LiteLLM、多工具、结构化输出、浏览器 Agent
capabilities/skills/动态/自定义/预置技能(代码评审、数据可视化、财务分析)
capabilities/rag/基于 Pinecone 与 Qdrant 的 RAG
capabilities/vision/视觉与多模态 Agent(multimodal_example.py)
capabilities/streaming/Token 流式输出、流式工具与循环
capabilities/prompt_caching/供应商侧提示缓存(Anthropic、OpenAI)
reasoning/推理 Agent:judge、一致性、迭代、reasoning duo/router
autonomy/autonomous_agents/自主循环、子 Agent、run_bashlooper(auto_agent.py)
autonomy/handoffs/Agent 间交接(duo_agent.py)
integrations/marketplace/市场提示词与发布 Agent 到市场
integrations/external_agents/外部 Agent 系统桥接

3.2 首个 Agent:最小可运行示例

getting_started/simple_agent.py 是官方推荐的新手起点:

from swarms import Agent agent = Agent( name="Research Agent", description="A research agent that can answer questions", model_name="claude-sonnet-4-20250514", streaming_on=True, max_loops=1, interactive=True, ) out = agent.run( "What are the best arbitrage trading strategies for altcoins? Give me research papers and articles on the topic." ) print(out)

关键参数说明(Agent类定义于 swarms/structs/agent.py):

  • name/agent_name:Agent 名称,用于日志与追踪;
  • description/agent_description:职责描述,供编排器与其他 Agent 理解该 Agent 的能力;
  • model_name:指定底层模型,统一经由 LiteLLM 路由;
  • streaming_on:是否启用 Token 流式输出;
  • max_loops:单次run()内最大自循环轮数(0 或 1 表示单次推理);
  • interactive:交互模式开关。

3.3 交互模式:REPL 风格 Agent

getting_started/interactive.py 演示交互式 REPL Agent,适合调试与即时问答:

from swarms import Agent agent = Agent( agent_name="Quantitative-Trading-Agent-test", agent_description="Advanced quantitative trading and algorithmic analysis agent", system_prompt="You are a helpful assistant ... and your name is Quantitative-Trading-Agent", model_name="gpt-5.4", max_loops=5, interactive=True, ) out = agent.run() print(out)

注意此处run()不带参数即进入交互式输入循环,max_loops=5限制单次交互的自循环次数。

需要说明:原文档提到的capabilities/tools/capabilities/skills/capabilities/rag/capabilities/vision/capabilities/streaming/capabilities/prompt_caching/reasoning/autonomy/autonomous_agents/autonomy/handoffs/等路径,在仓库实际布局中均位于single_agent/下(如 capabilities/vision/),阅读时请以实际路径为准。

四、工具与集成:tools/实战清单

tools/README.md 是完整的工具示例文档,核心条目包括:

示例说明
agent_as_tools.py将 Agent 作为工具使用
browser_use_as_tool.py/browser_use_demo.py浏览器自动化
claude_as_a_tool.py将 Claude 模型作为工具
exa_search_agent.py/exa_search_agent_quant.py/exa_search_test.pyExa 搜索集成与量化分析
firecrawl_agents_example.pyFirecrawl 网页抓取
base_tool_examples/BaseTool 基类实现示例(含函数转 schema、schema 校验、Anthropic 专项测试等)
stagehand/Stagehand UI 自动化:wrapper / tools / MCP / 多 Agent 工作流四个递进示例,配 tests/

工具系统的底层实现位于 swarms/tools/(base_tool.pytool_registry.pymcp_manager.py等),tests/tools/test_base_tool.py 与 tests/tools/test_mcp_manager.py 可验证其行为。

五、API 与协议

5.1 Swarms API

swarms_api/ 下的示例覆盖 Agent 概览、批量处理、客户端集成、团队示例(医院管理、法律团队、ICD-10 医疗编码分析)与速率限制。

5.2 Model Context Protocol(MCP)

mcp/README.md 是 MCP 集成的入口,按三个角色划分:

  • agents/(给 Agent 接入 MCP 服务器工具):01_deepwiki_repo_qa.py(无需 API key 的快速入门)、02_gitmcp_repo_docs.py04_multi_server_agent.py(单 Agent 多服务器)、05_exa_web_search.pydeepwiki_minimal.py(最小mcp_urlAgent)、mcp_connection_object.pyMCPConnection带 headers/auth/timeout)、multi_mcp_urls.pymcp_with_local_tools.py(MCP 工具与本地 Python 工具并存)等,FREE_MCP_SERVERS.md 收录真实公共 MCP 服务器目录。
  • servers/(构建 Agent 可连接的 MCP 服务器):crypto_price_server.py(FastMCP 加密价格)、okx_crypto_server.py(OKX 端口 8001)、agent_as_tool_server.py(将整个 Agent 暴露为单个 MCP 工具)、streamable_http_server.py;mcp_deployer/ 展示MCPDeployer:以 API-key/自定义/token 认证,经 HTTP、SSE 或 stdio 将任意 Agent 或 swarm 作为 MCP 工具对外服务。
  • client/(直接用MCPManager调用 MCP,不经过 Agent):工具发现(01_list_tools.py)、按名调用(02_call_tool.py)、执行 LLM 响应中的工具调用(03_execute_llm_tool_calls.py)、多服务器自动按工具路由(04_multi_server.py)、认证配置(05_auth_and_config.py:API keys、bearer、headers、env secrets、OAuth)、远程 Agent(06_remote_agents.py)。

MCP 的框架侧实现集中在 swarms/tools/mcp_manager.py,tests/tools/mcp_test_server.py 是配套的测试服务器。

六、高级能力

6.1 推理增强:reasoning_agents/

reasoning_agents/README.md 收录:

  • Agent Judge 评估系统(agent_judge_examples/:基础/技术/创意三类评估示例);
  • MoA 顺序示例(moa_seq_example.py);
  • Reasoning Agent Router(reasoning_agent_router_examples/:agent judge、GKP、IRE、reasoning duo、reflexion、self-consistency)。

对应实现可见 swarms/agents/agent_judge.py 与 swarms/structs/self_moa_seq.py。

6.2 语音 Agent

语音示例位于 guides/voice_agents/voice_agents_examples/,覆盖:agent_speech.pyagent_with_speech.pydebate_with_speech.py(语音辩论)、google_calendar_agent.py(语音 + Google Calendar)、hiearchical_speech_swarm.py(层级语音 swarm)、run_auto_agent_with_speech.py(终端访问 + 流式 TTS 的自主 Agent)。

七、Marketplace 与指南教程

7.1 Marketplace

市场提示词集成示例位于 single_agent/integrations/marketplace/(marketplace_prompt_example.pyquant_trader_agent.py),另有 zia_agent.py 使用市场提示词实现 Zia Agent。相关功能后端可见 swarms/agents/agent_marketplace_handler.py。

7.2 Guides 与教程

guides/README.md 是综合指南索引,重点包括:

  • 版本更新示例840_update/(agent rearrange、auto swarm builder、fallback)、850_workshop/(MOA、peer review、并发)、880_update_changelog_examples/(市场集成、工作流编排、语音、评测与辩论、路由、自动保存)、changelog_890/(2026 年 1 月发布:动态技能加载、自主 Agent 循环、Agent 交接、API key 校验、max loops 参数、多工具教程、层级语音 Agent、agent rearrange 模式);
  • 图工作流指南:graphworkflow_guide/ 含 GETTING_STARTED、技术指南、快速入门与综合 demo;
  • 部署:guides/deployment/ 含 FastAPI 部署与 cron job 示例;
  • 行业 Demodemos/覆盖 apps、crypto、finance、insurance、legal、medical、real_estate、science、synthetic_data 等;
  • 专题项目:geo guesser、hackathon judge、nano banana Jarvis、web scraper、smart database、x402、mem0 等。

八、CLI 命令示例

cli/README.md 演示 Swarms CLI 的全部常用功能,每个.sh脚本即一个可复现的命令流程:

脚本功能
01_setup_check.sh环境设置校验
02_onboarding.sh用户 onboarding
03_get_api_key.sh获取 API key
04_check_login.sh登录状态检查
05_create_agent.sh创建自定义 Agent
06_run_agents_yaml.sh从 YAML 配置运行 Agent
07_load_markdown.sh加载 Markdown 配置
08_llm_council.shLLM Council 协作
09_heavy_swarm.shHeavySwarm 复杂分析
10_autoswarm.shAuto Swarm Builder
11_features.sh特性演示
12_help.sh帮助文档
13_auto_upgrade.sh自动升级
14_book_call.sh预约通话
run_all_examples.sh一键运行全部 CLI 示例

CLI 的框架侧入口为 swarms/cli/main.py 与 swarms/cli/models.py,tests/test_cli.py 提供行为验证。

九、快速上手路径(Quick Start)

原文档提供了按使用场景的快速导航,结合仓库实际路径整理如下:

使用场景推荐示例
首次接触 Swarmssingle_agent/getting_started/simple_agent.py
使用 CLIcli/
多 Agent 工作流multi_agent/(如 duo_agent.py 的双 Agent 协作)
工具集成tools/agent_as_tools.py
社交算法multi_agent/social_algorithms_examples/
RAGsingle_agent/capabilities/rag/
推理 Agentreasoning_agents/
市场提示词single_agent/integrations/marketplace/
语音能力guides/voice_agents/voice_agents_examples/
动态技能single_agent/capabilities/skills/
自主 Agentsingle_agent/capabilities/autonomy/autonomous_agents/
Agent 交接single_agent/capabilities/autonomy/handoffs/
最新特性guides/changelog_890/

十、补充阅读与扩展资源

  • 每个子目录均含各自的 README.md,提供该目录下全部示例的详细描述与链接;
  • 框架核心 API 见 swarms/(主要实现位于 swarms/structs/,工具系统位于 swarms/tools/,Agent 扩展位于 swarms/agents/);
  • 框架级测试位于 tests/,例如 tests/structs/test_sequential_workflow.py、tests/structs/test_swarm_router.py;
  • 模型供应商示例已从原single_agent/迁出至 models/(OpenAI、Anthropic Claude、DeepSeek、Azure、Mistral、Qwen、Ollama 等),详见 models/README.md;
  • 贡献新示例请参照 CONTRIBUTING.md。

结语

examples/目录不仅是运行示例的集合,更是一份随版本持续演进的"能力索引"——从 single_agent/getting_started/simple_agent.py 的最小 Agent,到 multi_agent/graphworkflow_examples/ 的图工作流,再到 mcp/ 的协议级集成与 guides/changelog_890/ 的新特性示例,每一层都对应 swarms/structs/ 与 swarms/tools/ 中的具体实现。读者可按"单 Agent → 多 Agent → 协议集成 → 部署"的路径,结合对应源码与测试逐层深入,快速构建自己的生产级多智能体应用。

【免费下载链接】swarmsThe Enterprise-Grade Multi-Agent Orchestration Framework. Website: https://swarms.ai项目地址: https://gitcode.com/GitHub_Trending/swar/swarms

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询