Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created February 2, 2025 21:54
Show Gist options
  • Save yusukebe/f597d1caa600ea8c20d9ab300cedac33 to your computer and use it in GitHub Desktop.
Save yusukebe/f597d1caa600ea8c20d9ab300cedac33 to your computer and use it in GitHub Desktop.
import { Hono } from 'hono'
import { streamText } from 'hono/streaming'
type Env = {
Bindings: {
AI: Ai
}
}
const app = new Hono<Env>()
app.get('/', async (c) => {
const message = c.req.query('message') ?? 'Say hello!'
const messages = [
{ role: 'system', content: 'You are a friendly assistant' },
{
role: 'user',
content: message
}
]
const result = (await c.env.AI.run('@cf/deepseek-ai/deepseek-r1-distill-qwen-32b', {
messages,
stream: true
})) as ReadableStream
return streamText(c, async (writer) => {
await writer.write('🤔')
let thinking = true
for await (const chunk of result.pipeThrough(new TextDecoderStream()).values()) {
for (const line of chunk.split('\n')) {
const jsonString = line.replace('data: ', '')
try {
const jsonData = JSON.parse(jsonString)
if (jsonData.response.trim() === '</think>') {
thinking = false
await writer.write('💡')
} else {
thinking ? await writer.write('.') : await writer.write(jsonData.response)
}
} catch {}
}
}
})
})
export default app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment