# siddhesh-desai/legalaibot architecture

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

- Interactive diagram: https://gitdiagram.com/siddhesh-desai/legalaibot
- Repository: https://github.com/siddhesh-desai/legalaibot

## Overview

##### Project type and purpose
- This is a full-stack, AI-driven RAG (Retrieval-Augmented Generation) application called LegalAIBot.
- It ingests legal PDFs, stores embeddings in Pinecone (vector DB), and serves a Streamlit web UI that answers legal queries using Langchain, a ChatOpenRouter wrapper, and external LLM services.

##### File structure
- Top-level scripts and modules:
- ingest_data.py & DataIngestor/DataIngestor.py – PDF ingestion → Pinecone.
- LegalAIBot/LegalAIBot.py – core RAG pipeline logic.
- ChatOpenRouter/chat_open_router.py – wrapper for calls to the OpenRouter LLM API.
- app.py – Streamlit front end.
- main.py – example CLI usage of the bot.
- constants.py – shared configuration keys.
- Config & environment: .env.example, .devcontainer/devcontainer.json, requirements.txt.
- Assets: files/*.pdf, media/legalaibot.excalidraw.

##### README
- Confirms use of Pinecone, Langchain RAG pipeline, LangSmith tracing, OpenRouter, HF tokens, Google API.
- Usage: run ingest_data.py to populate Pinecone, then streamlit run app.py to launch UI.

##### Detail
- Configuration loading (.env) as a side-component feeding into all modules.
- Keep it high-level but granular enough so developers see where to hook new modules or replace services.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Layer
    subgraph "Data Layer"
        Files["Source PDFs"]:::dataInfra
        DB["Pinecone Vector DB"]:::dataInfra
    end

    %% Ingestion Layer
    subgraph "Ingestion Layer"
        IngestScript["ingest_data.py"]:::module
        DataIngestor["DataIngestor Module"]:::module
        EmbeddingService["Embedding Generator"]:::module
    end

    %% Application Layer
    subgraph "Application Layer"
        RAG["LegalAIBot (RAG Pipeline)"]:::module
        Router["ChatOpenRouter Adapter"]:::module
        Config["Configuration Loader"]:::module
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        UI["Streamlit UI"]:::module
        CLI["CLI Example"]:::module
    end

    %% External Services
    subgraph "External Services"
        PineconeAPI["Pinecone API"]:::external
        OpenRouterAPI["OpenRouter LLM API"]:::external
        LangSmith["LangSmith Tracing"]:::external
        HF["HuggingFace API"]:::external
        GoogleAPI["Google API"]:::external
    end

    %% Data Flow
    Files -->|reads PDFs| IngestScript
    IngestScript -->|uses| DataIngestor
    DataIngestor -->|generates embeddings| EmbeddingService
    EmbeddingService -->|upserts vectors| DB
    DB -->|serves vectors| RAG

    %% Application Flow
    UI -->|user query| RAG
    CLI -->|user query| RAG
    RAG -->|builds prompt| Router
    Router -->|LLM request| OpenRouterAPI
    Router -->|traces calls| LangSmith
    RAG -->|vector search via API| PineconeAPI
    RAG -->|optional enrichment| GoogleAPI
    Router -->|uses HF models| HF

    %% Config Relationships
    Config --> IngestScript
    Config --> DataIngestor
    Config --> RAG
    Config --> Router
    Config --> UI
    Config --> CLI

    %% Click Events
    click Files "https://github.com/siddhesh-desai/legalaibot/tree/main/files/"
    click IngestScript "https://github.com/siddhesh-desai/legalaibot/blob/main/ingest_data.py"
    click DataIngestor "https://github.com/siddhesh-desai/legalaibot/blob/main/DataIngestor/DataIngestor.py"
    click RAG "https://github.com/siddhesh-desai/legalaibot/blob/main/LegalAIBot/LegalAIBot.py"
    click Router "https://github.com/siddhesh-desai/legalaibot/blob/main/ChatOpenRouter/chat_open_router.py"
    click Config "https://github.com/siddhesh-desai/legalaibot/blob/main/constants.py"
    click UI "https://github.com/siddhesh-desai/legalaibot/blob/main/app.py"
    click CLI "https://github.com/siddhesh-desai/legalaibot/blob/main/main.py"
    click Config "https://github.com/siddhesh-desai/legalaibot/blob/main/.env.example"
    click Config "https://github.com/siddhesh-desai/legalaibot/blob/main/.devcontainer/devcontainer.json"

    %% Styles
    classDef dataInfra fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef module fill:#c8e6c9,stroke:#43a047,color:#1b5e20
    classDef external fill:#ffe0b2,stroke:#fb8c00,color:#e65100
```
