Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created March 14, 2024 10:05
Show Gist options
  • Save yusukebe/8875dc251b72007ab0caa7f4ad372e8b to your computer and use it in GitHub Desktop.
Save yusukebe/8875dc251b72007ab0caa7f4ad372e8b to your computer and use it in GitHub Desktop.
import { type ExportedHandler } from '@cloudflare/workers-types'
import { Ai } from '@cloudflare/ai'
type Env = {
AI: any
}
export default {
fetch: async (req, env) => {
const ai = new Ai(env.AI)
const res = await ai.run('@cf/meta/llama-2-7b-chat-int8', {
prompt: 'Please explain how to write a good code.',
stream: true
})
if (res instanceof ReadableStream) {
return new Response(res, {
headers: {
'Transfer-Encoding': 'chunked'
}
})
}
return new Response('response is not a stream.')
}
} satisfies ExportedHandler<Env>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment