Show me an example with express
- User
Show me an example with express
Assistant
Here's an example of using
streamTextfrom theaipackage with Express.js:import express, { Request, Response } from 'express'; import { streamText } from 'ai'; import { openai } from '@ai-sdk/openai'; const app = express(); app.use(express.json()); app.post('/chat', async (req: Request, res: Response) => { try { const { messages } = req.body; const result = await streamText({ model: openai('gpt-4-turbo'), messages, }); return result.toDataStreamResponse().then((dataStreamResponse) => { res.writeHead(dataStreamResponse.status, dataStreamResponse.headers); dataStreamResponse.body.pipe(res); }); } catch (error) { console.error('Error processing request:', error); res.status(500).send('Error processing request'); } }); const port = 3000; app.listen(port, () => { console.log(`Server listening on port ${port}`); });
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.