Skip to content

Commit

Permalink
docs(readme): add streaming helper documentation (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Dec 21, 2023
1 parent af8d2c5 commit 4e5ed20
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ We provide a [separate package](https://github.com/anthropics/anthropic-tokenize

See the [repository documentation](https://github.com/anthropics/anthropic-tokenizer-typescript) for more details.

### Streaming Helpers

This library provides several conveniences for streaming messages, for example:

```ts
import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic();

async function main() {
const stream = anthropic.beta.messages
.stream({
model: 'claude-2.1',
max_tokens: 1024,
messages: [
{
role: 'user',
content: 'Say hello there!',
},
],
})
.on('text', (text) => {
console.log(text);
});

const message = await stream.finalMessage();
console.log(message);
}

main();
```

Streaming with `client.beta.messages.stream(...)` exposes [various helpers for your convenience](helpers.md) including event handlers and accumulation.

Alternatively, you can use `client.beta.messages.create({ ..., stream: true })` which only returns an async iterable of the events in the stream and thus uses less memory (it does not build up a final message object for you).

## Handling errors

When the library is unable to connect to the API,
Expand Down
2 changes: 1 addition & 1 deletion helpers.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Chat Completion Helpers
# Message Helpers

## Streaming Responses

Expand Down

0 comments on commit 4e5ed20

Please sign in to comment.