# amitgupta4407/rag architecture

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

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

## Overview

##### Project Type and Purpose
- This is a full-stack, modular Retrieval-Augmented Generation (RAG) pipeline with:
- A Python backend that ingests PDF documents, chunks text, creates embeddings, stores/retrieves vectors, and generates answers via LLMs
- A Streamlit-based frontend for file upload, query input, and chat display
- It uses ChromaDB for vector storage and LangChain (plus Ollama/Gemini) for retrieval and generation.

##### Top-Level Components
a. UI Layer (Streamlit)
- streamlit_app.py (orchestrator)
- components/file_upload.py (PDF upload widget)
- components/chat_interface.py (chat history & response display)
- components/settings.py (model/embedding options)
- styles/main.css (styling)
b. Processing Layer
- pdf_processor.py (extract text from PDFs)
- text_chunker.py (split text into retrievable chunks)
c. Models Layer
- embedding_handler.py (call SBERT embeddings)
- llm_handler.py (wrap Ollama/Gemini APIs via LangChain)
d. RAG Orchestration Layer
- retriever.py (query vector store for relevant chunks)
- generator.py (feed chunks + user query to LLM for answer)
e. Storage Layer (ChromaDB)
- vector_store.py (persist and query embeddings)
- document_store.py (metadata and mapping from chunk to source)
f. Removed Modules (per instructions)
- config.py (configuration loader)
- utils/logger.py (logging utility)

##### Relationships and Data Flow
1. User uploads one or more PDF files via the UI
2. UI calls pdf_processor → text_chunker → embedding_handler
3. embedding_handler stores vectors & metadata in vector_store & document_store
4. User enters a query in the UI
5. UI forwards query to retriever → fetches top-K chunks from vector_store
6. retriever passes chunks + query to generator → llm_handler
7. llm_handler returns a grounded answer
8. UI displays answer in chat_interface

##### Architectural Patterns and Principles
- Pipeline pattern for document ingestion
- Separation of concerns: UI / Processing / Models / RAG / Storage
- Modular handlers to swap embedding or LLM backends
- Stateless services: each module receives input and returns output without global state (except ChromaDB)

## Diagram (Mermaid)

```mermaid
graph TB
    %% UI Layer
    subgraph "UI Layer"
        direction TB
        StreamlitApp["Streamlit App"]:::ui
        FileUpload["File Upload"]:::ui
        ChatInterface["Chat Interface"]:::ui
        Settings["Settings"]:::ui
    end

    %% Processing Layer
    subgraph "Processing Layer"
        direction TB
        PDFProcessor["PDF Processor"]:::processing
        TextChunker["Text Chunker"]:::processing
    end

    %% Models Layer
    subgraph "Models Layer"
        direction TB
        EmbeddingHandler["Embedding Handler"]:::processing
        LLMHandler["LLM Handler"]:::processing
    end

    %% RAG Orchestration Layer
    subgraph "RAG Orchestration Layer"
        direction TB
        Retriever["Retriever"]:::processing
        Generator["Generator"]:::processing
    end

    %% Storage Layer
    subgraph "Storage Layer"
        direction TB
        VectorStore["Vector Store"]:::storage
        DocumentStore["Document Store"]:::storage
    end

    %% Data Flow
    StreamlitApp -->|upload PDFs| FileUpload
    FileUpload -->|files| PDFProcessor
    PDFProcessor -->|raw text| TextChunker
    TextChunker -->|text chunks| EmbeddingHandler
    EmbeddingHandler -->|embeddings| VectorStore
    EmbeddingHandler -->|metadata| DocumentStore

    ChatInterface -->|query| Retriever
    Retriever -->|retrieve embeddings| VectorStore
    Retriever -->|retrieve metadata| DocumentStore
    Retriever -->|top-K chunks| Generator
    Generator -->|chunks+query| LLMHandler
    LLMHandler -->|response| ChatInterface

    %% Click Events
    click StreamlitApp "https://github.com/amitgupta4407/rag/blob/main/ui/streamlit_app.py"
    click FileUpload "https://github.com/amitgupta4407/rag/blob/main/ui/components/file_upload.py"
    click ChatInterface "https://github.com/amitgupta4407/rag/blob/main/ui/components/chat_interface.py"
    click Settings "https://github.com/amitgupta4407/rag/blob/main/ui/components/settings.py"
    click PDFProcessor "https://github.com/amitgupta4407/rag/blob/main/src/processing/pdf_processor.py"
    click TextChunker "https://github.com/amitgupta4407/rag/blob/main/src/processing/text_chunker.py"
    click EmbeddingHandler "https://github.com/amitgupta4407/rag/blob/main/src/models/embedding_handler.py"
    click LLMHandler "https://github.com/amitgupta4407/rag/blob/main/src/models/llm_handler.py"
    click Retriever "https://github.com/amitgupta4407/rag/blob/main/src/rag/retriever.py"
    click Generator "https://github.com/amitgupta4407/rag/blob/main/src/rag/generator.py"
    click VectorStore "https://github.com/amitgupta4407/rag/blob/main/src/storage/vector_store.py"
    click DocumentStore "https://github.com/amitgupta4407/rag/blob/main/src/storage/document_store.py"

    %% Styles
    classDef ui fill:#e0f7fa,stroke:#006064,stroke-width:1px
    classDef processing fill:#bbdefb,stroke:#0d47a1,stroke-width:1px
    classDef storage fill:#ffe0b2,stroke:#e65100,stroke-width:1px

    %% Legend
    subgraph "Legend"
        direction TB
        LegendUI["UI Components"]:::ui
        LegendProc["Processing/Models/RAG"]:::processing
        LegendStore["Storage"]:::storage
    end
```
