# ashishsinha5/rag_api architecture

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

- Interactive diagram: https://gitdiagram.com/ashishsinha5/rag_api
- Repository: https://github.com/ashishsinha5/rag_api

## Overview

##### Project Type and Purpose
- This is a backend service (microservice) for Retrieval Augmented Generation (RAG), exposing a RESTful API via FastAPI.
- It ingests user documents (PDF/HTML), builds a vector database for semantic search, and performs QnA by retrieving relevant chunks and passing them to an open-source LLM (llama.cpp).

##### High-Level Components
a. Client
- SwaggerUI or any HTTP client that sends requests to the API.
b. API Layer (FastAPI)
- main.py: defines endpoints (upload, query, init_llm).
- routes → delegates to service functions in load_data.py, vector_db.py, load_llm.py, prompts.py, utils.py.
c. Vector Database
- vector_db.py: abstracts creation and querying of the vector store (e.g., FAISS or in-memory index).
- Persists document embeddings by collection_name.
d. Document Processing
- load_data.py: handles file ingestion, chunking, text extraction.
- utils.py: shared utilities (e.g., text normalization).
e. LLM Service
- load_llm.py & llama2_config.yaml: initializes llama.cpp with configurable parameters.
- prompts.py: formats retrieved context + user query into a prompt.
f. Data Storage
- Local file system for uploaded docs and vector index files.
g. Configuration
- llama2_config.yaml: default model settings.
- requirements.txt / .gitignore.

##### Interactions & Data Flow
- Upload Document:
Client → POST /upload → FastAPI → load_data.py (extract/chunk) → vector_db.py (store embeddings) → Response.
- Query Document:
Client → POST /query → FastAPI → vector_db.py (retrieve top-N embeddings) → prompts.py (assemble prompt) → load_llm.py (call llama.cpp) → return generated answer.
- Init LLM Parameters:
Client → POST /init_llm → FastAPI → load_llm.py (update llama2_config.yaml and reinitialize model).

##### Architectural Patterns & Principles
- Layered Architecture:
- API Layer (HTTP interface)
- Service Layer (business logic: ingestion, retrieval, prompting)
- Data Layer (vector DB, file storage)
- Integration Layer (llama.cpp engine)
- Single Responsibility: each module (load_data, vector_db, load_llm, prompts) focuses on one concern.
- Configuration-as-Code: llama2_config.yaml drives model settings.

##### Technologies & Frameworks
- FastAPI (API framework + auto-generated SwaggerUI)
- llama.cpp (local LLM inference)
- Langchain (potentially for chaining; not in code but referenced)
- Python 3.x
- Vector store (e.g., FAISS under the hood)
- Uvicorn (ASGI server)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client\n(SwaggerUI/HTTP)"]:::external

    %% API Layer
    subgraph "API Layer"
        direction TB
        API["FastAPI Server\nmain.py"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        LD["Document Processing\nload_data.py"]:::service
        VB["Vector Database Service\nvector_db.py"]:::service
        PR["Prompt Construction\nprompts.py"]:::service
        LL["LLM Init & Invocation\nload_llm.py"]:::service
        UT["Shared Utilities\nutils.py"]:::service
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        FS["File Storage\n(local docs & indexes)"]:::storage
        VDB["Vector Store\n(FAISS/In-memory)"]:::storage
    end

    %% Integration Layer
    subgraph "Integration Layer"
        direction TB
        LLM["LLM Engine\nllama.cpp"]:::llm
        CFG["Config File\nllama2_config.yaml"]:::llm
    end

    %% Dependencies & Docs
    subgraph "Project Files"
        direction TB
        RE["README.md"]:::docs
        IG[".gitignore"]:::docs
        RN["requirements.txt"]:::docs
        DC["docs/"]:::docs
    end

    %% Flows: Upload
    Client -->|"POST /upload"| API
    API -->|"extract & chunk"| LD
    LD -->|"store embeddings"| VB
    VB --> FS
    VB --> VDB
    API -->|"200 OK"| Client

    %% Flows: Query
    Client -->|"POST /query"| API
    API --> VB
    VB -->|"top-N embeddings"| PR
    PR --> LL
    LL --> LLM
    LLM -->|"generated answer"| API
    API -->|"200 OK"| Client

    %% Flows: Init LLM
    Client -->|"POST /init_llm"| API
    API --> LL
    LL --> CFG
    LL --> LLM
    API -->|"200 OK"| Client

    %% Click Events
    click API "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/main.py"
    click LD "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/load_data.py"
    click VB "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/vector_db.py"
    click LL "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/load_llm.py"
    click PR "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/prompts.py"
    click UT "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/utils.py"
    click CFG "https://github.com/ashishsinha5/rag_api/blob/main/src/rag_app/llama2_config.yaml"
    click RN "https://github.com/ashishsinha5/rag_api/blob/main/requirements.txt"
    click IG "https://github.com/ashishsinha5/rag_api/blob/main/.gitignore"
    click DC "https://github.com/ashishsinha5/rag_api/tree/main/docs/"
    click RE "https://github.com/ashishsinha5/rag_api/blob/main/README.md"

    %% Styles
    classDef api fill:#D0E8FF,stroke:#007ACC,color:#000
    classDef service fill:#E8F4FF,stroke:#005A9E,color:#000
    classDef storage fill:#E0F8E0,stroke:#2E8B57,color:#000
    classDef llm fill:#FFE5B4,stroke:#FF8C00,color:#000
    classDef external fill:#F0F0F0,stroke:#A0A0A0,color:#000
    classDef docs fill:#F9F9F9,stroke:#CCCCCC,color:#000
```
