推理 API

批处理

查看 Markdown

创建批处理任务

/v1/batches

创建一个新的批处理任务来处理请求。

请求体

namestring

(字符串,必填)— 所创建批处理任务的名称。

响应体

batch_idstring

(字符串,必填)— 批处理任务的 ID。

create_api_key_idstring

(字符串,必填)— 用于创建批处理任务的 API 密钥 ID。

create_timestring

(string, required)— 创建批次的时间。

namestring

(字符串,必填)— 便于人阅读的批处理任务名称。

stateobject

(对象,必填)— 包含批处理过程当前状态的汇总信息。

Exampletext

text

{
  "name": "My New Batch"
}
Exampletext

text

{
  "batch_id": "batch_1934e8b5-f3dc-45f1-8329-9841b0aee9d8",
  "name": "My New Batch",
  "create_time": "2025-11-11",
  "expire_time": "2025-11-12",
  "create_api_key_id": "********-****-****-****-************",
  "cancel_time": null,
  "cancel_by_xai_message": null,
  "state": {
    "num_requests": 0,
    "num_pending": 0,
    "num_success": 0,
    "num_error": 0,
    "num_cancelled": 0
  }
}

列出批处理任务

/v1/batches

列出当前团队的所有批次。

查询参数

limitinteger | null

(整数或 null)— 要返回的元素数量。

pagination_tokenstring | null

(字符串或 null)— 用于检索特定页的可选分页 token,由 `ListBatchesResponse` 中的 `pagination_token` 提供。

响应体

batchesarray<object>

(对象数组,必填)— 批处理任务的信息。

Exampletext

text

No parameters.
Exampletext

text

{
  "batches": [
    {
      "batch_id": "batch_1934e8b5-f3dc-45f1-8329-9841b0aee9d8",
      "name": "My New Batch",
      "create_time": "2025-11-11",
      "expire_time": "2025-11-12",
      "create_api_key_id": "********-****-****-****-************",
      "cancel_time": null,
      "cancel_by_xai_message": null,
      "state": {
        "num_requests": 0,
        "num_pending": 0,
        "num_success": 0,
        "num_error": 0,
        "num_cancelled": 0
      }
    },
    {
      "batch_id": "batch_bac0e657-6bbf-46ba-a671-1d73a67c132a",
      "name": "MyNewBatch",
      "create_time": "2025-11-09",
      "expire_time": "2025-11-10",
      "create_api_key_id": "********-****-****-****-************",
      "cancel_time": "2025-11-09",
      "cancel_by_xai_message": null,
      "state": {
        "num_requests": 1,
        "num_pending": 0,
        "num_success": 1,
        "num_error": 0,
        "num_cancelled": 0
      }
    }
  ]
}

获取批处理任务

/v1/batches/{batch_id}

获取有关特定批次的信息。

路径参数

batch_idstring

(字符串,必填)— 批处理任务的唯一标识符

响应体

batch_idstring

(字符串,必填)— 批处理任务的 ID。

create_api_key_idstring

(字符串,必填)— 用于创建批处理任务的 API 密钥 ID。

create_timestring

(string, required)— 创建批次的时间。

namestring

(字符串,必填)— 便于人阅读的批处理任务名称。

stateobject

(对象,必填)— 包含批处理过程当前状态的汇总信息。

Exampletext

text

No parameters.
Exampletext

text

{
  "batch_id": "batch_1934e8b5-f3dc-45f1-8329-9841b0aee9d8",
  "name": "My New Batch",
  "create_time": "2025-11-11",
  "expire_time": "2025-11-12",
  "create_api_key_id": "********-****-****-****-************",
  "cancel_time": null,
  "cancel_by_xai_message": null,
  "state": {
    "num_requests": 0,
    "num_pending": 0,
    "num_success": 0,
    "num_error": 0,
    "num_cancelled": 0
  }
}

列出批处理任务中的请求

/v1/batches/{batch_id}/requests

列出批次中所有请求的元数据。

路径参数

batch_idstring

(字符串,必填)— 批处理任务的唯一标识符

查询参数

limitinteger | null

(整数或 null)— 单页最多返回的项目数(最多 1000 个)

pagination_tokenstring | null

(字符串或 null)— 用于检索下一页结果的 token

响应体

batch_request_metadataarray<object>

(对象数组,必填)— 给定批处理任务的请求元数据。

Exampletext

text

No parameters.
Exampletext

text

{
  "batch_request_metadata": [
    {
      "batch_request_id": "test_request_0",
      "endpoint": "xai_api.Chat/GetCompletion",
      "model": "grok-4",
      "state": "succeeded",
      "create_time": "2025-11-11",
      "finish_time": "2025-11-12"
    }
  ],
  "pagination_token": null
}

向批处理任务添加请求

/v1/batches/{batch_id}/requests

将多个请求添加到现有批次。

路径参数

batch_idstring

(字符串,必填)— 批处理任务的唯一标识符

请求体

batch_requestsarray<object>

(对象数组,必填)— 要添加到批处理任务的请求列表。

Exampletext

text

{
  "batch_requests": [
    {
      "batch_request_id": "test_request_0",
      "batch_request": {
        "chat_get_completion": {
          "messages": [
            {
              "role": "system",
              "content": "You are a helpful assistant that can answer questions and help with tasks."
            },
            {
              "role": "user",
              "content": "What is 101*3?"
            }
          ],
          "model": "grok-4"
        }
      }
    }
  ]
}
Exampletext

text

No response.

获取批处理结果

/v1/batches/{batch_id}/results

列出批次的处理结果。

路径参数

batch_idstring

(字符串,必填)— 批处理任务的唯一标识符

查询参数

limitinteger | null

(整数或 null)— 单页最多返回的项目数(最多 1000 个)

pagination_tokenstring | null

(字符串或 null)— 用于检索下一页结果的 token

响应体

resultsarray<object>

(对象数组,必填)— 已处理的结果。

Exampletext

text

No parameters.
Exampletext

text

{
  "results": [
    {
      "batch_request_id": "test_request_0",
      "batch_result": {
        "response": {
          "chat_get_completion": {
            "id": "e7c2162b-ca73-c181-2364-1feabef778fe_us-east-1",
            "object": "chat.completion",
            "created": 1762801725,
            "model": "grok-4",
            "choices": [
              {
                "index": 0,
                "message": {
                  "role": "assistant",
                  "content": "101 multiplied by 3 is 303. If you have more calculations or questions, feel free to ask!",
                  "refusal": null
                },
                "finish_reason": "stop"
              }
            ],
            "usage": {
              "prompt_tokens": 706,
              "completion_tokens": 22,
              "total_tokens": 827,
              "prompt_tokens_details": {
                "text_tokens": 706,
                "audio_tokens": 0,
                "image_tokens": 0,
                "cached_tokens": 679
              },
              "completion_tokens_details": {
                "reasoning_tokens": 99,
                "audio_tokens": 0,
                "accepted_prediction_tokens": 0,
                "rejected_prediction_tokens": 0
              },
              "num_sources_used": 0
            },
            "system_fingerprint": "fp_1944a19e1f"
          }
        }
      }
    }
  ],
  "pagination_token": null
}

取消批处理

/v1/batches/{batch_id}:cancel

取消批量处理所有请求。

路径参数

batch_idstring

(string, required)— 要取消的批次的唯一标识符

响应体

batch_idstring

(字符串,必填)— 批处理任务的 ID。

create_api_key_idstring

(字符串,必填)— 用于创建批处理任务的 API 密钥 ID。

create_timestring

(string, required)— 创建批次的时间。

namestring

(字符串,必填)— 便于人阅读的批处理任务名称。

stateobject

(对象,必填)— 包含批处理过程当前状态的汇总信息。

Exampletext

text

No parameters.
Exampletext

text

{
  "batch_id": "batch_1934e8b5-f3dc-45f1-8329-9841b0aee9d8",
  "name": "My New Batch",
  "create_time": "2025-11-11",
  "expire_time": "2025-11-12",
  "create_api_key_id": "********-****-****-****-************",
  "cancel_time": "2025-11-11",
  "cancel_by_xai_message": null,
  "state": {
    "num_requests": 1,
    "num_pending": 0,
    "num_success": 1,
    "num_error": 0,
    "num_cancelled": 0
  }
}

最后更新:2026 年 2 月 13 日