# rahullll101/conversation-rag architecture

> Architecture diagram of the GitHub repository rahullll101/conversation-rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rahullll101/conversation-rag
- Repository: https://github.com/rahullll101/conversation-rag

## Overview

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: [`backend/main.py`](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/main.py) (API App), [`backend/schemas`](https://github.com/rahullll101/conversation-rag/tree/llamaindex-rag/backend/schemas) (API Schemas), [`backend/services/retrieval_service.py`](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/retrieval_service.py) (Retrieval), [`backend/services/evaluation_service.py`](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/evaluation_service.py) (Evaluation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["Chat UI<br/>streamlit app<br/>[app.py]"]
end

subgraph group_api["Backend API"]
  node_backend_main["API App<br/>[main.py]"]
  node_route_upload["Upload API<br/>route<br/>[upload.py]"]
  node_route_process["Process API<br/>route<br/>[process.py]"]
  node_route_chat["Chat API<br/>route<br/>[chat.py]"]
  node_route_query["Query API<br/>route<br/>[query.py]"]
end

subgraph group_rag["RAG Pipeline"]
  node_upload_service["Upload Service<br/>ingestion service<br/>[upload_service.py]"]
  node_chunking_service["Chunking<br/>prep service"]
  node_embedding_service["Embeddings<br/>model prep"]
  node_vector_store_service[("Vector Store<br/>session index")]
  node_retrieval_service["Retrieval<br/>search service"]
  node_rerank_service["Reranker<br/>ranking service<br/>[rerank_service.py]"]
  node_query_rewrite_service["Query Rewrite<br/>context service"]
  node_memory_service[("Conversation Memory<br/>short-term memory<br/>[memory_service.py]")]
  node_prompt_service["Prompt Builder<br/>prompt orchestration<br/>[prompt_service.py]"]
  node_llm_service["LLM Service<br/>generation service<br/>[llm_service.py]"]
  node_llama_index_service["Index Bridge<br/>indexing service"]
end

subgraph group_shared["Shared Layer"]
  node_extract_utils["Extraction Utils<br/>file parsing<br/>[extraction.py]"]
  node_schemas["API Schemas<br/>pydantic contracts"]
  node_settings["Settings<br/>configuration<br/>[settings.py]"]
end

subgraph group_offline["Evaluation"]
  node_evaluation_service["Evaluation<br/>offline eval"]
  node_eval_samples["Sample Queries<br/>eval dataset"]
end

node_frontend_app -->|"HTTP API"| node_backend_main
node_backend_main -->|"mounts"| node_route_upload
node_backend_main -->|"mounts"| node_route_process
node_backend_main -->|"mounts"| node_route_chat
node_backend_main -->|"mounts"| node_route_query
node_route_upload -->|"calls"| node_upload_service
node_upload_service -->|"extracts"| node_extract_utils
node_route_process -->|"calls"| node_chunking_service
node_chunking_service -->|"feeds"| node_embedding_service
node_embedding_service -->|"stores"| node_vector_store_service
node_llama_index_service -->|"indexes"| node_vector_store_service
node_route_query -->|"calls"| node_query_rewrite_service
node_query_rewrite_service -->|"reads"| node_memory_service
node_route_chat -->|"calls"| node_retrieval_service
node_retrieval_service -->|"queries"| node_vector_store_service
node_retrieval_service -->|"candidates"| node_rerank_service
node_rerank_service -->|"supports"| node_prompt_service
node_prompt_service -->|"builds prompt"| node_llm_service
node_llm_service -->|"updates"| node_memory_service
node_schemas -->|"validates"| node_route_upload
node_schemas -->|"validates"| node_route_process
node_schemas -->|"validates"| node_route_chat
node_schemas -->|"validates"| node_route_query
node_settings -->|"configures"| node_backend_main
node_evaluation_service -->|"runs on"| node_eval_samples
node_evaluation_service -->|"reuses"| node_retrieval_service
node_evaluation_service -->|"reuses"| node_llm_service

click node_frontend_app "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/frontend/app.py"
click node_backend_main "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/main.py"
click node_route_upload "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/upload.py"
click node_route_process "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/process.py"
click node_route_chat "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/chat.py"
click node_route_query "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/query.py"
click node_upload_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/upload_service.py"
click node_extract_utils "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/utils/extraction.py"
click node_chunking_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/chunking_service.py"
click node_embedding_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/embedding_service.py"
click node_vector_store_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/vector_store_service.py"
click node_retrieval_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/retrieval_service.py"
click node_rerank_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/rerank_service.py"
click node_query_rewrite_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/query_rewrite_service.py"
click node_memory_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/memory_service.py"
click node_prompt_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/prompt_service.py"
click node_llm_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/llm_service.py"
click node_llama_index_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/llama_index_service.py"
click node_schemas "https://github.com/rahullll101/conversation-rag/tree/llamaindex-rag/backend/schemas"
click node_settings "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/config/settings.py"
click node_evaluation_service "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/evaluation_service.py"
click node_eval_samples "https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/evaluation/sample_queries.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app toneBlue
class node_backend_main,node_route_upload,node_route_process,node_route_chat,node_route_query toneAmber
class node_upload_service,node_chunking_service,node_embedding_service,node_vector_store_service,node_retrieval_service,node_rerank_service,node_query_rewrite_service,node_memory_service,node_prompt_service,node_llm_service,node_llama_index_service toneMint
class node_extract_utils,node_schemas,node_settings toneRose
class node_evaluation_service,node_eval_samples toneIndigo
```

## Components

### Frontend

Streamlit client UI

- [Chat UI](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/frontend/app.py): streamlit app · Document upload and conversational interface · `frontend/app.py`

### Backend API

FastAPI entrypoint and route layer

- [API App](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/main.py): fastapi app · Application entrypoint and route wiring · `backend/main.py`
- [Upload API](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/upload.py): route · Accepts documents and session context · `backend/routes/upload.py`
- [Process API](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/process.py): route · Starts document preparation and indexing · `backend/routes/process.py`
- [Chat API](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/chat.py): route · Handles conversational question answering · `backend/routes/chat.py`
- [Query API](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/routes/query.py): route · Query-related retrieval and support operations · `backend/routes/query.py`

### RAG Pipeline

Ingestion, retrieval, and answer generation

- [Upload Service](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/upload_service.py): ingestion service · Coordinates file intake and document setup · `backend/services/upload_service.py`
- [Chunking](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/chunking_service.py): prep service · Breaks text into retrieval chunks · `backend/services/chunking_service.py`
- [Embeddings](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/embedding_service.py): model prep · Converts chunks into vector representations · `backend/services/embedding_service.py`
- [Vector Store](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/vector_store_service.py): session index · Persists session-scoped retrievable chunks · `backend/services/vector_store_service.py`
- [Retrieval](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/retrieval_service.py): search service · Fetches candidate evidence from the vector store · `backend/services/retrieval_service.py`
- [Reranker](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/rerank_service.py): ranking service · Reorders retrieved candidates by relevance · `backend/services/rerank_service.py`
- [Query Rewrite](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/query_rewrite_service.py): context service · Rewrites follow-up questions using conversation context · `backend/services/query_rewrite_service.py`
- [Conversation Memory](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/memory_service.py): short-term memory · Stores bounded conversational context · `backend/services/memory_service.py`
- [Prompt Builder](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/prompt_service.py): prompt orchestration · Assembles grounded prompts for generation · `backend/services/prompt_service.py`
- [LLM Service](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/llm_service.py): generation service · Streams grounded answers from the model · `backend/services/llm_service.py`
- [Index Bridge](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/llama_index_service.py): indexing service · Supports index construction and ingestion integration · `backend/services/llama_index_service.py`

### Shared Layer

Schemas, config, and utilities

- [Extraction Utils](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/utils/extraction.py): file parsing · File handling and text extraction helpers · `backend/utils/extraction.py`
- [API Schemas](https://github.com/rahullll101/conversation-rag/tree/llamaindex-rag/backend/schemas): pydantic contracts · Request and response models for the API · `backend/schemas`
- [Settings](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/config/settings.py): configuration · Environment-driven runtime configuration · `backend/config/settings.py`

### Evaluation

Offline quality evaluation

- [Evaluation](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/backend/services/evaluation_service.py): offline eval · Runs quality checks over sample queries · `backend/services/evaluation_service.py`
- [Sample Queries](https://github.com/rahullll101/conversation-rag/blob/llamaindex-rag/evaluation/sample_queries.json): eval dataset · Offline evaluation prompts and references · `evaluation/sample_queries.json`

## Connections

- 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
