batches.mjs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 betaMessageBatch =
  22. * await client.beta.messages.batches.create({
  23. * requests: [
  24. * {
  25. * custom_id: 'my-custom-id-1',
  26. * params: {
  27. * max_tokens: 1024,
  28. * messages: [
  29. * { content: 'Hello, world', role: 'user' },
  30. * ],
  31. * model: 'claude-opus-4-6',
  32. * },
  33. * },
  34. * ],
  35. * });
  36. * ```
  37. */
  38. create(params, options) {
  39. const { betas, ...body } = params;
  40. return this._client.post('/v1/messages/batches?beta=true', {
  41. body,
  42. ...options,
  43. headers: buildHeaders([
  44. { 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString() },
  45. options?.headers,
  46. ]),
  47. });
  48. }
  49. /**
  50. * This endpoint is idempotent and can be used to poll for Message Batch
  51. * completion. To access the results of a Message Batch, make a request to the
  52. * `results_url` field in the response.
  53. *
  54. * Learn more about the Message Batches API in our
  55. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  56. *
  57. * @example
  58. * ```ts
  59. * const betaMessageBatch =
  60. * await client.beta.messages.batches.retrieve(
  61. * 'message_batch_id',
  62. * );
  63. * ```
  64. */
  65. retrieve(messageBatchID, params = {}, options) {
  66. const { betas } = params ?? {};
  67. return this._client.get(path `/v1/messages/batches/${messageBatchID}?beta=true`, {
  68. ...options,
  69. headers: buildHeaders([
  70. { 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString() },
  71. options?.headers,
  72. ]),
  73. });
  74. }
  75. /**
  76. * List all Message Batches within a Workspace. Most recently created batches are
  77. * returned first.
  78. *
  79. * Learn more about the Message Batches API in our
  80. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  81. *
  82. * @example
  83. * ```ts
  84. * // Automatically fetches more pages as needed.
  85. * for await (const betaMessageBatch of client.beta.messages.batches.list()) {
  86. * // ...
  87. * }
  88. * ```
  89. */
  90. list(params = {}, options) {
  91. const { betas, ...query } = params ?? {};
  92. return this._client.getAPIList('/v1/messages/batches?beta=true', (Page), {
  93. query,
  94. ...options,
  95. headers: buildHeaders([
  96. { 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString() },
  97. options?.headers,
  98. ]),
  99. });
  100. }
  101. /**
  102. * Delete a Message Batch.
  103. *
  104. * Message Batches can only be deleted once they've finished processing. If you'd
  105. * like to delete an in-progress batch, you must first cancel it.
  106. *
  107. * Learn more about the Message Batches API in our
  108. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  109. *
  110. * @example
  111. * ```ts
  112. * const betaDeletedMessageBatch =
  113. * await client.beta.messages.batches.delete(
  114. * 'message_batch_id',
  115. * );
  116. * ```
  117. */
  118. delete(messageBatchID, params = {}, options) {
  119. const { betas } = params ?? {};
  120. return this._client.delete(path `/v1/messages/batches/${messageBatchID}?beta=true`, {
  121. ...options,
  122. headers: buildHeaders([
  123. { 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString() },
  124. options?.headers,
  125. ]),
  126. });
  127. }
  128. /**
  129. * Batches may be canceled any time before processing ends. Once cancellation is
  130. * initiated, the batch enters a `canceling` state, at which time the system may
  131. * complete any in-progress, non-interruptible requests before finalizing
  132. * cancellation.
  133. *
  134. * The number of canceled requests is specified in `request_counts`. To determine
  135. * which requests were canceled, check the individual results within the batch.
  136. * Note that cancellation may not result in any canceled requests if they were
  137. * non-interruptible.
  138. *
  139. * Learn more about the Message Batches API in our
  140. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  141. *
  142. * @example
  143. * ```ts
  144. * const betaMessageBatch =
  145. * await client.beta.messages.batches.cancel(
  146. * 'message_batch_id',
  147. * );
  148. * ```
  149. */
  150. cancel(messageBatchID, params = {}, options) {
  151. const { betas } = params ?? {};
  152. return this._client.post(path `/v1/messages/batches/${messageBatchID}/cancel?beta=true`, {
  153. ...options,
  154. headers: buildHeaders([
  155. { 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString() },
  156. options?.headers,
  157. ]),
  158. });
  159. }
  160. /**
  161. * Streams the results of a Message Batch as a `.jsonl` file.
  162. *
  163. * Each line in the file is a JSON object containing the result of a single request
  164. * in the Message Batch. Results are not guaranteed to be in the same order as
  165. * requests. Use the `custom_id` field to match results to requests.
  166. *
  167. * Learn more about the Message Batches API in our
  168. * [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
  169. *
  170. * @example
  171. * ```ts
  172. * const betaMessageBatchIndividualResponse =
  173. * await client.beta.messages.batches.results(
  174. * 'message_batch_id',
  175. * );
  176. * ```
  177. */
  178. async results(messageBatchID, params = {}, options) {
  179. const batch = await this.retrieve(messageBatchID);
  180. if (!batch.results_url) {
  181. throw new AnthropicError(`No batch \`results_url\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`);
  182. }
  183. const { betas } = params ?? {};
  184. return this._client
  185. .get(batch.results_url, {
  186. ...options,
  187. headers: buildHeaders([
  188. {
  189. 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),
  190. Accept: 'application/binary',
  191. },
  192. options?.headers,
  193. ]),
  194. stream: true,
  195. __binaryResponse: true,
  196. })
  197. ._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));
  198. }
  199. }
  200. //# sourceMappingURL=batches.mjs.map