
Tech Stack
Description
LegalLaw is an AI legal assistant for the Bangladesh Penal Code, 1860. Users can ask what an offence means, check a punishment, or describe a situation, and it answers using only the relevant sections of the Penal Code, showing the exact section text so every answer can be verified.
It started as our Team 6 project for CSE 299 at North South University in 2025. That version used LangChain, a Chroma index and Mistral running locally through Ollama, which needed a CUDA GPU to run. In October 2026 I rebuilt it so anyone can use it for free: the LLM moved to a hosted, OpenAI-compatible API (Google Gemini's free tier) with automatic failover to backup models, embeddings run on CPU, and the three services are deployed on Vercel and Render.
- Built a retrieval pipeline over 560 Penal Code sections from 25 chapters: follow-up questions are rewritten into standalone queries, any section named in the question is always retrieved, and the LLM answers only from the retrieved text.
- Evaluated retrieval with a held-out question set: the expected section is in the top 6 results for 88.5% of 348 questions (Recall@6), measured on the raw questions before any LLM rewriting.
- Made the service resilient on free tiers: when a model is rate-limited, overloaded or slow, it automatically retries with backup models, and returns clear 429/503/504 errors when all of them fail.
- Kept the provider swappable: Gemini, Groq or OpenRouter (or any OpenAI-compatible API) can be selected with environment variables, with no code changes.
- Containerized the FastAPI service with the vector index built at Docker build time, running on Render's free plan in about 140 MB of memory.
- Wrote an Express + TypeScript API between the website and the model service that validates input, enforces CORS, keeps the service key server-side and turns model-service failures into clear errors, with Swagger docs and CI that type-checks and deploys to Vercel.
- Redesigned the Next.js chat: structured answer cards, cited-section source cards, follow-up suggestions generated in the browser (no extra LLM calls), conversation memory that survives a reload, progress steps, retry on errors, and light and dark themes.
Page Info
Architecture
Three services, all on free tiers: a Next.js website and an Express API on Vercel, and a FastAPI retrieval service in Docker on Render that calls a hosted LLM. The service key stays on the server and never reaches the browser.

Chat
Starter questions in three groups (look up an offence, analyze a scenario, check a punishment) show new users what they can ask.

Progress Steps
While an answer is generated, the chat shows each stage: searching the 560 sections, reading the most relevant ones, and writing the answer.

Grounded, Cited Answers
Each answer is split into labelled parts (Offence, Implications, Defence, Punishment for scenarios) and followed by expandable cards with the actual text of every cited section, so the answer can be checked.
