How rahullll101/conversation-rag works
1. What this repository is
This is a two-tier conversational RAG system: a FastAPI backend that ingests documents, indexes them, retrieves evidence, and generates grounded answers; and a Streamlit frontend that provides the chat/upload UI and streams backend responses to the user.
2. Top-level architecture boundary
The main architectural split is between:
- Frontend/UI layer in frontend/app.py
- Backend API + RAG pipeline in backend/
The frontend does not perform retrieval or LLM orchestration itself; it delegates everything to backend endpoints over HTTP.
3. Backend entrypoint and API surface
backend/main.py wires the FastAPI application and mounts the route modules under backend/routes/.
The API is organized around distinct capabilities:
- upload.py for accepting documents
- process.py for indexing preparation
- chat.py for conversational QA
- query.py for query-related operations
These routes expose the system as a pipeline rather than a single monolithic chat endpoint.
4. Document ingestion subsystem
Document handling starts with upload and is implemented through:
- backend/services/upload_service.py
- backend/utils/file_handling.py
- backend/utils/extraction.py
This layer is responsible for accepting PDF/TXT inputs, extracting text, and creating a session-scoped document context. The extracted content is then handed off to the processing/indexing path.
5. Processing and indexing subsystem
backend/routes/process.py triggers the transformation of uploaded text into retrievable chunks and embeddings. The service layer suggests a staged pipeline:
- chunking_service.py / utils/chunking.py
- embedding_service.py
- vector_store_service.py
This is the “offline” half of the RAG system: break documents into chunks, embed them, and store them in a vector database. The repository README indicates ChromaDB-backed session isolation, so each upload/session is treated as an isolated retrieval namespace.
6. Retrieval and reranking subsystem
The query-time retrieval path is separated into dedicated services:
- retrieval_service.py
- rerank_service.py
- query_rewrite_service.py
- memory_service.py
The intended flow is: rewrite the user query using conversation memory, retrieve candidate chunks from the vector store, then rerank them before generation. This makes retrieval a multi-stage filter rather than a single similarity lookup.
7. Generation and prompt orchestration
Answer generation is isolated from retrieval in:
- llm_service.py
- prompt_service.py
- llama_index_service.py
The architecture uses a hybrid orchestration pattern: one part of the stack prepares/indexes data, while another assembles prompts and streams the LLM response. The README describes this as a split between LlamaIndex-style ingestion and LangChain-style orchestration, which is consistent with the service boundaries.
8. Conversational state
memory_service.py and query_rewrite_service.py indicate a bounded conversation memory layer. Its role is not long-term user profiling; it is short-horizon context for resolving follow-up questions and rewriting ambiguous queries so retrieval targets the right document context.
9. Evaluation subsystem
The system includes a separate evaluation path rather than baking metrics into chat inference:
- backend/services/evaluation_service.py
- backend/schemas/evaluation.py
- backend/utils/evaluation.py
- evaluation/sample_queries.json
This subsystem evaluates answer quality against a test set, likely using LLM-as-judge style scoring for faithfulness/relevance as described in the README. It is an operational support function, not part of the live chat path.
10. Shared contracts and validation
backend/schemas/ contains Pydantic models for requests and responses:
- upload.py, pipeline.py, query.py, answer.py, evaluation.py
These schemas define the contract between UI, API routes, and services. They form the boundary that keeps upload, processing, retrieval, and evaluation payloads structurally separate.
11. Configuration and deployment boundary
backend/config/settings.py centralizes environment-driven configuration, which is important because the system depends on external services and runtime parameters such as model keys, vector store settings, and session behavior. The presence of Dockerfile, .env.example, and Azure deployment details in the README shows the backend is designed to run as a containerized service with externalized configuration.
12. How data flows through the system
The core end-to-end flow is:
- User uploads a document in the Streamlit UI
- Backend extracts text and assigns a session context
- Processing service chunks and embeds the content
- Chunks are stored in session-isolated vector storage
- User asks a question
- Backend rewrites the query using short-term memory, retrieves and reranks candidate chunks, builds a grounded prompt, and streams the answer back
- Optional evaluation runs the same retrieval/generation stack against sample queries
13. Important architectural boundary to diagram
For an architecture diagram, the key boundary is that the system is RAG-first and session-scoped:
- ingestion/indexing is separate from chat inference
- retrieval is separate from generation
- evaluation is separate from production chat
- frontend is only a client over HTTP
That separation is the main reason the system can support strict document grounding and avoid context bleed between sessions.
14. Repository structure as a map of responsibilities
The codebase is organized cleanly into:
- backend/routes for HTTP endpoints
- backend/services for business logic and pipeline stages
- backend/utils for low-level stateless helpers
- backend/schemas for API contracts
- frontend for the interactive UI
- evaluation and docs for offline validation and architecture support
This is a layered architecture with clear API, service, and utility boundaries rather than a single end-to-end script.
Main entry points: main.py API App · schemas API Schemas · retrieval_service.py Retrieval · evaluation_service.py Evaluation
Components 22
Backend API
FastAPI entrypoint and route layer
- API App · Application entrypoint and route wiring
backend/main.py - Upload API · Accepts documents and session context
backend/routes/upload.py - Process API · Starts document preparation and indexing
backend/routes/process.py - Chat API · Handles conversational question answering
backend/routes/chat.py - Query API · Query-related retrieval and support operations
backend/routes/query.py
RAG Pipeline
Ingestion, retrieval, and answer generation
- Upload Service · Coordinates file intake and document setup
backend/services/upload_service.py - Chunking · Breaks text into retrieval chunks
backend/services/chunking_service.py - Embeddings · Converts chunks into vector representations
backend/services/embedding_service.py - Vector Store · Persists session-scoped retrievable chunks
backend/services/vector_store_service.py - Retrieval · Fetches candidate evidence from the vector store
backend/services/retrieval_service.py - Reranker · Reorders retrieved candidates by relevance
backend/services/rerank_service.py - Query Rewrite · Rewrites follow-up questions using conversation context
backend/services/query_rewrite_service.py - Conversation Memory · Stores bounded conversational context
backend/services/memory_service.py - Prompt Builder · Assembles grounded prompts for generation
backend/services/prompt_service.py - LLM Service · Streams grounded answers from the model
backend/services/llm_service.py - Index Bridge · Supports index construction and ingestion integration
backend/services/llama_index_service.py
Shared Layer
Schemas, config, and utilities
- Extraction Utils · File handling and text extraction helpers
backend/utils/extraction.py - API Schemas · Request and response models for the API
backend/schemas - Settings · Environment-driven runtime configuration
backend/config/settings.py
Evaluation
Offline quality evaluation
- Evaluation · Runs quality checks over sample queries
backend/services/evaluation_service.py - Sample Queries · Offline evaluation prompts and references
evaluation/sample_queries.json
Connections 27
- Chat UI → API App: HTTP API · UI delegates upload and chat actions to the backend
- API App → Upload API: mounts · Registers upload endpoints
- API App → Process API: mounts · Registers processing endpoints
- API App → Chat API: mounts · Registers chat endpoints
- API App → Query API: mounts · Registers query endpoints
- Upload API → Upload Service: calls · Starts ingestion workflow
- Upload Service → Extraction Utils: extracts · Reads files and extracts text
- Process API → Chunking: calls · Begins document preparation
- Chunking → Embeddings: feeds · Transforms chunks into vectors
- Embeddings → Vector Store: stores · Persists embedded chunks in session namespace
- Index Bridge → Vector Store: indexes · Builds or updates retrieval index
- Query API → Query Rewrite: calls · Normalizes questions using context
- Query Rewrite → Conversation Memory: reads · Uses bounded conversation history
- Chat API → Retrieval: calls · Starts evidence lookup
- Retrieval → Vector Store: queries · Searches session-isolated chunks
- Retrieval → Reranker: candidates · Passes retrieved chunks for ranking
- Reranker → Prompt Builder: supports · Selects evidence for grounded prompting
- Prompt Builder → LLM Service: builds prompt · Hands off final prompt for generation
- LLM Service → Conversation Memory: updates · Stores conversational state
- API Schemas → Upload API: validates · Defines upload payloads
- API Schemas → Process API: validates · Defines processing payloads
- API Schemas → Chat API: validates · Defines chat payloads
- API Schemas → Query API: validates · Defines query payloads
- Settings → API App: configures · Supplies runtime settings
- Evaluation → Sample Queries: runs on · Evaluates against offline sample queries
- Evaluation → Retrieval: reuses · Exercises the same retrieval path
- Evaluation → LLM Service: reuses · Scores generated answers