# swagath18/rag-pdf-assistant architecture

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

- Interactive diagram: https://gitdiagram.com/swagath18/rag-pdf-assistant
- Repository: https://github.com/swagath18/rag-pdf-assistant

## Overview

##### Project Type and Purpose
- This is a local RAG-based PDF chatbot application – it uses Streamlit for the frontend/UI, processes uploaded PDF documents, uses FAISS for vector similarity search, leverages Hugging Face embeddings for converting text into vectors, and employs a local LLM (via Ollama’s Llama 2) for generating responses.
- The purpose is to allow users to upload PDFs, extract their content, generate and store embeddings, and subsequently query the content locally without any remote API calls.

##### Main Components
- User Interface (UI) / Frontend: Represented by Streamlit components as depicted in "pdf_chatbot/app.py".
- PDF Processing Module: Includes text extraction and chunking. This can be broken down into:
- PDF processing (similar to process_pdf.py and text_chunking.py).
- Utility configurations defined in config.py.
- Vector Store / FAISS Index Management: Responsible for creating, updating, and querying the FAISS vector database (faiss_index and vector_store.py).
- LLM Inference Module: Uses Ollama (Llama 2) for processing queries and generating answers (analogous to qa_system.py / llm_inference.py in the README structure).
- Auxiliary Components: Other utility modules that support configuration and integration between components.

##### Relationships and Data Flow
- User Interaction: The user uploads one or more PDFs and sets parameters (like chunk size) via the Streamlit UI.
- PDF Upload → PDF Processing: Documents are processed (extraction and chunking) within the PDF processing module.
- Processed Text → Embedding Generation: Text chunks are then converted to embeddings using Hugging Face models.
- Embeddings → FAISS Index: Generated embeddings are stored in or queried against the FAISS index – acting as the vector database.
- Query Handling: When the user asks a question, the system retrieves relevant text chunks from the FAISS vector store.
- Retrieved Data → LLM Inference: The retrieved chunks, along with the query, are sent to the local LLM module running Ollama (Llama 2) to generate the answer.
- Answer Display: The answer is then displayed in real time to the user via the Streamlit UI.

##### Architectural Patterns & Technologies
- Modular Design: The project is separated into distinct modules that encapsulate specific functions (PDF processing, vector store management, LLM inference).
- Separation of Concerns: UI, processing, and data management all reside in different parts of the project, ensuring clarity in roles.
- RAG Pattern: Clearly, the retrieval (FAISS vector search) and generation (LLM inference) are at the heart of the project, which is a classic RAG implementation.
- Technologies/Libraries in Use:
- Streamlit for interactive web UI.
- PyPDF2 for text extraction.
- FAISS for vector search capabilities.
- Hugging Face models for obtaining text embeddings.
- Ollama (Llama 2) for local inference that provides the chatbot’s response.

## Diagram (Mermaid)

```mermaid
flowchart TD
    User("User"):::ui
    UI("Streamlit UI (pdf_chatbot/app.py)"):::ui
    Config("Configuration & Utility\n(pdf_chatbot/utils/config.py)"):::utility

    %% User interaction
    User -->|"initiates"| UI

    %% Upload Flow Subgraph
    subgraph "Upload Flow"
        PDFEx("PDF Extraction\n(pdf_chatbot/utils/pdf_processing.py)"):::processing
        TextCh("Text Chunking\n(pdf_chatbot/utils/text_chunking.py)"):::processing
        Embed("Embedding Generation\n(using Hugging Face)"):::processing
        VectorStore("Vector Store / FAISS\n(pdf_chatbot/utils/vector_store.py)"):::store
        FAISSFiles("FAISS Files\n(faiss_index/index.faiss & index.pkl)"):::store
    end

    %% Query Flow Subgraph
    subgraph "Query Flow"
        Query("User Query Input"):::ui
        LLM("LLM Inference\n(pdf_chatbot/utils/qa_system.py)"):::inference
    end

    %% Upload Flow Data Flow
    UI -->|"uploadPDF"| PDFEx
    PDFEx --> TextCh
    TextCh --> Embed
    Embed --> VectorStore
    VectorStore -->|"stores data"| FAISSFiles
    
    %% Config usage in Upload Flow
    Config ---|"configures"| PDFEx
    Config --- TextCh
    Config --- VectorStore

    %% Query Flow Data Flow
    UI -->|"submitQuery"| Query
    Query -->|"searches"| VectorStore
    VectorStore -->|"retrieves"| LLM
    LLM -->|"responds"| UI

    %% Styles
    classDef ui fill:#FFD580,stroke:#FF8C00,stroke-width:2px;
    classDef processing fill:#AED6F1,stroke:#3498DB,stroke-width:2px;
    classDef store fill:#A9DFBF,stroke:#28B463,stroke-width:2px;
    classDef inference fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
    classDef utility fill:#F9E79F,stroke:#F1C40F,stroke-dasharray: 5,5,stroke-width:2px;

    %% Click Events
    click UI "https://github.com/swagath18/rag-pdf-assistant/blob/main/pdf_chatbot/app.py"
    click PDFEx "https://github.com/swagath18/rag-pdf-assistant/blob/main/pdf_chatbot/utils/pdf_processing.py"
    click TextCh "https://github.com/swagath18/rag-pdf-assistant/blob/main/pdf_chatbot/utils/text_chunking.py"
    click Config "https://github.com/swagath18/rag-pdf-assistant/blob/main/pdf_chatbot/utils/config.py"
    click VectorStore "https://github.com/swagath18/rag-pdf-assistant/blob/main/pdf_chatbot/utils/vector_store.py"
    click FAISSFiles "https://github.com/swagath18/rag-pdf-assistant/blob/main/faiss_index/index.faiss"
    click LLM "https://github.com/swagath18/rag-pdf-assistant/blob/main/pdf_chatbot/utils/qa_system.py"
```
