Deliver a professional, Indian-law–focused AI assistant that answers legal questions using the new Bharatiya Nyaya Sanhita (BNS), related legislation, and Supreme Court precedents.
Built with a Retrieval-Augmented-Generation (RAG) backend and a modern chat-style frontend.
User ↔ Next.js Frontend ↔ FastAPI Backend ↔ LangGraph-powered RAG System ↔
├─ Supreme-Court Vector DB (Chroma)
└─ Tavily Web Search API
- Frontend (Next.js 15 + Tailwind) – chat UI, markdown rendering, avatars, loading states, clear-chat, mobile responsive.
- Backend (FastAPI) – single
/askendpoint; handles CORS and calls RAG pipeline. - RAG Pipeline (LangGraph)
• Embeddings: Sentence-Transformers
• Vector DB: Chroma (persisted inbackend/db_data/sc-chroma)
• LLM: Llama-3-70B via Groq API
• Web search: Tavily API
• Graph nodes: routing → retrieval → grading → generation → grading.
| Stage | Key Code | Purpose |
|---|---|---|
| Initialization | initialize_rag_components() |
Loads models, vectorstore, tools, prompts, and compiles the LangGraph workflow. |
| Routing | route_question_edge |
LLM decides whether to use SC vectorstore, web search, or both. |
| Retrieval Nodes | retrieve_sc_node, retrieve_web_search_node, retrieve_combined_sources_node |
Pull relevant documents (k = 5) or search results (k = 3). |
| Document Grading | grade_documents_node |
LLM grades each doc for relevance (truncates to 2 k chars & safe-try). |
| Query Rewrite | transform_query_node |
Improves the question if no good docs found. |
| Answer Generation | generate_answer_node + RAG prompt |
Produces structured legal answer (summary, provisions, analysis, final). |
| Answer Grading | grade_generation_edge |
Checks hallucination + usefulness; may regenerate or rewrite. |
| Safety Guard | MAX_GENERATION_ATTEMPTS = 5 |
Stops infinite loops and returns fallback "Sorry, we could not understand your query…". |
Endpoint: POST /ask
{
"question": "What is the procedure for filing an FIR?",
"chat_history": [] // (reserved for future multi-turn use)
}Returns: answer (markdown) + sources (URL metadata).
Component: components/ChatInterface.tsx
- State:
messages,input,isLoading,error. - UX perks: avatars, markdown via
react-markdown+remark-gfm, thumbs-up/down placeholders, clear-chat, loading spinner. - Flow:
await fetch("http://127.0.0.1:8000/ask", {...}) → push assistant message with answer & sources
- Responsive layout: flex column → mobile stack; Tailwind
prosefor typography.
- User types a legal query → clicks Send.
- Frontend POSTs to
/ask. - Backend's LangGraph pipeline:
- Router chooses
sc_vectorstore/web_search. - Retrieves docs → grades relevance.
- If no good docs, question rewritten → new retrieval.
- RAG prompt + docs → draft answer.
- Hallucination & usefulness graded.
- If bad, regenerate or rewrite (max 5 loops).
- Router chooses
- Final answer & sources returned.
- Frontend renders markdown-styled answer, shows clickable sources.
# 1) Backend
cd backend
python -m venv venv && venv\Scripts\activate # Windows example
pip install -r requirements.txt
set GROQ_API_KEY=... && set TAVILY_API_KEY=...
uvicorn app.main:app --reload
# 2) Frontend (new shell)
cd nextjs_frontend
npm install
npm run devNavigate to http://localhost:3000
- Opening (30 s)
"Good day judges. I present the Legal AI Assistant, designed to democratize access to up-to-date Indian legal knowledge." - Problem (20 s)
"With BNS replacing IPC, practitioners need quick, reliable answers—existing search tools are slow and fragmented." - Solution Overview (40 s)
Show architecture slide / diagram above. Emphasise RAG to ground answers in law + cases. - Live Demo (90 s)
• Ask: "Explain the procedure for filing an FIR under BNSS."
• Highlight bold / italic markdown, structured answer sections, source links.
• Clear chat, ask "What was the SC view on Triple Talaq?" ‑ show Supreme-Court citations. - Safety & Quality (30 s)
Explain grading nodes, hallucination guard, retry cap. - Tech Highlights (20 s)
Groq Llama-3-70B speed, Chroma vectorstore, LangGraph orchestration. - Closing (30 s)
"Scalable to other jurisdictions, integrates new caselaw instantly via web search. Ready to assist lawyers, journalists, and citizens alike."
- Multi-turn memory using chat_history.
- Feedback buttons hooked to a scoring API for continual improvement.
- Deploy on GPU-backed cloud with authentication & usage analytics.
Prepared by Aman Gusain aka Callmeamann