gemini-2.5-flash| input-price: 0.3 output-price: 2.5 max-context-length: 128_000 (cost: $0.007) Β· Anfrage: 2025-09-13T18:23:27.264151
Generated by rs-summarizer Version unbekannt
Abstract:
This video demonstrates how to implement streaming OpenAI responses in a Django web application, providing a real-time, word-by-word display of AI-generated content rather than waiting for the complete response. The tutorial covers both backend and frontend development, showing how to leverage the OpenAI API's streaming functionality with a Python generator in Django's StreamingHttpResponse. On the frontend, JavaScript's fetch API and TextDecoder are used to read and progressively display the streamed data. An enhancement for parsing and rendering markdown within the streamed output is also presented.
Summarizing OpenAI Streaming in Django
views.py for OpenAI client and API key (from environment variables), a urls.py for routing, and an index.html template for the basic user interface.generate_response Function: A Python generator function (generate_response) is created to interact with the OpenAI API. It uses client.chat.completions.create with the stream=True parameter to get chunked responses and yields chunk.choices[0].delta.content to process each piece of content as it arrives.StreamingHttpResponse: A Django view (answer) is created to handle user requests. It utilizes Django's StreamingHttpResponse to return the output of the generate_response generator, maintaining an open connection until the AI response is complete.csrf_exempt decorator is applied to the answer view to allow POST requests without CSRF token handling.fetch: In index.html, JavaScript is used to capture user input and send an asynchronous POST request to the /answer endpoint using the fetch API. The user's question is sent as a JSON message in the request body.fetch request using response.body.getReader(). It continuously decodes (TextDecoder().decode()) and appends incoming text chunks to a display element on the page, creating the real-time streaming effect.marked JavaScript library is integrated. The streamed output is passed through marked.parse() before being displayed, converting markdown into HTML for enhanced formatting.