Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yusukebe/chatgpt-streaming
https://github.com/yusukebe/chatgpt-streaming
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yusukebe/chatgpt-streaming
- Owner: yusukebe
- Created: 2023-09-15T07:08:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-30T09:18:01.000Z (about 1 year ago)
- Last Synced: 2024-11-01T13:34:14.101Z (9 days ago)
- Language: TypeScript
- Homepage: https://chatgpt-streaming.pages.dev
- Size: 47.9 KB
- Stars: 45
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ChatGPT Streaming Example
An example using the ChatGPT API and responding with a streaming response.
## Stack
* ChatGPT API
* [Sonik](https://github.com/yusukebe/sonik) - The [Hono](https://hono.dev/) based meta-framework.
* [`c.stream()`](https://github.com/honojs/hono/pull/1437) - A new feature of the Hono.
* React
* Cloudflare Pages## From the source code
Here is the key code:
```ts
export const route = defineRoute((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
https://github.com/yusukebe/chatgpt-streaming/assets/10682/ef0eedfc-813b-4eb3-941a-769319a3ed18
## Authors
- Yusuke Wada
## License
MIT