# nirdiamant/controllable-rag-agent architecture

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

- Interactive diagram: https://gitdiagram.com/nirdiamant/controllable-rag-agent
- Repository: https://github.com/nirdiamant/controllable-rag-agent

## Overview

##### Main Components
- Data Ingestion & Preprocessing Module:
- PDF Loader/Processor: This component loads PDF documents, splits them into chapters, and pre-processes the text.
- Text Preprocessing and Cleaning: Functions that clean and process the raw text.
- Summarization Pipeline: Uses large language models to generate chapter summaries.

- Vector Store Creation & Management:
- “Book Quotes” Vector Store: Stored in book_quotes_vectorstore (using FAISS and a pickle index).
- “Chapter Summaries” Vector Store: Stored in chapter_summaries_vector_store.
- “Chunks” Vector Store: Stored in chunks_vector_store.
- These containers are the backend for fast retrieval and are represented by FAISS-based indices.

- Question Processing & Reasoning Engine:
- Question Anonymization: Prepares the question by replacing named entities.
- High-Level Planning Module: Generates a plan to answer the anonymized query.
- Task Execution Modules: Decide whether to retrieve or generate responses based on context. This includes retrieving relevant information from the vector stores.
- Chain-of-Thought Reasoning Component: Uses an LLM (integrated via LangChain) to synthesize the final answer using incremental reasoning.
- Re-planning and Verification: Verifies content to avoid hallucinations and refines the plan when needed.

- Visualization & User Interaction:
- Notebooks (full_graph_visualization.ipynb and sophisticated_rag_agent_harry_potter.ipynb): Used for step-by-step walkthroughs and demonstrations.

- Supporting Components and Tools:
- Helper Functions & Pipeline Logic: Contained in files like functions_for_pipeline.py and helper_functions.py.
- Evaluation Metrics Module: Integrates Ragas metrics (answer correctness, faithfulness, relevancy, etc.) for performance assessment.
- Docker & Environment Management: Dockerfile, docker-compose.yml, and environment variable settings indicate containerization and deployment practices.

##### Relationships and Interactions
- Data flows from the ingestion pipeline into the vector store creation modules.
- The reasoning engine interacts with the vector stores to retrieve context based on a processed and anonymized user question.
- The output from the reasoning module passes to the verification stage before the final answer is generated.
- There is an iterative feedback loop between task execution, retrieval, and re-planning processes.
- For visualization, processed data and final outputs are passed to the Streamlit UI for real-time display.
- Docker and environmental configurations manage and encapsulate the complete system for both development and production.

##### Architectural Patterns & Technologies
- The design leverages modular separation of concerns (ingestion, processing, retrieval, reasoning, visualization).
- Utilizes a deterministic graph structure for multi-step reasoning.
- Multiple vector stores (implemented via FAISS) are used as the backbone for retrieval.
- Incorporates third-party libraries and APIs (LangChain for LLM interactions, Streamlit for UI, and Ragas for metrics).
- Dockerization ensures portability and easy deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Ingestion & Preprocessing
    subgraph "Data Ingestion & Preprocessing"
        A["PDF Loader"]:::ingestion
        B["Text Preprocessor"]:::ingestion
        C["Summarizer"]:::ingestion
    end

    %% Vector Stores
    subgraph "Vector Stores"
        D["Book Quotes Vector Store"]:::vector
        E["Chapter Summaries Vector Store"]:::vector
        F["Chunks Vector Store"]:::vector
    end

    %% Question Processing & Reasoning Engine
    subgraph "Question Processing & Reasoning Engine"
        G["Question Anonymizer"]:::reasoning
        H["High-Level Planner"]:::reasoning
        I["Task Execution & Chain-of-Thought Reasoner"]:::reasoning
        J["Verification & Re-planning"]:::reasoning
        K["Final Answer"]:::reasoning
    end

    %% Visualization & User Interaction
    subgraph "Visualization & User Interaction"
        L["User Interface (Streamlit)"]:::ui
        M["Graph Visualization Notebook"]:::ui
        N["RAG Agent Notebook"]:::ui
    end

    %% Supporting & Infrastructure
    subgraph "Supporting & Infrastructure"
        O["Helper Functions & Pipeline Logic"]:::support
        P["Evaluation Metrics Module"]:::support
        Q["Dockerfile"]:::support
        R["Docker Compose"]:::support
    end

    %% External Input
    external["User Query"]:::external

    %% Data Flow Connections
    external --> G
    A --> B
    B --> C
    C --> D
    C --> E
    C --> F
    G --> H
    H --> I
    I --> J
    J -->|"re-planning"| I
    D --> I
    E --> I
    F --> I
    I --> K
    K --> L
    K --> P

    %% Optional links to visualization artifacts
    L --> M
    L --> N

    %% Supporting Relationships (dotted lines)
    O --- A
    O --- G
    O --- I

    %% Click Events
    click A "https://github.com/nirdiamant/controllable-rag-agent/blob/main/functions_for_pipeline.py"
    click B "https://github.com/nirdiamant/controllable-rag-agent/blob/main/helper_functions.py"
    click C "https://github.com/nirdiamant/controllable-rag-agent/blob/main/functions_for_pipeline.py"
    click D "https://github.com/nirdiamant/controllable-rag-agent/tree/main/book_quotes_vectorstore"
    click E "https://github.com/nirdiamant/controllable-rag-agent/tree/main/chapter_summaries_vector_store"
    click F "https://github.com/nirdiamant/controllable-rag-agent/tree/main/chunks_vector_store"
    click G "https://github.com/nirdiamant/controllable-rag-agent/blob/main/helper_functions.py"
    click L "https://github.com/nirdiamant/controllable-rag-agent/blob/main/simulate_agent.py"
    click M "https://github.com/nirdiamant/controllable-rag-agent/blob/main/full_graph_visualization.ipynb"
    click N "https://github.com/nirdiamant/controllable-rag-agent/blob/main/sophisticated_rag_agent_harry_potter.ipynb"
    click Q "https://github.com/nirdiamant/controllable-rag-agent/tree/main/dockerfile"
    click R "https://github.com/nirdiamant/controllable-rag-agent/blob/main/docker-compose.yml"

    %% Styles
    classDef ingestion fill:#edf6ff,stroke:#0a74da,stroke-width:2px;
    classDef vector fill:#e0ffe0,stroke:#228B22,stroke-width:2px;
    classDef reasoning fill:#fffacd,stroke:#c49a00,stroke-width:2px;
    classDef ui fill:#ffe6e6,stroke:#ff0000,stroke-width:2px;
    classDef support fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef external fill:#d3d3d3,stroke:#696969,stroke-width:2px;
```
