diff --git a/examples/cancellation.ts b/examples/cancellation.ts index 54607cc..23fb7ec 100755 --- a/examples/cancellation.ts +++ b/examples/cancellation.ts @@ -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 @@ -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(); diff --git a/examples/demo.ts b/examples/demo.ts index 5907fa2..609e63e 100755 --- a/examples/demo.ts +++ b/examples/demo.ts @@ -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(); diff --git a/examples/raw-streaming.ts b/examples/raw-streaming.ts index 1205368..559a6ca 100755 --- a/examples/raw-streaming.ts +++ b/examples/raw-streaming.ts @@ -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();