messages.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { APIResource } from "../../core/resource.mjs";
  3. import { buildHeaders } from "../../internal/headers.mjs";
  4. import { stainlessHelperHeader } from "../../lib/stainless-helper-header.mjs";
  5. import { MessageStream } from "../../lib/MessageStream.mjs";
  6. import { parseMessage, } from "../../lib/parser.mjs";
  7. import * as BatchesAPI from "./batches.mjs";
  8. import { Batches, } from "./batches.mjs";
  9. import { MODEL_NONSTREAMING_TOKENS } from "../../internal/constants.mjs";
  10. export class Messages extends APIResource {
  11. constructor() {
  12. super(...arguments);
  13. this.batches = new BatchesAPI.Batches(this._client);
  14. }
  15. create(body, options) {
  16. if (body.model in DEPRECATED_MODELS) {
  17. console.warn(`The model '${body.model}' is deprecated and will reach end-of-life on ${DEPRECATED_MODELS[body.model]}\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`);
  18. }
  19. if (body.model in MODELS_TO_WARN_WITH_THINKING_ENABLED &&
  20. body.thinking &&
  21. body.thinking.type === 'enabled') {
  22. console.warn(`Using Claude with ${body.model} and 'thinking.type=enabled' is deprecated. Use 'thinking.type=adaptive' instead which results in better model performance in our testing: https://platform.claude.com/docs/en/build-with-claude/adaptive-thinking`);
  23. }
  24. let timeout = this._client._options.timeout;
  25. if (!body.stream && timeout == null) {
  26. const maxNonstreamingTokens = MODEL_NONSTREAMING_TOKENS[body.model] ?? undefined;
  27. timeout = this._client.calculateNonstreamingTimeout(body.max_tokens, maxNonstreamingTokens);
  28. }
  29. // Collect helper info from tools and messages
  30. const helperHeader = stainlessHelperHeader(body.tools, body.messages);
  31. return this._client.post('/v1/messages', {
  32. body,
  33. timeout: timeout ?? 600000,
  34. ...options,
  35. headers: buildHeaders([helperHeader, options?.headers]),
  36. stream: body.stream ?? false,
  37. });
  38. }
  39. /**
  40. * Send a structured list of input messages with text and/or image content, along with an expected `output_config.format` and
  41. * the response will be automatically parsed and available in the `parsed_output` property of the message.
  42. *
  43. * @example
  44. * ```ts
  45. * const message = await client.messages.parse({
  46. * model: 'claude-sonnet-4-5-20250929',
  47. * max_tokens: 1024,
  48. * messages: [{ role: 'user', content: 'What is 2+2?' }],
  49. * output_config: {
  50. * format: zodOutputFormat(z.object({ answer: z.number() })),
  51. * },
  52. * });
  53. *
  54. * console.log(message.parsed_output?.answer); // 4
  55. * ```
  56. */
  57. parse(params, options) {
  58. return this.create(params, options).then((message) => parseMessage(message, params, { logger: this._client.logger ?? console }));
  59. }
  60. /**
  61. * Create a Message stream.
  62. *
  63. * If `output_config.format` is provided with a parseable format (like `zodOutputFormat()`),
  64. * the final message will include a `parsed_output` property with the parsed content.
  65. *
  66. * @example
  67. * ```ts
  68. * const stream = client.messages.stream({
  69. * model: 'claude-sonnet-4-5-20250929',
  70. * max_tokens: 1024,
  71. * messages: [{ role: 'user', content: 'What is 2+2?' }],
  72. * output_config: {
  73. * format: zodOutputFormat(z.object({ answer: z.number() })),
  74. * },
  75. * });
  76. *
  77. * const message = await stream.finalMessage();
  78. * console.log(message.parsed_output?.answer); // 4
  79. * ```
  80. */
  81. stream(body, options) {
  82. return MessageStream.createMessage(this, body, options, { logger: this._client.logger ?? console });
  83. }
  84. /**
  85. * Count the number of tokens in a Message.
  86. *
  87. * The Token Count API can be used to count the number of tokens in a Message,
  88. * including tools, images, and documents, without creating it.
  89. *
  90. * Learn more about token counting in our
  91. * [user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)
  92. *
  93. * @example
  94. * ```ts
  95. * const messageTokensCount =
  96. * await client.messages.countTokens({
  97. * messages: [{ content: 'string', role: 'user' }],
  98. * model: 'claude-opus-4-6',
  99. * });
  100. * ```
  101. */
  102. countTokens(body, options) {
  103. return this._client.post('/v1/messages/count_tokens', { body, ...options });
  104. }
  105. }
  106. const DEPRECATED_MODELS = {
  107. 'claude-1.3': 'November 6th, 2024',
  108. 'claude-1.3-100k': 'November 6th, 2024',
  109. 'claude-instant-1.1': 'November 6th, 2024',
  110. 'claude-instant-1.1-100k': 'November 6th, 2024',
  111. 'claude-instant-1.2': 'November 6th, 2024',
  112. 'claude-3-sonnet-20240229': 'July 21st, 2025',
  113. 'claude-3-opus-20240229': 'January 5th, 2026',
  114. 'claude-2.1': 'July 21st, 2025',
  115. 'claude-2.0': 'July 21st, 2025',
  116. 'claude-3-7-sonnet-latest': 'February 19th, 2026',
  117. 'claude-3-7-sonnet-20250219': 'February 19th, 2026',
  118. 'claude-3-5-haiku-latest': 'February 19th, 2026',
  119. 'claude-3-5-haiku-20241022': 'February 19th, 2026',
  120. };
  121. const MODELS_TO_WARN_WITH_THINKING_ENABLED = ['claude-opus-4-6'];
  122. Messages.Batches = Batches;
  123. //# sourceMappingURL=messages.mjs.map