DEV Community

quandale dingle
quandale dingle

Posted on

How do I make the Openai API message output in real time?

I'm trying to make a chatbot website. I want the ai's response to be sent chunk by chunk as soon as the chunk is processed, but the output message is being sent all at once on my website.
In the terminal, I made it so that the response gets outputted as soon as each chunk is processed, and it worked for the terminal using this code:

print(chunk_message['content'], end='', flush=True)
Enter fullscreen mode Exit fullscreen mode

But I can't figure out how to make it send as soon as each chunk was processed in my website.
I made the browser console print out the times when the chunks were sent to it and they all were apparently sent at the same time. I know that is the problem but I can't figure out how to solve it. Here is my github link for the full code: https://github.com/Breath3Manually/chat-bot.git
the important files are the consumers.py file and the chat_view.html file
any help will be appreciated, I've spent 2 days trying to fix this <3

Top comments (0)