-
-
Save codemem/d15c6368fe096f1881aafaa41754cf7a to your computer and use it in GitHub Desktop.
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
import { Hono } from 'hono' | |
import { streamText } from 'hono/streaming' | |
import { Ai } from '@cloudflare/ai' | |
import { events } from 'fetch-event-stream' | |
type Bindings = { | |
AI: any | |
} | |
const app = new Hono<{ Bindings: Bindings }>() | |
app.get('/', async (c) => { | |
const ai = new Ai(c.env.AI) | |
const aiStream = (await ai.run('@cf/meta/llama-2-7b-chat-int8', { | |
prompt: 'tell me about ramen', | |
stream: true | |
})) as ReadableStream | |
return streamText(c, async (stream) => { | |
const chunks = events(new Response(aiStream)) | |
for await (const chunk of chunks) { | |
const data = JSON.parse(chunk.data!) | |
stream.write(data.response) | |
} | |
}) | |
}) | |
export default app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment