Initial commit
This commit is contained in:
39
templates/stream-text-chat.ts
Normal file
39
templates/stream-text-chat.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
// Streaming chat with messages
|
||||
// AI SDK Core - streamText() with chat messages
|
||||
|
||||
import { streamText } from 'ai';
|
||||
import { anthropic } from '@ai-sdk/anthropic';
|
||||
|
||||
async function main() {
|
||||
const stream = streamText({
|
||||
model: anthropic('claude-sonnet-4-5'),
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: 'You are a helpful assistant that writes concise responses.',
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: 'Tell me a short story about AI and humanity working together.',
|
||||
},
|
||||
],
|
||||
maxOutputTokens: 500,
|
||||
});
|
||||
|
||||
console.log('Streaming response:');
|
||||
console.log('---');
|
||||
|
||||
// Stream text chunks to console
|
||||
for await (const chunk of stream.textStream) {
|
||||
process.stdout.write(chunk);
|
||||
}
|
||||
|
||||
console.log('\n---');
|
||||
|
||||
// Get final result with metadata
|
||||
const result = await stream.result;
|
||||
console.log('\nTokens used:', result.usage.totalTokens);
|
||||
console.log('Finish reason:', result.finishReason);
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user