# nguyenhongsac/rag_chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/nguyenhongsac/rag_chatbot
- Repository: https://github.com/nguyenhongsac/rag_chatbot

## Overview

##### Project Type and Purpose
- This is a Python-based full-stack web application implementing a Retrieval-Augmented Generation (RAG) chatbot.
- The backend uses Flask (app.py) to expose HTTP endpoints; the front-end is a simple static HTML/JS/CSS client under the “Simple_test” folder.
- Core functionality: index a document collection, build or load a vector/graph store, retrieve relevant passages, call an external LLM API, and return generated responses.

##### File Structure Highlights
- Top-level scripts:
- app.py – Flask web server
- main.py – orchestrator or CLI entry point
- indexing_knowledge.py – batch job to ingest and index documents
- llm_test.py – standalone LLM invocation tests
- “components” directory: modular pipeline pieces
- graph.py – knowledge-graph builder/manager
- indexing_pipeline.py – ingestion → embedding → store
- rag_pipeline.py – retrieval + generation orchestration
- llm_model.py – wrapper around external LLM service
- Front-end sample (“Simple_test”): static assets (HTML, CSS, JS) demonstrating how the client consumes the Flask API.

##### Key System Components
a. User’s Browser / Front-end Client
b. Flask Backend (app.py) with REST/WebSocket endpoints
c. RAG Pipeline Modules (components/indexing_pipeline, components/rag_pipeline, components/llm_model)
d. Knowledge Store
– Vector database (e.g., FAISS, Pinecone) or local embedding store
– Graph database or in-memory graph from components/graph.py
e. Batch Indexing Service (indexing_knowledge.py)
f. External LLM Service (OpenAI API or similar)
g. Document Source (static files, external data feeds)

##### Relationships and Data Flow
- Separate flow: Batch Indexer (indexing_knowledge.py) → Indexing Pipeline → Knowledge Store

##### Architectural Patterns and Principles
- Layered/pipeline architecture: ingestion → storage → retrieval → generation → API
- Separation of concerns: distinct modules for indexing, retrieval, LLM interfacing
- Stateless web tier (Flask) vs. stateful data store (vector or graph DB)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Front-end
    subgraph "Front-end Client" 
        direction TB
        Browser["User Browser"]:::frontend
        HTML["index.html"]:::frontend
        CSS["style.css"]:::frontend
        JS["script.js"]:::frontend
        Manifest["site.webmanifest"]:::frontend
    end

    %% Backend
    subgraph "Backend Services"
        direction TB
        FlaskAPI["Flask API\n(app.py)"]:::backend
        MainCLI["CLI Orchestrator\n(main.py)"]:::backend
        BatchIndexer["Batch Indexing Service\n(indexing_knowledge.py)"]:::backend
        LLmTest["LLM Test\n(llm_test.py)"]:::backend
    end

    %% Components
    subgraph "RAG Components"
        direction TB
        IndexingPipeline["Indexing Pipeline\n(components/indexing_pipeline.py)"]:::pipeline
        RAGPipeline["RAG Pipeline\n(components/rag_pipeline.py)"]:::pipeline
        LLMModel["LLM Model Wrapper\n(components/llm_model.py)"]:::pipeline
        GraphManager["Graph Builder\n(components/graph.py)"]:::pipeline
    end

    %% Data Stores
    subgraph "Knowledge Stores"
        direction TB
        VectorDB["Vector Database"]:::db
        GraphDB["Graph Database"]:::db
    end

    %% External
    subgraph "External Services"
        direction TB
        OpenAI["OpenAI API"]:::external
    end

    %% Flows
    Browser -->|HTTP JSON| FlaskAPI
    HTML -->|served via| FlaskAPI
    CSS -->|served via| FlaskAPI
    JS -->|served via| FlaskAPI
    Manifest -->|served via| FlaskAPI

    FlaskAPI -->|invoke| RAGPipeline
    RAGPipeline -->|retrieve embeddings| VectorDB
    RAGPipeline -->|retrieve graph nodes| GraphDB
    RAGPipeline -->|call| LLMModel
    LLMModel -->|API request| OpenAI
    OpenAI -->|API response| LLMModel
    LLMModel -->|return response| RAGPipeline
    RAGPipeline -->|send reply| FlaskAPI
    FlaskAPI -->|HTTP JSON| Browser

    BatchIndexer -->|start| IndexingPipeline
    IndexingPipeline -->|index to| VectorDB
    IndexingPipeline -->|index to| GraphDB

    LLmTest -->|invoke| LLMModel

    MainCLI -->|option: index| BatchIndexer
    MainCLI -->|option: run| FlaskAPI

    %% Click Events
    click Browser "https://github.com/nguyenhongsac/rag_chatbot/blob/main/Simple_test/index.html"
    click HTML "https://github.com/nguyenhongsac/rag_chatbot/blob/main/Simple_test/index.html"
    click CSS "https://github.com/nguyenhongsac/rag_chatbot/blob/main/Simple_test/css/style.css"
    click JS "https://github.com/nguyenhongsac/rag_chatbot/blob/main/Simple_test/js/script.js"
    click Manifest "https://github.com/nguyenhongsac/rag_chatbot/blob/main/Simple_test/assets/site.webmanifest"
    click FlaskAPI "https://github.com/nguyenhongsac/rag_chatbot/blob/main/app.py"
    click MainCLI "https://github.com/nguyenhongsac/rag_chatbot/blob/main/main.py"
    click BatchIndexer "https://github.com/nguyenhongsac/rag_chatbot/blob/main/indexing_knowledge.py"
    click LLmTest "https://github.com/nguyenhongsac/rag_chatbot/blob/main/llm_test.py"
    click RAGPipeline "https://github.com/nguyenhongsac/rag_chatbot/blob/main/components/rag_pipeline.py"
    click IndexingPipeline "https://github.com/nguyenhongsac/rag_chatbot/blob/main/components/indexing_pipeline.py"
    click LLMModel "https://github.com/nguyenhongsac/rag_chatbot/blob/main/components/llm_model.py"
    click GraphManager "https://github.com/nguyenhongsac/rag_chatbot/blob/main/components/graph.py"

    %% Styles
    classDef frontend fill:#E8F1FC,stroke:#2B6CB0,stroke-width:2px
    classDef backend fill:#F0F4C3,stroke:#7CB342,stroke-width:2px
    classDef pipeline fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px
    classDef db fill:#E0F7FA,stroke:#006064,stroke-width:2px
    classDef external fill:#FCE4EC,stroke:#C2185B,stroke-width:2px
```
