ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

如何用 batch_runner.py 批量生成带工具调用统计的 Agent 轨迹数据(断点续跑与工具集分布)

如何用 batch_runner.py 批量生成带工具调用统计的 Agent 轨迹数据(断点续跑与工具集分布) 如何用 batch_runner.py 批量生成带工具调用统计的 Agent 轨迹数据断点续跑与工具集分布【免费下载链接】hermes-agentThe agent that grows with you项目地址: https://gitcode.com/GitHub_Trending/he/hermes-agent如果你需要用 Hermes agent 在一批 prompt 上批量跑完整的 agent 会话并产出带工具调用统计的结构化轨迹数据ShareGPT 风格对话 每个工具的 count/success/failurebatch_runner.py就是仓库根目录提供的入口脚本。它支持多进程并行、按批次检查点checkpoint与内容匹配的断点续跑以及按工具集分布为每个 prompt 随机抽样启用工具集保证训练数据覆盖多样的工具组合。产出默认落在data/run_name/下包括合并后的trajectories.jsonl、逐批次的batch_*.jsonl、checkpoint.json和聚合统计statistics.json。主文档是 Batch Processing实现见 batch_runner.py 和 toolset_distributions.py。准备条件数据集文件格式输入是一个 JSONL 文件每行一个 JSON 对象每行必须有prompt字段。缺prompt的行会被跳过并打印警告坏 JSON 行同样跳过如果整个文件没有有效条目会直接报错。{prompt: Write a Python function that finds the longest palindromic substring} {prompt: Create a REST API endpoint for user authentication using Flask} {prompt: Debug this error: TypeError: cannot unpack non-iterable NoneType object}每条记录还有两个可选字段image或docker_image为该 prompt 指定沙箱容器镜像适用于 Docker、Modal、Singularity 后端。使用 Docker 后端且TERMINAL_ENVdocker时runner 会在消耗模型调用前先验证镜像可访问本地缓存检查缺失则docker pull拉取失败该 prompt 记为失败。cwd覆盖该任务 terminal 会话的工作目录。三个必传 CLI 参数--dataset_fileJSONL 路径、--batch_size每批 prompt 数须为正整数、--run_name用于输出目录和检查点命名。--api_key未显式传时取自环境变量--base_url默认https://openrouter.ai/api/v1--model默认anthropic/claude-sonnet-4.6--max_turns默认 10--num_workers默认 4。选择工具集分布--list_distributions 与采样机制每个 prompt 会从指定的**分布distribution**中随机抽样得到一组启用的工具集而不是手工指定固定组合。--distribution默认default。先查看内置的全部分布python batch_runner.py --list_distributions输出会按概率从高到低列出每个分布的描述和各工具集的启用概率。内置分布定义在 toolset_distributions.py 的DISTRIBUTIONS中共 17 个分布名说明源文件描述各工具集启用概率defaultAll available tools, all the timeweb 100, vision 100, image_gen 100, terminal 100, file 100, browser 100image_genHeavy focus on image generation with vision and web supportimage_gen 90, vision 90, web 55, terminal 45researchWeb research with vision analysis and reasoningweb 90, browser 70, vision 50, terminal 10scienceScientific research with web, terminal, file, and browser capabilitiesweb 94, terminal 94, file 94, vision 65, browser 50, image_gen 15developmentTerminal, file tools, and reasoning with occasional web lookupterminal 80, file 80, web 30, vision 10safeAll tools except terminal for safetyweb 80, browser 70, vision 60, image_gen 60balancedEqual probability of all toolsetsweb/vision/image_gen/terminal/file/browser 各 50minimalOnly web tools for basic researchweb 100terminal_onlyTerminal and file tools for code execution tasksterminal 100, file 100terminal_webTerminal and file tools with web search for documentation lookupterminal 100, file 100, web 100creativeImage generation and vision analysis focusimage_gen 90, vision 90, web 30reasoningHeavy research/reasoning distribution with minimal other toolsweb 90, file 60, terminal 20browser_useFull browser-based web interaction with search, vision, and page controlbrowser 100, web 80, vision 70browser_onlyOnly browser automation tools for pure web interaction tasksbrowser 100browser_tasksBrowser-focused distribution with web_search for finding URLsbrowsersearch 97, vision 12, terminal 15terminal_tasksTerminal-focused distribution with high terminal/file availabilityterminal 97, file 97, web 97, browser 75, vision 50, image_gen 10mixed_tasksMixed distribution with high browser, terminal, and file availabilitybrowser 92, terminal 92, file 92, web 35, vision 15, image_gen 15抽样规则与实现一致每个工具集条目独立按概率掷骰子可能同时启用多个工具集若所有条目都没抽中回退到概率最高的那个条目保证至少启用一个工具集形如browsersearch的组合条目只掷一次骰子全体成员同时启用或同时跳过用以保证共存关系两个独立 97% 掷骰的共存率只有约 94%。传一个不存在的分布名会直接报Unknown distribution并列出所有可用分布名。执行第一次批量运行在仓库根目录运行batch_runner.py在根目录python batch_runner.py \ --dataset_filedata/prompts.jsonl \ --batch_size10 \ --run_namemy_first_run \ --modelanthropic/claude-sonnet-4.6 \ --num_workers4启动时会打印初始化摘要数据集条数、批次数、run name、分布、输出目录data/my_first_run、worker 数随后每个批次开始时打印Batch N: Starting (K prompts)每个 prompt 完成打印✅或⚠️ partial、失败打印❌ ... (will retry on resume)。几个常用可选参数python batch_runner.py \ --dataset_filedata/coding_prompts.jsonl \ --batch_size20 \ --run_namecoding_v1 \ --modelanthropic/claude-sonnet-4.6 \ --num_workers8 \ --distributiondefault \ --max_turns15 \ --max_samples50--max_samples只处理数据集的前 N 条适合先小规模验证--providers_allowed/--providers_ignored/--providers_order/--provider_sortOpenRouter 路由控制provider_sort可选price、throughput、latency--reasoning_effortnone/minimal/low/medium/high/xhigh/max/ultra与--reasoning_disabled完全关闭 reasoning/thinking tokens二者同时给时--reasoning_disabled优先--ephemeral_system_prompt执行时使用但不写入轨迹的 system prompt--prefill_messages_file指向一个 JSON 文件{role, content}消息数组作为 few-shot 前缀。注意源码注释给出的限制Anthropic Sonnet/Opus 4.6 会拒绝以 assistant 角色结尾的 prefill400对这些模型要用 user 角色开头做 priming。断点续跑--resume 的工作原理批量跑被中断手动 Ctrl-C、崩溃、机器重启后用相同参数加--resume继续python batch_runner.py \ --dataset_filedata/prompts.jsonl \ --batch_size10 \ --run_namemy_first_run \ --resume续跑逻辑见BatchRunner._apply_resume与文档的 How Resume Works 一节扫描输出目录下所有batch_*.jsonl文件按prompt 实际文本内容匹配已完成的条目而不是索引所以数据集顺序变化也能正确恢复过滤掉已完成 prompt对剩余 prompt 重新分批只处理剩余部分结束时把所有批次文件旧的 本次新的合并进trajectories.jsonl。几个必须知道的边界检查点文件checkpoint.json在每个批次返回结果后增量写入原子写记录已完成 prompt 索引与各批处理/跳过/丢弃数只有成功产出轨迹的 prompt 才标记为完成——失败的 prompt 在 resume 时会重试被质量过滤丢弃的样本会写一条 tombstone 行{prompt_index: ..., discarded: no_reasoning, ...}resume 时视为已完成不会重跑合并trajectories.jsonl时会排除如果数据集里所有 prompt 都已完成runner 打印All prompts have already been processed!后直接返回不再发起任何模型调用还有剩余工作时启动阶段会打印RESUME SUMMARY含原始条数、已完成条数、本次续跑条数和新批次数。输出结构与轨迹字段验证运行结束前所有产物都在data/run_name/data/my_run/ ├── trajectories.jsonl # Combined final output (all batches merged) ├── batch_0.jsonl # Individual batch results ├── batch_1.jsonl ├── ... ├── checkpoint.json # Resume checkpoint └── statistics.json # Aggregate tool usage statstrajectories.jsonl每行一个 JSON 对象文档给出的示例结构如下文档示例{ prompt_index: 42, conversations: [ {from: human, value: Write a function...}, {from: gpt, value: Ill create that function..., tool_calls: [...]}, {from: tool, value: ...}, {from: gpt, value: Heres the completed function...} ], metadata: { batch_num: 2, timestamp: 2026-01-15T10:30:00, model: anthropic/claude-sonnet-4.6 }, completed: true, partial: false, api_calls: 3, toolsets_used: [terminal, file], tool_stats: { terminal: {count: 2, success: 2, failure: 0}, read_file: {count: 1, success: 1, failure: 0} }, tool_error_counts: { terminal: 0, read_file: 0 } }字段含义conversationsShareGPT 风格的from/value对话序列toolsets_used该 prompt 从分布中抽中并实际启用的工具集tool_stats按工具名归一化后的{count, success, failure}。归一化会补全所有可能工具并填 0 默认值schema 来自model_tools.TOOL_TO_TOOLSET_MAP保证每行 schema 一致便于直接载入 HuggingFace datasetstool_error_counts每个工具名对应其 failure 计数api_calls该 prompt 消耗的模型调用次数partial为true表示因无效工具调用等原因提前停止成功/失败的判定规则在 batch_runner.py 的_tool_call_succeeded中工具返回 JSON 时error非 null 或success: false记失败非 JSON 文本仅当为空或以Error:开头才记失败非零退出码不算失败模型可以自行纠正。statistics.json保存整次 run 的聚合结果run_name、distribution、总 prompt 数、批次数、model、tool_statistics每工具 count/success/failure 及 success_rate/failure_rate、reasoning_statistics和discarded_no_reasoning计数可程序化分析。完成时的汇总输出与质量过滤全部批次处理完后runner 打印BATCH PROCESSING COMPLETE汇总本次处理的 prompt 数、合并文件中的轨迹总数、合并的批次文件数、总耗时以及两部分统计Tool Usage Statistics按调用次数降序的工具表列为 Tool Name / Count / Success / Failure / Success RateReasoning Coverage总 assistant 轮数、有 reasoning 的轮数及百分比、无 reasoning 的轮数及百分比若有丢弃样本还会打印Samples discarded (zero reasoning)计数。同时自动执行两类质量过滤见文档 Quality Filtering 与合并逻辑无 reasoning 过滤所有 assistant 轮都不含 reasoning既没有REASONING_SCRATCHPAD也没有原生 thinking tokens的样本直接丢弃并写入上述 tombstone 行。由此推出一个实际限制如果你传了--reasoning_disabled关掉全部 reasoning样本会因不满足 reasoning 覆盖而被过滤跑训练数据时不要同时使用两者损坏条目过滤合并阶段tool_stats里出现合法工具列表之外的工具名幻觉工具名或 JSON 解析失败的行会被丢弃并打印Filtering corrupted entry (batch N)及首个非法工具名预览。限制与边界批量运行会为每个 prompt 启动独立 agent 会话独立task_id环境跳过 SOUL.md/AGENTS.md 上下文文件和持久记忆skip_context_files、skip_memory不要期待会话里有主 agent 的长期记忆platform固定记为batch用于遥测归属无需配置按 prompt 指定容器镜像时只有 Docker 后端会做本地docker image inspectdocker pull预检Docker CLI 不存在时跳过检查例如 Modal 后端在服务器侧拉取checkpoint.json与 run_name 不匹配时换名字复用目录会重置为空检查点成本提示批量运行会并发发起大量模型调用文档提示可用hermes setup --portal配置 Nous Portal 后把--model指向 Nous 模型来统一计费。跑完后核对data/run_name/trajectories.jsonl的行数是否等于汇总里Total trajectories in merged file、抽查某行的tool_stats是否覆盖了全部工具名的 0 值默认项再读statistics.json里的tool_statistics与reasoning_statistics即完成整条生成—续跑—统计闭环的验证。【免费下载链接】hermes-agentThe agent that grows with you项目地址: https://gitcode.com/GitHub_Trending/he/hermes-agent创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表