Skip to content

进阶

批量推理(Batch API)

批量推理(Batch API)适用于离线数据合成、大规模评测与异步生成任务。你可以把大量推理请求打包为 JSONL 文件上传到 Workspace,异步执行完成后统一下载输出与错误文件。

PyTRIO 提供两种使用方式:

1. 准备输入文件 ​

输入文件为标准 JSONL(每行一条 JSON 请求),文件不可为空(0 字节文件会被 SDK 拒绝)。示例如下:

json
{"custom_id": "req-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "Qwen/Qwen3.5-4B", "messages": [{"role": "user", "content": "用一句话解释 LoRA"}], "max_tokens": 128}}
{"custom_id": "req-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "Qwen/Qwen3.5-4B", "messages": [{"role": "user", "content": "什么是 GRPO?"}], "max_tokens": 128}}

body.model 可填基础模型 ID 或你已保存的 Sampler 权重路径。

2. 使用 CLI 运行完整流程 ​

bash
# 1. 上传文件并直接提交批量任务(也可先 trio file upload 再传 FILE_ID)
trio batch create --file requests.jsonl \
  --completion-window 24h \
  --description "nightly eval"

# 2. 查看任务列表或单个任务状态与计数
trio batch list --limit 20
trio batch info BATCH_ID

# 3. 任务完成后下载输出与错误 JSONL(保存为 <BATCH_ID>-output.jsonl / <BATCH_ID>-error.jsonl)
trio batch download BATCH_ID -o ./results

# 如需取消运行中的任务
trio batch cancel BATCH_ID --yes

所有 trio file 与 trio batch 子命令均支持在子命令前加 --format json 或 --format csv 便于脚本解析,例如:

bash
trio --format json batch info BATCH_ID

3. 使用 Python SDK(RestClient) ​

python
import time
import pytrio as trio

with trio.RestClient() as rest_client:
    # 1. 上传 JSONL 输入文件
    uploaded = rest_client.upload_file("requests.jsonl").result()
    print("Uploaded file:", uploaded.file_id)

    # 2. 创建批量推理任务
    batch = rest_client.create_batch(
        input_file_id=uploaded.file_id,
        completion_window="24h",
        description="nightly eval",
    ).result()
    print("Created batch:", batch.batch_id, batch.status)

    # 3. 轮询状态直到进入终态
    while batch.finished_at is None:
        time.sleep(10)
        batch = rest_client.get_batch(batch.batch_id).result()
        counts = batch.request_counts
        print(
            f"status={batch.status} "
            f"completed={counts.completed}/{counts.total} failed={counts.failed}"
        )

    # 4. 下载输出与错误文件
    if batch.output_file_id:
        out_path = rest_client.download_file(
            batch.output_file_id,
            f"results/{batch.batch_id}-output.jsonl",
        ).result()
        print("Saved output to:", out_path)

    if batch.error_file_id:
        err_path = rest_client.download_file(
            batch.error_file_id,
            f"results/{batch.batch_id}-error.jsonl",
        ).result()
        print("Saved errors to:", err_path)

在异步代码中,可将上述方法替换为 upload_file_async、create_batch_async、get_batch_async、download_file_async(一段式 await,直接返回结果对象)。

4. 参数约束与故障排查 ​

  • completion_window:必须为 24h 至 168h 之间的整小时字符串(默认 "24h")。
  • description:去除首尾空白后最多 512 个字符。
  • 提交结果不确定(submission_unknown):为避免重复计费,create_batch() 不会在网络中断后自动重放提交请求。若抛出 PyTrioError 且 err.details.get("submission_unknown") is True,说明服务端可能已接单,请先通过 trio batch list 或 rest_client.list_batches_page() 核对任务是否已创建,再决定是否重新提交。
  • 输入校验错误:如输入 JSONL 存在格式问题,batch.validation_errors 会列出每条错误的 line、code 与 message。
  • 删除文件限制:仍被活跃批量任务引用的输入文件不可删除(服务端返回 file_in_use)。

相关文档 ​