yusukebe / chatgpt-streaming

Home Page:https://chatgpt-streaming.pages.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ChatGPT Streaming Example

An example using the ChatGPT API and responding with a streaming response.

Stack

  • ChatGPT API
  • Sonik - The Hono based meta-framework.
  • c.stream() - A new feature of the Hono.
  • React
  • Cloudflare Pages

From the source code

Here is the key code:

export const route = defineRoute<Env>((app) => {
  app.post('/api', async (c) => {
    const body = await c.req.json<{ message: string }>()

    const openai = new OpenAI({
      apiKey: c.env.OPENAI_API_KEY
    })

    const chatStream = await openai.chat.completions.create({
      messages: PROMPT(body.message),
      model: 'gpt-3.5-turbo',
      stream: true
    })

    return c.streamText(async (stream) => {
      for await (const message of chatStream) {
        await stream.write(message.choices[0]?.delta.content ?? '')
      }
    })
  })
})

Demo

Screen.Recording.2023-09-15.at.16.19.38.mov

Authors

License

MIT

About

https://chatgpt-streaming.pages.dev


Languages

Language:TypeScript 79.6%Language:CSS 20.4%