Ask Your Question
2

What is the method for retaining the returned content while utilizing the OpenAI stream=true attribute within a Django Rest Framework response?

asked 2023-06-06 16:00:13 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-06 16:19:02 +0000

nofretete gravatar image

To retain the returned content while utilizing the OpenAI stream=true attribute within a Django Rest Framework response, follow these steps:

  1. Create a streaming response object using Django's StreamingHttpResponse class.

  2. Generate the OpenAI response using the OpenAI API.

  3. Iterate over the response, sending chunks of the data to the streaming response object.

  4. Send a response with the streaming response object.

Here's an example code snippet:

from django.http import StreamingHttpResponse
from openai import api_key, Model, models

class OpenAIView(APIView):
    def get(self, request):
        # Set up the OpenAI API parameters
        prompt = "Some prompt"
        model = Model(engine="davinci", api_key=api_key)

        # Generate the OpenAI response
        response = model.complete(prompt=prompt, max_tokens=50, n=1, stop=None, temperature=0.5)

        # Create the streaming response object
        response_streaming = StreamingHttpResponse(streaming_content=(chunk for chunk in response), content_type='text/plain')

        # Return the streaming response object
        return response_streaming

This code will create a streaming response object that iterates over the OpenAI response and sends it to the client in chunks. This allows the response to be retained while also utilizing the OpenAI stream=true attribute.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-06 16:00:13 +0000

Seen: 12 times

Last updated: Jun 06 '23