Show me an example with express
- User
Show me an example with express
Assistant
Here's an example of using the
ai-sdkpackage with Express to create a streaming text response usingstreamText.toDataStreamResponse()or a directResponseobject:Example 1: Using
streamText.toDataStreamResponse()with Expressimport express from 'express'; import { streamText } from 'ai-sdk'; const app = express(); const port = 3000; app.get('/stream-text', async (req, res) => { try { // Generate streaming text const { readable, toDataStreamResponse } = await streamText({ model: 'your-model-name', // Replace with your model prompt: 'Tell me a story about a brave knight.', }); // Convert to a data stream response const response = toDataStreamResponse(readable); // Set headers and pipe the response res.setHeader('Content-Type', 'text/plain; charset=utf-8'); response.pipe(res); } catch (error) { console.error('Error generating text:', error); res.status(500).send('Error generating text'); } }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });Example 2: Directly Using
Responsewith Expressimport express from 'express'; import { streamText } from 'ai-sdk'; const app = express(); const port = 3000; app.get('/stream-text', async (req, res) => { try { // Generate streaming text const { readable } = await streamText({ model: 'your-model-name', // Replace with your model prompt: 'Tell me a story about a brave knight.', }); // Create a Response object const response = new Response(readable, { headers: { 'Content-Type': 'text/plain; charset=utf-8', }, }); // Pipe the response to the Express response response.body.pipe(res); } catch (error) { console.error('Error generating text:', error); res.status(500).send('Error generating text'); } }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });Example 3: Using
streamTextwith Express and Node.js StreamsIf you want to handle the streaming more directly with Node.js streams:
import express from 'express'; import { streamText } from 'ai-sdk'; const app = express(); const port = 3000; app.get('/stream-text', async (req, res) => { try { // Generate streaming text const { readable } = await streamText({ model: 'your-model-name', // Replace with your model prompt: 'Tell me a story about a brave knight.', }); // Set headers res.setHeader('Content-Type', 'text/plain; charset=utf-8'); // Pipe the readable stream to the response readable.pipe(res); } catch (error) { console.error('Error generating text:', error); res.status(500).send('Error generating text'); } }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });Notes:
-
Model Configuration: Replace
'your-model-name'with the actual model you want to use from the AI service you're connecting to. -
Error Handling: The examples include basic error handling. You might want to add more detailed error handling based on your application's needs.
-
Express Version: These examples assume you're using Express 4.x or later. If you're using an older version, you might need to adjust the syntax slightly.
-
Streaming: The examples demonstrate how to handle streaming responses. The client (e.g., a web browser or another service) will receive the text as it's generated, rather than waiting for the entire response.
-
Dependencies: Make sure you have the required dependencies installed:
npm install express ai-sdk # or yarn add express ai-sdk
These examples should help you get started with streaming text responses using the
ai-sdkpackage in an Express application.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.