OpenAI Streaming — Aquilia Documentation
Comprehensive guide and documentation for OpenAI Streaming in the Aquilia framework. View API reference, examples, and implementation patterns.
SSE SYSTEM / OPENAI STREAMING AI Streaming with OpenAI Implement real-time ChatGPT-style chat streaming by connecting OpenAI's async client stream to Aquilia's SSEResponse engine. Scenario Integration When building generative AI interfaces, displaying completion blocks after a long wait ruins the user experience. By streaming individual response tokens as they are generated by models (like gpt-4o), the page feels instantaneous. This scenario demonstrates how to invoke OpenAI's asynchronous stream API inside an Aquilia controller method, capture tokens sequentially, and tunnel them directly to client browsers using SSEResponse.text . Backend Controller The controller action reads the prompt parameter, initializes the async OpenAI client, and returns a token generator stream: import os from openai import AsyncOpenAI from aquilia.controller import Controller, GET, RequestCtx from aquilia.sse import SSEResponse class OpenAIChatController(Controller): def initialize(self): # Initialize OpenAI async client self.client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY")) @GET("/sse/chat") async def chat_stream(self, ctx: RequestCtx): prompt = ctx.query.get("prompt", "Tell me a joke.") # Stream response back to the client return SSEResponse.text(self._openai_token_generator(prompt)) async def _openai_token_generator(self, prompt: str): # Request completion stream from OpenAI API stream = await self.client.chat.completions.create( model="gpt-4", messages=[ ], stream=True ) # Iterate over stream chunks asynchronously and yield content async for chunk in stream: token = chunk.choices[0].delta.content or "" if token: yield token Frontend Client Integration On the browser side, query the chat stream endpoint and append tokens to your DOM elements: function startChatStream(userPrompt) \`); eventSource.onmessage = (event) => ; eventSource.onerror = (error) => ; } Text & JSON Streams Resource Management )
Go to Homepage