在终端管理异步 Batch 推理:把 CSV 转成 JSONL、创建 Chat 或 Embedding 批量任务、跟踪进度、取消任务并下载结果和错误文件。
异步 Batch 任务与 bl text chat --batch 是两种不同的调用方式。--batch 是单次同步请求;本页命令处理文件级异步任务。本地文件批量推理请使用 bl batch create 和 bl batch get / bl batch download。
任意命令追加 --help 可查看完整参数说明,例如 bl batch create --help。本文命令明细以 bl 2.8.5 为基准核实。
命令速查
bl batch convert
在本地把 CSV 转换为 Batch JSONL。无需鉴权。
语法
bl batch convert --file <csv> --model <model> --output-file <jsonl> [flags]
| 参数 | 类型 | 必填 | 说明 |
|---|
--file <csv> | string | 是 | 带表头的 UTF-8 CSV 文件 |
--output-file <jsonl> | string | 是 | 本地 JSONL 输出路径 |
--model <model> | string | 是 | 每条记录使用的模型 |
--id-column <name> | string | 否 | ID 列名(默认:custom_id) |
--input-column <name> | string | 否 | 输入列名(默认:content) |
--system <text> | string | 否 | 全部记录共用的 system 提示词(仅 Chat) |
--body-file <json> | string | 否 | 共用的请求体参数,不能包含 model、messages 或 input |
--endpoint <path> | string | 否 | Batch Endpoint:/v1/chat/completions、/v1/embeddings 或 /v1/chat/ds-test;默认 /v1/chat/completions,Embedding 输入需指定 /v1/embeddings |
--force | switch | 否 | 覆盖已存在的输出文件(按文件原子替换) |
示例:把 CSV 转换为 Chat JSONL
bl batch convert --file input.csv --model qwen-plus --output-file input.jsonl
示例:转换 Embedding 输入并附加默认请求体
bl batch convert --file input.csv --model text-embedding-v4 --endpoint /v1/embeddings --body-file defaults.json --output-file vectors.jsonl
CSV 至少包含 custom_id 和 content 两列:
custom_id,content
req-001,Introduce yourself
req-002,2+2?
转换后的 Chat JSONL 示例:
{"custom_id":"req-001","method":"POST","url":"/v1/chat/completions","body":{"model":"qwen-plus","messages":[{"role":"user","content":"Introduce yourself"}]}}
- 只支持文本和 Embedding 的字段映射。多模态或多轮输入请直接编写原生 JSONL。
- ID 始终按字符串处理。在表格软件中打开 CSV 时,将 ID 列设为文本,避免前导零丢失。
--dry-run 不调用业务 API,也不写业务文件。
bl batch create
上传或引用输入文件,创建异步 Batch 任务。需要 API Key 鉴权。
语法
bl batch create (--file <jsonl> | --input-file-id <source> --endpoint <path>) [flags]
| 参数 | 类型 | 必填 | 说明 |
|---|
--file <jsonl> | string | 否 | 本地 Batch JSONL;与 --input-file-id 互斥 |
--input-file-id <source> | string | 否 | 文件 ID、OSS HTTPS URL 或 oss:region:bucket/path |
--completion-window <24h|7d> | string | 否 | 完成窗口,24–336 小时(默认:24h) |
--name <text> | string | 否 | 任务名称(最多 100 个字符) |
--description <text> | string | 否 | 任务描述(最多 200 个字符) |
--callback-url <url> | string | 否 | 完成回调地址(文档目前仅列出北京地域) |
--metadata <KEY=VALUE> | array | 否 | 自定义任务元数据(可重复) |
--endpoint <path> | string | 否 | Batch Endpoint:/v1/chat/completions、/v1/embeddings 或 /v1/chat/ds-test;默认从本地 JSONL 推断,使用 --input-file-id 时需显式指定 |
示例:预览任务创建
bl batch create --file input.jsonl --name eval-1 --dry-run
示例:引用 OSS 文件创建 7 天完成窗口的任务
bl batch create --input-file-id oss:cn-beijing:my-bucket/input.jsonl --endpoint /v1/chat/completions --completion-window 7d
- 本地输入上限为 50,000 条请求、500 MiB,单行不超过 6 MiB;
batch-test-model 上限为 100 条请求和 1 MiB。单行超过 1 MiB 时 CLI 会提示文档限制存在冲突。
- 远程引用需要输入文件已授权且 URL 在有效期内。CLI 不会下载远程文件做校验。
- 创建失败时不会自动重放 POST,也不会清理已上传的输入文件。
--dry-run 不调用业务 API,也不写业务文件。
bl batch get
查询 Batch 任务详情,可跟踪进度。需要 API Key 鉴权。
语法
bl batch get --batch-id <id> [--follow] [--interval <seconds>] [--poll-timeout <seconds>]
| 参数 | 类型 | 必填 | 说明 |
|---|
--batch-id <id> | string | 是 | Batch 任务 ID |
--follow | switch | 否 | 轮询到终态;Ctrl-C 只停止本地轮询 |
--interval <seconds> | number | 否 | 查询间隔,最小 60 秒(默认:65 秒;仅 --follow) |
--poll-timeout <seconds> | number | 否 | 总轮询预算(默认:不限制;仅 --follow) |
示例:跟踪任务直到终态
bl batch get --batch-id batch_xxx --follow --interval 65 --poll-timeout 86400
- 单次查询成功即返回退出码 0,与任务状态无关。
--follow 在 completed 时返回 0(结果中仍可能包含失败行);failed、expired 或 cancelled 返回 1;客户端超时返回 5;Ctrl-C 返回 130。
finalizing 和 cancelling 不是终态。进度写入 stderr,最终结果写入 stdout。
- 仅传输错误、超时或 HTTP 429/5xx 会重试,最多 3 次。服务端保留结果的时间有限,请及时下载。
bl batch list
分页列出 Batch 任务。需要 API Key 鉴权。
语法
bl batch list [filters] [--after <id>] [--limit <n>]
| 参数 | 类型 | 必填 | 说明 |
|---|
--limit <n> | number | 否 | 每页条数,1–100(默认:20) |
--after <id> | string | 否 | 上一页返回的 last_id |
--name <text> | string | 否 | 按任务名称筛选 |
--input-file-id <id> | array | 否 | 按输入文件筛选(可重复,最多 20 个) |
--status <status> | array | 否 | 按任务状态筛选(可重复):validating、in_progress、finalizing、completed、failed、expired、cancelling、cancelled |
--created-after <yyyyMMddHHmmss> | string | 否 | 创建时间晚于该 API 时间戳 |
--created-before <yyyyMMddHHmmss> | string | 否 | 创建时间早于该 API 时间戳 |
示例:列出已完成和失败的任务
bl batch list --status completed --status failed --limit 20
- 返回
data、last_id 和 has_more;下一页把 last_id 传给 --after。
- API 时间戳不进行时区转换。
bl batch cancel
请求取消 Batch 任务。需要 API Key 鉴权。
语法
bl batch cancel --batch-id <id>
| 参数 | 类型 | 必填 | 说明 |
|---|
--batch-id <id> | string | 是 | Batch 任务 ID |
示例:请求取消任务
bl batch cancel --batch-id batch_xxx
- 取消请求被接受后,任务可能返回
cancelling 而不是 cancelled;使用 bl batch get --follow 确认最终状态。
- 已经执行的请求仍会产生费用,输入和输出文件不会被删除。
bl batch download
下载原始结果或错误文件,可选生成 CSV。需要 API Key 鉴权。
语法
bl batch download --batch-id <id> [--out-dir <path>] [--csv] [--force]
| 参数 | 类型 | 必填 | 说明 |
|---|
--batch-id <id> | string | 是 | Batch 任务 ID |
--out-dir <path> | string | 否 | 输出目录(默认:./batch-results/<batch-id>/) |
--csv | switch | 否 | 同时生成 result.csv(有损的展示投影) |
--force | switch | 否 | 覆盖已存在的输出文件(按文件原子替换) |
示例:下载结果并生成 CSV
bl batch download --batch-id batch_xxx --out-dir ./out --csv
- JSONL 是权威结果。CSV 只读取
choices[0].message.content、Embedding 数据、response.request_id / status_code、usage 以及 error.code / error.message,会省略工具调用和其他 choices。
- CSV 会把公式样式的文本做转义。导入表格软件时把 ID 列设为文本,保留前导零。
- 不保证结果与原始输入顺序一致。写入按文件原子替换,不是按目录;CSV 生成失败时保留已下载的 JSONL,可用
--force 重新执行。
- 服务端保留结果的时间有限,任务完成后请尽快下载。