diff --git a/.stats.yml b/.stats.yml index 63da3bd2..61bec13e 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ -configured_endpoints: 9 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-134649e8f68418dc1cde6589ef6c63f79f65d8cbcc8732a6652354ade6548d16.yml +configured_endpoints: 10 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-d95f5b98650cf1d0a75bd514eaa6705bef41aa89e8fe37e849ccdde57a91aaa2.yml diff --git a/api.md b/api.md index 7b3b69cf..ab1abd4c 100644 --- a/api.md +++ b/api.md @@ -63,6 +63,8 @@ Types: Types: +- BetaBase64PDFBlock +- BetaBase64PDFSource - BetaCacheControlEphemeral - BetaContentBlock - BetaContentBlockParam @@ -71,6 +73,7 @@ Types: - BetaMessage - BetaMessageDeltaUsage - BetaMessageParam +- BetaMessageTokensCount - BetaMetadata - BetaRawContentBlockDeltaEvent - BetaRawContentBlockStartEvent @@ -99,6 +102,7 @@ Types: Methods: - client.beta.messages.create({ ...params }) -> BetaMessage +- client.beta.messages.countTokens({ ...params }) -> BetaMessageTokensCount ### Batches diff --git a/src/resources/beta/beta.ts b/src/resources/beta/beta.ts index 1a9e19bb..80b1366c 100644 --- a/src/resources/beta/beta.ts +++ b/src/resources/beta/beta.ts @@ -3,6 +3,8 @@ import { APIResource } from '../../resource'; import * as MessagesAPI from './messages/messages'; import { + BetaBase64PDFBlock, + BetaBase64PDFSource, BetaCacheControlEphemeral, BetaContentBlock, BetaContentBlockParam, @@ -11,6 +13,7 @@ import { BetaMessage, BetaMessageDeltaUsage, BetaMessageParam, + BetaMessageTokensCount, BetaMetadata, BetaRawContentBlockDeltaEvent, BetaRawContentBlockStartEvent, @@ -35,6 +38,7 @@ import { BetaToolUseBlock, BetaToolUseBlockParam, BetaUsage, + MessageCountTokensParams, MessageCreateParams, MessageCreateParamsNonStreaming, MessageCreateParamsStreaming, @@ -52,7 +56,8 @@ export type AnthropicBeta = | (string & {}) | 'message-batches-2024-09-24' | 'prompt-caching-2024-07-31' - | 'computer-use-2024-10-22'; + | 'computer-use-2024-10-22' + | 'pdfs-2024-09-25'; export interface BetaAPIError { message: string; @@ -130,6 +135,8 @@ export declare namespace Beta { export { Messages as Messages, + type BetaBase64PDFBlock as BetaBase64PDFBlock, + type BetaBase64PDFSource as BetaBase64PDFSource, type BetaCacheControlEphemeral as BetaCacheControlEphemeral, type BetaContentBlock as BetaContentBlock, type BetaContentBlockParam as BetaContentBlockParam, @@ -138,6 +145,7 @@ export declare namespace Beta { type BetaMessage as BetaMessage, type BetaMessageDeltaUsage as BetaMessageDeltaUsage, type BetaMessageParam as BetaMessageParam, + type BetaMessageTokensCount as BetaMessageTokensCount, type BetaMetadata as BetaMetadata, type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent, type BetaRawContentBlockStartEvent as BetaRawContentBlockStartEvent, @@ -165,6 +173,7 @@ export declare namespace Beta { type MessageCreateParams as MessageCreateParams, type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming, type MessageCreateParamsStreaming as MessageCreateParamsStreaming, + type MessageCountTokensParams as MessageCountTokensParams, }; export { PromptCaching as PromptCaching }; diff --git a/src/resources/beta/index.ts b/src/resources/beta/index.ts index a89803bd..6e2b0a89 100644 --- a/src/resources/beta/index.ts +++ b/src/resources/beta/index.ts @@ -15,6 +15,8 @@ export { } from './beta'; export { Messages, + type BetaBase64PDFBlock, + type BetaBase64PDFSource, type BetaCacheControlEphemeral, type BetaContentBlock, type BetaContentBlockParam, @@ -23,6 +25,7 @@ export { type BetaMessage, type BetaMessageDeltaUsage, type BetaMessageParam, + type BetaMessageTokensCount, type BetaMetadata, type BetaRawContentBlockDeltaEvent, type BetaRawContentBlockStartEvent, @@ -50,5 +53,6 @@ export { type MessageCreateParams, type MessageCreateParamsNonStreaming, type MessageCreateParamsStreaming, + type MessageCountTokensParams, } from './messages/index'; export { PromptCaching } from './prompt-caching/index'; diff --git a/src/resources/beta/messages/index.ts b/src/resources/beta/messages/index.ts index 45f71739..2cf85964 100644 --- a/src/resources/beta/messages/index.ts +++ b/src/resources/beta/messages/index.ts @@ -19,6 +19,8 @@ export { } from './batches'; export { Messages, + type BetaBase64PDFBlock, + type BetaBase64PDFSource, type BetaCacheControlEphemeral, type BetaContentBlock, type BetaContentBlockParam, @@ -27,6 +29,7 @@ export { type BetaMessage, type BetaMessageDeltaUsage, type BetaMessageParam, + type BetaMessageTokensCount, type BetaMetadata, type BetaRawContentBlockDeltaEvent, type BetaRawContentBlockStartEvent, @@ -54,4 +57,5 @@ export { type MessageCreateParams, type MessageCreateParamsNonStreaming, type MessageCreateParamsStreaming, + type MessageCountTokensParams, } from './messages'; diff --git a/src/resources/beta/messages/messages.ts b/src/resources/beta/messages/messages.ts index 31174f5e..156fd6b2 100644 --- a/src/resources/beta/messages/messages.ts +++ b/src/resources/beta/messages/messages.ts @@ -61,6 +61,43 @@ export class Messages extends APIResource { stream: params.stream ?? false, }) as APIPromise | APIPromise>; } + + /** + * Count the number of tokens in a Message. + * + * The Token Count API can be used to count the number of tokens in a Message, + * including tools, images, and documents, without creating it. + */ + countTokens( + params: MessageCountTokensParams, + options?: Core.RequestOptions, + ): Core.APIPromise { + const { betas, ...body } = params; + return this._client.post('/v1/messages/count_tokens?beta=true', { + body, + ...options, + headers: { + ...(betas?.toString() != null ? { 'anthropic-beta': betas?.toString() } : undefined), + ...options?.headers, + }, + }); + } +} + +export interface BetaBase64PDFBlock { + source: BetaBase64PDFSource; + + type: 'document'; + + cache_control?: BetaCacheControlEphemeral | null; +} + +export interface BetaBase64PDFSource { + data: string; + + media_type: 'application/pdf'; + + type: 'base64'; } export interface BetaCacheControlEphemeral { @@ -73,7 +110,8 @@ export type BetaContentBlockParam = | BetaTextBlockParam | BetaImageBlockParam | BetaToolUseBlockParam - | BetaToolResultBlockParam; + | BetaToolResultBlockParam + | BetaBase64PDFBlock; export interface BetaImageBlockParam { source: BetaImageBlockParam.Source; @@ -217,6 +255,14 @@ export interface BetaMessageParam { role: 'user' | 'assistant'; } +export interface BetaMessageTokensCount { + /** + * The total number of tokens across the provided list of messages, system prompt, + * and tools. + */ + input_tokens: number; +} + export interface BetaMetadata { /** * An external identifier for the user who is associated with the request. @@ -327,6 +373,11 @@ export interface BetaTool { */ input_schema: BetaTool.InputSchema; + /** + * Name of the tool. + * + * This is how the tool will be called by the model and in tool_use blocks. + */ name: string; cache_control?: BetaCacheControlEphemeral | null; @@ -360,6 +411,11 @@ export namespace BetaTool { } export interface BetaToolBash20241022 { + /** + * Name of the tool. + * + * This is how the tool will be called by the model and in tool_use blocks. + */ name: 'bash'; type: 'bash_20241022'; @@ -424,16 +480,30 @@ export interface BetaToolChoiceTool { } export interface BetaToolComputerUse20241022 { + /** + * The height of the display in pixels. + */ display_height_px: number; + /** + * The width of the display in pixels. + */ display_width_px: number; + /** + * Name of the tool. + * + * This is how the tool will be called by the model and in tool_use blocks. + */ name: 'computer'; type: 'computer_20241022'; cache_control?: BetaCacheControlEphemeral | null; + /** + * The X11 display number (e.g. 0, 1) for the display. + */ display_number?: number | null; } @@ -450,6 +520,11 @@ export interface BetaToolResultBlockParam { } export interface BetaToolTextEditor20241022 { + /** + * Name of the tool. + * + * This is how the tool will be called by the model and in tool_use blocks. + */ name: 'str_replace_editor'; type: 'text_editor_20241022'; @@ -801,11 +876,204 @@ export interface MessageCreateParamsStreaming extends MessageCreateParamsBase { stream: true; } +export interface MessageCountTokensParams { + /** + * Body param: Input messages. + * + * Our models are trained to operate on alternating `user` and `assistant` + * conversational turns. When creating a new `Message`, you specify the prior + * conversational turns with the `messages` parameter, and the model then generates + * the next `Message` in the conversation. Consecutive `user` or `assistant` turns + * in your request will be combined into a single turn. + * + * Each input message must be an object with a `role` and `content`. You can + * specify a single `user`-role message, or you can include multiple `user` and + * `assistant` messages. + * + * If the final message uses the `assistant` role, the response content will + * continue immediately from the content in that message. This can be used to + * constrain part of the model's response. + * + * Example with a single `user` message: + * + * ```json + * [{ "role": "user", "content": "Hello, Claude" }] + * ``` + * + * Example with multiple conversational turns: + * + * ```json + * [ + * { "role": "user", "content": "Hello there." }, + * { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + * { "role": "user", "content": "Can you explain LLMs in plain English?" } + * ] + * ``` + * + * Example with a partially-filled response from Claude: + * + * ```json + * [ + * { + * "role": "user", + * "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + * }, + * { "role": "assistant", "content": "The best answer is (" } + * ] + * ``` + * + * Each input message `content` may be either a single `string` or an array of + * content blocks, where each block has a specific `type`. Using a `string` for + * `content` is shorthand for an array of one content block of type `"text"`. The + * following input messages are equivalent: + * + * ```json + * { "role": "user", "content": "Hello, Claude" } + * ``` + * + * ```json + * { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + * ``` + * + * Starting with Claude 3 models, you can also send image content blocks: + * + * ```json + * { + * "role": "user", + * "content": [ + * { + * "type": "image", + * "source": { + * "type": "base64", + * "media_type": "image/jpeg", + * "data": "/9j/4AAQSkZJRg..." + * } + * }, + * { "type": "text", "text": "What is in this image?" } + * ] + * } + * ``` + * + * We currently support the `base64` source type for images, and the `image/jpeg`, + * `image/png`, `image/gif`, and `image/webp` media types. + * + * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for + * more input examples. + * + * Note that if you want to include a + * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use + * the top-level `system` parameter — there is no `"system"` role for input + * messages in the Messages API. + */ + messages: Array; + + /** + * Body param: The model that will complete your prompt.\n\nSee + * [models](https://docs.anthropic.com/en/docs/models-overview) for additional + * details and options. + */ + model: MessagesAPI.Model; + + /** + * Body param: System prompt. + * + * A system prompt is a way of providing context and instructions to Claude, such + * as specifying a particular goal or role. See our + * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts). + */ + system?: string | Array; + + /** + * Body param: How the model should use the provided tools. The model can use a + * specific tool, any available tool, or decide by itself. + */ + tool_choice?: BetaToolChoice; + + /** + * Body param: Definitions of tools that the model may use. + * + * If you include `tools` in your API request, the model may return `tool_use` + * content blocks that represent the model's use of those tools. You can then run + * those tools using the tool input generated by the model and then optionally + * return results back to the model using `tool_result` content blocks. + * + * Each tool definition includes: + * + * - `name`: Name of the tool. + * - `description`: Optional, but strongly-recommended description of the tool. + * - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + * shape that the model will produce in `tool_use` output content blocks. + * + * For example, if you defined `tools` as: + * + * ```json + * [ + * { + * "name": "get_stock_price", + * "description": "Get the current stock price for a given ticker symbol.", + * "input_schema": { + * "type": "object", + * "properties": { + * "ticker": { + * "type": "string", + * "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + * } + * }, + * "required": ["ticker"] + * } + * } + * ] + * ``` + * + * And then asked the model "What's the S&P 500 at today?", the model might produce + * `tool_use` content blocks in the response like this: + * + * ```json + * [ + * { + * "type": "tool_use", + * "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + * "name": "get_stock_price", + * "input": { "ticker": "^GSPC" } + * } + * ] + * ``` + * + * You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + * input, and return the following back to the model in a subsequent `user` + * message: + * + * ```json + * [ + * { + * "type": "tool_result", + * "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + * "content": "259.75 USD" + * } + * ] + * ``` + * + * Tools can be used for workflows that include running client-side tools and + * functions, or more generally whenever you want the model to produce a particular + * JSON structure of output. + * + * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details. + */ + tools?: Array; + + /** + * Header param: Optional header to specify the beta version(s) you want to use. + */ + betas?: Array; +} + Messages.Batches = Batches; Messages.BetaMessageBatchesPage = BetaMessageBatchesPage; export declare namespace Messages { export { + type BetaBase64PDFBlock as BetaBase64PDFBlock, + type BetaBase64PDFSource as BetaBase64PDFSource, type BetaCacheControlEphemeral as BetaCacheControlEphemeral, type BetaContentBlock as BetaContentBlock, type BetaContentBlockParam as BetaContentBlockParam, @@ -814,6 +1082,7 @@ export declare namespace Messages { type BetaMessage as BetaMessage, type BetaMessageDeltaUsage as BetaMessageDeltaUsage, type BetaMessageParam as BetaMessageParam, + type BetaMessageTokensCount as BetaMessageTokensCount, type BetaMetadata as BetaMetadata, type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent, type BetaRawContentBlockStartEvent as BetaRawContentBlockStartEvent, @@ -841,6 +1110,7 @@ export declare namespace Messages { type MessageCreateParams as MessageCreateParams, type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming, type MessageCreateParamsStreaming as MessageCreateParamsStreaming, + type MessageCountTokensParams as MessageCountTokensParams, }; export { diff --git a/src/resources/beta/prompt-caching/messages.ts b/src/resources/beta/prompt-caching/messages.ts index 01aa4f89..4ae7449b 100644 --- a/src/resources/beta/prompt-caching/messages.ts +++ b/src/resources/beta/prompt-caching/messages.ts @@ -213,6 +213,11 @@ export interface PromptCachingBetaTool { */ input_schema: PromptCachingBetaTool.InputSchema; + /** + * Name of the tool. + * + * This is how the tool will be called by the model and in tool_use blocks. + */ name: string; cache_control?: PromptCachingBetaCacheControlEphemeral | null; diff --git a/src/resources/messages.ts b/src/resources/messages.ts index bd79fcc4..f100d875 100644 --- a/src/resources/messages.ts +++ b/src/resources/messages.ts @@ -349,6 +349,11 @@ export interface Tool { */ input_schema: Tool.InputSchema; + /** + * Name of the tool. + * + * This is how the tool will be called by the model and in tool_use blocks. + */ name: string; /** diff --git a/tests/api-resources/beta/messages/batches.test.ts b/tests/api-resources/beta/messages/batches.test.ts index 6437483c..ed2027c8 100644 --- a/tests/api-resources/beta/messages/batches.test.ts +++ b/tests/api-resources/beta/messages/batches.test.ts @@ -17,7 +17,7 @@ describe('resource batches', () => { params: { max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', }, }, { @@ -25,7 +25,7 @@ describe('resource batches', () => { params: { max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', }, }, { @@ -33,7 +33,7 @@ describe('resource batches', () => { params: { max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', }, }, ], @@ -55,7 +55,7 @@ describe('resource batches', () => { params: { max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', metadata: { user_id: '13803d75-b4b5-4c3e-b2a2-6f21399b021b' }, stop_sequences: ['string', 'string', 'string'], stream: false, @@ -123,7 +123,7 @@ describe('resource batches', () => { params: { max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', metadata: { user_id: '13803d75-b4b5-4c3e-b2a2-6f21399b021b' }, stop_sequences: ['string', 'string', 'string'], stream: false, @@ -191,7 +191,7 @@ describe('resource batches', () => { params: { max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', metadata: { user_id: '13803d75-b4b5-4c3e-b2a2-6f21399b021b' }, stop_sequences: ['string', 'string', 'string'], stream: false, diff --git a/tests/api-resources/beta/messages/messages.test.ts b/tests/api-resources/beta/messages/messages.test.ts index b9c98f61..64b6299c 100644 --- a/tests/api-resources/beta/messages/messages.test.ts +++ b/tests/api-resources/beta/messages/messages.test.ts @@ -13,7 +13,7 @@ describe('resource messages', () => { const responsePromise = client.beta.messages.create({ max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', }); const rawResponse = await responsePromise.asResponse(); expect(rawResponse).toBeInstanceOf(Response); @@ -28,7 +28,7 @@ describe('resource messages', () => { const response = await client.beta.messages.create({ max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', metadata: { user_id: '13803d75-b4b5-4c3e-b2a2-6f21399b021b' }, stop_sequences: ['string', 'string', 'string'], stream: false, @@ -81,4 +81,77 @@ describe('resource messages', () => { betas: ['string', 'string', 'string'], }); }); + + test('countTokens: only required params', async () => { + const responsePromise = client.beta.messages.countTokens({ + messages: [ + { content: 'string', role: 'user' }, + { content: 'string', role: 'user' }, + { content: 'string', role: 'user' }, + ], + model: 'string', + }); + const rawResponse = await responsePromise.asResponse(); + expect(rawResponse).toBeInstanceOf(Response); + const response = await responsePromise; + expect(response).not.toBeInstanceOf(Response); + const dataAndResponse = await responsePromise.withResponse(); + expect(dataAndResponse.data).toBe(response); + expect(dataAndResponse.response).toBe(rawResponse); + }); + + test('countTokens: required and optional params', async () => { + const response = await client.beta.messages.countTokens({ + messages: [ + { content: 'string', role: 'user' }, + { content: 'string', role: 'user' }, + { content: 'string', role: 'user' }, + ], + model: 'string', + system: [{ text: "Today's date is 2024-06-01.", type: 'text', cache_control: { type: 'ephemeral' } }], + tool_choice: { type: 'auto', disable_parallel_tool_use: true }, + tools: [ + { + input_schema: { + type: 'object', + properties: { + location: { description: 'The city and state, e.g. San Francisco, CA', type: 'string' }, + unit: { description: 'Unit for the output - one of (celsius, fahrenheit)', type: 'string' }, + }, + }, + name: 'x', + cache_control: { type: 'ephemeral' }, + description: 'Get the current weather in a given location', + type: 'custom', + }, + { + input_schema: { + type: 'object', + properties: { + location: { description: 'The city and state, e.g. San Francisco, CA', type: 'string' }, + unit: { description: 'Unit for the output - one of (celsius, fahrenheit)', type: 'string' }, + }, + }, + name: 'x', + cache_control: { type: 'ephemeral' }, + description: 'Get the current weather in a given location', + type: 'custom', + }, + { + input_schema: { + type: 'object', + properties: { + location: { description: 'The city and state, e.g. San Francisco, CA', type: 'string' }, + unit: { description: 'Unit for the output - one of (celsius, fahrenheit)', type: 'string' }, + }, + }, + name: 'x', + cache_control: { type: 'ephemeral' }, + description: 'Get the current weather in a given location', + type: 'custom', + }, + ], + betas: ['string', 'string', 'string'], + }); + }); }); diff --git a/tests/api-resources/beta/prompt-caching/messages.test.ts b/tests/api-resources/beta/prompt-caching/messages.test.ts index 37034485..dd94b3a7 100644 --- a/tests/api-resources/beta/prompt-caching/messages.test.ts +++ b/tests/api-resources/beta/prompt-caching/messages.test.ts @@ -13,7 +13,7 @@ describe('resource messages', () => { const responsePromise = client.beta.promptCaching.messages.create({ max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', }); const rawResponse = await responsePromise.asResponse(); expect(rawResponse).toBeInstanceOf(Response); @@ -28,7 +28,7 @@ describe('resource messages', () => { const response = await client.beta.promptCaching.messages.create({ max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', metadata: { user_id: '13803d75-b4b5-4c3e-b2a2-6f21399b021b' }, stop_sequences: ['string', 'string', 'string'], stream: false, diff --git a/tests/api-resources/messages.test.ts b/tests/api-resources/messages.test.ts index 870e9b53..0497742e 100644 --- a/tests/api-resources/messages.test.ts +++ b/tests/api-resources/messages.test.ts @@ -13,7 +13,7 @@ describe('resource messages', () => { const responsePromise = client.messages.create({ max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', }); const rawResponse = await responsePromise.asResponse(); expect(rawResponse).toBeInstanceOf(Response); @@ -28,7 +28,7 @@ describe('resource messages', () => { const response = await client.messages.create({ max_tokens: 1024, messages: [{ content: 'Hello, world', role: 'user' }], - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', metadata: { user_id: '13803d75-b4b5-4c3e-b2a2-6f21399b021b' }, stop_sequences: ['string', 'string', 'string'], stream: false,