名称: swarm
描述: 将 LLM 成本降低 200 倍。将并行、批量和研究工作卸载给 Gemini Flash 工作节点,而不是消耗昂贵的主模型。
主页: https://github.com/Chair4ce/node-scaling
元数据: {"clawdbot":{"emoji":"🐝","requires":{"bins":["node"]}}}
让昂贵的模型成为您负担得起的日常工具。将枯燥的工作——并行、批量、研究——以极低的成本卸载给 Gemini Flash 工作节点。
| 处理 30 个任务的方式 | 时间 | 成本 |
|---|---|---|
| Opus (顺序执行) | ~30 秒 | ~$0.50 |
| Swarm (并行执行) | ~1 秒 | ~$0.003 |
Swarm 非常适合以下情况:
- 3 个以上独立任务(研究、摘要、比较)
- 比较或研究多个主题
- 需要获取/分析的多个 URL
- 批处理(文档、实体、事实)
- 需要多角度分析的复杂任务 → 使用链式模式
# 检查守护进程(每次会话都建议执行)
swarm status
# 如果未运行,则启动
swarm start
# 并行提示词
swarm parallel "什么是 X?" "什么是 Y?" "什么是 Z?"
# 研究多个主题
swarm research "OpenAI" "Anthropic" "Mistral" --topic "AI 安全"
# 发现可用功能
swarm capabilities
N 个提示词 → N 个工作节点同时执行。最适合独立任务。
swarm parallel "提示词1" "提示词2" "提示词3"
多阶段:搜索 → 获取 → 分析。使用 Google 搜索进行信息锚定。
swarm research "Buildertrend" "Jobber" --topic "2026 年定价"
数据流经多个阶段,每个阶段具有不同的视角/过滤器。阶段按顺序运行;阶段内的任务并行运行。
阶段模式:
- parallel — N 个输入 → N 个工作节点(相同视角)
- single — 合并的输入 → 1 个工作节点
- fan-out — 1 个输入 → N 个具有不同视角的工作节点
- reduce — N 个输入 → 1 个综合输出
自动链 — 描述您的需求,获取最优流水线:
curl -X POST http://localhost:9999/chain/auto \
-d '{"task":"寻找商业机会","data":"...市场数据...","depth":"standard"}'
手动链:
swarm chain pipeline.json
# 或
echo '{"stages":[...]}' | swarm chain --stdin
深度预设: quick (2 阶段), standard (4), deep (6), exhaustive (8)
内置视角: 提取器、过滤器、丰富器、分析师、合成器、挑战者、优化器、策略师、研究员、评论家
预览而不执行:
curl -X POST http://localhost:9999/chain/preview \
-d '{"task":"...","depth":"standard"}'
使用 LLM 作为评判员,在同一任务上比较单次执行、并行执行和链式执行的得分。
curl -X POST http://localhost:9999/benchmark \
-d '{"task":"分析 X","data":"...","depth":"standard"}'
基于 6 个 FLASK 维度评分:准确性 (2x 权重)、深度 (1.5x)、完整性、连贯性、可操作性 (1.5x)、细微差别。
让编排器发现可用的执行模式:
swarm capabilities
# 或
curl http://localhost:9999/capabilities
LLM 响应的 LRU 缓存。缓存命中时并行模式提速 212 倍,链式模式提速 514 倍。
task.cache = false# 查看缓存统计
curl http://localhost:9999/cache
# 清除缓存
curl -X DELETE http://localhost:9999/cache
缓存统计信息显示在 swarm status 中。
如果链式阶段中的任务失败,仅重试失败的任务(而非整个阶段)。默认:1 次重试。可通过 phase.retries 按阶段配置,或通过 options.stageRetries 全局配置。
所有端点在其 complete 事件中返回成本数据:
- session — 当前守护进程会话总计
- daily — 跨重启持久化,全天累积
swarm status # 显示会话 + 每日成本
swarm savings # 月度节省报告
工作节点通过 Google 搜索锚定实时搜索网络(仅限 Gemini,无额外成本)。
# 研究模式默认使用网络搜索
swarm research "主题" --topic "角度"
# 带网络搜索的并行模式
curl -X POST http://localhost:9999/parallel \
-d '{"prompts":["X 的当前价格?"],"options":{"webSearch":true}}'
const { parallel, research } = require('~/clawd/skills/node-scaling/lib');
const { SwarmClient } = require('~/clawd/skills/node-scaling/lib/client');
// 简单并行
const result = await parallel(['提示词1', '提示词2', '提示词3']);
// 带流式传输的客户端
const client = new SwarmClient();
for await (const event of client.parallel(prompts)) { ... }
for await (const event of client.research(subjects, topic)) { ... }
// 链式模式
const result = await client.chainSync({ task, data, depth });
swarm start # 启动守护进程(后台)
swarm stop # 停止守护进程
swarm status # 状态、成本、缓存统计
swarm restart # 重启守护进程
swarm savings # 月度节省报告
swarm logs [N] # 守护进程日志的最后 N 行
| 模式 | 任务数 | 时间 | 备注 |
|---|---|---|---|
| 并行 (简单) | 5 | ~700 毫秒 | 有效 142 毫秒/任务 |
| 并行 (压力) | 10 | ~1.2 秒 | 有效 123 毫秒/任务 |
| 链式 (标准) | 5 | ~14 秒 | 3 阶段多视角 |
| 链式 (快速) | 2 | ~3 秒 | 2 阶段提取+合成 |
| 缓存命中 | 任意 | ~3-5 毫秒 | 200-500 倍加速 |
| 研究 (网络) | 2 | ~15 秒 | Google 锚定延迟 |
位置:~/.config/clawdbot/node-scaling.yaml
node_scaling:
enabled: true
limits:
max_nodes: 16
max_concurrent_api: 16
provider:
name: gemini
model: gemini-2.0-flash
web_search:
enabled: true
parallel_default: false
cost:
max_daily_spend: 10.00
| 问题 | 解决方法 |
|---|---|
| 守护进程未运行 | swarm start |
| 无 API 密钥 | 设置 GEMINI_API_KEY 或运行 npm run setup |
| 速率受限 | 在配置中降低 max_concurrent_api |
| 网络搜索不工作 | 确保提供商为 gemini 且 web_search.enabled 为 true |
| 缓存结果过时 | curl -X DELETE http://localhost:9999/cache |
| 链式模式太慢 | 使用 depth: "quick" 或检查上下文大小 |
强制 JSON 输出并进行模式验证 —— 在结构化任务上实现零解析失败。
# 使用内置模式
curl -X POST http://localhost:9999/structured \
-d '{"prompt":"从以下文本提取实体:Tim Cook 宣布了 iPhone 17","schema":"entities"}'
# 使用自定义模式
curl -X POST http://localhost:9999/structured \
-d '{"prompt":"对这段文本进行分类","data":"...","schema":{"type":"object","properties":{"category":{"type":"string"}}}}'
# JSON 模式(无模式,仅强制 JSON)
curl -X POST http://localhost:9999/structured \
-d '{"prompt":"返回一个包含虚构人物的姓名、年龄、城市的 JSON 对象"}'
# 列出可用模式
curl http://localhost:9999/structured/schemas
内置模式: entities, summary, comparison, actions, classification, qa
使用 Gemini 原生的 response_mime_type: application/json + responseSchema 来保证 JSON 输出。包含对响应的模式验证。
相同提示词 → N 次并行执行 → 选择最佳答案。在事实性/分析性任务上具有更高的准确性。
# 评判策略(LLM 选择最佳 —— 最可靠)
curl -X POST http://localhost:9999/vote \
-d '{"prompt":"SaaS 定价的关键因素是什么?","n":3,"strategy":"judge"}'
# 相似性策略(共识 —— 零额外成本)
curl -X POST http://localhost:9999/vote \
-d '{"prompt":"Python 是哪一年发布的?","n":3,"strategy":"similarity"}'
# 最长策略(启发式 —— 零额外成本)
curl -X POST http://localhost:9999/vote \
-d '{"prompt":"解释递归","n":3,"strategy":"longest"}'
策略:
- judge — LLM 根据准确性/完整性/清晰度/可操作性对所有候选答案评分,选择获胜者 (N+1 次调用)
- similarity — Jaccard 词集相似度,选择共识答案 (N 次调用,零额外成本)
- longest — 选择最长的回答作为详尽性的启发式方法 (N 次调用,零额外成本)
何时使用: 事实性问题、关键决策,或任何准确性 > 速度的任务。
| 策略 | 调用次数 | 额外成本 | 质量 |
|---|---|---|---|
| similarity | N | $0 | 良好(共识) |
| longest | N | $0 | 尚可(启发式) |
| judge | N+1 | ~$0.0001 | 最佳(LLM 评分) |
链式/骨架输出后可选的评论家审查环节。对 5 个维度进行评分,如果低于阈值则自动优化。
# 在任何链式或骨架请求中添加 reflect:true
curl -X POST http://localhost:9999/chain/auto \
-d '{"task":"分析 AI 芯片市场","data":"...","reflect":true}'
curl -X POST http://localhost:9999/skeleton \
-d '{"task":"撰写市场分析","reflect":true}'
已验证:将弱输出从平均 5.0 分提升至 7.6 分。骨架 + 反思模式得分为 9.4/10。
生成大纲 → 并行扩展每个部分 → 合并成连贯文档。最适合长篇内容。
curl -X POST http://localhost:9999/skeleton \
-d '{"task":"撰写一份关于 SaaS 定价的全面指南","maxSections":6,"reflect":true}'
性能: 21 秒内生成 14,478 个字符 (675 字符/秒) —— 比链式模式多 5.1 倍内容,吞吐量高 2.9 倍。
| 指标 | 链式模式 | 思维骨架 | 胜者 |
|---|---|---|---|
| 输出大小 | 2,856 字符 | 14,478 字符 | SoT (5.1 倍) |
| 吞吐量 | 234 字符/秒 | 675 字符/秒 | SoT (2.9 倍) |
| 持续时间 | 12 秒 | 21 秒 | 链式(更快) |
| 质量 (带反思) | ~7-8/10 | 9.4/10 | SoT |
何时使用哪种模式:
- SoT → 长篇内容、报告、指南、文档(任何有自然分节的内容)
- 链式 → 分析、研究、对抗性审查(任何需要多视角的任务)
- 并行 → 独立任务、批处理
- 结构化 → 实体提取、分类、任何需要可靠 JSON 的任务
- 投票 → 事实准确性、关键决策、建立共识
| 方法 | 路径 | 描述 |
|---|---|---|
| GET | /health | 健康检查 |
| GET | /status | 详细状态 + 成本 + 缓存 |
| GET | /capabilities | 发现执行模式 |
| POST | /parallel | 并行执行 N 个提示词 |
| POST | /research | 多阶段网络研究 |
| POST | /skeleton | 思维骨架(大纲 → 扩展 → 合并) |
| POST | /chain | 手动链式流水线 |
| POST | /chain/auto | 自动构建 + 执行链 |
| POST | /chain/preview | 预览链而不执行 |
| POST | /chain/template | 执行预构建模板 |
| POST | /structured | 强制 JSON 输出并进行模式验证 |
| GET | /structured/schemas | 列出内置模式 |
| POST | /vote | 多数投票(N 选最佳) |
| POST | /benchmark | 质量比较测试 |
| GET | /templates | 列出链式模板 |
| GET | /cache | 缓存统计 |
| DELETE | /cache | 清除缓存 |
| 模型 | 每 100 万 token 成本 | 相对成本 |
|---|---|---|
| Claude Opus 4 | ~$15 输入 / $75 输出 | 1x |
| GPT-4o | ~$2.50 输入 / $10 输出 | ~7 倍便宜 |
| Gemini Flash | ~$0.075 输入 / $0.30 输出 | 200 倍便宜 |
缓存命中基本上是免费的 (~3-5 毫秒,无 API 调用)。