batches.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { APIResource } from "../../core/resource.mjs";
  3. import { Page } from "../../core/pagination.mjs";
  4. import { buildHeaders } from "../../internal/headers.mjs";
  5. import { JSONLDecoder } from "../../internal/decoders/jsonl.mjs";
  6. import { AnthropicError } from "../../error.mjs";
  7. import { path } from "../../internal/utils/path.mjs";
  8. export class Batches extends APIResource {
  9. /**
  10. * Send a batch of Message creation requests.
  11. *
  12. * The Message Batches API can be used to process multiple Messages API requests at
  13. * once. Once a Message Batch is created, it begins processing immediately. Batches
  14. * can take up to 24 hours to complete.
  15. *
  16. * Learn more about the Message Batches API in our
  17. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  18. *
  19. * @example
  20. * ```ts
  21. * const messageBatch = await client.messages.batches.create({
  22. * requests: [
  23. * {
  24. * custom_id: 'my-custom-id-1',
  25. * params: {
  26. * max_tokens: 1024,
  27. * messages: [
  28. * { content: 'Hello, world', role: 'user' },
  29. * ],
  30. * model: 'claude-opus-4-6',
  31. * },
  32. * },
  33. * ],
  34. * });
  35. * ```
  36. */
  37. create(body, options) {
  38. return this._client.post('/v1/messages/batches', { body, ...options });
  39. }
  40. /**
  41. * This endpoint is idempotent and can be used to poll for Message Batch
  42. * completion. To access the results of a Message Batch, make a request to the
  43. * `results_url` field in the response.
  44. *
  45. * Learn more about the Message Batches API in our
  46. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  47. *
  48. * @example
  49. * ```ts
  50. * const messageBatch = await client.messages.batches.retrieve(
  51. * 'message_batch_id',
  52. * );
  53. * ```
  54. */
  55. retrieve(messageBatchID, options) {
  56. return this._client.get(path `/v1/messages/batches/${messageBatchID}`, options);
  57. }
  58. /**
  59. * List all Message Batches within a Workspace. Most recently created batches are
  60. * returned first.
  61. *
  62. * Learn more about the Message Batches API in our
  63. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  64. *
  65. * @example
  66. * ```ts
  67. * // Automatically fetches more pages as needed.
  68. * for await (const messageBatch of client.messages.batches.list()) {
  69. * // ...
  70. * }
  71. * ```
  72. */
  73. list(query = {}, options) {
  74. return this._client.getAPIList('/v1/messages/batches', (Page), { query, ...options });
  75. }
  76. /**
  77. * Delete a Message Batch.
  78. *
  79. * Message Batches can only be deleted once they've finished processing. If you'd
  80. * like to delete an in-progress batch, you must first cancel it.
  81. *
  82. * Learn more about the Message Batches API in our
  83. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  84. *
  85. * @example
  86. * ```ts
  87. * const deletedMessageBatch =
  88. * await client.messages.batches.delete('message_batch_id');
  89. * ```
  90. */
  91. delete(messageBatchID, options) {
  92. return this._client.delete(path `/v1/messages/batches/${messageBatchID}`, options);
  93. }
  94. /**
  95. * Batches may be canceled any time before processing ends. Once cancellation is
  96. * initiated, the batch enters a `canceling` state, at which time the system may
  97. * complete any in-progress, non-interruptible requests before finalizing
  98. * cancellation.
  99. *
  100. * The number of canceled requests is specified in `request_counts`. To determine
  101. * which requests were canceled, check the individual results within the batch.
  102. * Note that cancellation may not result in any canceled requests if they were
  103. * non-interruptible.
  104. *
  105. * Learn more about the Message Batches API in our
  106. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  107. *
  108. * @example
  109. * ```ts
  110. * const messageBatch = await client.messages.batches.cancel(
  111. * 'message_batch_id',
  112. * );
  113. * ```
  114. */
  115. cancel(messageBatchID, options) {
  116. return this._client.post(path `/v1/messages/batches/${messageBatchID}/cancel`, options);
  117. }
  118. /**
  119. * Streams the results of a Message Batch as a `.jsonl` file.
  120. *
  121. * Each line in the file is a JSON object containing the result of a single request
  122. * in the Message Batch. Results are not guaranteed to be in the same order as
  123. * requests. Use the `custom_id` field to match results to requests.
  124. *
  125. * Learn more about the Message Batches API in our
  126. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  127. *
  128. * @example
  129. * ```ts
  130. * const messageBatchIndividualResponse =
  131. * await client.messages.batches.results('message_batch_id');
  132. * ```
  133. */
  134. async results(messageBatchID, options) {
  135. const batch = await this.retrieve(messageBatchID);
  136. if (!batch.results_url) {
  137. throw new AnthropicError(`No batch \`results_url\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`);
  138. }
  139. return this._client
  140. .get(batch.results_url, {
  141. ...options,
  142. headers: buildHeaders([{ Accept: 'application/binary' }, options?.headers]),
  143. stream: true,
  144. __binaryResponse: true,
  145. })
  146. ._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));
  147. }
  148. }
  149. //# sourceMappingURL=batches.mjs.map