|
| 1 | +--- |
| 2 | +title: Hono |
| 3 | +description: Example of using Vercel AI SDK in a Hono server. |
| 4 | +--- |
| 5 | + |
| 6 | +# Hono |
| 7 | + |
| 8 | +You can use the Vercel AI SDK in a Hono server to generate and stream text and objects to the client. |
| 9 | + |
| 10 | +## Examples |
| 11 | + |
| 12 | +The examples start a simple HTTP server that listens on port 8080. You can e.g. test it using `curl`: |
| 13 | + |
| 14 | +```bash |
| 15 | +curl -X POST http://localhost:8080 |
| 16 | +``` |
| 17 | + |
| 18 | +<Note> |
| 19 | + The examples use the OpenAI `gpt-4o` model. Ensure that the OpenAI API key is |
| 20 | + set in the `OPENAI_API_KEY` environment variable. |
| 21 | +</Note> |
| 22 | + |
| 23 | +### Data Stream |
| 24 | + |
| 25 | +You can use the `toDataStream` method to get a data stream from the result and then pipe it to the response. |
| 26 | + |
| 27 | +```ts file='index.ts' |
| 28 | +import { openai } from '@ai-sdk/openai'; |
| 29 | +import { serve } from '@hono/node-server'; |
| 30 | +import { streamText } from 'ai'; |
| 31 | +import { Hono } from 'hono'; |
| 32 | +import { stream } from 'hono/streaming'; |
| 33 | + |
| 34 | +const app = new Hono(); |
| 35 | + |
| 36 | +app.post('/', async c => |
| 37 | + stream(c, async stream => { |
| 38 | + const result = await streamText({ |
| 39 | + model: openai('gpt-4o'), |
| 40 | + prompt: 'Invent a new holiday and describe its traditions.', |
| 41 | + }); |
| 42 | + |
| 43 | + // Mark the response as a v1 data stream: |
| 44 | + c.header('X-Vercel-AI-Data-Stream', 'v1'); |
| 45 | + c.header('Content-Type', 'text/plain; charset=utf-8'); |
| 46 | + |
| 47 | + await stream.pipe(result.toDataStream()); |
| 48 | + }), |
| 49 | +); |
| 50 | + |
| 51 | +serve({ fetch: app.fetch, port: 8080 }); |
| 52 | +``` |
| 53 | + |
| 54 | +### Data Stream With Stream Data |
| 55 | + |
| 56 | +`toDataStream` can be used with `StreamData` to send additional data to the client. |
| 57 | + |
| 58 | +```ts file='index.ts' highlight="11-13,18-21,28" |
| 59 | +import { openai } from '@ai-sdk/openai'; |
| 60 | +import { serve } from '@hono/node-server'; |
| 61 | +import { StreamData, streamText } from 'ai'; |
| 62 | +import { Hono } from 'hono'; |
| 63 | +import { stream } from 'hono/streaming'; |
| 64 | + |
| 65 | +const app = new Hono(); |
| 66 | + |
| 67 | +app.post('/', async c => |
| 68 | + stream(c, async stream => { |
| 69 | + // use stream data (optional): |
| 70 | + const data = new StreamData(); |
| 71 | + data.append('initialized call'); |
| 72 | + |
| 73 | + const result = await streamText({ |
| 74 | + model: openai('gpt-4o'), |
| 75 | + prompt: 'Invent a new holiday and describe its traditions.', |
| 76 | + onFinish() { |
| 77 | + data.append('call completed'); |
| 78 | + data.close(); |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + // Mark the response as a v1 data stream: |
| 83 | + c.header('X-Vercel-AI-Data-Stream', 'v1'); |
| 84 | + c.header('Content-Type', 'text/plain; charset=utf-8'); |
| 85 | + |
| 86 | + await stream.pipe(result.toDataStream({ data })); |
| 87 | + }), |
| 88 | +); |
| 89 | + |
| 90 | +serve({ fetch: app.fetch, port: 8080 }); |
| 91 | +``` |
| 92 | + |
| 93 | +### Text Stream |
| 94 | + |
| 95 | +You can use the `toTextStream` method to get a text stream from the result and then pipe it to the response. |
| 96 | + |
| 97 | +```ts file='index.ts' |
| 98 | +import { openai } from '@ai-sdk/openai'; |
| 99 | +import { serve } from '@hono/node-server'; |
| 100 | +import { streamText } from 'ai'; |
| 101 | +import { Hono } from 'hono'; |
| 102 | +import { stream } from 'hono/streaming'; |
| 103 | + |
| 104 | +const app = new Hono(); |
| 105 | + |
| 106 | +app.post('/', async c => |
| 107 | + stream(c, async stream => { |
| 108 | + const result = await streamText({ |
| 109 | + model: openai('gpt-4o'), |
| 110 | + prompt: 'Invent a new holiday and describe its traditions.', |
| 111 | + }); |
| 112 | + |
| 113 | + c.header('Content-Type', 'text/plain; charset=utf-8'); |
| 114 | + |
| 115 | + await stream.pipe(result.toTextStream()); |
| 116 | + }), |
| 117 | +); |
| 118 | + |
| 119 | +serve({ fetch: app.fetch, port: 8080 }); |
| 120 | +``` |
0 commit comments