Legal Adviser Chatbot
# Legal Adviser Chatbot - Brief Overview **Rakshabandhan** is an AI-powered full-stack chat application that empowers women in India by providing accessible information about legal rights and government financial schemes through an intelligent conversational interface. The application combines **RAG (Retrieval-Augmented Generation)** with Google Gemini LLM to deliver accurate, contextually relevant responses. It features audio-to-text transcription capabilities, multi-user support with organized chat groups, and a modern React frontend backed by a robust FastAPI server. The system uses Pinecone vector database for semantic search over legal documents and financial scheme information, ensuring users get personalized, knowledge-grounded answers. Built for scalability and ease of deployment, it includes automatic database cleanup, multilingual support for Indian languages, and real-time async processing for a seamless user experience.
Rakshabandhan: Building an AI-Powered Legal Adviser Chatbot for Women's Empowerment
Table of Contents
- Introduction
- Project Vision & Motivation
- System Architecture
- Technology Stack Deep Dive
- Core Components & Implementation
- RAG System: The Knowledge Engine
- Real-World Workflow
- Deployment & Scalability
- Challenges & Solutions
- Future Enhancements
Introduction
Imagine a woman in rural India needing guidance on dowry laws, but having no access to affordable legal counsel. Or a small business owner trying to understand government financial schemes for women entrepreneurs, but lacking the resources to hire a legal advisor.
Rakshabandhan solves this problem by building an intelligent, accessible AI chatbot that provides instant legal guidance and financial scheme information, entirely in a user's native language and through a simple conversational interface. The name "Rakshabandhan" (meaning "protection" in Hindi) encapsulates the project's core mission: protecting women's rights through technology.
This isn't just another chatbot. It's a knowledge system that combines cutting-edge AI techniques with domain expertise to deliver reliable, contextually aware legal advice. Let's dive deep into how it works.
Project Vision & Motivation
The Problem
-
Information Asymmetry: Most legal resources in India are inaccessible to the average person due to language barriers, complex terminology, and geographic distance from legal services.
-
Gender Inequality: Women often face additional barriers—social stigma, time constraints, and limited educational access—when seeking legal information.
-
Financial Schemes Complexity: Numerous government financial schemes exist for women, but they're scattered across different portals, with complex eligibility criteria and application processes.
The Solution
Rakshabandhan leverages modern AI to democratize access to legal information. By combining:
- Conversational AI for natural, intuitive interaction
- Retrieval-Augmented Generation for accurate, sourced responses
- Multimodal Input (text and audio) for accessibility
- Multilingual Support for inclusive coverage
The platform becomes a 24/7 personal legal advisor accessible to anyone with internet access.
System Architecture
High-Level Architecture Diagram
┌─────────────────────────────────────────────────────────────────────────┐
│ RAKSHABANDHAN SYSTEM ARCHITECTURE │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌────────────────────────────────────────────────────────────────┐ │
│ │ USER INTERFACE LAYER │ │
│ │ │ │
│ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
│ │ │ React UI │ │ WebSocket Connections │ │ │
│ │ │ (Vite) │────────▶│ Real-time Updates │ │ │
│ │ │ │ │ Live Message Streaming │ │ │
│ │ │ Components: │ └──────────────────────────────┘ │ │
│ │ │ • Chat View │ │ │
│ │ │ • Audio In │ │ │
│ │ │ • Groups │ │ │
│ │ │ • Settings │ │ │
│ │ └──────────────┘ │ │
│ └────────────────────────────────────────────────────────────────┘ │
│ ▲ │
│ │ HTTP/WebSocket (Axios) │
│ ▼ │
│ ┌────────────────────────────────────────────────────────────────┐ │
│ │ API GATEWAY & REQUEST ROUTING │ │
│ │ │ │
│ │ FastAPI Application (main.py) │ │
│ │ ├─ CORS Handling │ │
│ │ ├─ Request Validation │ │
│ │ ├─ Authentication/Authorization │ │
│ │ └─ Rate Limiting │ │
│ └────────────────────────────────────────────────────────────────┘ │
│ ▲ │
│ ┌─────────┼─────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ CORE PROCESSING LAYER │ │
│ │ │ │
│ │ ┌──────────────────┐ ┌──────────────────┐ │ │
│ │ │ Chat Handler │ │ Audio Handler │ │ │
│ │ │ (chatmodel.py) │ │ (audiototext.py) │ │ │
│ │ │ │ │ │ │ │
│ │ │ • Message Parse │ │ • Audio Upload │ │ │
│ │ │ • RAG Retrieval │ │ • Speech-to-Text │ │ │
│ │ │ • Context Build │ │ (Gemini API) │ │ │
│ │ │ • LLM Invocation │ │ • Text Routing │ │ │
│ │ │ • Response Form │ │ │ │ │
│ │ └──────────────────┘ └──────────────────┘ │ │
│ │ ▲ ▲ │ │
│ │ │ │ │ │
│ │ ┌────────┴────────┐ ┌──────────┴──────────┐ │ │
│ │ │ │ │ │ │ │
│ │ ▼ ▼ ▼ ▼ │ │
│ │┌──────────┐┌──────────────┐┌──────────────────────────┐ │ │
│ ││ LLM ││ RAG Engine ││ Document Ingestion │ │ │
│ ││ Wrapper ││ (rag.py) ││ (ingest.py) │ │ │
│ ││ ││ ││ │ │ │
│ ││ Google ││ 1. Query ││ • Parse Legal Docs │ │ │
│ ││ Gemini ││ Embed ││ • Convert to Embeddings│ │ │
│ ││ 2.5 ││ 2. Semantic ││ • Store in Vector DB │ │ │
│ ││ Flash ││ Search ││ • Index Management │ │ │
│ ││ ││ 3. Retrieve ││ │ │ │
│ ││ ││ Context ││ │ │ │
│ ││ ││ 4. Construct ││ │ │ │
│ ││ ││ Prompt ││ │ │ │
│ │└──────────┘└──────────────┘└──────────────────────────┘ │ │
│ └──────────────────────────────────────────────────────────────┘ │
│ ▲ │
│ │ │
│ ┌─────────┼──────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌────────────────────────────────────────────────────────────────┐ │
│ │ DATA & STORAGE LAYER │ │
│ │ │ │
│ │ ┌─────────────────────────┐ ┌──────────────────────────┐ │ │
│ │ │ PostgreSQL Database │ │ Pinecone Vector Store │ │ │
│ │ │ (SQLAlchemy ORM) │ │ (Vector Embeddings) │ │ │
│ │ │ │ │ │ │ │
│ │ │ Tables: │ │ Index: │ │ │
│ │ │ • Users │ │ rag-schemes-index │ │ │
│ │ │ • ChatGroups │ │ │ │ │
│ │ │ • ChatMessages │ │ Contains: │ │ │
│ │ │ • MessageMetadata │ │ • Legal Documents │ │ │
│ │ │ │ │ • Financial Schemes │ │ │
│ │ │ Relationships: │ │ • Government Policies │ │ │
│ │ │ User ──1:n── Groups │ │ • Case Law Examples │ │ │
│ │ │ Group ──1:n── Messages │ │ │ │ │
│ │ └─────────────────────────┘ └──────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────────────┐ │ │
│ │ │ Automatic Database Cleanup (APScheduler) │ │ │
│ │ │ • Runs every 24 hours (configurable) │ │ │
│ │ │ • Purges old chat messages and groups │ │ │
│ │ │ • Preserves user accounts │ │ │
│ │ │ • Enables/Disables via environment config │ │ │
│ │ └──────────────────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────────────────┐ │
│ │ EXTERNAL SERVICES & INTEGRATIONS │ │
│ │ │ │
│ │ Google Gemini API │ Pinecone API │ PostgreSQL Server │ │
│ │ • Text Generation │ • Embedding │ • Data Persistence │ │
│ │ • Multimodal Input │ • Indexing │ • User Management │ │
│ │ • Function Calling │ • Search │ • Chat History │ │
│ └────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘
DATA FLOW DIAGRAM: User Query Processing
User Input (Text/Audio)
▼
┌─────────────────────────────────────────┐
│ Frontend - Capture Input │
│ • Audio recording (browser API) │
│ • Text input (textarea) │
└─────────────────────────────────────────┘
▼
HTTP POST to /chat or /chat/audio
▼
┌─────────────────────────────────────────┐
│ Backend - Route Request │
│ • Validate request schema │
│ • Extract user_id, group_id │
└─────────────────────────────────────────┘
▼
┌──────────────────────────────────┐
│ Audio File? │
└──────────────────────────────────┘
│ │
YES│ │NO
▼ ▼
┌─────────────────┐ ┌──────────────────┐
│ Audio to Text │ │ Text Processing │
│ (audiototext.py)│ │ (Direct to LLM) │
│ • Call Gemini │ │ │
│ API │ │ │
│ • Decode audio │ │ │
│ • Return text │ │ │
└─────────────────┘ └──────────────────┘
│ │
└────────┬───────────┘
▼
┌──────────────────────────────────────┐
│ RAG Engine Initialization (rag.py) │
│ • Generate embedding for query │
│ • Search Pinecone vector database │
│ • Retrieve top-k relevant documents│
│ • Extract document context │
└──────────────────────────────────────┘
▼
┌──────────────────────────────────────┐
│ Prompt Construction │
│ • System prompt (legal advisor role)│
│ • Retrieved context documents │
│ • Chat history (conversation) │
│ • User query │
│ • Special instructions │
└──────────────────────────────────────┘
▼
┌──────────────────────────────────────┐
│ LLM Processing (llmWrap.py) │
│ • Call Google Gemini API │
│ • Stream response to client │
│ • Handle errors gracefully │
└──────────────────────────────────────┘
▼
┌──────────────────────────────────────┐
│ Response Persistence │
│ • Store user message │
│ • Store AI response │
│ • Save metadata (tokens, time) │
│ • Update message index │
└──────────────────────────────────────┘
▼
HTTP Response to Frontend
▼
┌──────────────────────────────────────┐
│ Frontend - Display Response │
│ • Parse markdown │
│ • Render formatted answer │
│ • Add to chat thread │
│ • Update UI state │
└──────────────────────────────────────┘
Technology Stack Deep Dive
Backend: The Brains of the System
FastAPI
Why FastAPI?
- Async Support: Critical for handling multiple concurrent users without blocking I/O
- Type Safety: Python type hints enable automatic validation and documentation
- Performance: Nearly as fast as NodeJS/Go, much faster than traditional Flask/Django
- Easy to Deploy: Single Python module with no heavy framework baggage
# Example of async request handling
@app.post("/chat")
async def chat(request: ChatRequest):
# Non-blocking I/O operations
response = await process_chat(request)
return response
PostgreSQL + SQLAlchemy
Database Design:
- Users Table: Stores user profiles and preferences
- ChatGroups Table: Separates conversations per user (multi-chat support)
- ChatMessages Table: Maintains conversation history with timestamps
- Relationships: Enforces referential integrity and enables efficient queries
Why SQLAlchemy ORM?
- Database agnostic (can switch to MySQL, Oracle, etc.)
- Automatic migrations possible with Alembic
- Query building is intuitive and safe from SQL injection
Vector Database: The Knowledge Retrieval Engine
Pinecone
What is it? Pinecone is a managed vector database that stores high-dimensional embeddings of documents. Instead of keyword matching, it performs semantic search—finding documents with similar meaning to your query, even if they don't share exact words.
Why Pinecone over alternatives?
- Serverless: No infrastructure management
- Scalable: Handles millions of vectors efficiently
- Fast: Retrieves similar documents in milliseconds
- Managed Service: Automatic scaling, backups, reliability
Integration with LangChain:
from langchain.vectorstores import Pinecone
from langchain.embeddings import HuggingFaceEmbeddings
# Create embeddings using open-source model
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
# Connect to Pinecone
vector_store = Pinecone.from_documents(
documents=docs,
embedding=embeddings,
index_name="rag-schemes-index"
)
LLM: Google Gemini 2.5 Flash
Why Gemini 2.5 Flash?
- Multimodal: Handles text, images, audio, and video
- Fast Inference: 2.5 Flash is optimized for speed without sacrificing quality
- Context Window: 1 million token context enables rich document understanding
- Cost-Effective: Significantly cheaper than GPT-4 for streaming applications
Capabilities Leveraged:
- Text Generation: Primary role for legal advice synthesis
- Audio Processing: Native speech-to-text without separate APIs
- Function Calling: Could enable structured outputs for specific query types
- Vision: Could analyze legal documents as images if needed
Frontend: User-Friendly Interface
React 18 + Vite
Why React?
- Component Reusability: Chat message, input box, group list—all reusable components
- State Management: Zustand or Context API for managing chat state
- Rich Ecosystem: Thousands of libraries for any UI need
Why Vite?
- Fast Development: Near-instant hot module replacement
- Optimized Builds: Automatic code splitting and tree-shaking
- Modern Tooling: Uses native ES modules instead of bundling everything
Key Components:
- Chat View: Displays messages with markdown rendering
- Audio Input: Browser's MediaRecorder API for voice input
- Group Management: Switch between multiple chats
- Settings Panel: User preferences, language selection
Tailwind CSS
- Utility-First: Rapid UI development without writing custom CSS
- Responsive: Mobile-first design out of the box
- Dark Mode: Built-in theme support
Core Components & Implementation
1. Chat Model (chatmodel.py)
Orchestrates the conversation flow:
async def process_user_message(user_id: str, group_id: str, message: str):
# 1. Retrieve chat group and history
group = db.query(ChatGroup).filter(
ChatGroup.id == group_id,
ChatGroup.user_id == user_id
).first()
history = group.messages[-10:] # Last 10 messages for context
# 2. Call RAG to retrieve relevant documents
context = await rag_retrieval(message)
# 3. Build conversation history
messages = format_conversation_history(history, message)
# 4. Invoke LLM with context
response = await llm.generate(
system_prompt=LEGAL_ADVISOR_SYSTEM_PROMPT,
messages=messages,
context=context
)
# 5. Save both message and response
user_msg = ChatMessage(group_id=group_id, role="user", content=message)
ai_msg = ChatMessage(group_id=group_id, role="assistant", content=response)
db.add(user_msg)
db.add(ai_msg)
db.commit()
return response
2. RAG Engine (rag.py)
The heart of knowledge retrieval:
async def retrieve_context(query: str, top_k: int = 5):
# 1. Create embedding of user query
query_embedding = embeddings.embed_query(query)
# 2. Search Pinecone for similar documents
results = vector_store.similarity_search_with_score(
query_embedding,
k=top_k
)
# 3. Format retrieved documents as context
context = "\n\n".join([
f"Source: {doc.metadata['source']}\n{doc.page_content}"
for doc, score in results
])
return context
Why RAG is Critical: Without RAG, the LLM would hallucinate answers based on its training data. With RAG:
- Every answer is grounded in actual legal documents
- Users see sources for the information
- Responses stay current as documents are updated
- Reduces hallucination risk significantly
3. Audio Processing (audiototext.py)
Converts voice to text using Gemini's multimodal capabilities:
async def transcribe_audio(audio_file: UploadFile):
# 1. Read audio bytes
audio_bytes = await audio_file.read()
# 2. Send to Gemini API
client = genai.Client(api_key=GOOGLE_API_KEY)
audio_part = genai.types.Part(
inline_data=genai.types.Blob(
mime_type=audio_file.content_type,
data=audio_bytes
)
)
# 3. Generate transcription
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[
audio_part,
"Convert this audio to text. Return only the transcribed text."
]
)
return response.text
Supported Formats: MP3, WAV, OGG, M4A, FLAC
4. Document Ingestion (ingest.py)
One-time process to populate the vector database:
async def ingest_documents(documents_dir: str):
# 1. Load all documents from directory
documents = load_documents(documents_dir)
# 2. Split into chunks for better retrieval
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = text_splitter.split_documents(documents)
# 3. Generate embeddings
embeddings = HuggingFaceEmbeddings(
model_name="BAAI/bge-small-en-v1.5"
)
# 4. Upload to Pinecone
Pinecone.from_documents(
chunks,
embeddings,
index_name="rag-schemes-index"
)
RAG System: The Knowledge Engine
How RAG Works: A Step-by-Step Example
Scenario: A woman asks: "What are the subsidies available for women starting a business?"
Step 1: Query Embedding
The query is converted into a 384-dimensional vector representing its semantic meaning:
Query: "What are the subsidies available for women starting a business?"
▼
Embedding: [0.234, -0.156, 0.892, ..., 0.341] // 384 dimensions
Step 2: Semantic Search
Pinecone searches for documents with similar embeddings:
Pinecone Index Search:
├─ Government Financial Scheme for Women Entrepreneurs (similarity: 0.92)
├─ PMMY - Pradhan Mantri Mudra Yojana Details (similarity: 0.88)
├─ STEP Scheme - Support to Existing Ventures (similarity: 0.85)
├─ Women's Business Loan Subsidy Programs (similarity: 0.82)
└─ GST Benefits for Women-Owned Enterprises (similarity: 0.79)
Step 3: Context Construction
Retrieved documents are formatted as context:
Context Documents:
---
Government Financial Scheme for Women Entrepreneurs:
- PMMY provides loans up to Rs. 10 lakhs without collateral
- Interest rate subsidy available for first 3 years
- Eligibility: Must be Indian citizen, business plan required
...
STEP Scheme - Support to Existing Ventures:
- Provides Rs. 50,000-500,000 for existing women businesses
- Training and mentoring included
- Sectors: Manufacturing, Services, Trade
...
Step 4: Prompt Construction
The system builds a comprehensive prompt:
System Prompt:
You are an expert legal advisor specializing in women's rights and
financial schemes in India. Provide accurate, referenced answers based
on the provided documents. Always cite your sources.
Retrieved Context:
[Documents from Step 3]
Chat History:
User: Previous questions and answers...
Current Query:
"What are the subsidies available for women starting a business?"
Step 5: LLM Generation
Gemini generates a response grounded in the retrieved documents:
Response:
Several government subsidies are available for women starting businesses:
1. **Pradhan Mantri Mudra Yojana (PMMY)**
- Loan amount: Up to Rs. 10 lakhs
- Interest rate subsidy for first 3 years
- No collateral required
- Source: Government Financial Scheme for Women Entrepreneurs
2. **STEP Scheme (Support to Existing Ventures)**
- Support: Rs. 50,000-500,000
- Includes training and mentoring
- Applicable sectors: Manufacturing, Services, Trade
- Source: STEP Scheme Documentation
[Response continues with more details and sources]
Step 6: Response Delivery
The formatted response is sent to the frontend and displayed to the user.
Advantages of RAG
| Traditional LLM | RAG System |
|---|---|
| Hallucinations possible | Grounded in actual documents |
| Outdated knowledge | Can be updated by ingesting new docs |
| No sources provided | Every answer includes citations |
| Slower to update | Instant updates via document ingestion |
Real-World Workflow
Complete User Journey: From Query to Answer
User: Priya, a woman from Chennai wanting to start a tailoring business
Timeline: 9:30 AM, July 28, 2026
[1] Priya opens the Rakshabandhan app on her mobile
▼
[2] React Frontend loads, presents login/signup
• She logs in with her email
• Backend validates credentials from PostgreSQL
▼
[3] Dashboard shows her existing chat groups
• Group 1: "Startup Queries" (5 messages)
• Group 2: "Legal Rights Questions" (12 messages)
• She clicks "New Chat" to start fresh
▼
[4] Frontend creates new ChatGroup in PostgreSQL
• group_id: "uuid-12345"
• user_id: "priya-uuid"
• created_at: "2026-07-28 09:32:00"
▼
[5] Priya decides to use voice input
• Clicks "Record Audio" button
• Browser's MediaRecorder API captures:
"What subsidies can I get for starting my tailoring business?"
• Audio file (30 seconds, 500KB MP3)
▼
[6] Frontend uploads audio to /chat/audio endpoint
POST /chat/audio {
"user_id": "priya-uuid",
"group_id": "uuid-12345",
"audio_file": <binary MP3 data>
}
▼
[7] Backend: AudioToText Handler kicks in
• Calls Google Gemini API with audio
• Gemini transcribes: "What subsidies can I get for starting my
tailoring business?"
• Returns transcribed text
▼
[8] Backend: RAG Engine activates
• Embeds query using HuggingFace embeddings
• Queries Pinecone with embedding vector
• Pinecone returns top 5 documents:
1. PMMY Documentation (0.95 relevance)
2. STEP Scheme Details (0.89)
3. Women Business Loan Schemes (0.87)
4. Textile Industry Support (0.84)
5. GST and Tax Benefits (0.81)
▼
[9] Backend: Context Construction
• Extracts relevant sections from retrieved documents
• Formats with sources and metadata
• Creates final context string (~2000 tokens)
▼
[10] Backend: Chat Model Handler
• Retrieves last 5 messages from Priya's chat history
• Builds system prompt (Legal advisor role + instructions)
• Assembles full prompt with context + history + query
▼
[11] Backend: LLM Wrapper (Google Gemini)
• Sends full prompt to Gemini API
• Streams response tokens back:
Token 1: "For"
Token 2: "starting"
Token 3: "your"
Token 4: "tailoring"
...
• Accumulates into full response
▼
[12] Backend: Response Persistence
• Stores user message in ChatMessages table
• Stores AI response in ChatMessages table
• Metadata: tokens_used (1247), response_time (2.3s), etc.
• Commits transaction to PostgreSQL
▼
[13] Frontend: Real-time Updates
• Receives streamed response tokens via Axios
• Renders markdown (converts **bold** to bold, etc.)
• Displays animated response in chat
▼
[14] Priya sees formatted answer:
"For starting your tailoring business, you can access:
1. Pradhan Mantri Mudra Yojana (PMMY)
• Loan: Up to ₹10 lakhs
• Interest subsidy: 2% for first 3 years
• No collateral needed
• Source: PMMY Official Documentation
[Rest of detailed answer with schemes and eligibility...]"
▼
[15] Priya asks a follow-up question
"What's the application process for PMMY?"
• RAG retrieves PMMY application guide
• LLM contextualizes with previous question
• Detailed step-by-step process is provided
▼
[16] After 24 hours (or configured interval)
• APScheduler triggers database cleanup job
• Deletes old messages and unused groups
• Preserves Priya's account and recent history
• Frees up database storage
Deployment & Scalability
Local Development Setup
Backend (FastAPI + PostgreSQL)
cd backend
python -m venv venv
source venv/bin/activate # Linux/Mac
pip install -r requirements.txt
# Configure .env
export DATABASE_URL="postgresql://user:pass@localhost:5432/rakshabandhan"
export GOOGLE_API_KEY="your_key"
export PINECONE_API_KEY="your_key"
# Run migrations and start server
python -m uvicorn main:app --reload --port 8000
Frontend (React + Vite)
cd frontend
npm install
npm run dev # Starts at http://localhost:5173
Production Deployment Strategy
Option 1: Docker Containerization
# Backend Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Option 2: Cloud Deployment
AWS Deployment:
- Backend: ECS (Elastic Container Service) with auto-scaling
- Database: RDS PostgreSQL managed service
- Frontend: S3 + CloudFront CDN
- Vector DB: Pinecone (serverless, no infrastructure)
GCP Deployment:
- Backend: Cloud Run (serverless containers)
- Database: Cloud SQL PostgreSQL
- Frontend: Cloud Storage + Cloud CDN
- Vector DB: Pinecone
Scalability Considerations
Horizontal Scaling:
- Database: PostgreSQL connection pooling (PgBouncer)
- Backend: Load balancer distributes requests to multiple FastAPI instances
- Caching: Redis cache for frequently accessed documents
- Async Processing: APScheduler for background tasks
Performance Optimization:
- Database Indexing: Indexes on (user_id, created_at) for fast queries
- Query Optimization: SQLAlchemy lazy loading, selective field retrieval
- Vector DB: Pinecone's built-in indexing and caching
- Frontend: Code splitting, lazy component loading
Challenges & Solutions
Challenge 1: Hallucination in Legal Advice
Problem: LLMs can generate plausible-sounding but incorrect legal information, which is dangerous in a legal context.
Solution - RAG:
- Every response is grounded in actual legal documents
- Only information in the knowledge base can be returned
- If the knowledge base doesn't have an answer, the system says so
Implementation:
# Prevent hallucination by explicitly constraining LLM
system_prompt = """
You are a legal advisor. You MUST:
1. Only use the provided document context
2. Always cite the source document
3. If information is not in provided docs, say:
"I don't have sufficient information to answer this. Please
consult a qualified lawyer or contact [government office]"
4. Never make assumptions about laws or policies
"""
Challenge 2: Multilingual Support
Problem: India has 22 official languages; ensuring accuracy across all is complex.
Solution:
- Document Translation: Store documents in multiple languages in Pinecone
- Query Translation: Translate user input to English before embedding
- Response Translation: Generate response in queried language
async def handle_multilingual_query(query: str, language: str):
# 1. Detect/confirm language
if language not in SUPPORTED_LANGUAGES:
language = detect_language(query)
# 2. Translate to English for embeddings
query_english = translate(query, language, "en")
# 3. Retrieve and respond
context = await rag_retrieval(query_english)
response = await llm.generate(query_english, context)
# 4. Translate response back
response_translated = translate(response, "en", language)
return response_translated
Challenge 3: Latency in Large Queries
Problem: Retrieving from Pinecone, embedding, and LLM inference can take 3-5 seconds.
Solutions:
- Streaming: Send tokens to frontend as they arrive (perceived speed increase)
- Caching: Cache embeddings for common questions
- Index Optimization: Use Pinecone's approximate nearest neighbor (ANN) search
- Prompt Compression: Summarize context to reduce LLM processing
Challenge 4: Handling Sensitive Data
Problem: Users might share personal information; need privacy protections.
Solutions:
- End-to-End Encryption: TLS for data in transit
- Database Encryption: Encrypt sensitive fields at rest
- Data Retention: Automatic cleanup of old data
- Compliance: GDPR/India's DPDP compliance ready
class ChatMessage(Base):
__tablename__ = "chat_messages"
id = Column(String, primary_key=True)
user_id = Column(String, ForeignKey("users.id"))
content = Column(String, nullable=False)
# Soft delete for privacy
deleted_at = Column(DateTime, nullable=True)
# TTL (Time To Live) for automatic cleanup
created_at = Column(DateTime, default=datetime.utcnow)
expires_at = Column(DateTime, default=lambda: datetime.utcnow() + timedelta(days=30))
Future Enhancements
Phase 2: Advanced Features
-
Video Consultation
- Integrate video streaming for consultations
- Record sessions for reference
- Transcribe video consultations
-
Document Generation
- Generate legal documents (petitions, applications)
- Template library for common documents
- Automatic filling based on user info
-
Case Tracking
- Track ongoing legal cases
- Receive updates on case status
- Integration with court records (where available)
-
Legal Network
- Connect with human lawyers
- Escalate complex cases
- Payment integration for consultations
-
Offline Capability
- Progressive Web App (PWA) for offline access
- Sync when connectivity returns
- Critical information cached locally
Phase 3: Enterprise Features
-
Admin Dashboard
- Analytics on user queries
- Document management interface
- Performance metrics
-
Custom Knowledge Bases
- Organizations can add their own documents
- Multi-tenant support
- Custom branding
-
API for Third-Party Integration
- NGOs integrate the chatbot into their platforms
- Government portals embed the advisor
- Partner organizations extend functionality
Conclusion
Rakshabandhan represents a convergence of cutting-edge AI technologies deployed toward a deeply human goal: democratizing access to legal knowledge and financial empowerment for women in India.
By combining RAG (Retrieval-Augmented Generation) with streaming LLMs, multimodal input, and intelligent caching, the system delivers accurate, responsive legal advice without hallucination. The modular architecture enables easy scaling and extension.
Most importantly, the project demonstrates that technology, when built with empathy and domain expertise, can be a powerful tool for social impact.
Key Takeaways
✅ RAG is Essential: Never use raw LLMs for factual, sourced information
✅ Streaming Improves UX: Users perceive faster responses with token streaming
✅ Async-First: FastAPI's async enables handling concurrent users efficiently
✅ Modular Design: Each component (RAG, LLM, Chat, Audio) is independently testable
✅ Data Privacy Matters: Automatic cleanup and encryption build user trust
Resources & References
- FastAPI Documentation
- LangChain RAG Tutorial
- Pinecone Vector Database
- Google Gemini API
- React 18 with TypeScript
- SQLAlchemy ORM Guide
Last Updated: July 28, 2026
Author: Technical Documentation Team
Version: 1.0.0
