Skip to content

Commit 459956a

Browse files
committed
chore: update examples
1 parent 372361c commit 459956a

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

examples/cancellation.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const client = new Anthropic();
1515
async function main() {
1616
const question = 'Hey Claude! How can I recursively list all files in a directory in Rust?';
1717

18-
const stream = await client.completions.create({
19-
prompt: `${Anthropic.HUMAN_PROMPT}${question}${Anthropic.AI_PROMPT}:`,
18+
const stream = await client.messages.create({
2019
model: 'claude-3-opus-20240229',
2120
stream: true,
22-
max_tokens_to_sample: 500,
21+
max_tokens: 500,
22+
messages: [{ role: 'user', content: question }],
2323
});
2424

2525
// If you need to, you can cancel a stream from outside the iterator
@@ -29,19 +29,18 @@ async function main() {
2929
stream.controller.abort();
3030
}, 1500);
3131

32-
for await (const completion of stream) {
33-
process.stdout.write(completion.completion);
32+
for await (const event of stream) {
33+
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
34+
process.stdout.write(event.delta.text);
3435

35-
// Most typically, you can cancel the stream by using "break"
36-
if (completion.completion.includes('unwrap')) {
37-
console.log('\nCancelling after seeing "unwrap".');
38-
clearTimeout(timeout);
39-
break;
36+
// Most typically, you can cancel the stream by using "break"
37+
if (event.delta.text.includes('unwrap')) {
38+
console.log('\nCancelling after seeing "unwrap".');
39+
clearTimeout(timeout);
40+
break;
41+
}
4042
}
4143
}
4244
}
4345

44-
main().catch((err) => {
45-
console.error(err);
46-
process.exit(1);
47-
});
46+
main();

examples/demo.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import Anthropic from '@anthropic-ai/sdk';
55
const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY
66

77
async function main() {
8-
const result = await client.completions.create({
9-
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
8+
const result = await client.messages.create({
9+
messages: [
10+
{
11+
role: 'user',
12+
content: 'Hey Claude!?',
13+
},
14+
],
1015
model: 'claude-3-opus-20240229',
11-
max_tokens_to_sample: 300,
16+
max_tokens: 1024,
1217
});
13-
console.log(result.completion);
18+
console.dir(result);
1419
}
1520

16-
main().catch((err) => {
17-
console.error(err);
18-
process.exit(1);
19-
});
21+
main();

examples/raw-streaming.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ import Anthropic from '@anthropic-ai/sdk';
55
const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY
66

77
async function main() {
8-
const question = 'Hey Claude! How can I recursively list all files in a directory in Rust?';
9-
10-
const stream = await client.completions.create({
11-
prompt: `${Anthropic.HUMAN_PROMPT}${question}${Anthropic.AI_PROMPT}:`,
8+
const stream = await client.messages.create({
129
model: 'claude-3-opus-20240229',
1310
stream: true,
14-
max_tokens_to_sample: 500,
11+
max_tokens: 500,
12+
messages: [
13+
{
14+
role: 'user',
15+
content: 'Hey Claude!',
16+
},
17+
],
1518
});
1619

17-
for await (const completion of stream) {
18-
process.stdout.write(completion.completion);
20+
for await (const event of stream) {
21+
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
22+
process.stdout.write(event.delta.text);
23+
}
1924
}
25+
process.stdout.write('\n');
2026
}
2127

22-
main().catch((err) => {
23-
console.error(err);
24-
process.exit(1);
25-
});
28+
main();

0 commit comments

Comments
 (0)