docs: prepare for chat components (#3844)

This commit is contained in:
Benjamin Canac
2025-04-10 19:31:12 +02:00
committed by GitHub
parent fb4c210b41
commit 981de8b295
6 changed files with 243 additions and 16 deletions

22
docs/server/api/chat.ts Normal file
View File

@@ -0,0 +1,22 @@
import { streamText } from 'ai'
import { createWorkersAI } from 'workers-ai-provider'
export default defineEventHandler(async (event) => {
const { messages } = await readBody(event)
// Enable AI Gateway if defined in environment variables
const gateway = process.env.CLOUDFLARE_AI_GATEWAY_ID
? {
id: process.env.CLOUDFLARE_AI_GATEWAY_ID,
cacheTtl: 60 * 60 * 24 // 24 hours
}
: undefined
const workersAI = createWorkersAI({ binding: hubAI(), gateway })
return streamText({
model: workersAI('@cf/meta/llama-3.2-3b-instruct'),
maxTokens: 10000,
system: 'You are a helpful assistant that can answer questions and help.',
messages
}).toDataStreamResponse()
})