Spaces:
Build error
Build error
|
Download docs/explanations/middleware.md from AIenthusSP/RAG-Documind-AI: direct link, hf CLI and curl.
- Browser
- Download file 3.98 kB
-
https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/docs/explanations/middleware.md
- Command line
-
hf download hf://spaces/AIenthusSP/RAG-Documind-AI/docs/explanations/middleware.md
-
curl -L -o middleware.md https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/docs/explanations/middleware.md
3.98 kB
| # π Deep Dive: Streaming & Error Handling | |
| ## π§ Why Streaming for RAG? | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β WITHOUT STREAMING β | |
| β β | |
| β User asks β [3-5 second wait...] β Full answer appears β | |
| β β | |
| β WITH STREAMING (SSE) β | |
| β β | |
| β User asks β Answer | streams | word | by | word | instantly β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ``` | |
| --- | |
| ## π Middleware Architecture | |
| ``` | |
| Request β RateLimitMiddleware β RequestLoggingMiddleware β ErrorHandlerMiddleware β Route | |
| β | |
| Response β RateLimitMiddleware β RequestLoggingMiddleware β ErrorHandlerMiddleware β | |
| ``` | |
| --- | |
| ## π― ErrorHandlerMiddleware | |
| ```python | |
| class ErrorHandlerMiddleware(BaseHTTPMiddleware): | |
| async def dispatch(self, request, call_next): | |
| try: | |
| return await call_next(request) | |
| except Exception as e: | |
| logger.error(f"Error: {e}") | |
| return JSONResponse(status_code=500, content={...}) | |
| ``` | |
| **Purpose:** Catch all unhandled exceptions and return proper JSON errors. | |
| --- | |
| ## π RequestLoggingMiddleware | |
| ```python | |
| class RequestLoggingMiddleware(BaseHTTPMiddleware): | |
| async def dispatch(self, request, call_next): | |
| start_time = time.time() | |
| response = await call_next(request) | |
| duration = time.time() - start_time | |
| logger.info(f"{request.method} {request.url.path} - {duration:.3f}s") | |
| response.headers["X-Process-Time"] = f"{duration:.3f}" | |
| return response | |
| ``` | |
| **Purpose:** Log all requests with timing for observability. | |
| --- | |
| ## β±οΈ RateLimitMiddleware | |
| ```python | |
| class RateLimitMiddleware(BaseHTTPMiddleware): | |
| def __init__(self, app, requests_per_minute=60): | |
| # Track requests per IP in memory | |
| async def dispatch(self, request, call_next): | |
| if too_many_requests: | |
| return JSONResponse(status_code=429, content={...}) | |
| return await call_next(request) | |
| ``` | |
| **Purpose:** Prevent abuse with simple rate limiting (60 req/min default). | |
| --- | |
| ## π Server-Sent Events (SSE) Streaming | |
| ### Endpoint | |
| ```python | |
| @router.post("/query/stream") | |
| async def stream_query(request: QueryRequest): | |
| async def generate(): | |
| for chunk in pipeline.stream(request.question): | |
| yield f"data: {chunk}\n\n" | |
| yield "data: [DONE]\n\n" | |
| return StreamingResponse( | |
| generate(), | |
| media_type="text/event-stream", | |
| headers={ | |
| "Cache-Control": "no-cache", | |
| "Connection": "keep-alive", | |
| } | |
| ) | |
| ``` | |
| ### SSE Format | |
| ``` | |
| data: Based on | |
| data: the | |
| data: documentation | |
| data: , | |
| data: refunds | |
| data: are | |
| data: ... | |
| data: [DONE] | |
| ``` | |
| ### Frontend Consumption | |
| ```javascript | |
| const eventSource = new EventSource('/api/query/stream'); | |
| eventSource.onmessage = (event) => { | |
| if (event.data === '[DONE]') { | |
| eventSource.close(); | |
| } else { | |
| // Append chunk to UI | |
| responseDiv.textContent += event.data; | |
| } | |
| }; | |
| ``` | |
| --- | |
| ## π Summary | |
| | Component | Purpose | | |
| |-----------|---------| | |
| | `ErrorHandlerMiddleware` | Global error catching | | |
| | `RequestLoggingMiddleware` | Request timing logs | | |
| | `RateLimitMiddleware` | Abuse prevention | | |
| | SSE Streaming | Real-time response delivery | | |
| | `X-Process-Time` header | Performance monitoring | | |