Created
April 4, 2024 12:48
-
-
Save n4cr/a751f9108c103c8284541054701f5c46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import JsonResponse, StreamingHttpResponse | |
from django.views.decorators.csrf import csrf_exempt | |
import json | |
def streaming_view(view_func): | |
""" | |
Decorator that converts a Django view function into a streaming response view. | |
The view function needs to accept a 'prompt' and return a generator yielding data chunks. | |
""" | |
def wrapper(request, *args, **kwargs): | |
# Extract prompt from the request | |
if request.method == 'POST': | |
data = json.loads(request.body) | |
prompt = data.get('prompt') | |
elif request.method == 'GET': | |
prompt = request.GET.get('prompt') | |
else: | |
return JsonResponse({"error": "Invalid request method"}, status=405) | |
if not prompt: | |
return JsonResponse({"error": "Prompt is required"}, status=400) | |
# Set the OpenAI API key | |
# Call the original view function, which should return a generator | |
response_generator = view_func(prompt) | |
# Return a StreamingHttpResponse using the generator | |
return StreamingHttpResponse(response_generator, content_type='text/event-stream') | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment