Skip to content

Manu080405/Lex-AI-Legal-Assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Version License Status Branch

βš–οΈ LexAI

AI-Powered Legal Research & Analysis System for Indian Criminal Law

Semantic search and intelligent analysis of the Bharatiya Nyaya Sanhita (BNS) with IPC-to-BNS transition mapping


Features Β· Architecture Β· Tech Stack Β· Setup Β· API Docs Β· Usage Β· Disclaimer


πŸ“Œ Overview

LexAI is an intelligent Retrieval-Augmented Generation (RAG) system designed to assist in understanding and analyzing criminal law provisions in India. It provides semantic search and detailed analysis of the Bharatiya Nyaya Sanhita (BNS) β€” India's new criminal code β€” with intelligent IPC-to-BNS mapping to facilitate the transition from the legacy Indian Penal Code.

The system combines advanced natural language processing, vector similarity search, and large language models to deliver comprehensive legal insights, including applicable sections, essential offense ingredients, legal reasoning, and prescribed punishments.

⚠️ Disclaimer: LexAI is a research prototype. It must not be used as a substitute for professional legal advice. Always consult qualified legal professionals for actual legal matters.


✨ Features

πŸ” Semantic Search

  • Search across 358+ BNS sections using natural language queries
  • Find relevant legal provisions based on case descriptions
  • Similarity scoring with configurable thresholds (default: 0.30)
  • Fast retrieval using FAISS vector indexing

πŸ“š Comprehensive Knowledge Base

  • 358 BNS Sections fully indexed and searchable
  • 500+ IPC Sections mapped to their BNS equivalents
  • 20 Chapters covering all offense categories
  • Complete statutory text with explanations, illustrations, and exceptions
  • Bidirectional IPC ↔ BNS mapping

πŸ€– AI-Powered Legal Analysis

  • Automatic query reformulation for improved search accuracy
  • LLM-based analysis generation covering:
    • Relevant BNS section identification
    • Essential ingredients of the offense
    • Legal reasoning and precedents
    • Prescribed punishments and penalties
  • Real-time streaming responses for interactive analysis

πŸ’¬ Interactive Chatbot Interface

  • Multi-turn conversations with legal context awareness
  • Persistent chat history with search and filtering
  • Suggested prompts to guide legal research
  • Specialized legal response renderer with expandable sections
  • Real-time thinking animations during processing

πŸ” User Authentication & Management

  • Secure JWT-based authentication (2-hour token expiry)
  • Email provider validation (Gmail, Yahoo, Outlook, Hotmail)
  • Strong password enforcement (uppercase, lowercase, numbers, special chars)
  • Bcrypt password hashing with salt rounds
  • Persistent user profiles and chat history

πŸ—‚οΈ IPC to BNS Migration Guide

  • Side-by-side navigation between old IPC and new BNS provisions
  • Clear mapping of corresponding sections and their differences
  • Built to help legal professionals transition to the new code

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Frontend (React)                      β”‚
β”‚      Chatbot Interface β”‚ Auth β”‚ Home β”‚ Chat History      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚ HTTP / Axios
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Backend (Express.js)                    β”‚
β”‚     Auth β”‚ Chat Management β”‚ API Orchestration           β”‚
β”‚     /signup  /login  /chats  /analyze-case               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚ HTTP / Axios
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚             AI Service (FastAPI + Python)                β”‚
β”‚     Query Reformulation β”‚ FAISS Retrieval β”‚ LLM          β”‚
β”‚     Semantic Search β”‚ Legal Analysis β”‚ Streaming         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚ File I/O              β”‚ DB Queries
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Knowledge Base        β”‚  β”‚        MongoDB             β”‚
β”‚  FAISS Index            β”‚  β”‚  User Profiles             β”‚
β”‚  BNS Sections (358)     β”‚  β”‚  Chat History              β”‚
β”‚  IPC-BNS Mapping (500+) β”‚  β”‚  Metadata                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”„ Data Flow Pipeline

User Input
    β”‚
    β–Ό
Backend (Auth + Request Handling)
    β”‚
    β–Ό
Query Reformulation  ──── LLM
    β”‚
    β–Ό
FAISS Semantic Search  ──── BNS Embeddings
    β”‚
    β–Ό
Top-k Sections Retrieved (k=5, threshold=0.30)
    β”‚
    β–Ό
LLM Legal Analysis Generation (Streaming)
    β”‚
    β–Ό
Frontend Renderer  ──── Chat Persistence (MongoDB)

πŸ› οΈ Tech Stack

Layer Technology
Frontend React 19.2.0, Vite, React Router DOM 7.12.0, Axios 1.13.2
Backend Node.js, Express.js 5.2.1, JWT, bcryptjs
Database MongoDB 9.1.3
AI / ML FastAPI, FAISS, Sentence Transformers (BAAI/bge-large-en-v1.5)
LLM Ollama (Gemma model)
Data JSON files for BNS sections and IPC-BNS mappings
Python 3.8+

πŸ“ Project Structure

mini_project/
β”‚
β”œβ”€β”€ frontend/                        # React frontend application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx                  # Main app component with routes
β”‚   β”‚   β”œβ”€β”€ main.jsx                 # React entry point
β”‚   β”‚   β”œβ”€β”€ Chatbot.jsx              # Main chatbot interface
β”‚   β”‚   β”œβ”€β”€ Home.jsx                 # Landing page with feature showcase
β”‚   β”‚   β”œβ”€β”€ Auth.jsx                 # Login / Signup component
β”‚   β”‚   β”œβ”€β”€ Layout.jsx               # Navigation wrapper
β”‚   β”‚   β”œβ”€β”€ ProtectedRoute.jsx       # Auth protection wrapper
β”‚   β”‚   β”œβ”€β”€ *.css                    # Component-specific styles
β”‚   β”‚   └── assets/                  # Images and static files
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ vite.config.js
β”‚   └── index.html
β”‚
β”œβ”€β”€ backend/                         # Express.js backend server
β”‚   β”œβ”€β”€ index.js                     # Main server
β”‚   β”‚   β”œβ”€β”€ Auth routes              # /signup, /login
β”‚   β”‚   β”œβ”€β”€ Chat routes              # /chats, /chats/:id
β”‚   β”‚   β”œβ”€β”€ AI integration           # /analyze-case, /analyze-case-stream
β”‚   β”‚   β”œβ”€β”€ Auth middleware
β”‚   β”‚   └── Error handling
β”‚   β”œβ”€β”€ package.json
β”‚   └── .env                         # Environment variables (not committed)
β”‚
β”œβ”€β”€ ai-service/legal_rag/            # Python ML pipeline
β”‚   β”œβ”€β”€ main.py                      # FastAPI application entry point
β”‚   β”œβ”€β”€ pipeline.py                  # RAG pipeline (streaming + non-streaming)
β”‚   β”œβ”€β”€ retriever.py                 # FAISS-based semantic search
β”‚   β”œβ”€β”€ llm_ollama.py                # LLM integration (Gemma)
β”‚   β”œβ”€β”€ requirements.txt             # Python dependencies
β”‚   β”œβ”€β”€ faiss_max.bin                # Pre-computed FAISS index
β”‚   β”œβ”€β”€ faiss_max.pkl                # Index metadata
β”‚   β”œβ”€β”€ bns_max.json                 # BNS sections database
β”‚   β”œβ”€β”€ bns_ipc_map_updated.json     # IPC-to-BNS mapping
β”‚   β”œβ”€β”€ embeddings_max.pkl           # Pre-computed embeddings
β”‚   └── venv/                        # Python virtual environment
β”‚
β”œβ”€β”€ bns_ipc_map_updated.json         # Root-level reference mapping
β”œβ”€β”€ README.md
└── .gitignore

βš™οΈ Setup & Installation

Prerequisites

  • Node.js 16+ and npm
  • Python 3.8+
  • MongoDB (local or cloud instance)
  • Ollama (for LLM inference)

1. Frontend Setup

cd frontend
npm install

Create .env in the frontend/ directory:

VITE_API_URL=http://localhost:5000

2. Backend Setup

cd backend
npm install

Create .env in the backend/ directory:

MONGODB_URI=mongodb://localhost:27017/lexai
JWT_SECRET=your_secret_key_here
PORT=5000
AI_SERVICE_URL=http://localhost:8000

3. AI Service Setup

cd ai-service/legal_rag
python -m venv venv

# Activate virtual environment
source venv/bin/activate          # macOS / Linux
venv\Scripts\activate             # Windows

pip install -r requirements.txt

Ensure Ollama is running with the Gemma model:

ollama run gemma

πŸš€ Running the Application

Open three separate terminals:

# Terminal 1 β€” AI Service (http://localhost:8000)
cd ai-service/legal_rag
source venv/bin/activate
python main.py

# Terminal 2 β€” Backend (http://localhost:5000)
cd backend
npm start

# Terminal 3 β€” Frontend (http://localhost:5173)
cd frontend
npm run dev

Visit http://localhost:5173 to access LexAI.


πŸ“‘ API Documentation

Authentication

POST /api/auth/signup

Create a new user account.

// Request
{
  "email": "user@gmail.com",
  "password": "SecurePass123!",
  "fullName": "John Doe"
}

// Response
{
  "token": "<jwt_token>",
  "user": { "id": "...", "email": "...", "fullName": "..." }
}

POST /api/auth/login

Authenticate an existing user.

// Request
{
  "email": "user@gmail.com",
  "password": "SecurePass123!"
}

// Response
{
  "token": "<jwt_token>",
  "user": { "id": "...", "email": "...", "fullName": "..." }
}

Chat Management

All chat endpoints require Authorization: Bearer <token>

Method Endpoint Description
GET /api/chats Retrieve all chats for the authenticated user
POST /api/chats Create a new chat
GET /api/chats/:id Get a specific chat by ID
PUT /api/chats/:id Update title or add a message
DELETE /api/chats/:id Delete a chat

AI Analysis

POST /api/analyze-case

Get a complete legal analysis for a case description.

// Request
{
  "caseDescription": "A stole B's property worth β‚Ή5000..."
}

// Response
{
  "relevantSections": [
    { "section": "BNS 304", "score": 0.95 }
  ],
  "analysis": {
    "sections": "...",
    "ingredients": "...",
    "reasoning": "...",
    "punishment": "..."
  }
}

POST /api/analyze-case-stream

Same request format β€” response is streamed as Server-Sent Events for real-time display.


πŸ“– Usage Guide

For Lawyers & Legal Professionals

  1. Sign up and log in with your email
  2. Describe your case or legal scenario in natural language
  3. LexAI returns the most relevant BNS sections with similarity scores
  4. Review structured analysis: applicable sections, offense ingredients, legal reasoning, punishments
  5. Save analyses in persistent chat history
  6. Use IPC-to-BNS mapping for code transition reference

For Law Students

  1. Explore the Home page to understand the BNS structure
  2. Test case scenarios through the chatbot
  3. Learn how different sections apply to real-world situations
  4. Review explanations and legal reasoning in an accessible format

For Researchers

  1. Leverage semantic search to find related provisions across the BNS
  2. Analyze legal patterns across sections and chapters
  3. Use IPC-BNS mapping for comparative legal research
  4. Export chat history for documentation and annotation

πŸ”‘ Key Algorithms

Semantic Search (FAISS)

  • Pre-trained Sentence Transformer embeddings (BAAI/bge-large-en-v1.5)
  • Converts case descriptions to dense vector embeddings
  • Cosine similarity search against indexed BNS section embeddings
  • Returns top-5 sections above similarity threshold (0.30)

Query Reformulation

  • LLM rephrases user queries for better semantic alignment with legal text
  • Handles legal terminology normalization and ambiguity resolution
  • Improves relevance of retrieved sections significantly

Legal Analysis Generation

  • Ollama Gemma generates structured legal analysis
  • Output sections: applicable provisions, ingredients, reasoning, punishment
  • Full streaming support for real-time progressive display

πŸ“Š Performance Metrics

Operation Latency
FAISS Search (358 sections) ~50–100ms
Query Reformulation (LLM) ~500–1000ms
Legal Analysis Generation ~2–5s (streamed)
Auth API Requests ~100–200ms
Chat History Retrieval (MongoDB) <50ms

πŸ”’ Security

βœ… Implemented

  • JWT-based authentication with 2-hour token expiry
  • Password hashing with bcrypt (10 salt rounds)
  • Email provider validation
  • CORS middleware configuration
  • Protected API endpoints with auth middleware

⚠️ Recommendations for Production

  • Enforce HTTPS/TLS for all communications
  • Implement rate limiting on authentication endpoints
  • Add request validation and input sanitization
  • Enable MongoDB authentication and at-rest encryption
  • Implement audit logging for sensitive operations
  • Add CSRF protection
  • Consider OAuth2 for third-party access

πŸ—ΊοΈ Roadmap

βœ… Completed

  • Core RAG pipeline with semantic search
  • 358 BNS sections fully indexed
  • 500+ IPC-to-BNS mappings
  • JWT user authentication system
  • Chatbot interface with auto-scrolling
  • Chat persistence in MongoDB
  • Streaming response support
  • Query reformulation
  • Legal response renderer with section expansion

πŸ”„ In Progress

  • Embedding optimization for all sections
  • Performance improvements

πŸ“‹ Planned

  • Case law summaries and precedent analysis
  • Sentencing guidelines integration
  • Advanced filtering and sorting
  • Export functionality (PDF, Word)
  • Multi-language support
  • Mobile app version
  • Offline mode support

πŸ› οΈ Troubleshooting

AI Service not responding

ollama list
ollama run gemma
curl http://localhost:8000/docs

MongoDB connection issues

mongod --version
# Verify MONGODB_URI in backend/.env
# Format: mongodb://localhost:27017/lexai

CORS errors

  • Confirm VITE_API_URL in frontend .env matches the backend URL exactly
  • Review CORS configuration in backend/index.js

Chat history not saving

  • Ensure MongoDB is connected and running
  • Check that JWT token has not expired (2-hour limit)
  • Verify auth middleware is applied to all chat endpoints

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Make your changes with clear, descriptive commit messages
  4. Test across all three services (frontend, backend, AI service)
  5. Submit a pull request with a clear description of the changes

Please ensure your code follows existing project conventions and includes relevant comments.


πŸ“œ Citation

LexAI: An AI-Powered Legal Research System for Indian Criminal Law
Bharatiya Nyaya Sanhita (BNS) Analysis and IPC Mapping
Version 1.0.0 (Beta) β€” March 2026

⚠️ Disclaimer

LexAI is an AI-powered research and educational tool. It is not a substitute for professional legal advice. The system may contain errors or incomplete information, and all legal information should be independently verified by a qualified legal professional before being relied upon.


Last Updated: March 2, 2026 Β |Β  Version: 1.0.0 (Beta) Β |Β  Branch: adii

Built for legal research and education purposes

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors