Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:23:50 +08:00
commit 5135e7aaf4
24 changed files with 4973 additions and 0 deletions

View 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);