How dhyanshah22/lexilaw works
Project Type and Purpose
• LexiLaw is a full‑stack, web‑based corporate legal assistant built with Streamlit. It allows users to query legal acts and case‑law PDFs in natural language using a retrieval‑augmented generation pipeline based on LangChain and Google’s Gemini API, with FAISS for vector similarity search and MongoDB for persistence.
Top‑Level Components
a. Frontend (Streamlit UI)
• Implements sidebar (case selection, filters, dark mode, creativity slider) and main chat interface.
• File: v5‑prod.py (formerly app.py).
b. PDF Data Store
• data/case_pdfs: raw case‑law PDF files.
• data/*.pdf: legal acts.
c. Vector Store Layer
• Prebuilt FAISS indexes under vectorstores/acts and vectorstores/cases.
• build_faiss_vectorstores.py to (re)build indexes for acts.
d. Retrieval & Generation Engine
• PyPDF2 for PDF text extraction (on‑the‑fly for selected cases).
• FAISS for similarity search.
• LangChain’s ConversationalRetrievalChain (ConversationBufferMemory + FAISS retriever).
• ChatGoogleGenerativeAI model (Gemini API) for answer generation.
• gTTS for text‑to‑speech output.
e. Database & Logging
• MongoDB (configured via MONGO_URI) to store chat logs, metadata, source documents, timestamps.
f. External Services
• Google Gemini API (requires GEMINI_API_KEY).
Relationships and Data Flow
1. User interacts with Streamlit UI → issues a natural‑language query or selects a case.
2. If a case is newly selected:
• PDF Extraction Module reads data/case_pdfs/<selected>.PDF via PyPDF2.
• On‑the‑fly FAISS Index Builder converts extracted text into embeddings via Gemini → stores vectors in memory (or disk).
3. Retrieval Module uses FAISS (acts index or case index) to find relevant document snippets.
4. Generation Module (ChatGoogleGenerativeAI) receives query + retrieved snippets → returns answer text.
5. If TTS enabled: gTTS converts answer to audio.
6. Logging Module writes user query, response text, case ID, sources, timestamp to MongoDB.
7. Response (text ± audio) is rendered back in Streamlit UI.
Architectural Patterns & Design Principles
• Monolithic service with clear separation of concerns: UI layer, data ingestion layer, vector retrieval layer, LLM orchestration layer, persistence layer.
• Retrieval‑Augmented Generation (RAG) for domain‑specific QA.
• Modular scripts: build_faiss_vectorstores.py standalone for offline vectorstore creation.