Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie committed Mar 4, 2024
1 parent 372361c commit 459956a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
27 changes: 13 additions & 14 deletions examples/cancellation.ts
Expand Up @@ -15,11 +15,11 @@ const client = new Anthropic();
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}:`,
const stream = await client.messages.create({
model: 'claude-3-opus-20240229',
stream: true,
max_tokens_to_sample: 500,
max_tokens: 500,
messages: [{ role: 'user', content: question }],
});

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

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

// Most typically, you can cancel the stream by using "break"
if (completion.completion.includes('unwrap')) {
console.log('\nCancelling after seeing "unwrap".');
clearTimeout(timeout);
break;
// Most typically, you can cancel the stream by using "break"
if (event.delta.text.includes('unwrap')) {
console.log('\nCancelling after seeing "unwrap".');
clearTimeout(timeout);
break;
}
}
}
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
main();
18 changes: 10 additions & 8 deletions examples/demo.ts
Expand Up @@ -5,15 +5,17 @@ import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY

async function main() {
const result = await client.completions.create({
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
const result = await client.messages.create({
messages: [
{
role: 'user',
content: 'Hey Claude!?',
},
],
model: 'claude-3-opus-20240229',
max_tokens_to_sample: 300,
max_tokens: 1024,
});
console.log(result.completion);
console.dir(result);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
main();
25 changes: 14 additions & 11 deletions examples/raw-streaming.ts
Expand Up @@ -5,21 +5,24 @@ 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}:`,
const stream = await client.messages.create({
model: 'claude-3-opus-20240229',
stream: true,
max_tokens_to_sample: 500,
max_tokens: 500,
messages: [
{
role: 'user',
content: 'Hey Claude!',
},
],
});

for await (const completion of stream) {
process.stdout.write(completion.completion);
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
process.stdout.write('\n');
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
main();

0 comments on commit 459956a

Please sign in to comment.