-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add messages endpoint with streaming helpers (#235)
More information here: https://docs.anthropic.com/claude/reference/messages_post
- Loading branch information
1 parent
5506174
commit 12b914f
Showing
16 changed files
with
1,801 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
configured_endpoints: 1 | ||
configured_endpoints: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env -S npm run tsn -T | ||
|
||
import Anthropic from '@anthropic-ai/sdk'; | ||
|
||
const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY | ||
|
||
async function main() { | ||
const question = 'Hey Claude! How can I recursively list all files in a directory in Rust?'; | ||
|
||
const stream = await client.completions.create({ | ||
prompt: `${Anthropic.HUMAN_PROMPT}${question}${Anthropic.AI_PROMPT}:`, | ||
model: 'claude-2.1', | ||
stream: true, | ||
max_tokens_to_sample: 500, | ||
}); | ||
|
||
for await (const completion of stream) { | ||
process.stdout.write(completion.completion); | ||
} | ||
} | ||
|
||
main().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Chat Completion Helpers | ||
|
||
## Streaming Responses | ||
|
||
```ts | ||
anthropic.beta.messages.stream({ … }, options?): MessageStream | ||
``` | ||
`anthropic.beta.messages.stream()` returns a `MessageStream`, which emits events, has an async | ||
iterator, and exposes helper methods to accumulate stream events into a convenient shape and make it easy to reason | ||
about the conversation. | ||
Alternatively, you can use `anthropic.beta.messages.create({ stream: true, … })` which returns an async | ||
iterable of the chunks in the stream and uses less memory (most notably, it does not accumulate a message | ||
object for you). | ||
If you need to cancel a stream, you can `break` from a `for await` loop or call `stream.abort()`. | ||
See an example of streaming helpers in action in [`examples/streaming.ts`](examples/streaming.ts). | ||
## MessageStream API | ||
### Events | ||
#### `.on('connect', () => …)` | ||
The first event that is fired when the connection with the Anthropic API is established. | ||
#### `.on('streamEvent', (event: MessageStreamEvent, snapshot: Message) => …)` | ||
The event fired when a stream event is received from the API. Not fired when it is not streaming. The snapshot | ||
returns an accumulated `Message` which is progressively built-up over events. | ||
#### `.on('text', (textDelta: string, textSnapshot: string) => …)` | ||
The event fired when a text delta is sent by the API. The second parameter returns a `textSnapshot`. | ||
#### `.on('message', (message: Message) => …)` | ||
The event fired when a message is done being streamed by the API. Corresponds to the `message_stop` SSE event. | ||
#### `.on('contentBlock', (content: ContentBlock) => …)` | ||
The event fired when a content block is done being streamed by the API. Corresponds to the | ||
`content_block_stop` SSE event. | ||
#### `.on('finalMessage', (message: Message) => …)` | ||
The event fired for the final message. Currently this is equivalent to the `message` event, but is fired after | ||
it. | ||
#### `.on('error', (error: AnthropicError) => …)` | ||
The event fired when an error is encountered while streaming. | ||
#### `.on('abort', (error: APIUserAbortError) => …)` | ||
The event fired when the stream receives a signal to abort. | ||
#### `.on('end', () => …)` | ||
The last event fired in the stream. | ||
### Methods | ||
#### `.abort()` | ||
Aborts the runner and the streaming request, equivalent to `.controller.abort()`. Calling `.abort()` on a | ||
`MessageStream` will also abort any in-flight network requests. | ||
#### `await .done()` | ||
An empty promise which resolves when the stream is done. | ||
#### `.currentMessage` | ||
Returns the current state of the message that is being accumulated, or `undefined` if there is no such | ||
message. | ||
#### `await .finalMessage()` | ||
A promise which resolves with the last message received from the API. Throws if no such message exists. | ||
#### `await .finalText()` | ||
A promise which resolves with the text of the last message received from the API. | ||
### Fields | ||
#### `.messages` | ||
A mutable array of all messages in the conversation. | ||
#### `.controller` | ||
The underlying `AbortController` for the runner. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.