# amgad-abdelkhaleq/pdf-rag architecture

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

- Interactive diagram: https://gitdiagram.com/amgad-abdelkhaleq/pdf-rag
- Repository: https://github.com/amgad-abdelkhaleq/pdf-rag

## Overview

##### Main Components
a. Frontend – a Streamlit application (located under src/ui/streamlit_app.py) that provides the user interface.
b. Backend – a FastAPI-based API handling routes for chat and document management (see src/api/routes and main.py).
c. Document Processing Pipeline – components responsible for PDF upload, text extraction, chunking, and indexing. In the README, these are represented by nodes like “PDF Processor”, “Chunking Service” and “Indexing Service.”
d. Embedding and Vector Storage – services for generating embeddings (using all-MiniLM-L6-v2) and storing them in a vector store (Qdrant), supported by the src/repositories/vector_store.py.
e. Query Processing & LLM Integration – processes that receive a user query, generate an embedding, retrieve context from the vector store, and then use a LLM (TinyLlama) for generating responses. This involves services like the rag_service and chat_history_service.
f. Supporting Modules – configuration (src/core/config.py), dependency management (src/core/dependencies.py), and data models (src/models).

##### Relationships and Interactions
a. The Frontend (Streamlit UI) sends user interactions to the Backend (FastAPI API).
b. The API routes handle document uploads and chat interactions, interacting with the services.
c. For documents: the API triggers PDF processing, which passes through text extraction and chunking, then to the embedding service before storing data in the Vector Store.
d. For queries: the API receives the user query, processes it through the embedding service to match against stored embeddings, retrieves context, and forwards the request to the LLM service with RAG logic.

##### Architectural Patterns and Design Principles
a. Separation of Concerns: The project clearly separates UI, API routes, core logic, services, and data models.
b. Modularity: Services such as document service, embedding service, chat history service, and RAG service are split into different modules (each in src/services).
c. Pipeline Processing: The document processing (from PDF upload to vector storage) and query processing (user query to LLM response) represent sequential workflows.
d. API-Driven Architecture: FastAPI orchestrates interactions between different internal components and external dependencies (like the vector store or LLM).

##### Final Assembly and Detailing
- Detail out the API endpoints supported, noting that one handles document uploads while another handles chat interactions.
- Optionally, annotate with technologies (e.g., “Built with FastAPI”, “Powered by LangChain”, “Using Qdrant for vector storage”) to provide clarity on the tech stack used.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend
    UI["Streamlit UI"]:::frontend

    %% Backend
    API["FastAPI API"]:::backend

    UI -->|"sends_request"| API

    %% Document Processing Pipeline
    subgraph "Document Processing Pipeline"
        D1["Document Upload"]:::processing
        D2["PDF Processor"]:::processing
        D3["Text Extraction"]:::processing
        D4["Text Chunking"]:::processing
        D5["Embedding Generation"]:::processing
        D6["Qdrant Vector Store"]:::external
    end

    %% Query Processing Pipeline
    subgraph "Query Processing Pipeline"
        Q1["User Query"]:::processing
        Q2["Query Embedding"]:::processing
        Q3["Context Retrieval"]:::processing
        Q4["LLM Service (TinyLlama)/RAG"]:::processing
        Q5["Response Generation"]:::processing
    end

    %% Core Modules
    subgraph "Core Modules"
        CM1["Configuration"]:::support
        CM2["Dependencies"]:::support
        CM3["Data Models"]:::support
    end

    %% Connections for Document Processing
    API -->|"handles_document"| D1
    D1 -->|"triggers_processing"| D2
    D2 -->|"performs_extraction"| D3
    D3 -->|"performs_chunking"| D4
    D4 -->|"generates_embeddings"| D5
    D5 -->|"stores_embeddings"| D6

    %% Connections for Query Processing
    API -->|"handles_query"| Q1
    Q1 -->|"embeds_query"| Q2
    Q2 -->|"retrieves_context"| Q3
    Q3 -->|"invokes_LLM"| Q4
    Q4 -->|"generates_response"| Q5
    Q5 -->|"returns_response"| API

    %% Connections to Core Modules
    API -->|"loads_config"| CM1
    API -->|"injects_dependencies"| CM2
    API -->|"uses_models"| CM3

    %% Click Events
    click UI "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/ui/streamlit_app.py"
    click API "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/main.py"
    click D2 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/services/document_service.py"
    click D5 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/services/embedding_service.py"
    click D6 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/repositories/vector_store.py"
    click Q4 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/services/rag_service.py"
    click Q5 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/services/chat_history_service.py"
    click CM1 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/core/config.py"
    click CM2 "https://github.com/amgad-abdelkhaleq/pdf-rag/blob/main/src/core/dependencies.py"
    click CM3 "https://github.com/amgad-abdelkhaleq/pdf-rag/tree/main/src/models"

    %% Styles
    classDef frontend fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef backend fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef processing fill:#C1E1C1,stroke:#333,stroke-width:2px;
    classDef external fill:#F7A1A1,stroke:#333,stroke-width:2px;
    classDef support fill:#EEDDEE,stroke:#333,stroke-width:2px;
```
