Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LegalRAG AI — Production Grade Legal AI RAG System

Overview

LegalRAG AI is a production-focused Legal AI system built using Retrieval-Augmented Generation (RAG), local LLMs via Ollama, semantic search, vector databases, and grounded legal reasoning pipelines.

This project was built to create a domain-specialized AI assistant capable of answering legal questions using grounded legal documents instead of hallucinated responses.

Unlike normal AI chatbots, this system retrieves relevant legal context from trained legal datasets before generating responses.


What Problem Does This Solve?

Most general AI chatbots:

  • hallucinate legal information
  • provide ungrounded responses
  • lack legal domain specialization
  • cannot retrieve real legal acts and sections

LegalRAG AI solves this by:

  • training on legal documents
  • retrieving relevant legal chunks
  • grounding answers in actual legal context
  • filtering non-legal questions
  • supporting scalable legal ingestion pipelines

Who Is This For?

LegalRAG AI can be useful for:

  • law students
  • legal researchers
  • compliance teams
  • startups
  • legal-tech platforms
  • cyber law analysts
  • corporate legal teams
  • AI legal assistant experimentation
  • RAG engineering research

Core Features

  • Production-grade Legal RAG pipeline
  • PGVector semantic retrieval
  • Hybrid legal search
  • Semantic chunking
  • Contextual compression
  • Conversation memory
  • Legal query guardrails
  • Streaming responses
  • Local LLM inference using Ollama
  • Domain-specialized legal embeddings
  • FastAPI backend
  • Modular AI pipeline architecture

Tech Stack

Backend

  • Python
  • FastAPI
  • AsyncIO
  • Uvicorn

AI / RAG

  • Ollama
  • LangChain
  • Sentence Transformers
  • PGVector
  • PostgreSQL
  • Semantic Embeddings
  • Hybrid Retrieval
  • Contextual Compression

Models Tested

Lightweight Models

  • phi3:mini
  • qwen2.5:1.5b

Larger Models Tested

  • llama3
  • mistral
  • qwen2.5

Legal Datasets Used

The system was trained and structured for:

  • Indian Constitution
  • IPC
  • CRPC
  • Cyber Law
  • IT Act
  • Company Law
  • GST / Taxation
  • Contracts
  • Labor Law
  • Compliance Documents

The architecture supports ingesting thousands of additional legal PDFs and documents.


Architecture

User Query
   ↓
Legal Guardrails
   ↓
Retriever
   ↓
PGVector Semantic Search
   ↓
Context Compression
   ↓
Conversation Memory
   ↓
LLM Generation (Ollama)
   ↓
Grounded Legal Response

What I Built

I built:

  • a modular production-style RAG architecture
  • custom ingestion pipelines
  • legal document semantic retrieval
  • vector search pipelines
  • grounded legal reasoning system
  • streaming AI responses
  • domain-specific AI guardrails
  • scalable legal AI backend APIs

This project was built to deeply understand:

  • AI engineering
  • LLM systems
  • RAG pipelines
  • vector databases
  • production AI architecture
  • retrieval systems
  • local inference systems

Challenges Faced

During development, several real-world AI engineering challenges were encountered:

  • Ollama CUDA runtime instability
  • GPU memory allocation issues
  • streaming inference crashes
  • async inference debugging
  • Windows GPU compatibility issues
  • empty model responses
  • vector indexing optimization
  • retrieval tuning
  • semantic chunking strategies

These challenges helped in understanding real production AI infrastructure debugging.


Important Runtime Note

Local GPU inference may require environment-specific CUDA configuration depending on hardware and Ollama runtime compatibility.

Some Windows + NVIDIA GPU environments may require:

  • VRAM optimization
  • reduced context windows
  • CPU fallback
  • Ollama runtime tuning

What I Learned

This project helped me learn:

  • production RAG architecture
  • vector databases
  • semantic search
  • LLM inference pipelines
  • Ollama runtime systems
  • async FastAPI engineering
  • streaming AI systems
  • grounding AI responses
  • prompt engineering
  • AI infrastructure debugging
  • memory optimization
  • legal domain specialization

Example Queries

Explain Article 21 of the Indian Constitution.

What is the punishment under IPC Section 420?

Explain the IT Act related to cybercrime.

What are Fundamental Rights in India?

What is the legal procedure for filing an FIR?

Example API Test Request

Use this JSON body inside FastAPI Swagger Docs (/docs) or Postman to test the LegalRAG AI system.

POST /chat

Example Request Body

{
  "question": "Explain Article 21 of the Indian Constitution and the Right to Life.",
  "jurisdiction": "india"
}

More Legal Test Queries

Fundamental Rights

{
  "question": "What are the Fundamental Rights guaranteed under the Constitution of India?",
  "jurisdiction": "india"
}

IPC Query

{
  "question": "Explain IPC Section 420 related to cheating and fraud.",
  "jurisdiction": "india"
}

Cyber Law

{
  "question": "What does the IT Act say about cybercrime in India?",
  "jurisdiction": "india"
}

FIR Procedure

{
  "question": "What is the legal process to file an FIR in India?",
  "jurisdiction": "india"
}

Non-Legal Query Test

{
  "question": "Who won the IPL yesterday?",
  "jurisdiction": "india"
}

Expected behavior: The system should reject non-legal questions using legal guardrails.


Installation

Clone Repository

git clone <your-repo-url>
cd nexhire-rag

Create Virtual Environment

python -m venv venv

Windows

venv\Scripts\activate

Linux / Mac

source venv/bin/activate

Install Requirements

pip install -r requirements.txt

Ollama Setup

Install Ollama:

https://ollama.com

Pull model:

ollama pull phi3:mini

Verify:

ollama list

Environment Variables

Create .env

OLLAMA_MODEL=phi3:mini
OLLAMA_BASE_URL=http://localhost:11434

Start Backend

uvicorn app.api.main:app --reload

API Docs

http://127.0.0.1:8000/docs

Future Improvements

  • Multi-agent legal workflows
  • Legal citation generation
  • Court judgement retrieval
  • Legal PDF summarization
  • Reranking models
  • Fine-tuned legal embeddings
  • Agentic legal reasoning
  • Docker deployment
  • Kubernetes scaling
  • Redis caching
  • Authentication system
  • Legal analytics dashboard

Training More Legal Knowledge

You can continuously improve the system by ingesting:

  • latest legal acts
  • court judgements
  • compliance documents
  • government circulars
  • updated cyber laws
  • new taxation laws

The architecture is designed to scale with additional legal datasets.


Disclaimer

This system is intended for educational, research, and AI engineering purposes only.

It should not be considered a substitute for professional legal advice.


Author

Uzain Mohid S

About

Production-grade Legal AI RAG system using Ollama, FastAPI, PGVector, and semantic retrieval for grounded Indian law question answering.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages