-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (ai/core): add toDataStream to streamText result (#2938)
- Loading branch information
Showing
15 changed files
with
500 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
feat (ai/core): add toDataStream to streamText result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
--- | ||
title: Hono | ||
description: Example of using Vercel AI SDK in a Hono server. | ||
--- | ||
|
||
# Hono | ||
|
||
You can use the Vercel AI SDK in a Hono server to generate and stream text and objects to the client. | ||
|
||
## Examples | ||
|
||
The examples start a simple HTTP server that listens on port 8080. You can e.g. test it using `curl`: | ||
|
||
```bash | ||
curl -X POST http://localhost:8080 | ||
``` | ||
|
||
<Note> | ||
The examples use the OpenAI `gpt-4o` model. Ensure that the OpenAI API key is | ||
set in the `OPENAI_API_KEY` environment variable. | ||
</Note> | ||
|
||
### Data Stream | ||
|
||
You can use the `toDataStream` method to get a data stream from the result and then pipe it to the response. | ||
|
||
```ts file='index.ts' | ||
import { openai } from '@ai-sdk/openai'; | ||
import { serve } from '@hono/node-server'; | ||
import { streamText } from 'ai'; | ||
import { Hono } from 'hono'; | ||
import { stream } from 'hono/streaming'; | ||
|
||
const app = new Hono(); | ||
|
||
app.post('/', async c => | ||
stream(c, async stream => { | ||
const result = await streamText({ | ||
model: openai('gpt-4o'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}); | ||
|
||
// Mark the response as a v1 data stream: | ||
c.header('X-Vercel-AI-Data-Stream', 'v1'); | ||
c.header('Content-Type', 'text/plain; charset=utf-8'); | ||
|
||
await stream.pipe(result.toDataStream()); | ||
}), | ||
); | ||
|
||
serve({ fetch: app.fetch, port: 8080 }); | ||
``` | ||
|
||
### Data Stream With Stream Data | ||
|
||
`toDataStream` can be used with `StreamData` to send additional data to the client. | ||
|
||
```ts file='index.ts' highlight="11-13,18-21,28" | ||
import { openai } from '@ai-sdk/openai'; | ||
import { serve } from '@hono/node-server'; | ||
import { StreamData, streamText } from 'ai'; | ||
import { Hono } from 'hono'; | ||
import { stream } from 'hono/streaming'; | ||
|
||
const app = new Hono(); | ||
|
||
app.post('/', async c => | ||
stream(c, async stream => { | ||
// use stream data (optional): | ||
const data = new StreamData(); | ||
data.append('initialized call'); | ||
|
||
const result = await streamText({ | ||
model: openai('gpt-4o'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
onFinish() { | ||
data.append('call completed'); | ||
data.close(); | ||
}, | ||
}); | ||
|
||
// Mark the response as a v1 data stream: | ||
c.header('X-Vercel-AI-Data-Stream', 'v1'); | ||
c.header('Content-Type', 'text/plain; charset=utf-8'); | ||
|
||
await stream.pipe(result.toDataStream({ data })); | ||
}), | ||
); | ||
|
||
serve({ fetch: app.fetch, port: 8080 }); | ||
``` | ||
|
||
### Text Stream | ||
|
||
You can use the `toTextStream` method to get a text stream from the result and then pipe it to the response. | ||
|
||
```ts file='index.ts' | ||
import { openai } from '@ai-sdk/openai'; | ||
import { serve } from '@hono/node-server'; | ||
import { streamText } from 'ai'; | ||
import { Hono } from 'hono'; | ||
import { stream } from 'hono/streaming'; | ||
|
||
const app = new Hono(); | ||
|
||
app.post('/', async c => | ||
stream(c, async stream => { | ||
const result = await streamText({ | ||
model: openai('gpt-4o'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}); | ||
|
||
c.header('Content-Type', 'text/plain; charset=utf-8'); | ||
|
||
await stream.pipe(result.toTextStream()); | ||
}), | ||
); | ||
|
||
serve({ fetch: app.fetch, port: 8080 }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ANTHROPIC_API_KEY="" | ||
OPENAI_API_KEY="" | ||
MISTRAL_API_KEY="" | ||
GOOGLE_GENERATIVE_AI_API_KEY="" | ||
FIREWORKS_API_KEY="" | ||
GROQ_API_KEY="" | ||
PERPLEXITY_API_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Hono + Vercel AI SDK Example | ||
|
||
## Usage | ||
|
||
1. Create .env file with the following content (and more settings, depending on the providers you want to use): | ||
|
||
```sh | ||
OPENAI_API_KEY="YOUR_OPENAI_API_KEY" | ||
``` | ||
|
||
2. Run the following commands from the root directory of the AI SDK repo: | ||
|
||
```sh | ||
pnpm install | ||
pnpm build | ||
``` | ||
|
||
3. Run the following command: | ||
|
||
```sh | ||
pnpm tsx src/server.ts | ||
``` | ||
|
||
4. Test the endpoint with Curl: | ||
|
||
```sh | ||
curl -X POST http://localhost:8080 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "ai-sdk-hono-example", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@ai-sdk/openai": "latest", | ||
"@hono/node-server": "1.12.2", | ||
"ai": "latest", | ||
"dotenv": "16.4.5", | ||
"hono": "4.5.11" | ||
}, | ||
"scripts": { | ||
"type-check": "tsc --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.11.20", | ||
"tsx": "4.7.1", | ||
"typescript": "5.5.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { serve } from '@hono/node-server'; | ||
import { StreamData, streamText } from 'ai'; | ||
import 'dotenv/config'; | ||
import { Hono } from 'hono'; | ||
import { stream } from 'hono/streaming'; | ||
|
||
const app = new Hono(); | ||
|
||
app.post('/', async c => | ||
stream(c, async stream => { | ||
// use stream data (optional): | ||
const data = new StreamData(); | ||
data.append('initialized call'); | ||
|
||
const result = await streamText({ | ||
model: openai('gpt-4o'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
onFinish() { | ||
data.append('call completed'); | ||
data.close(); | ||
}, | ||
}); | ||
|
||
// Mark the response as a v1 data stream: | ||
c.header('X-Vercel-AI-Data-Stream', 'v1'); | ||
c.header('Content-Type', 'text/plain; charset=utf-8'); | ||
|
||
await stream.pipe(result.toDataStream({ data })); | ||
}), | ||
); | ||
|
||
serve({ fetch: app.fetch, port: 8080 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"declaration": true, | ||
"sourceMap": true, | ||
"target": "es2022", | ||
"lib": ["es2022", "dom"], | ||
"module": "esnext", | ||
"types": ["node"], | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"moduleResolution": "node", | ||
"rootDir": "./src", | ||
"outDir": "./build", | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.