How to stream ChatGPT output from python backend to react frontend

ChatGPT is great, no question. But when I tried using the API, I found the speed of the response lacking.
When using OpenAIs web frontend I saw that the answer is appearing word for word.
I thought: »I might be able to stream the chatGPT api response in realtime. This way the user has immediate feedback without needing to wait for 20 seconds«.
And - lo and behold - this is possible!
What we’re building today:
- Call ChatGPT API
- Stream the data with SocketIO from python (flask)
- Receive the data in react using SocketIO and display it bit as bit in realtime