Express
Learn how to use the AI TOOLKIT in an Express server
You can use the AI TOOLKIT in an Express server to generate and stream text and objects to the client.
Run it locally
$ npm install aiExamples
The examples start a simple HTTP server that listens on port 8080. You can e.g. test it using curl:
curl -X POST http://localhost:8080
<Note>
The examples use the Vercel AI Gateway. Ensure that your AI Gateway API key is
set in the AI_GATEWAY_API_KEY environment variable.
</Note>
Full example: github.com/khulnasoft/ai-toolkit/examples/express
### UI Message Stream
You can use the pipeUIMessageStreamToResponse method to pipe the stream data to the server response.
import { streamText } from 'ai-toolkit';
import express, { Request, Response } from 'express';
const app = express();
app.post('/', async (req: Request, res: Response) => {
const result = streamText({
model: 'openai/gpt-4o',
prompt: 'Invent a new holiday and describe its traditions.',
});
result.pipeUIMessageStreamToResponse(res);
});
app.listen(8080, () => {
console.log(Example app listening on port ${8080});
});
### Sending Custom Data
pipeUIMessageStreamToResponse can be used to send custom data to the client.
import {
createUIMessageStream,
pipeUIMessageStreamToResponse,
streamText,
} from 'ai-toolkit';
import express, { Request, Response } from 'express';
const app = express();
app.post('/custom-data-parts', async (req: Request, res: Response) => {
pipeUIMessageStreamToResponse({
response: res,
stream: createUIMessageStream({
execute: async ({ writer }) => {
writer.write({ type: 'start' });
writer.write({
type: 'data-custom',
data: {
custom: 'Hello, world!',
},
});
const result = streamText({
model: 'openai/gpt-4o',
prompt: 'Invent a new holiday and describe its traditions.',
});
writer.merge(result.toUIMessageStream({ sendStart: false }));
},
}),
});
});
app.listen(8080, () => {
console.log(Example app listening on port ${8080});
});
### Text Stream
You can send a text stream to the client using pipeTextStreamToResponse.
import { streamText } from 'ai-toolkit';
import express, { Request, Response } from 'express';
const app = express();
app.post('/', async (req: Request, res: Response) => {
const result = streamText({
model: 'openai/gpt-4o',
prompt: 'Invent a new holiday and describe its traditions.',
});
result.pipeTextStreamToResponse(res);
});
app.listen(8080, () => {
console.log(Example app listening on port ${8080});
});
- 1curl -X POST http://localhost:8080
- 1import { streamText } from 'ai-toolkit';
- 2import express, { Request, Response } from 'express';
- 3const app = express();
- 4app.post('/', async (req: Request, res: Response) => {
- 5 const result = streamText({
- 6 model: 'openai/gpt-4o',
- 7 prompt: 'Invent a new holiday and describe its traditions.',
- 8 });
- 9 result.pipeUIMessageStreamToResponse(res);
- 10});
- 11app.listen(8080, () => {
- 12 console.log(`Example app listening on port ${8080}`);
- 13});
- 1import {
- 2 createUIMessageStream,
- 3 pipeUIMessageStreamToResponse,
- 4 streamText,
- 5} from 'ai-toolkit';
- 6import express, { Request, Response } from 'express';
- 7const app = express();
- 8app.post('/custom-data-parts', async (req: Request, res: Response) => {
- 9 pipeUIMessageStreamToResponse({
- 10 response: res,
- 11 stream: createUIMessageStream({
- 12 execute: async ({ writer }) => {
- 13 writer.write({ type: 'start' });
- 14 writer.write({
- 15 type: 'data-custom',
- 16 data: {
- 17 custom: 'Hello, world!',
- 18 },
- 19 });
- 20 const result = streamText({
- 21 model: 'openai/gpt-4o',
- 22 prompt: 'Invent a new holiday and describe its traditions.',
- 23 });
- 24 writer.merge(result.toUIMessageStream({ sendStart: false }));
- 25 },
- 26 }),
- 27 });
- 28});
- 29app.listen(8080, () => {
- 30 console.log(`Example app listening on port ${8080}`);
- 31});
- 1import { streamText } from 'ai-toolkit';
- 2import express, { Request, Response } from 'express';
- 3const app = express();
- 4app.post('/', async (req: Request, res: Response) => {
- 5 const result = streamText({
- 6 model: 'openai/gpt-4o',
- 7 prompt: 'Invent a new holiday and describe its traditions.',
- 8 });
- 9 result.pipeTextStreamToResponse(res);
- 10});
- 11app.listen(8080, () => {
- 12 console.log(`Example app listening on port ${8080}`);
- 13});
Troubleshooting
- Streaming not working when proxied