消息批次 API 支持与消息 API 相同的功能集。虽然本页重点介绍如何使用消息批次 API,但有关消息 API 功能集的示例,请参阅消息 API 示例

创建消息批次

JSON
{
  "id": "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF",
  "type": "message_batch",
  "processing_status": "in_progress",
  "request_counts": {
    "processing": 2,
    "succeeded": 0,
    "errored": 0,
    "canceled": 0,
    "expired": 0
  },
  "ended_at": null,
  "created_at": "2024-09-24T18:37:24.100435Z",
  "expires_at": "2024-09-25T18:37:24.100435Z",
  "cancel_initiated_at": null,
  "results_url": null
}

轮询消息批次完成情况

要轮询消息批次,您需要其 id,该 id 在创建请求时的响应中提供,或通过列出批次获得。示例 idmsgbatch_013Zva2CMHLNnXjNJJKqJ2EF

列出工作区中的所有消息批次

Output
{
  "id": "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF",
  "type": "message_batch",
  ...
}
{
  "id": "msgbatch_01HkcTjaV5uDC8jWR4ZsDV8d",
  "type": "message_batch",
  ...
}

获取消息批次结果

一旦您的消息批次状态为 ended,您就可以查看批次的 results_url 并以 .jsonl 文件的形式获取结果。

Output
{
  "id": "my-second-request",
  "result": {
    "type": "succeeded",
    "message": {
      "id": "msg_018gCsTGsXkYJVqYPxTgDHBU",
      "type": "message",
      ...
    }
  }
}
{
  "custom_id": "my-first-request",
  "result": {
    "type": "succeeded",
    "message": {
      "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
      "type": "message",
      ...
    }
  }
}

取消消息批次

在取消后立即,批次的 processing_status 将变为 canceling。您可以使用相同的轮询批次完成技术来轮询取消何时完成,因为被取消的批次最终也会变为 ended 状态,并可能包含结果。

JSON
{
  "id": "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF",
  "type": "message_batch",
  "processing_status": "canceling",
  "request_counts": {
    "processing": 2,
    "succeeded": 0,
    "errored": 0,
    "canceled": 0,
    "expired": 0
  },
  "ended_at": null,
  "created_at": "2024-09-24T18:37:24.100435Z",
  "expires_at": "2024-09-25T18:37:24.100435Z",
  "cancel_initiated_at": "2024-09-24T18:39:03.114875Z",
  "results_url": null
}