Skip to content

feat(rag): implement real-time token streaming via SSE and FastAPI background tasks - #194

Open
YTxFSGAMERz wants to merge 1 commit into
devloperdevesh:mainfrom
YTxFSGAMERz:feat-rag-sse-v2
Open

feat(rag): implement real-time token streaming via SSE and FastAPI background tasks#194
YTxFSGAMERz wants to merge 1 commit into
devloperdevesh:mainfrom
YTxFSGAMERz:feat-rag-sse-v2

Conversation

@YTxFSGAMERz

Copy link
Copy Markdown

Summary

This PR resolves a significant UX bottleneck in the /rag/query/stream endpoint by implementing true auto-regressive Server-Sent Events (SSE).

Previously, the endpoint awaited the complete text generation from the LLM before yielding chunks for cosmetic UI streaming. By integrating sse-starlette and converting the Ollama client into a real-time generator, tokens are now streamed immediately as they arrive over an EventSourceResponse, drastically reducing perceived latency.

Architectural Changes

  • Dependencies: Added sse-starlette to manage the EventSource streaming protocol standard.
  • LLM Service (app/rag/llm.py): Implemented generate_answer_stream using requests.post(stream=True) to yield JSON-decoded tokens asynchronously.
  • FastAPI Routes (app/api/routes/rag.py): Offloaded the blocking generator to a background thread (asyncio.to_thread) and wrapped the output in an EventSourceResponse. Background metadata tracking via _dispatch_background remains intact.

Note: Submitted as part of GirlScript Summer of Code (GSSoC) 2026.

@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

@YTxFSGAMERz is attempting to deploy a commit to the devloperdevesh's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant