# abaoxomtieu/rag-api-vietnamesepdf architecture

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

- Interactive diagram: https://gitdiagram.com/abaoxomtieu/rag-api-vietnamesepdf
- Repository: https://github.com/abaoxomtieu/rag-api-vietnamesepdf

## Overview

##### Main Components
- Data Ingestion & Preprocessing: This includes the PDF files placed in the data/ folder, and the processing scripts (app_transform_data.py, prepare_vector_db.py, remove_vector_store.py) that transform and ingest PDF content.
- Embedding and Vector Storage: The project uses models (located in the models/ directory) such as all-MiniLM-L6-v2-f16.gguf and vinallama-7b-chat_q5_0.gguf. The embeddings are stored in a vector database, with FAISS used for indexing (see vectorstores/db_faiss/).
- API and Q&A Engine: The API.py script leverages FastAPI to allow question-answering on the private PDF data.
- User Interface: The app.py script (using Streamlit) provides a GUI for users to interact with the system, primarily for initiating the transformation process and visualizing outputs.
- Utility Functions: Files such as utils.py (and similar in the "using_LLM&Embeedin_of_GEMINI" folder) support common functionalities across the system.

##### Relationships and Interactions
- The process starts with PDF file ingestion, where raw files (in the data/ folder) are processed by the transformation scripts. The outcome of this transformation is the generation of embedding vectors.
- The embedding vectors are then stored in the vector database (managed by FAISS), making them accessible for similarity queries.
- When a user submits a question (via the FastAPI endpoint or through the Streamlit UI), that question is matched against the stored embeddings.
- The retrieved context from the vector store is fed into a language model (using the provided GGUF models) which constructs the answer.
- Lastly, the answer is returned to the user through the API response or rendered via the Streamlit interface.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Notice clear separation between data ingestion/preprocessing, storage (vector DB), and user interaction (API and UI). This helps in modularizing the components for easier maintenance and scaling.
- Layered Architecture: The system consists of distinct layers (Data Layer, Embedding/Processing Layer, Application Layer) that interact through well-defined interfaces.
- Extensibility: By having utility functions and clearly separated data processing scripts, the project is designed to allow future enhancements, such as integrating additional models, data sources, or different vector databases.

##### Relevant Technologies, Frameworks, and Libraries
- Langchain: For connecting language models and chaining actions.
- FastAPI: As the framework for the API.
- Streamlit: For building the interactive UI.
- FAISS: For creating and managing the vector database.
- CUDA / GPU Libraries: To leverage hardware acceleration for model inference.
- Python: As the primary programming language, with utilities and configurations defined in requirements.txt.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Layer
    subgraph "Data Layer"
        direction TB
        PDF["PDF Files (data/)"]:::data
        T1["app_transform_data.py"]:::script
        T2["prepare_vector_db.py"]:::script
        T3["remove_vector_store.py"]:::script
        ALT_T1["using_LLM&Embeedin_of_GEMINI/app_transform_data.py"]:::script
        ALT_T2["using_LLM&Embeedin_of_GEMINI/prepare_vector_db.py"]:::script
        ALT_T3["using_LLM&Embeedin_of_GEMINI/remove_vector_store.py"]:::script
        
        PDF --> T1
        PDF --> ALT_T1
        T1 --> T2
        T2 --> T3
        ALT_T1 --> ALT_T2
        ALT_T2 --> ALT_T3
    end

    %% Processing & Embedding
    subgraph "Processing & Embedding"
        direction TB
        LM["Language Model Inference\n(GPU: RTX 2060 Max-Q & CUDA 11.8)"]:::gpu
        MODEL["Model: all-MiniLM-L6-v2-f16.gguf"]:::model
        MODEL --> LM
    end

    %% Vector Storage
    subgraph "Vector Storage"
        direction TB
        VDB["FAISS Vector Database"]:::db
        VINDEX["(index.faiss, index.pkl)"]:::db
        VDB --> VINDEX
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        API["FastAPI Q&A Endpoint: API.py"]:::api
        ALT_API["Alternate API: using_LLM&Embeedin_of_GEMINI/API.py"]:::api
        UI["Streamlit UI: app.py"]:::ui
        ALT_UI["Alternate UI: using_LLM&Embeedin_of_GEMINI/app.py"]:::ui
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        UTIL["utils.py"]:::util
        ALT_UTIL["Alternate utils: using_LLM&Embeedin_of_GEMINI/utils.py"]:::util
    end

    %% Data Flow Connections
    T3 --> VDB
    ALT_T3 --> VDB
    VDB --> LM
    LM --> API
    LM --> UI

    %% Utility function associations (non critical flow)
    UTIL --> T1
    UTIL --> API
    ALT_UTIL --> ALT_T1
    ALT_UTIL --> ALT_API

    %% Click Events
    click T1 "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/app_transform_data.py"
    click T2 "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/prepare_vector_db.py"
    click T3 "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/remove_vector_store.py"
    click ALT_T1 "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/using_LLM&Embeedin_of_GEMINI/app_transform_data.py"
    click ALT_T2 "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/using_LLM&Embeedin_of_GEMINI/prepare_vector_db.py"
    click ALT_T3 "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/using_LLM&Embeedin_of_GEMINI/remove_vector_store.py"
    click MODEL "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/models/all-MiniLM-L6-v2-f16.gguf"
    click VDB "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/tree/main/using_LLM&Embeedin_of_GEMINI/vectorstores/db_faiss/"
    click API "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/API.py"
    click ALT_API "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/using_LLM&Embeedin_of_GEMINI/API.py"
    click UI "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/app.py"
    click ALT_UI "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/using_LLM&Embeedin_of_GEMINI/app.py"
    click UTIL "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/utils.py"
    click ALT_UTIL "https://github.com/abaoxomtieu/rag-api-vietnamesepdf/blob/main/using_LLM&Embeedin_of_GEMINI/utils.py"

    %% Styles
    classDef data fill:#FFFAE6,stroke:#E6AC00,stroke-width:2px;
    classDef script fill:#FFF2F2,stroke:#FF6F61,stroke-width:2px;
    classDef gpu fill:#E6F7FF,stroke:#1890FF,stroke-width:2px;
    classDef model fill:#F0FFF0,stroke:#52C41A,stroke-width:2px;
    classDef db fill:#FFF0F6,stroke:#EB2F96,stroke-width:2px;
    classDef api fill:#F9F0FF,stroke:#722ED1,stroke-width:2px;
    classDef ui fill:#F6FFED,stroke:#52C41A,stroke-width:2px;
    classDef util fill:#E6FFFB,stroke:#13C2C2,stroke-width:2px;
```
