help.khb.com 批量处理 API: 上传文件 (Files Upload)

API: 上传文件 (Files Upload)

Upload files

Upload files for use with batch inference and other endpoints.

端点:POST /v1/files

Request Body

purpose (string, required)

文件用途。当前仅支持:batch(用于批量推理任务)。

file (file, required)

待上传的文件。批量推理场景下为 .jsonl 格式,每行一个完整的请求体。文件大小限制 1 GB,行数限制 5000 行。

代码示例

cURL

curl --request post \
  --url https://khb.net.cn/v1/files \
  --header 'authorization: Bearer YOUR_API_KEY' \
  --header 'content-type: multipart/form-data' \
  --form purpose=batch \
  --form 'file=@batch_input.jsonl'

Python

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://khb.net.cn/v1"
)

with open("batch_input.jsonl", "rb") as f:
    file_response = client.files.create(
        file=f,
        purpose="batch"
    )

print(f"File ID: {file_response.id}")
print(f"File size: {file_response.bytes} bytes")

Response Body

{
  "code": 20000,
  "message": "Ok",
  "status": true,
  "data": {
    "id": "file-jkvytbjtow",
    "object": "file",
    "bytes": 8509,
    "createdAt": 1741685396,
    "filename": "requests.jsonl",
    "purpose": "batch"
  }
}

后续步骤

上传文件后,使用 file_id 调用 /v1/batches 接口创建批量推理任务。详见 批量推理

错误码

状态码 说明
200 上传成功
400 文件格式错误或超过大小限制
401 API Key 无效
403 权限不足
404 资源不存在
500/503/504 服务端错误

Related Post