@anthropic-ai/sdk
Advanced tools
Comparing version 0.21.1 to 0.22.0
# Changelog | ||
## 0.22.0 (2024-05-30) | ||
Full Changelog: [sdk-v0.21.1...sdk-v0.22.0](https://github.com/anthropics/anthropic-sdk-typescript/compare/sdk-v0.21.1...sdk-v0.22.0) | ||
### Features | ||
* **api/types:** add stream event type aliases with a Raw prefix ([#428](https://github.com/anthropics/anthropic-sdk-typescript/issues/428)) ([1e367e4](https://github.com/anthropics/anthropic-sdk-typescript/commit/1e367e4020fa4691c565c89bdfba40c2f6060871)) | ||
* **api:** tool use is GA and available on 3P ([#429](https://github.com/anthropics/anthropic-sdk-typescript/issues/429)) ([2decf85](https://github.com/anthropics/anthropic-sdk-typescript/commit/2decf85e7471932dad98c21d4ed2d476ab1588a6)) | ||
* **bedrock:** support tools ([91fc61a](https://github.com/anthropics/anthropic-sdk-typescript/commit/91fc61ae7246705d26e96a95dae38b46e9ad9290)) | ||
* **streaming:** add tools support ([4c83bb1](https://github.com/anthropics/anthropic-sdk-typescript/commit/4c83bb111735cd513c09d5ed57a5cb0888534afd)) | ||
* **vertex:** support tools ([acf0aa7](https://github.com/anthropics/anthropic-sdk-typescript/commit/acf0aa7571425c8582740616e24883c2ec65218b)) | ||
### Documentation | ||
* **helpers:** mention inputJson event ([0ef0e39](https://github.com/anthropics/anthropic-sdk-typescript/commit/0ef0e39a870541bbe800b03c1bdcf88eb6e1350c)) | ||
* **readme:** add bundle size badge ([#426](https://github.com/anthropics/anthropic-sdk-typescript/issues/426)) ([bf7c1fd](https://github.com/anthropics/anthropic-sdk-typescript/commit/bf7c1fdaf3476d5c43079e8a0789ed0dd0c807a6)) | ||
## 0.21.1 (2024-05-21) | ||
@@ -4,0 +22,0 @@ |
@@ -86,3 +86,2 @@ import * as Core from "./core.js"; | ||
messages: API.Messages; | ||
beta: API.Beta; | ||
protected defaultQuery(): Core.DefaultQuery | undefined; | ||
@@ -130,2 +129,3 @@ protected defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers; | ||
export import ImageBlockParam = API.ImageBlockParam; | ||
export import InputJsonDelta = API.InputJsonDelta; | ||
export import Message = API.Message; | ||
@@ -138,5 +138,16 @@ export import MessageDeltaEvent = API.MessageDeltaEvent; | ||
export import MessageStreamEvent = API.MessageStreamEvent; | ||
export import RawContentBlockDeltaEvent = API.RawContentBlockDeltaEvent; | ||
export import RawContentBlockStartEvent = API.RawContentBlockStartEvent; | ||
export import RawContentBlockStopEvent = API.RawContentBlockStopEvent; | ||
export import RawMessageDeltaEvent = API.RawMessageDeltaEvent; | ||
export import RawMessageStartEvent = API.RawMessageStartEvent; | ||
export import RawMessageStopEvent = API.RawMessageStopEvent; | ||
export import RawMessageStreamEvent = API.RawMessageStreamEvent; | ||
export import TextBlock = API.TextBlock; | ||
export import TextBlockParam = API.TextBlockParam; | ||
export import TextDelta = API.TextDelta; | ||
export import Tool = API.Tool; | ||
export import ToolResultBlockParam = API.ToolResultBlockParam; | ||
export import ToolUseBlock = API.ToolUseBlock; | ||
export import ToolUseBlockParam = API.ToolUseBlockParam; | ||
export import Usage = API.Usage; | ||
@@ -147,5 +158,4 @@ export import MessageCreateParams = API.MessageCreateParams; | ||
export import MessageStreamParams = API.MessageStreamParams; | ||
export import Beta = API.Beta; | ||
} | ||
export default Anthropic; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -64,3 +64,2 @@ "use strict"; | ||
this.messages = new API.Messages(this); | ||
this.beta = new API.Beta(this); | ||
this._options = options; | ||
@@ -146,3 +145,2 @@ this.apiKey = apiKey; | ||
Anthropic.Messages = API.Messages; | ||
Anthropic.Beta = API.Beta; | ||
})(Anthropic = exports.Anthropic || (exports.Anthropic = {})); | ||
@@ -149,0 +147,0 @@ exports = module.exports = Anthropic; |
import * as Core from '@anthropic-ai/sdk/core'; | ||
import { AnthropicError, APIUserAbortError } from '@anthropic-ai/sdk/error'; | ||
import { ContentBlock, Messages, Message, MessageStreamEvent, MessageParam, MessageCreateParams, MessageStreamParams } from '@anthropic-ai/sdk/resources/messages'; | ||
import { ContentBlock, Messages, Message, MessageStreamEvent, MessageParam, MessageCreateParams, MessageCreateParamsBase } from '@anthropic-ai/sdk/resources/messages'; | ||
import { type ReadableStream } from '@anthropic-ai/sdk/_shims/index'; | ||
@@ -10,2 +10,3 @@ export interface MessageStreamEvents { | ||
text: (textDelta: string, textSnapshot: string) => void; | ||
inputJson: (partialJson: string, jsonSnapshot: unknown) => void; | ||
message: (message: Message) => void; | ||
@@ -32,3 +33,3 @@ contentBlock: (content: ContentBlock) => void; | ||
static fromReadableStream(stream: ReadableStream): MessageStream; | ||
static createMessage(messages: Messages, params: MessageStreamParams, options?: Core.RequestOptions): MessageStream; | ||
static createMessage(messages: Messages, params: MessageCreateParamsBase, options?: Core.RequestOptions): MessageStream; | ||
protected _run(executor: () => Promise<any>): void; | ||
@@ -35,0 +36,0 @@ protected _addMessageParam(message: MessageParam): void; |
@@ -18,2 +18,4 @@ "use strict"; | ||
const streaming_1 = require("@anthropic-ai/sdk/streaming"); | ||
const parser_1 = require("../_vendor/partial-json-parser/parser.js"); | ||
const JSON_BUF_PROPERTY = '__json_buf'; | ||
class MessageStream { | ||
@@ -316,5 +318,11 @@ constructor() { | ||
case 'content_block_delta': { | ||
if (event.delta.type === 'text_delta') { | ||
this._emit('text', event.delta.text, messageSnapshot.content.at(-1).text || ''); | ||
const content = messageSnapshot.content.at(-1); | ||
if (event.delta.type === 'text_delta' && content.type === 'text') { | ||
this._emit('text', event.delta.text, content.text || ''); | ||
} | ||
else if (event.delta.type === 'input_json_delta' && content.type === 'tool_use') { | ||
if (content.input) { | ||
this._emit('inputJson', event.delta.partial_json, content.input); | ||
} | ||
} | ||
break; | ||
@@ -376,2 +384,17 @@ } | ||
} | ||
else if (snapshotContent?.type === 'tool_use' && event.delta.type === 'input_json_delta') { | ||
// we need to keep track of the raw JSON string as well so that we can | ||
// re-parse it for each delta, for now we just store it as an untyped | ||
// non-enumerable property on the snapshot | ||
let jsonBuf = snapshotContent[JSON_BUF_PROPERTY] || ''; | ||
jsonBuf += event.delta.partial_json; | ||
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, { | ||
value: jsonBuf, | ||
enumerable: false, | ||
writable: true, | ||
}); | ||
if (jsonBuf) { | ||
snapshotContent.input = (0, parser_1.partialParse)(jsonBuf); | ||
} | ||
} | ||
return snapshot; | ||
@@ -378,0 +401,0 @@ } |
{ | ||
"name": "@anthropic-ai/sdk", | ||
"version": "0.21.1", | ||
"version": "0.22.0", | ||
"description": "The official TypeScript library for the Anthropic API", | ||
@@ -5,0 +5,0 @@ "author": "Anthropic <support@anthropic.com>", |
# Anthropic TypeScript API Library | ||
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/sdk.svg)](https://npmjs.org/package/@anthropic-ai/sdk) | ||
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/sdk.svg)](https://npmjs.org/package/@anthropic-ai/sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@anthropic-ai/sdk) | ||
@@ -5,0 +5,0 @@ This library provides convenient access to the Anthropic REST API from server-side TypeScript or JavaScript. |
@@ -1,4 +0,3 @@ | ||
export { Beta } from "./beta/beta.js"; | ||
export { Completion, CompletionCreateParams, CompletionCreateParamsNonStreaming, CompletionCreateParamsStreaming, Completions, } from "./completions.js"; | ||
export { ContentBlock, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent, ImageBlockParam, Message, MessageDeltaEvent, MessageDeltaUsage, MessageParam, MessageStartEvent, MessageStopEvent, MessageStreamEvent, TextBlock, TextBlockParam, TextDelta, Usage, MessageCreateParams, MessageCreateParamsNonStreaming, MessageCreateParamsStreaming, MessageStreamParams, Messages, } from "./messages.js"; | ||
export { ContentBlock, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent, ImageBlockParam, InputJsonDelta, Message, MessageDeltaEvent, MessageDeltaUsage, MessageParam, MessageStartEvent, MessageStopEvent, MessageStreamEvent, RawContentBlockDeltaEvent, RawContentBlockStartEvent, RawContentBlockStopEvent, RawMessageDeltaEvent, RawMessageStartEvent, RawMessageStopEvent, RawMessageStreamEvent, TextBlock, TextBlockParam, TextDelta, Tool, ToolResultBlockParam, ToolUseBlock, ToolUseBlockParam, Usage, MessageCreateParams, MessageCreateParamsNonStreaming, MessageCreateParamsStreaming, MessageStreamParams, Messages, } from "./messages.js"; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Messages = exports.Completions = exports.Beta = void 0; | ||
var beta_1 = require("./beta/beta.js"); | ||
Object.defineProperty(exports, "Beta", { enumerable: true, get: function () { return beta_1.Beta; } }); | ||
exports.Messages = exports.Completions = void 0; | ||
var completions_1 = require("./completions.js"); | ||
@@ -8,0 +6,0 @@ Object.defineProperty(exports, "Completions", { enumerable: true, get: function () { return completions_1.Completions; } }); |
@@ -19,4 +19,4 @@ import * as Core from '@anthropic-ai/sdk/core'; | ||
create(body: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>; | ||
create(body: MessageCreateParamsStreaming, options?: Core.RequestOptions): APIPromise<Stream<MessageStreamEvent>>; | ||
create(body: MessageCreateParamsBase, options?: Core.RequestOptions): APIPromise<Stream<MessageStreamEvent> | Message>; | ||
create(body: MessageCreateParamsStreaming, options?: Core.RequestOptions): APIPromise<Stream<RawMessageStreamEvent>>; | ||
create(body: MessageCreateParamsBase, options?: Core.RequestOptions): APIPromise<Stream<RawMessageStreamEvent> | Message>; | ||
/** | ||
@@ -27,17 +27,6 @@ * Create a Message stream | ||
} | ||
export type ContentBlock = TextBlock; | ||
export interface ContentBlockDeltaEvent { | ||
delta: TextDelta; | ||
index: number; | ||
type: 'content_block_delta'; | ||
} | ||
export interface ContentBlockStartEvent { | ||
content_block: TextBlock; | ||
index: number; | ||
type: 'content_block_start'; | ||
} | ||
export interface ContentBlockStopEvent { | ||
index: number; | ||
type: 'content_block_stop'; | ||
} | ||
export type ContentBlock = TextBlock | ToolUseBlock; | ||
export type ContentBlockDeltaEvent = RawContentBlockDeltaEvent; | ||
export type ContentBlockStartEvent = RawContentBlockStartEvent; | ||
export type ContentBlockStopEvent = RawContentBlockStopEvent; | ||
export interface ImageBlockParam { | ||
@@ -54,2 +43,6 @@ source: ImageBlockParam.Source; | ||
} | ||
export interface InputJsonDelta { | ||
partial_json: string; | ||
type: 'input_json_delta'; | ||
} | ||
export interface Message { | ||
@@ -66,3 +59,3 @@ /** | ||
* This is an array of content blocks, each of which has a `type` that determines | ||
* its shape. Currently, the only `type` in responses is `"text"`. | ||
* its shape. | ||
* | ||
@@ -97,3 +90,3 @@ * Example: | ||
*/ | ||
content: Array<TextBlock>; | ||
content: Array<ContentBlock>; | ||
/** | ||
@@ -117,2 +110,3 @@ * The model that handled the request. | ||
* - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated | ||
* - `"tool_use"`: the model invoked one or more tools | ||
* | ||
@@ -122,3 +116,3 @@ * In non-streaming mode this value is always non-null. In streaming mode, it is | ||
*/ | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null; | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null; | ||
/** | ||
@@ -153,4 +147,32 @@ * Which custom stop sequence was generated, if any. | ||
} | ||
export interface MessageDeltaEvent { | ||
delta: MessageDeltaEvent.Delta; | ||
export type MessageDeltaEvent = RawMessageDeltaEvent; | ||
export interface MessageDeltaUsage { | ||
/** | ||
* The cumulative number of output tokens which were used. | ||
*/ | ||
output_tokens: number; | ||
} | ||
export interface MessageParam { | ||
content: string | Array<TextBlockParam | ImageBlockParam | ToolUseBlockParam | ToolResultBlockParam>; | ||
role: 'user' | 'assistant'; | ||
} | ||
export type MessageStartEvent = RawMessageStartEvent; | ||
export type MessageStopEvent = RawMessageStopEvent; | ||
export type MessageStreamEvent = RawMessageStreamEvent; | ||
export interface RawContentBlockDeltaEvent { | ||
delta: TextDelta | InputJsonDelta; | ||
index: number; | ||
type: 'content_block_delta'; | ||
} | ||
export interface RawContentBlockStartEvent { | ||
content_block: TextBlock | ToolUseBlock; | ||
index: number; | ||
type: 'content_block_start'; | ||
} | ||
export interface RawContentBlockStopEvent { | ||
index: number; | ||
type: 'content_block_stop'; | ||
} | ||
export interface RawMessageDeltaEvent { | ||
delta: RawMessageDeltaEvent.Delta; | ||
type: 'message_delta'; | ||
@@ -173,26 +195,16 @@ /** | ||
} | ||
export declare namespace MessageDeltaEvent { | ||
export declare namespace RawMessageDeltaEvent { | ||
interface Delta { | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null; | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null; | ||
stop_sequence: string | null; | ||
} | ||
} | ||
export interface MessageDeltaUsage { | ||
/** | ||
* The cumulative number of output tokens which were used. | ||
*/ | ||
output_tokens: number; | ||
} | ||
export interface MessageParam { | ||
content: string | Array<TextBlockParam | ImageBlockParam>; | ||
role: 'user' | 'assistant'; | ||
} | ||
export interface MessageStartEvent { | ||
export interface RawMessageStartEvent { | ||
message: Message; | ||
type: 'message_start'; | ||
} | ||
export interface MessageStopEvent { | ||
export interface RawMessageStopEvent { | ||
type: 'message_stop'; | ||
} | ||
export type MessageStreamEvent = MessageStartEvent | MessageDeltaEvent | MessageStopEvent | ContentBlockStartEvent | ContentBlockDeltaEvent | ContentBlockStopEvent; | ||
export type RawMessageStreamEvent = RawMessageStartEvent | RawMessageDeltaEvent | RawMessageStopEvent | RawContentBlockStartEvent | RawContentBlockDeltaEvent | RawContentBlockStopEvent; | ||
export interface TextBlock { | ||
@@ -210,2 +222,52 @@ text: string; | ||
} | ||
export interface Tool { | ||
/** | ||
* [JSON schema](https://json-schema.org/) for this tool's input. | ||
* | ||
* This defines the shape of the `input` that your tool accepts and that the model | ||
* will produce. | ||
*/ | ||
input_schema: Tool.InputSchema; | ||
name: string; | ||
/** | ||
* Description of what this tool does. | ||
* | ||
* Tool descriptions should be as detailed as possible. The more information that | ||
* the model has about what the tool is and how to use it, the better it will | ||
* perform. You can use natural language descriptions to reinforce important | ||
* aspects of the tool input JSON schema. | ||
*/ | ||
description?: string; | ||
} | ||
export declare namespace Tool { | ||
/** | ||
* [JSON schema](https://json-schema.org/) for this tool's input. | ||
* | ||
* This defines the shape of the `input` that your tool accepts and that the model | ||
* will produce. | ||
*/ | ||
interface InputSchema { | ||
type: 'object'; | ||
properties?: unknown | null; | ||
[k: string]: unknown; | ||
} | ||
} | ||
export interface ToolResultBlockParam { | ||
tool_use_id: string; | ||
type: 'tool_result'; | ||
content?: Array<TextBlockParam | ImageBlockParam>; | ||
is_error?: boolean; | ||
} | ||
export interface ToolUseBlock { | ||
id: string; | ||
input: unknown; | ||
name: string; | ||
type: 'tool_use'; | ||
} | ||
export interface ToolUseBlockParam { | ||
id: string; | ||
input: unknown; | ||
name: string; | ||
type: 'tool_use'; | ||
} | ||
export interface Usage { | ||
@@ -371,103 +433,59 @@ /** | ||
/** | ||
* Only sample from the top K options for each subsequent token. | ||
* | ||
* Used to remove "long tail" low probability responses. | ||
* [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). | ||
* | ||
* Recommended for advanced use cases only. You usually only need to use | ||
* `temperature`. | ||
* How the model should use the provided tools. The model can use a specific tool, | ||
* any available tool, or decide by itself. | ||
*/ | ||
top_k?: number; | ||
tool_choice?: MessageCreateParams.ToolChoiceAuto | MessageCreateParams.ToolChoiceAny | MessageCreateParams.ToolChoiceTool; | ||
/** | ||
* Use nucleus sampling. | ||
* Definitions of tools that the model may use. | ||
* | ||
* In nucleus sampling, we compute the cumulative distribution over all the options | ||
* for each subsequent token in decreasing probability order and cut it off once it | ||
* reaches a particular probability specified by `top_p`. You should either alter | ||
* `temperature` or `top_p`, but not both. | ||
* 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. | ||
* | ||
* Recommended for advanced use cases only. You usually only need to use | ||
* `temperature`. | ||
*/ | ||
top_p?: number; | ||
} | ||
export declare namespace MessageCreateParams { | ||
/** | ||
* An object describing metadata about the request. | ||
*/ | ||
interface Metadata { | ||
/** | ||
* An external identifier for the user who is associated with the request. | ||
* | ||
* This should be a uuid, hash value, or other opaque identifier. Anthropic may use | ||
* this id to help detect abuse. Do not include any identifying information such as | ||
* name, email address, or phone number. | ||
*/ | ||
user_id?: string | null; | ||
} | ||
type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming; | ||
type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming; | ||
} | ||
export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* Each tool definition includes: | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream?: false; | ||
} | ||
export interface MessageCreateParamsStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* - `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. | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream: true; | ||
} | ||
export interface MessageStreamParams { | ||
/** | ||
* The maximum number of tokens to generate before stopping. | ||
* For example, if you defined `tools` as: | ||
* | ||
* Note that our models may stop _before_ reaching this maximum. This parameter | ||
* only specifies the absolute maximum number of tokens to generate. | ||
* | ||
* Different models have different maximum values for this parameter. See | ||
* [models](https://docs.anthropic.com/en/docs/models-overview) for details. | ||
*/ | ||
max_tokens: number; | ||
/** | ||
* 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. | ||
* | ||
* 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. The first message must always use the `user` role. | ||
* | ||
* 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" }] | ||
* [ | ||
* { | ||
* "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"] | ||
* } | ||
* } | ||
* ] | ||
* ``` | ||
* | ||
* Example with multiple conversational turns: | ||
* 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 | ||
* [ | ||
* { "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?" } | ||
* { | ||
* "type": "tool_use", | ||
* "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", | ||
* "name": "get_stock_price", | ||
* "input": { "ticker": "^GSPC" } | ||
* } | ||
* ] | ||
* ``` | ||
* | ||
* Example with a partially-filled response from Claude: | ||
* 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: | ||
* | ||
@@ -477,96 +495,17 @@ * ```json | ||
* { | ||
* "role": "user", | ||
* "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" | ||
* }, | ||
* { "role": "assistant", "content": "The best answer is (" } | ||
* "type": "tool_result", | ||
* "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", | ||
* "content": "259.75 USD" | ||
* } | ||
* ] | ||
* ``` | ||
* | ||
* 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: | ||
* 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. | ||
* | ||
* ```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) 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. | ||
* See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details. | ||
*/ | ||
messages: Array<MessageParam>; | ||
tools?: Array<Tool>; | ||
/** | ||
* The model that will complete your prompt. | ||
* | ||
* See [models](https://docs.anthropic.com/en/docs/models-overview) for additional | ||
* details and options. | ||
*/ | ||
model: (string & {}) | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307' | 'claude-2.1' | 'claude-2.0' | 'claude-instant-1.2'; | ||
/** | ||
* An object describing metadata about the request. | ||
*/ | ||
metadata?: MessageStreamParams.Metadata; | ||
/** | ||
* Custom text sequences that will cause the model to stop generating. | ||
* | ||
* Our models will normally stop when they have naturally completed their turn, | ||
* which will result in a response `stop_reason` of `"end_turn"`. | ||
* | ||
* If you want the model to stop generating when it encounters custom strings of | ||
* text, you can use the `stop_sequences` parameter. If the model encounters one of | ||
* the custom sequences, the response `stop_reason` value will be `"stop_sequence"` | ||
* and the response `stop_sequence` value will contain the matched stop sequence. | ||
*/ | ||
stop_sequences?: Array<string>; | ||
/** | ||
* 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; | ||
/** | ||
* Amount of randomness injected into the response. | ||
* | ||
* Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` | ||
* for analytical / multiple choice, and closer to `1.0` for creative and | ||
* generative tasks. | ||
* | ||
* Note that even with `temperature` of `0.0`, the results will not be fully | ||
* deterministic. | ||
*/ | ||
temperature?: number; | ||
/** | ||
* Only sample from the top K options for each subsequent token. | ||
@@ -594,3 +533,3 @@ * | ||
} | ||
export declare namespace MessageStreamParams { | ||
export declare namespace MessageCreateParams { | ||
/** | ||
@@ -609,3 +548,46 @@ * An object describing metadata about the request. | ||
} | ||
/** | ||
* The model will automatically decide whether to use tools. | ||
*/ | ||
interface ToolChoiceAuto { | ||
type: 'auto'; | ||
} | ||
/** | ||
* The model will use any available tools. | ||
*/ | ||
interface ToolChoiceAny { | ||
type: 'any'; | ||
} | ||
/** | ||
* The model will use the specified tool with `tool_choice.name`. | ||
*/ | ||
interface ToolChoiceTool { | ||
/** | ||
* The name of the tool to use. | ||
*/ | ||
name: string; | ||
type: 'tool'; | ||
} | ||
type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming; | ||
type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming; | ||
} | ||
export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream?: false; | ||
} | ||
export interface MessageCreateParamsStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream: true; | ||
} | ||
export type MessageStreamParams = MessageCreateParamsBase; | ||
export declare namespace Messages { | ||
@@ -617,2 +599,3 @@ export import ContentBlock = MessagesAPI.ContentBlock; | ||
export import ImageBlockParam = MessagesAPI.ImageBlockParam; | ||
export import InputJsonDelta = MessagesAPI.InputJsonDelta; | ||
export import Message = MessagesAPI.Message; | ||
@@ -625,5 +608,16 @@ export import MessageDeltaEvent = MessagesAPI.MessageDeltaEvent; | ||
export import MessageStreamEvent = MessagesAPI.MessageStreamEvent; | ||
export import RawContentBlockDeltaEvent = MessagesAPI.RawContentBlockDeltaEvent; | ||
export import RawContentBlockStartEvent = MessagesAPI.RawContentBlockStartEvent; | ||
export import RawContentBlockStopEvent = MessagesAPI.RawContentBlockStopEvent; | ||
export import RawMessageDeltaEvent = MessagesAPI.RawMessageDeltaEvent; | ||
export import RawMessageStartEvent = MessagesAPI.RawMessageStartEvent; | ||
export import RawMessageStopEvent = MessagesAPI.RawMessageStopEvent; | ||
export import RawMessageStreamEvent = MessagesAPI.RawMessageStreamEvent; | ||
export import TextBlock = MessagesAPI.TextBlock; | ||
export import TextBlockParam = MessagesAPI.TextBlockParam; | ||
export import TextDelta = MessagesAPI.TextDelta; | ||
export import Tool = MessagesAPI.Tool; | ||
export import ToolResultBlockParam = MessagesAPI.ToolResultBlockParam; | ||
export import ToolUseBlock = MessagesAPI.ToolUseBlock; | ||
export import ToolUseBlockParam = MessagesAPI.ToolUseBlockParam; | ||
export import Usage = MessagesAPI.Usage; | ||
@@ -630,0 +624,0 @@ export import MessageCreateParams = MessagesAPI.MessageCreateParams; |
@@ -125,3 +125,2 @@ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
messages: API.Messages = new API.Messages(this); | ||
beta: API.Beta = new API.Beta(this); | ||
@@ -246,2 +245,3 @@ protected override defaultQuery(): Core.DefaultQuery | undefined { | ||
export import ImageBlockParam = API.ImageBlockParam; | ||
export import InputJsonDelta = API.InputJsonDelta; | ||
export import Message = API.Message; | ||
@@ -254,5 +254,16 @@ export import MessageDeltaEvent = API.MessageDeltaEvent; | ||
export import MessageStreamEvent = API.MessageStreamEvent; | ||
export import RawContentBlockDeltaEvent = API.RawContentBlockDeltaEvent; | ||
export import RawContentBlockStartEvent = API.RawContentBlockStartEvent; | ||
export import RawContentBlockStopEvent = API.RawContentBlockStopEvent; | ||
export import RawMessageDeltaEvent = API.RawMessageDeltaEvent; | ||
export import RawMessageStartEvent = API.RawMessageStartEvent; | ||
export import RawMessageStopEvent = API.RawMessageStopEvent; | ||
export import RawMessageStreamEvent = API.RawMessageStreamEvent; | ||
export import TextBlock = API.TextBlock; | ||
export import TextBlockParam = API.TextBlockParam; | ||
export import TextDelta = API.TextDelta; | ||
export import Tool = API.Tool; | ||
export import ToolResultBlockParam = API.ToolResultBlockParam; | ||
export import ToolUseBlock = API.ToolUseBlock; | ||
export import ToolUseBlockParam = API.ToolUseBlockParam; | ||
export import Usage = API.Usage; | ||
@@ -263,6 +274,4 @@ export import MessageCreateParams = API.MessageCreateParams; | ||
export import MessageStreamParams = API.MessageStreamParams; | ||
export import Beta = API.Beta; | ||
} | ||
export default Anthropic; |
@@ -10,6 +10,8 @@ import * as Core from "../core.js"; | ||
MessageCreateParams, | ||
MessageStreamParams, | ||
MessageCreateParamsBase, | ||
} from "../resources/messages.js"; | ||
import { type ReadableStream } from "../_shims/index.js"; | ||
import { Stream } from "../streaming.js"; | ||
import { TextBlock } from "../resources.js"; | ||
import { partialParse } from "../_vendor/partial-json-parser/parser.js"; | ||
@@ -20,2 +22,3 @@ export interface MessageStreamEvents { | ||
text: (textDelta: string, textSnapshot: string) => void; | ||
inputJson: (partialJson: string, jsonSnapshot: unknown) => void; | ||
message: (message: Message) => void; | ||
@@ -34,2 +37,4 @@ contentBlock: (content: ContentBlock) => void; | ||
const JSON_BUF_PROPERTY = '__json_buf'; | ||
export class MessageStream implements AsyncIterable<MessageStreamEvent> { | ||
@@ -91,3 +96,3 @@ messages: MessageParam[] = []; | ||
messages: Messages, | ||
params: MessageStreamParams, | ||
params: MessageCreateParamsBase, | ||
options?: Core.RequestOptions, | ||
@@ -271,3 +276,3 @@ ): MessageStream { | ||
.at(-1)! | ||
.content.filter((block) => block.type === 'text') | ||
.content.filter((block): block is TextBlock => block.type === 'text') | ||
.map((block) => block.text); | ||
@@ -377,4 +382,9 @@ if (textBlocks.length === 0) { | ||
case 'content_block_delta': { | ||
if (event.delta.type === 'text_delta') { | ||
this._emit('text', event.delta.text, messageSnapshot.content.at(-1)!.text || ''); | ||
const content = messageSnapshot.content.at(-1)!; | ||
if (event.delta.type === 'text_delta' && content.type === 'text') { | ||
this._emit('text', event.delta.text, content.text || ''); | ||
} else if (event.delta.type === 'input_json_delta' && content.type === 'tool_use') { | ||
if (content.input) { | ||
this._emit('inputJson', event.delta.partial_json, content.input); | ||
} | ||
} | ||
@@ -468,2 +478,18 @@ break; | ||
snapshotContent.text += event.delta.text; | ||
} else if (snapshotContent?.type === 'tool_use' && event.delta.type === 'input_json_delta') { | ||
// we need to keep track of the raw JSON string as well so that we can | ||
// re-parse it for each delta, for now we just store it as an untyped | ||
// non-enumerable property on the snapshot | ||
let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || ''; | ||
jsonBuf += event.delta.partial_json; | ||
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, { | ||
value: jsonBuf, | ||
enumerable: false, | ||
writable: true, | ||
}); | ||
if (jsonBuf) { | ||
snapshotContent.input = partialParse(jsonBuf); | ||
} | ||
} | ||
@@ -470,0 +496,0 @@ return snapshot; |
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
export { Beta } from "./beta/beta.js"; | ||
export { | ||
@@ -17,2 +16,3 @@ Completion, | ||
ImageBlockParam, | ||
InputJsonDelta, | ||
Message, | ||
@@ -25,5 +25,16 @@ MessageDeltaEvent, | ||
MessageStreamEvent, | ||
RawContentBlockDeltaEvent, | ||
RawContentBlockStartEvent, | ||
RawContentBlockStopEvent, | ||
RawMessageDeltaEvent, | ||
RawMessageStartEvent, | ||
RawMessageStopEvent, | ||
RawMessageStreamEvent, | ||
TextBlock, | ||
TextBlockParam, | ||
TextDelta, | ||
Tool, | ||
ToolResultBlockParam, | ||
ToolUseBlock, | ||
ToolUseBlockParam, | ||
Usage, | ||
@@ -30,0 +41,0 @@ MessageCreateParams, |
@@ -25,11 +25,11 @@ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
options?: Core.RequestOptions, | ||
): APIPromise<Stream<MessageStreamEvent>>; | ||
): APIPromise<Stream<RawMessageStreamEvent>>; | ||
create( | ||
body: MessageCreateParamsBase, | ||
options?: Core.RequestOptions, | ||
): APIPromise<Stream<MessageStreamEvent> | Message>; | ||
): APIPromise<Stream<RawMessageStreamEvent> | Message>; | ||
create( | ||
body: MessageCreateParams, | ||
options?: Core.RequestOptions, | ||
): APIPromise<Message> | APIPromise<Stream<MessageStreamEvent>> { | ||
): APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>> { | ||
return this._client.post('/v1/messages', { | ||
@@ -40,3 +40,3 @@ body, | ||
stream: body.stream ?? false, | ||
}) as APIPromise<Message> | APIPromise<Stream<MessageStreamEvent>>; | ||
}) as APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>>; | ||
} | ||
@@ -52,26 +52,10 @@ | ||
export type ContentBlock = TextBlock; | ||
export type ContentBlock = TextBlock | ToolUseBlock; | ||
export interface ContentBlockDeltaEvent { | ||
delta: TextDelta; | ||
export type ContentBlockDeltaEvent = RawContentBlockDeltaEvent; | ||
index: number; | ||
export type ContentBlockStartEvent = RawContentBlockStartEvent; | ||
type: 'content_block_delta'; | ||
} | ||
export type ContentBlockStopEvent = RawContentBlockStopEvent; | ||
export interface ContentBlockStartEvent { | ||
content_block: TextBlock; | ||
index: number; | ||
type: 'content_block_start'; | ||
} | ||
export interface ContentBlockStopEvent { | ||
index: number; | ||
type: 'content_block_stop'; | ||
} | ||
export interface ImageBlockParam { | ||
@@ -93,2 +77,8 @@ source: ImageBlockParam.Source; | ||
export interface InputJsonDelta { | ||
partial_json: string; | ||
type: 'input_json_delta'; | ||
} | ||
export interface Message { | ||
@@ -106,3 +96,3 @@ /** | ||
* This is an array of content blocks, each of which has a `type` that determines | ||
* its shape. Currently, the only `type` in responses is `"text"`. | ||
* its shape. | ||
* | ||
@@ -137,3 +127,3 @@ * Example: | ||
*/ | ||
content: Array<TextBlock>; | ||
content: Array<ContentBlock>; | ||
@@ -160,2 +150,3 @@ /** | ||
* - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated | ||
* - `"tool_use"`: the model invoked one or more tools | ||
* | ||
@@ -165,3 +156,3 @@ * In non-streaming mode this value is always non-null. In streaming mode, it is | ||
*/ | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null; | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null; | ||
@@ -200,5 +191,48 @@ /** | ||
export interface MessageDeltaEvent { | ||
delta: MessageDeltaEvent.Delta; | ||
export type MessageDeltaEvent = RawMessageDeltaEvent; | ||
export interface MessageDeltaUsage { | ||
/** | ||
* The cumulative number of output tokens which were used. | ||
*/ | ||
output_tokens: number; | ||
} | ||
export interface MessageParam { | ||
content: string | Array<TextBlockParam | ImageBlockParam | ToolUseBlockParam | ToolResultBlockParam>; | ||
role: 'user' | 'assistant'; | ||
} | ||
export type MessageStartEvent = RawMessageStartEvent; | ||
export type MessageStopEvent = RawMessageStopEvent; | ||
export type MessageStreamEvent = RawMessageStreamEvent; | ||
export interface RawContentBlockDeltaEvent { | ||
delta: TextDelta | InputJsonDelta; | ||
index: number; | ||
type: 'content_block_delta'; | ||
} | ||
export interface RawContentBlockStartEvent { | ||
content_block: TextBlock | ToolUseBlock; | ||
index: number; | ||
type: 'content_block_start'; | ||
} | ||
export interface RawContentBlockStopEvent { | ||
index: number; | ||
type: 'content_block_stop'; | ||
} | ||
export interface RawMessageDeltaEvent { | ||
delta: RawMessageDeltaEvent.Delta; | ||
type: 'message_delta'; | ||
@@ -223,5 +257,5 @@ | ||
export namespace MessageDeltaEvent { | ||
export namespace RawMessageDeltaEvent { | ||
export interface Delta { | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null; | ||
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null; | ||
@@ -232,16 +266,3 @@ stop_sequence: string | null; | ||
export interface MessageDeltaUsage { | ||
/** | ||
* The cumulative number of output tokens which were used. | ||
*/ | ||
output_tokens: number; | ||
} | ||
export interface MessageParam { | ||
content: string | Array<TextBlockParam | ImageBlockParam>; | ||
role: 'user' | 'assistant'; | ||
} | ||
export interface MessageStartEvent { | ||
export interface RawMessageStartEvent { | ||
message: Message; | ||
@@ -252,13 +273,13 @@ | ||
export interface MessageStopEvent { | ||
export interface RawMessageStopEvent { | ||
type: 'message_stop'; | ||
} | ||
export type MessageStreamEvent = | ||
| MessageStartEvent | ||
| MessageDeltaEvent | ||
| MessageStopEvent | ||
| ContentBlockStartEvent | ||
| ContentBlockDeltaEvent | ||
| ContentBlockStopEvent; | ||
export type RawMessageStreamEvent = | ||
| RawMessageStartEvent | ||
| RawMessageDeltaEvent | ||
| RawMessageStopEvent | ||
| RawContentBlockStartEvent | ||
| RawContentBlockDeltaEvent | ||
| RawContentBlockStopEvent; | ||
@@ -283,2 +304,69 @@ export interface TextBlock { | ||
export interface Tool { | ||
/** | ||
* [JSON schema](https://json-schema.org/) for this tool's input. | ||
* | ||
* This defines the shape of the `input` that your tool accepts and that the model | ||
* will produce. | ||
*/ | ||
input_schema: Tool.InputSchema; | ||
name: string; | ||
/** | ||
* Description of what this tool does. | ||
* | ||
* Tool descriptions should be as detailed as possible. The more information that | ||
* the model has about what the tool is and how to use it, the better it will | ||
* perform. You can use natural language descriptions to reinforce important | ||
* aspects of the tool input JSON schema. | ||
*/ | ||
description?: string; | ||
} | ||
export namespace Tool { | ||
/** | ||
* [JSON schema](https://json-schema.org/) for this tool's input. | ||
* | ||
* This defines the shape of the `input` that your tool accepts and that the model | ||
* will produce. | ||
*/ | ||
export interface InputSchema { | ||
type: 'object'; | ||
properties?: unknown | null; | ||
[k: string]: unknown; | ||
} | ||
} | ||
export interface ToolResultBlockParam { | ||
tool_use_id: string; | ||
type: 'tool_result'; | ||
content?: Array<TextBlockParam | ImageBlockParam>; | ||
is_error?: boolean; | ||
} | ||
export interface ToolUseBlock { | ||
id: string; | ||
input: unknown; | ||
name: string; | ||
type: 'tool_use'; | ||
} | ||
export interface ToolUseBlockParam { | ||
id: string; | ||
input: unknown; | ||
name: string; | ||
type: 'tool_use'; | ||
} | ||
export interface Usage { | ||
@@ -462,110 +550,63 @@ /** | ||
/** | ||
* Only sample from the top K options for each subsequent token. | ||
* | ||
* Used to remove "long tail" low probability responses. | ||
* [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). | ||
* | ||
* Recommended for advanced use cases only. You usually only need to use | ||
* `temperature`. | ||
* How the model should use the provided tools. The model can use a specific tool, | ||
* any available tool, or decide by itself. | ||
*/ | ||
top_k?: number; | ||
tool_choice?: | ||
| MessageCreateParams.ToolChoiceAuto | ||
| MessageCreateParams.ToolChoiceAny | ||
| MessageCreateParams.ToolChoiceTool; | ||
/** | ||
* Use nucleus sampling. | ||
* Definitions of tools that the model may use. | ||
* | ||
* In nucleus sampling, we compute the cumulative distribution over all the options | ||
* for each subsequent token in decreasing probability order and cut it off once it | ||
* reaches a particular probability specified by `top_p`. You should either alter | ||
* `temperature` or `top_p`, but not both. | ||
* 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. | ||
* | ||
* Recommended for advanced use cases only. You usually only need to use | ||
* `temperature`. | ||
*/ | ||
top_p?: number; | ||
} | ||
export namespace MessageCreateParams { | ||
/** | ||
* An object describing metadata about the request. | ||
*/ | ||
export interface Metadata { | ||
/** | ||
* An external identifier for the user who is associated with the request. | ||
* | ||
* This should be a uuid, hash value, or other opaque identifier. Anthropic may use | ||
* this id to help detect abuse. Do not include any identifying information such as | ||
* name, email address, or phone number. | ||
*/ | ||
user_id?: string | null; | ||
} | ||
export type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming; | ||
export type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming; | ||
} | ||
export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* Each tool definition includes: | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream?: false; | ||
} | ||
export interface MessageCreateParamsStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* - `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. | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream: true; | ||
} | ||
export interface MessageStreamParams { | ||
/** | ||
* The maximum number of tokens to generate before stopping. | ||
* For example, if you defined `tools` as: | ||
* | ||
* Note that our models may stop _before_ reaching this maximum. This parameter | ||
* only specifies the absolute maximum number of tokens to generate. | ||
* | ||
* Different models have different maximum values for this parameter. See | ||
* [models](https://docs.anthropic.com/en/docs/models-overview) for details. | ||
*/ | ||
max_tokens: number; | ||
/** | ||
* 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. | ||
* | ||
* 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. The first message must always use the `user` role. | ||
* | ||
* 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" }] | ||
* [ | ||
* { | ||
* "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"] | ||
* } | ||
* } | ||
* ] | ||
* ``` | ||
* | ||
* Example with multiple conversational turns: | ||
* 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 | ||
* [ | ||
* { "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?" } | ||
* { | ||
* "type": "tool_use", | ||
* "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", | ||
* "name": "get_stock_price", | ||
* "input": { "ticker": "^GSPC" } | ||
* } | ||
* ] | ||
* ``` | ||
* | ||
* Example with a partially-filled response from Claude: | ||
* 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: | ||
* | ||
@@ -575,109 +616,18 @@ * ```json | ||
* { | ||
* "role": "user", | ||
* "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" | ||
* }, | ||
* { "role": "assistant", "content": "The best answer is (" } | ||
* "type": "tool_result", | ||
* "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", | ||
* "content": "259.75 USD" | ||
* } | ||
* ] | ||
* ``` | ||
* | ||
* 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: | ||
* 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. | ||
* | ||
* ```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) 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. | ||
* See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details. | ||
*/ | ||
messages: Array<MessageParam>; | ||
tools?: Array<Tool>; | ||
/** | ||
* The model that will complete your prompt. | ||
* | ||
* See [models](https://docs.anthropic.com/en/docs/models-overview) for additional | ||
* details and options. | ||
*/ | ||
model: | ||
| (string & {}) | ||
| 'claude-3-opus-20240229' | ||
| 'claude-3-sonnet-20240229' | ||
| 'claude-3-haiku-20240307' | ||
| 'claude-2.1' | ||
| 'claude-2.0' | ||
| 'claude-instant-1.2'; | ||
/** | ||
* An object describing metadata about the request. | ||
*/ | ||
metadata?: MessageStreamParams.Metadata; | ||
/** | ||
* Custom text sequences that will cause the model to stop generating. | ||
* | ||
* Our models will normally stop when they have naturally completed their turn, | ||
* which will result in a response `stop_reason` of `"end_turn"`. | ||
* | ||
* If you want the model to stop generating when it encounters custom strings of | ||
* text, you can use the `stop_sequences` parameter. If the model encounters one of | ||
* the custom sequences, the response `stop_reason` value will be `"stop_sequence"` | ||
* and the response `stop_sequence` value will contain the matched stop sequence. | ||
*/ | ||
stop_sequences?: Array<string>; | ||
/** | ||
* 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; | ||
/** | ||
* Amount of randomness injected into the response. | ||
* | ||
* Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` | ||
* for analytical / multiple choice, and closer to `1.0` for creative and | ||
* generative tasks. | ||
* | ||
* Note that even with `temperature` of `0.0`, the results will not be fully | ||
* deterministic. | ||
*/ | ||
temperature?: number; | ||
/** | ||
* Only sample from the top K options for each subsequent token. | ||
@@ -707,3 +657,3 @@ * | ||
export namespace MessageStreamParams { | ||
export namespace MessageCreateParams { | ||
/** | ||
@@ -722,4 +672,55 @@ * An object describing metadata about the request. | ||
} | ||
/** | ||
* The model will automatically decide whether to use tools. | ||
*/ | ||
export interface ToolChoiceAuto { | ||
type: 'auto'; | ||
} | ||
/** | ||
* The model will use any available tools. | ||
*/ | ||
export interface ToolChoiceAny { | ||
type: 'any'; | ||
} | ||
/** | ||
* The model will use the specified tool with `tool_choice.name`. | ||
*/ | ||
export interface ToolChoiceTool { | ||
/** | ||
* The name of the tool to use. | ||
*/ | ||
name: string; | ||
type: 'tool'; | ||
} | ||
export type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming; | ||
export type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming; | ||
} | ||
export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream?: false; | ||
} | ||
export interface MessageCreateParamsStreaming extends MessageCreateParamsBase { | ||
/** | ||
* Whether to incrementally stream the response using server-sent events. | ||
* | ||
* See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for | ||
* details. | ||
*/ | ||
stream: true; | ||
} | ||
export type MessageStreamParams = MessageCreateParamsBase; | ||
export namespace Messages { | ||
@@ -731,2 +732,3 @@ export import ContentBlock = MessagesAPI.ContentBlock; | ||
export import ImageBlockParam = MessagesAPI.ImageBlockParam; | ||
export import InputJsonDelta = MessagesAPI.InputJsonDelta; | ||
export import Message = MessagesAPI.Message; | ||
@@ -739,5 +741,16 @@ export import MessageDeltaEvent = MessagesAPI.MessageDeltaEvent; | ||
export import MessageStreamEvent = MessagesAPI.MessageStreamEvent; | ||
export import RawContentBlockDeltaEvent = MessagesAPI.RawContentBlockDeltaEvent; | ||
export import RawContentBlockStartEvent = MessagesAPI.RawContentBlockStartEvent; | ||
export import RawContentBlockStopEvent = MessagesAPI.RawContentBlockStopEvent; | ||
export import RawMessageDeltaEvent = MessagesAPI.RawMessageDeltaEvent; | ||
export import RawMessageStartEvent = MessagesAPI.RawMessageStartEvent; | ||
export import RawMessageStopEvent = MessagesAPI.RawMessageStopEvent; | ||
export import RawMessageStreamEvent = MessagesAPI.RawMessageStreamEvent; | ||
export import TextBlock = MessagesAPI.TextBlock; | ||
export import TextBlockParam = MessagesAPI.TextBlockParam; | ||
export import TextDelta = MessagesAPI.TextDelta; | ||
export import Tool = MessagesAPI.Tool; | ||
export import ToolResultBlockParam = MessagesAPI.ToolResultBlockParam; | ||
export import ToolUseBlock = MessagesAPI.ToolUseBlock; | ||
export import ToolUseBlockParam = MessagesAPI.ToolUseBlockParam; | ||
export import Usage = MessagesAPI.Usage; | ||
@@ -744,0 +757,0 @@ export import MessageCreateParams = MessagesAPI.MessageCreateParams; |
@@ -1,1 +0,1 @@ | ||
export const VERSION = '0.21.1'; // x-release-please-version | ||
export const VERSION = '0.22.0'; // x-release-please-version |
@@ -1,2 +0,2 @@ | ||
export declare const VERSION = "0.21.1"; | ||
export declare const VERSION = "0.22.0"; | ||
//# sourceMappingURL=version.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.VERSION = void 0; | ||
exports.VERSION = '0.21.1'; // x-release-please-version | ||
exports.VERSION = '0.22.0'; // x-release-please-version | ||
//# sourceMappingURL=version.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
744516
208
11657