# saina-panda12/legal_qa architecture

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

- Interactive diagram: https://gitdiagram.com/saina-panda12/legal_qa
- Repository: https://github.com/saina-panda12/legal_qa

## Overview

##### Project Type and Purpose
- This is a backend-only, Retrieval-Augmented Generation (RAG) service for Legal QA.
- It exposes REST endpoints via FastAPI, orchestrates NER and semantic search over legal documents, then generates answers using an LLM pipeline (LangChain + HuggingFace).

##### File Structure Analysis
Top-level modules:
- main.py, app.py – FastAPI application entrypoints
- auth_routes.py – authentication endpoints (JWT)
- chat_routes.py – conversational QA endpoints
- doc_routes.py – document ingestion/search endpoints
- database.py, collection.py, rag.db – persistence (vector DB & metadata)
- embedding_model.py, vectorization.py – embedding generator
- q_client.py – Qdrant client wrapper
- model.py, response_model1.py, response_model2.py, schemas.py – Pydantic models & response schemas
- jwt.py – JWT token utilities
- logging_config.py – centralized logging setup

##### README Insights
- Named Entity Recognition (NER) for legal entities
- Sentence-Transformer embeddings stored in Qdrant
- LangChain orchestrates retrieval + LLM generation
- API deployed via FastAPI

a. Main Components
1) Client(s) (e.g., Web UI, CLI)
2) FastAPI Service
– auth_routes.py
– doc_routes.py
– chat_routes.py
3) Service Layer
– JWT auth (jwt.py)
– NER & preprocessing (embedding_model.py + vectorization.py)
– LangChain pipeline (model.py + response_model*.py)
4) Data Layer
– Qdrant Vector DB (q_client.py + collection.py)
– rag.db (SQLite or metadata store via database.py)
5) External Services
– HuggingFace / Sentence-Transformers model repo
– Qdrant server (vector storage)

b. Relationships & Data Flow
1) HTTP Request → FastAPI endpoint (auth / chat / doc)
2) Auth endpoint uses jwt.py → returns access token
3) Document endpoint
- NER extraction → creates embeddings → stores to Qdrant via q_client.py
4) Chat endpoint
- Receives query → vectorizes query → Qdrant similarity search → retrieves top-k docs
- Feeds docs + query into LangChain chain → LLM generates answer
5) FastAPI serializes response via Pydantic models → returns JSON

c. Architectural Patterns
– Layered: API layer, Service/Orchestration layer, Data layer
– RAG pipeline pattern (Retrieve → Augment → Generate)
– Modular routing (auth, doc, chat)
– Dependency injection (FastAPI Depends)

d. Key Technologies
– FastAPI (API framework)
– Pydantic (schemas & validation)
– JWT (authentication)
– Sentence-Transformers (embeddings)
– Qdrant (vector DB)
– LangChain (pipeline orchestration)
– HuggingFace LLMs

##### Final Advice
- Be exhaustive: show all major modules and how they interact.
- Keep it clear: group related files into logical clusters (e.g., routing, models, data).
- Avoid clutter: collapse trivial utilities (logging_config.py) into a “Utilities” grouping.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Client["Client (Web UI/CLI)"]:::external

    %% API Layer
    subgraph "API Layer"
        direction TB
        App["app.py"]:::api
        Main["main.py"]:::api
        Auth["auth_routes.py"]:::api
        Doc["doc_routes.py"]:::api
        Chat["chat_routes.py"]:::api
        subgraph Utilities
            direction TB
            Log["logging_config.py"]:::utility
            Init["__init__.py"]:::utility
        end
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        JWT["jwt.py"]:::service
        subgraph "NER & Embedding"
            direction TB
            Embed["embedding_model.py"]:::service
            Vector["vectorization.py"]:::service
        end
        subgraph "RAG Pipeline"
            direction TB
            Model["model.py"]:::service
            Resp1["response_model1.py"]:::service
            Resp2["response_model2.py"]:::service
            Schema["schemas.py"]:::service
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        QClient["q_client.py"]:::data
        Coll["collection.py"]:::data
        DBCode["database.py"]:::data
        SQLite["rag.db"]:::data
        QDB["Qdrant Vector Store"]:::db
    end

    %% External Services
    subgraph "External Services"
        direction TB
        HF["HuggingFace Models"]:::external
        QServer["Qdrant Server"]:::external
    end

    %% Connections
    Client -->|HTTP Request| Auth
    Client -->|HTTP Request| Doc
    Client -->|HTTP Request| Chat

    Auth -->|Validate Token| JWT
    JWT -->|Return Token| Auth

    Doc -->|Receive & Preprocess| Embed
    Embed -->|Generate Embeddings| Vector
    Vector -->|Upsert Vectors| QClient
    QClient -->|Store Vectors| QDB
    QClient -->|Call Qdrant API| QServer

    Chat -->|Receive Query| Vector
    Vector -->|Embed Query| QClient
    QClient -->|Search Similarity| QDB
    QClient -->|Call Qdrant API| QServer
    QDB -->|Return Docs| Model
    Model -->|Use Schemas| Schema
    Model -->|Format Response| Resp1
    Model -->|Format Response| Resp2
    Model -->|Invoke LLM| HF
    HF -->|Generate Answer| Model
    Model -->|Return Answer| Chat

    %% Click Events
    click App "https://github.com/saina-panda12/legal_qa/blob/main/app.py"
    click Main "https://github.com/saina-panda12/legal_qa/blob/main/main.py"
    click Auth "https://github.com/saina-panda12/legal_qa/blob/main/auth_routes.py"
    click Doc "https://github.com/saina-panda12/legal_qa/blob/main/doc_routes.py"
    click Chat "https://github.com/saina-panda12/legal_qa/blob/main/chat_routes.py"
    click JWT "https://github.com/saina-panda12/legal_qa/blob/main/jwt.py"
    click Embed "https://github.com/saina-panda12/legal_qa/blob/main/embedding_model.py"
    click Vector "https://github.com/saina-panda12/legal_qa/blob/main/vectorization.py"
    click Model "https://github.com/saina-panda12/legal_qa/blob/main/model.py"
    click Resp1 "https://github.com/saina-panda12/legal_qa/blob/main/response_model1.py"
    click Resp2 "https://github.com/saina-panda12/legal_qa/blob/main/response_model2.py"
    click Schema "https://github.com/saina-panda12/legal_qa/blob/main/schemas.py"
    click QClient "https://github.com/saina-panda12/legal_qa/blob/main/q_client.py"
    click Coll "https://github.com/saina-panda12/legal_qa/blob/main/collection.py"
    click DBCode "https://github.com/saina-panda12/legal_qa/blob/main/database.py"
    click SQLite "https://github.com/saina-panda12/legal_qa/blob/main/rag.db"
    click Log "https://github.com/saina-panda12/legal_qa/blob/main/logging_config.py"
    click Init "https://github.com/saina-panda12/legal_qa/blob/main/__init__.py"

    %% Styles
    classDef api fill:#cce5ff,stroke:#007bff,stroke-width:1px;
    classDef service fill:#d4edda,stroke:#28a745,stroke-width:1px,shape:circle;
    classDef data fill:#fff3cd,stroke:#fd7e14,stroke-width:1px;
    classDef db fill:#ffeeba,stroke:#fd7e14,stroke-width:1px,shape:cylinder;
    classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:1px,shape:cloud;
    classDef utility fill:#f8f9fa,stroke:#6c757d,stroke-width:1px;
```
