A production-grade Sovereign AI assistant designed to provide expert-level legal advisory on the Constitution of India and updated Bharatiya Nyaya Sanhita (BNS) codes.
The system is built on a modular Monolith-over-Microservices architecture, leveraging LangGraph for deterministic state-controlled RAG orchestration.
- LangGraph State Machine: Deterministic flow control using a 4-node state graph (CLASSIFY → RETRIEVE → GENERATE → POST_PROCESS).
- Parent-Child Chunking: Implemented recursive character splitting with Jina AI embeddings. Child chunks (small) optimize vector search retrieval, while parent chunks (contextual) provide the LLM with surrounding knowledge to mitigate hallucinations.
- Confidence-Gated HITL Fallback: Dynamic thresholding (80% confidence cutoff). Automatically triggers an interactive Human-in-the-Loop Web Search via Tavily when the Vector DB similarity score is too low, effectively eliminating hallucination for out-of-database queries.
- Dual Search Strategy: Hybrid retrieval combining semantic vector search with keyword-based expansion for specific legal terminology (e.g., FIR, BNS Sections).
- Intent Classification Guardrails: LangGraph nodes autonomously identify Prompt Injections, Time-Sensitive questions, and Out-of-Scope (OOS) queries before they reach the LLM, redirecting them to secure fallbacks.
- PII Masking Integration: Leverages Microsoft Presidio + spaCy to detect and anonymize names, phone numbers, and identifying data before transmission to the LLM.
- Circuit Breaker Pattern: Integrated
pybreakeraround external LLM calls (fail_max=10) to ensure system stability during upstream provider outages. - Rate Limiting: IP-based rate limiting (5 req/min) via SlowAPI to prevent API abuse and control operational costs.
- Automated Re-indexing: Custom engine using SHA-256 hash comparison between Supabase Storage and a PostgreSQL registry.
- Orphan Cleanup: Automatic detection and deletion of vector embeddings for files removed from the knowledge base.
- Admin Dashboard: Real-time monitoring of document status (Indexed, Pending, Deleted) with UI-driven sync triggers.
- Three-Tier Caching: Response caching, active user tracking, and stream simulation via Upstash Redis.
- Observability: Real-time tracing of every user intent and LLM inference chain using Langfuse.
- GDPR Compliance: MongoDB-backed chat history with automatic 30-day TTL index for data-at-rest protection.
- Frontend: React 18, Tailwind CSS (Glassmorphism UI), Lucide Icons
- Backend: FastAPI, Uvicorn, LangChain, LangGraph, Pydantic
- AI Models: Qwen 3 235B (via OpenRouter), Jina AI Embeddings v2, Tavily Search API
- Infrastructure: Docker, Render (Compute), Qdrant Cloud (Vector Store), MongoDB Atlas (NoSQL), Supabase (Postgres + Object Storage), Upstash (Redis)
The backend follows a Domain-Driven Design (DDD) architecture to ensure scalability, security, and separation of concerns.
backend/
├── app/
│ ├── auth/ # Authentication, JWT, and Google OAuth
│ │ ├── __init__.py
│ │ ├── jwt.py
│ │ ├── oauth.py
│ │ ├── routes.py
│ │ └── schemas.py # Auth-specific Pydantic models
│ ├── core/ # Application Brain (Logging, Config, Rate Limiting)
│ │ ├── __init__.py
│ │ ├── config.py
│ │ ├── limiter.py
│ │ └── logger.py
│ ├── db/ # Database Clients (MongoDB, Supabase)
│ │ ├── __init__.py
│ │ ├── database.py
│ │ └── supabase_client.py
│ ├── rag/ # Retrieval-Augmented Generation Logic
│ │ ├── __init__.py
│ │ ├── graph.py # LangGraph State Machine
│ │ ├── pipeline.py# Processing, Qdrant, Presidio PII
│ │ ├── routes.py # Endpoints for RAG Chat
│ │ └── schemas.py # RAG-specific Pydantic models
│ ├── utils/ # Generic Helper Functions
│ │ ├── __init__.py
│ │ └── helpers.py # e.g., calculate_sha256
│ └── main.py # FastAPI Entry Point
├── .env.example # Example Environment Variables
├── requirements.txt # Python Dependencies
└── Dockerfile # Multi-stage production build
-
Clone the repository:
git clone https://github.com/Ambuj123-lab/indian-legal-ai-expert.git cd indian-legal-ai-expert -
Backend Setup:
cd backend python -m venv venv source venv/bin/activate # Or venv\Scripts\activate pip install -r requirements.txt
-
Frontend Setup:
cd frontend npm install -
Environment Variables: Create a
.envfile in the root using.env.exampleas a template.
This application is an AI-powered educational tool designed to assist with legal research. It does not constitute legal advice. Users should consult a qualified legal professional for critical matters.
Developed with ❤️ by Ambuj Kumar Tripathi