# lehen20/agentic-rag architecture

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

- Interactive diagram: https://gitdiagram.com/lehen20/agentic-rag
- Repository: https://github.com/lehen20/agentic-rag

## Overview

##### Project type and purpose
- This is an Agentic Retrieval-Augmented Generation (RAG) system built in Python.
- It ingests PDFs, embeds them, stores embeddings in LanceDB, retrieves relevant chunks at query time, runs an LLM agent (via Ollama + LangGraph) with tool use and grading, and exposes a Gradio frontend.

##### File structure
- Top-level:
- config/ – application settings (settings.yml)
- db/ – connectors for LanceDB and Redis cache
- docs/ – input PDF knowledge base
- models/ – wrapper around Ollama LLaMA3.1 (“ollama_model.py”)
- pipeline/ – core agent graph and step definitions (agent.py, graph.py, steps.py, retriever_tools.py)
- prompts/ – prompt templates for retrieval, grading and final generation
- src/ – utility modules (pdf_loader.py, embeddings.py)
- main.py – orchestrates startup: loads config, spins up Gradio UI, initializes pipeline
- flow.psql – sample DB schema for relational storage if used
- requirements.txt and .gitignore round out dependencies and ignored files.

##### Key insights from the README
- Tech stack: LangGraph + LangChain for agent orchestration, LanceDB for vector store, Redis for LLM caching, Ollama for LLM, Gradio for UI.
- Flow: PDF ingestion → chunking → embedding → storage → retrieval → agent decision → grading → generation → UI.

##### Final advice
- Every major module: config, db clients, pipeline steps, prompt templates, model wrappers, and main startup.
- Don’t over-complicate—represent each Python module or directory as its own component if it has unique responsibilities.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Storage components
    Documents["Documents (Input PDFs)"]:::storage
    LanceDB["LanceDB\n(Vector Store)"]:::storage
    Redis["Redis Cache"]:::storage

    %% Compute components
    Ingestion["Ingestion Service"]:::compute
    Embedding["Embedding Service"]:::compute
    Retrieval["Retrieval"]:::compute
    AgentGraph["Agent Graph\n(& Orchestration)"]:::compute
    RetrieverTools["Retriever Tools"]:::compute
    PromptTemplates["Prompt Templates"]:::compute
    OllamaModel["Ollama Model Wrapper"]:::external

    %% Frontend and Config
    UI["Gradio Frontend"]:::external
    Config["Configuration\n(settings.yml)"]:::config

    %% Pipeline flow
    Documents -->|ingest PDFs| Ingestion
    Ingestion -->|create chunks| Embedding
    Embedding -->|store embeddings| LanceDB

    UI -->|user query| Redis
    Redis -->|cache hit| UI
    Redis -->|cache miss| Retrieval
    Retrieval -->|fetch top-k| LanceDB
    Retrieval -->|pass chunks| AgentGraph

    AgentGraph -->|use tools| RetrieverTools
    AgentGraph -->|assemble prompt| PromptTemplates
    AgentGraph -->|invoke LLM| OllamaModel
    OllamaModel -->|response| AgentGraph
    AgentGraph -->|final answer| UI
    AgentGraph -->|cache result| Redis

    %% Config injection
    Config --> Ingestion
    Config --> Embedding
    Config --> AgentGraph
    Config --> UI

    %% Click Events
    click Documents "https://github.com/lehen20/agentic-rag/tree/main/docs/"
    click Ingestion "https://github.com/lehen20/agentic-rag/blob/main/src/pdf_loader.py"
    click Embedding "https://github.com/lehen20/agentic-rag/blob/main/src/embeddings.py"
    click LanceDB "https://github.com/lehen20/agentic-rag/blob/main/db/lancedb_client.py"
    click Redis "https://github.com/lehen20/agentic-rag/blob/main/db/redis_cache.py"
    click AgentGraph "https://github.com/lehen20/agentic-rag/blob/main/pipeline/graph.py"
    click AgentGraph "https://github.com/lehen20/agentic-rag/blob/main/pipeline/agent.py"
    click AgentGraph "https://github.com/lehen20/agentic-rag/blob/main/pipeline/steps.py"
    click RetrieverTools "https://github.com/lehen20/agentic-rag/blob/main/pipeline/retriever_tools.py"
    click PromptTemplates "https://github.com/lehen20/agentic-rag/blob/main/prompts/templates.py"
    click OllamaModel "https://github.com/lehen20/agentic-rag/blob/main/models/ollama_model.py"
    click UI "https://github.com/lehen20/agentic-rag/blob/main/main.py"
    click Config "https://github.com/lehen20/agentic-rag/blob/main/config/settings.yml"

    %% Styles
    classDef storage fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef compute fill:#90EE90,stroke:#000,stroke-width:1px
    classDef external fill:#DDA0DD,stroke:#000,stroke-width:1px
    classDef config fill:#F5DEB3,stroke:#000,stroke-width:1px
```
