Stream Text with File Prompt
Learn how to stream text with file prompt using the AI TOOLKIT and Node
Working with files in AI applications often requires analyzing documents, processing structured data, or extracting information from various file formats. File prompts allow you to send file content directly to the model, enabling tasks like document analysis, data extraction, or generating responses based on file contents. import { streamText } from 'ai-toolkit'; __PROVIDER_IMPORT__; import 'dotenv/config'; import fs from 'node:fs'; async function main() { const result = streamText({ model: __MODEL__, messages: [ { role: 'user', content: [ { type: 'text', text: 'What is an embedding model according to this document?', }, { type: 'file', data: fs.readFileSync('./data/ai.pdf'), mediaType: 'application/pdf', }, ], }, ], }); for await (const textPart of result.textStream) { process.stdout.write(textPart); } } main().catch(console.error);
- 1import { streamText } from 'ai-toolkit';
- 2__PROVIDER_IMPORT__;
- 3import 'dotenv/config';
- 4import fs from 'node:fs';
- 5async function main() {
- 6const result = streamText({
- 7model: __MODEL__,
- 8messages: [
- 9{
- 10role: 'user',
- 11content: [
- 12{
- 13type: 'text',
- 14text: 'What is an embedding model according to this document?',
- 15},
- 16{
- 17type: 'file',
- 18data: fs.readFileSync('./data/ai.pdf'),
- 19mediaType: 'application/pdf',
- 20},
- 21],
- 22},
- 23],
- 24});
- 25for await (const textPart of result.textStream) {
- 26process.stdout.write(textPart);
- 27}
- 28}
- 29main().catch(console.error);
Run it locally
$ npm install ai