Skip to content

Instantly share code, notes, and snippets.

@n4cr
Created April 4, 2024 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n4cr/a751f9108c103c8284541054701f5c46 to your computer and use it in GitHub Desktop.
Save n4cr/a751f9108c103c8284541054701f5c46 to your computer and use it in GitHub Desktop.
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