Hermes Agent 子 Agent 委托与批量处理
本章介绍 Hermes 的两种并行处理能力:子 Agent 委托让一个主 Agent 将任务分发给多个子 Agent 并行执行,批量处理系统则能在数百个 Prompt 上并行运行 Agent 并生成结构化训练数据。
子 Agent 委托与并行
Hermes 支持从主 Agent 中委托任务给子 Agent,实现并行工作流。
子 Agent 通过 delegation 工具集实现:
实例
# 文件路径:~/.hermes/config.yaml
# 确保 delegation 工具集已启用
toolsets:
- delegation
# 确保 delegation 工具集已启用
toolsets:
- delegation
主 Agent 使用 spawn_agent 或 spawn_parallel 工具派生子 Agent。
以下伪代码说明并行委托的机制:
实例
# Agent 在会话中的并行委托机制(伪代码,说明原理)
# spawn_parallel 同时派发多个子 Agent,各自独立执行
spawn_parallel([
{"prompt": "分析第一季度销售数据,生成趋势报告"},
{"prompt": "分析第二季度销售数据,生成趋势报告"},
{"prompt": "分析第三季度销售数据,生成趋势报告"}
])
# 三个子 Agent 并行工作,结果汇总后返回主 Agent
# spawn_parallel 同时派发多个子 Agent,各自独立执行
spawn_parallel([
{"prompt": "分析第一季度销售数据,生成趋势报告"},
{"prompt": "分析第二季度销售数据,生成趋势报告"},
{"prompt": "分析第三季度销售数据,生成趋势报告"}
])
# 三个子 Agent 并行工作,结果汇总后返回主 Agent
后台任务
在会话中直接启动后台任务:
实例
# 在会话中启动后台任务
/background 检查所有服务器并报告宕机情况
/background 检查所有服务器并报告宕机情况
Kanban 多 Agent 协作
Kanban 工具集提供更结构化的多 Agent 协作——一个编排者 Agent 将任务分发到看板,多个工作者 Agent 认领并执行:
实例
# 开启 kanban 工具集(需显式启用,all/* 也不会自动开启)
hermes tools enable kanban
# 为 Profile 添加角色描述,让编排者知道各 Agent 的能力
hermes profile create researcher \
--description "擅长阅读源代码和外部文档,输出调研报告"
hermes profile create coder \
--description "擅长实现功能、修复 Bug、编写测试"
hermes tools enable kanban
# 为 Profile 添加角色描述,让编排者知道各 Agent 的能力
hermes profile create researcher \
--description "擅长阅读源代码和外部文档,输出调研报告"
hermes profile create coder \
--description "擅长实现功能、修复 Bug、编写测试"
Kanban 工具集需要显式启用——即使是 all 或 * 也不会自动开启它。这是有意设计,因为多 Agent 协作会消耗更多 Token。
批量处理与 RL 训练数据生成
批量处理让你在数百或数千个 Prompt 上并行运行 Agent,生成结构化的轨迹数据——主要用于模型微调训练数据生成和评估。
快速上手
实例
# 准备 JSONL 格式的 Prompt 数据集
cat data/prompts.jsonl
# {"prompt": "写一个找最长回文子串的 Python 函数"}
# {"prompt": "用 Flask 创建用户认证 REST API 端点"}
# {"prompt": "调试这个错误:TypeError: cannot unpack non-iterable"}
# 运行批量处理
python batch_runner.py \
--dataset_file=data/prompts.jsonl \
--batch_size=10 \
--run_name=my_first_run \
--model=anthropic/claude-sonnet-4-6 \
--num_workers=4
# 从检查点续传中断的任务
python batch_runner.py \
--dataset_file=data/prompts.jsonl \
--batch_size=10 \
--run_name=my_first_run \
--resume
cat data/prompts.jsonl
# {"prompt": "写一个找最长回文子串的 Python 函数"}
# {"prompt": "用 Flask 创建用户认证 REST API 端点"}
# {"prompt": "调试这个错误:TypeError: cannot unpack non-iterable"}
# 运行批量处理
python batch_runner.py \
--dataset_file=data/prompts.jsonl \
--batch_size=10 \
--run_name=my_first_run \
--model=anthropic/claude-sonnet-4-6 \
--num_workers=4
# 从检查点续传中断的任务
python batch_runner.py \
--dataset_file=data/prompts.jsonl \
--batch_size=10 \
--run_name=my_first_run \
--resume
数据集格式
每行一个 JSON 对象,必须包含 prompt 字段,可选包含额外上下文:
实例
{"prompt": "写一个查找回文的 Python 函数"}
{"prompt": "调试这个错误", "cwd": "/home/user/project", "docker_image": "python:3.12"}
{"prompt": "调试这个错误", "cwd": "/home/user/project", "docker_image": "python:3.12"}
主要参数
| 参数 | 默认值 | 说明 |
|---|---|---|
| --dataset_file | 必填 | JSONL 数据集路径 |
| --batch_size | 必填 | 每批 Prompt 数量 |
| --run_name | 必填 | 运行名称(用于输出目录和断点续传) |
| --model | claude-sonnet-4.6 | 使用的模型 |
| --num_workers | 4 | 并行工作进程数 |
| --max_turns | 10 | 每个 Prompt 最大工具调用轮次 |
| --distribution | "default" | 工具集分布(随机采样不同工具组合) |
| --reasoning_effort | — | 推理努力程度:none/minimal/low/medium/high/xhigh |
| --resume | false | 从检查点续传 |
输出结构
批量处理生成如下目录结构:
data/my_first_run/ ├── trajectories.jsonl # 最终合并输出(所有批次) ├── batch_0.jsonl # 各批次结果 ├── batch_1.jsonl ├── checkpoint.json # 断点续传记录 └── statistics.json # 工具使用统计
每条轨迹以 ShareGPT 格式输出,包含完整对话历史、工具调用记录和推理覆盖率统计:
实例
{
"prompt_index": 42,
"conversations": [
{"from": "human", "value": "写一个查找回文的函数..."},
{"from": "gpt", "value": "我来创建这个函数...",
"tool_calls": [{"name": "terminal", "arguments": {...}}]},
{"from": "tool", "value": "...执行结果..."},
{"from": "gpt", "value": "这是完成的函数..."}
],
"completed": true,
"toolsets_used": ["terminal", "file"],
"tool_stats": {
"terminal": {"count": 2, "success": 2, "failure": 0}
}
}
"prompt_index": 42,
"conversations": [
{"from": "human", "value": "写一个查找回文的函数..."},
{"from": "gpt", "value": "我来创建这个函数...",
"tool_calls": [{"name": "terminal", "arguments": {...}}]},
{"from": "tool", "value": "...执行结果..."},
{"from": "gpt", "value": "这是完成的函数..."}
],
"completed": true,
"toolsets_used": ["terminal", "file"],
"tool_stats": {
"terminal": {"count": 2, "success": 2, "failure": 0}
}
}
质量过滤
批量运行结束后自动应用两项过滤:
| 过滤项 | 规则 | 目的 |
|---|---|---|
| 零推理过滤 | 没有任何推理痕迹的样本被丢弃 | 确保输出包含思考过程,提升训练数据质量 |
| 幻觉工具名过滤 | 包含不在有效工具列表中的工具调用的条目被过滤 | 排除模型幻觉产生的无效工具调用 |
推理痕迹指 <REASONING_SCRATCHPAD> 标记或原生 thinking tokens。过滤后的数据更适合用于模型微调。
与 Atropos RL 环境集成
批量处理可直接生成 Atropos 兼容的训练数据:
实例
# 生成 Atropos 兼容的训练数据
python batch_runner.py \
--dataset_file=data/coding_tasks.jsonl \
--run_name=atropos_run \
--model=nous-hermes-3.1 \
--num_workers=8 \
--reasoning_effort=high
# 输出的 trajectories.jsonl 可直接作为 Atropos 的训练输入
python batch_runner.py \
--dataset_file=data/coding_tasks.jsonl \
--run_name=atropos_run \
--model=nous-hermes-3.1 \
--num_workers=8 \
--reasoning_effort=high
# 输出的 trajectories.jsonl 可直接作为 Atropos 的训练输入
