# arshnoor-singh-sohi/advanced-rag-system architecture

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

- Interactive diagram: https://gitdiagram.com/arshnoor-singh-sohi/advanced-rag-system
- Repository: https://github.com/arshnoor-singh-sohi/advanced-rag-system

## Overview

##### Project Type and Purpose
- This is a full‐stack Retrieval‐Augmented Generation (RAG) knowledge management system combined with an experimentation and analysis platform.
- The “src/app” directory hosts both a CLI demo (`rag_app.py`) and a Streamlit web UI (`streamlit_app.py`).
- The “src/components” folder implements the core RAG pipeline (chunking, embedding, retrieval, query processing, reranking, generation).
- The “experiments” folder contains scripts to systematically vary RAG subcomponents and log results.
- The “analysis” folder processes experiment outputs into figures and paper tables for academic reporting.

##### Core Architectural Layers
A. Presentation / UI Layer
- CLI interface (`rag_app.py`)
- Web interface (`streamlit_app.py`)
B. Application / Service Layer (RAG Pipeline)
- Document chunking strategies
- Embedding providers
- Retrieval methods
- Query processing (expansion, HyDE)
- Reranking mechanisms
- Generation/prompting
C. Data & Storage Layer
- Corpus loader (`data/`, pickled files)
- Vectorstore handler (FAISS index & metadata)
- File‐based results storage (`results/`)
- Figures & paper tables (`figures/`, `paper_figures/`)
D. Experimentation Orchestration
- `run_experiments.py` driver
- Individual experiment modules (`experiments/*.py`)
- Experiment tracking & logging (`src/utils/experiment_tracker.py`, `experiment_run.log`)
E. Analysis & Reporting
- `analysis/analyze_results.py`
- `analysis/paper_figures.py`
- Generates CSVs, charts, LaTeX tables

##### Component Interactions and Data Flow
1. User submits a query via CLI or Web UI
2. UI invokes the RAG pipeline service
– Input documents are chunked → embeddings generated → vector search (and/or BM25) → candidate passages
– Query transformations or HyDE may augment the query
– Reranking stage refines top candidates
– Prompt + context sent to LLM (OpenAI/HuggingFace) for final answer
3. Answer returned to UI and logged if in “demo” mode
4. In experiment mode, `run_experiments.py` loops over component configurations, invokes the pipeline, writes JSON/CSV to `results/`
5. Analysis scripts consume `results/` → produce recommendation JSONs + performance CSVs in `figures/` → optionally generate IEEE‐style tables in `paper_figures/`

##### Architectural Patterns & Design Principles
- Layered architecture separating UI, service, data, experiment, and analysis
- Pipeline pattern: data flows through a fixed sequence of modular components
- Single source of truth for configuration (`user_config.json`, CLI flags)
- Clear directory separation: `src/components`, `experiments`, `analysis`, `results`

##### Key Technologies & Frameworks
- Python 3.x
- Streamlit for web UI
- FAISS for vector indexing
- SentenceTransformers, HuggingFace, OpenAI APIs for embeddings/LLM calls
- JSON/CSV for config & results
- Experiment tracking via custom `experiment_tracker.py`
- Matplotlib/Seaborn or similar in `analysis` for chart generation

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation / UI Layer
    subgraph "Presentation / UI Layer"
        direction TB
        CLI["CLI Interface\n(rag_app.py)"]:::ui
        Web["Web UI\n(streamlit_app.py)"]:::ui
    end

    %% RAG Pipeline Layer
    subgraph "RAG Pipeline" 
        direction TB
        Orchestrator["RAG Pipeline Orchestrator\n(rag_components.py)"]:::rag
        Chunking["Document Chunking\n(advanced_chunking.py)"]:::rag
        Embeddings["Embedding Providers\n(llm_integrations.py)"]:::rag
        Retrieval["Retrieval / Vector Search\n(vectorstore_handler.py)"]:::rag
        QueryProc["Query Processing\n(enhanced_hyde.py)"]:::rag
        Transforms["Query Transformations\n(query_transformations.py)"]:::rag
        Rerank["Reranking Mechanisms\n(reranking.py)"]:::rag
        AdvRerank["Advanced Reranking\n(advanced_reranking.py)"]:::rag
        Generation["Generation / Prompting\n(llm_integrations.py)"]:::rag
    end

    %% Data & Storage Layer
    subgraph "Data & Storage Layer"
        direction TB
        Corpus["Raw Data Storage\n(data/)"]:::data
        CorpusLoader["Corpus Loader Utilities\n(data_processing.py)"]:::data
        VectorStore["Vectorstore Handler\n(vectorstore_handler.py)"]:::data
        Config["Configuration\n(user_config.json)"]:::data
        Results["Results Storage\n(results/)"]:::data
        FiguresDir["Figures Output\n(figures/)"]:::data
        PaperFigDir["Paper Figures\n(paper_figures/)"]:::data
    end

    %% Experimentation Layer
    subgraph "Experimentation Orchestration"
        direction TB
        ExpDriver["Experiment Driver\n(run_experiments.py)"]:::exp
        ExpModules["Experiment Modules\n(experiments/*.py)"]:::exp
        Tracker["Experiment Tracker\n(src/utils/experiment_tracker.py)"]:::exp
        Utils["General Utilities\n(src/utils/utils.py)"]:::exp
    end

    %% Analysis & Reporting Layer
    subgraph "Analysis & Reporting"
        direction TB
        Analyze["Analyze Results\n(analysis/analyze_results.py)"]:::ana
        PaperFigures["Generate Paper Figures\n(analysis/paper_figures.py)"]:::ana
    end

    %% External Services
    subgraph "External Services"
        direction TB
        LLM["LLM Providers\n(OpenAI/HuggingFace)"]:::ext
        FAISS["FAISS Index\n(persisted)"]:::ext
    end

    %% Connections
    CLI -->|submit query| Orchestrator
    Web -->|submit query| Orchestrator

    Orchestrator -->|chunk docs| Chunking
    Chunking -->|chunks| Embeddings
    Embeddings -->|vectors| Retrieval
    Retrieval -->|candidates| QueryProc
    QueryProc -->|augmented query| Transforms
    Transforms -->|transformed query| Rerank
    Rerank -->|ranked| AdvRerank
    AdvRerank -->|final candidates| Generation
    Generation -->|prompts| LLM
    LLM -->|response| Generation
    Generation -->|answer| Orchestrator
    Orchestrator -->|return answer| CLI
    Orchestrator -->|return answer| Web

    %% Experiment flow
    ExpDriver -->|runs| ExpModules
    ExpDriver -->|uses| Tracker
    ExpDriver -->|writes JSON/CSV| Results
    ExpModules -->|invoke pipeline| Orchestrator

    %% Analysis flow
    Analyze -->|reads| Results
    Analyze -->|produces| FiguresDir
    PaperFigures -->|reads| FiguresDir
    PaperFigures -->|outputs| PaperFigDir

    %% Data interactions
    Orchestrator -->|load corpus| CorpusLoader
    CorpusLoader -->|reads| Corpus
    Retrieval -->|reads/writes| FAISS
    Results -->|stored on disk| Results

    %% Styles
    classDef ui fill:#cfe2f3,stroke:#000,stroke-width:1px
    classDef rag fill:#d9ead3,stroke:#000,stroke-width:1px
    classDef data fill:#d9d9d9,stroke:#000,stroke-width:1px
    classDef exp fill:#fce5cd,stroke:#000,stroke-width:1px
    classDef ana fill:#ead1dc,stroke:#000,stroke-width:1px
    classDef ext fill:#f4cccc,stroke:#000,stroke-width:1px

    %% Click Events
    click CLI "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/app/rag_app.py"
    click Web "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/app/streamlit_app.py"
    click Orchestrator "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/rag_components.py"
    click Chunking "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/advanced_chunking.py"
    click Embeddings "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/llm_integrations.py"
    click Retrieval "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/vectorstore_handler.py"
    click QueryProc "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/enhanced_hyde.py"
    click Transforms "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/query_transformations.py"
    click Rerank "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/reranking.py"
    click AdvRerank "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/advanced_reranking.py"
    click Generation "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/llm_integrations.py"
    click CorpusLoader "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/components/data_processing.py"
    click ExpDriver "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/run_experiments.py"
    click ExpModules "https://github.com/arshnoor-singh-sohi/advanced-rag-system/tree/main/experiments/"
    click Tracker "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/utils/experiment_tracker.py"
    click Utils "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/src/utils/utils.py"
    click Config "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/user_config.json"
    click Corpus "https://github.com/arshnoor-singh-sohi/advanced-rag-system/tree/main/data/"
    click Results "https://github.com/arshnoor-singh-sohi/advanced-rag-system/tree/main/results/"
    click Analyze "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/analysis/analyze_results.py"
    click PaperFigures "https://github.com/arshnoor-singh-sohi/advanced-rag-system/blob/main/analysis/paper_figures.py"
    click FiguresDir "https://github.com/arshnoor-singh-sohi/advanced-rag-system/tree/main/figures/"
    click PaperFigDir "https://github.com/arshnoor-singh-sohi/advanced-rag-system/tree/main/paper_figures/"
```
