How to use the OpenAI stream=true property with a Django Rest Framework response, and still save the content returned?

I'm trying to use the stream=true property as follows.

completion = openai.Completion.create(
            prompt="Write me a story about dogs.",

Unfortunately, I don't know what to do from here to return it to my React frontend. Typically, I've used standard response objects, setting a status and the as the data. From my readings online, it seems I have to use the StreamingHttpResponse, but I'm not sure how to integrate that with the iterator object of completion, and actually save the outputted data once it is done streaming, as the view will end after returning the iterator to the endpoint. Any help?

Answer ended up being I was thinking of approaching it incorrectly. Use websockets! Not StreamingHttpResponse.

Back to Top