From f278616714df61b931280be16282778b67f8b3f4 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Wed, 21 Aug 2024 10:38:40 +0200 Subject: [PATCH] feat (docs): add anthropic 8192 token example (#2751) --- .../01-ai-sdk-providers/05-anthropic.mdx | 24 +++++++++++++++ .../src/stream-text/anthropic-max-tokens.ts | 29 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 examples/ai-core/src/stream-text/anthropic-max-tokens.ts diff --git a/content/providers/01-ai-sdk-providers/05-anthropic.mdx b/content/providers/01-ai-sdk-providers/05-anthropic.mdx index 6844683656eb..50564853a441 100644 --- a/content/providers/01-ai-sdk-providers/05-anthropic.mdx +++ b/content/providers/01-ai-sdk-providers/05-anthropic.mdx @@ -145,6 +145,30 @@ console.log(result.experimental_providerMetadata?.anthropic); // e.g. { cacheCreationInputTokens: 2118, cacheReadInputTokens: 0 } ``` +### Example: 8192 Max Tokens + +The Anthropic beta `max-tokens-3-5-sonnet-2024-07-15` allows you to generate up to 8192 tokens. +You can use it as follows: + +```ts highlight="6-8" +import { anthropic } from '@ai-sdk/anthropic'; +import { streamText } from 'ai'; + +const result = await generateText({ + model: anthropic('claude-3-5-sonnet-20240620'), + // enable max tokens beta: + headers: { 'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15' }, + maxTokens: 8192, // important: specify max tokens + system: + "Don't shy away from generating long text. Follow the user's instructions'", + prompt: + 'Invent a new holiday and describe its traditions. Write at least 20 pages.', +}); + +console.log(result.text); +console.log('Token usage:', result.usage); +``` + ### Model Capabilities | Model | Image Input | Object Generation | Tool Usage | Tool Streaming | diff --git a/examples/ai-core/src/stream-text/anthropic-max-tokens.ts b/examples/ai-core/src/stream-text/anthropic-max-tokens.ts new file mode 100644 index 000000000000..e3f809609854 --- /dev/null +++ b/examples/ai-core/src/stream-text/anthropic-max-tokens.ts @@ -0,0 +1,29 @@ +import { anthropic } from '@ai-sdk/anthropic'; +import { streamText } from 'ai'; +import dotenv from 'dotenv'; + +dotenv.config(); + +async function main() { + const result = await streamText({ + model: anthropic('claude-3-5-sonnet-20240620'), + + headers: { 'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15' }, + maxTokens: 8192, // important: specify max tokens + + system: + "Don't shy away from generating long text. Follow the user's instructions'", + prompt: + 'Invent a new holiday and describe its traditions. Write at least 20 pages.', + }); + + for await (const textPart of result.textStream) { + process.stdout.write(textPart); + } + + console.log(); + console.log('Token usage:', await result.usage); + console.log('Finish reason:', await result.finishReason); +} + +main().catch(console.error);