How zubairmohd/nyayabot works
Project Type and Purpose
• NyayaBot is a full-stack AI legal assistant web application focused on Indian law.
• It provides a React/Next.js frontend, Node.js/Next.js API routes for HTTP endpoints, Python-based NLP/RAG utilities, and a PostgreSQL database for persistence.
• It integrates multiple external AI services (OpenAI, Anthropic, xAI) and supports document upload, processing, and voice interaction.
File Structure Analysis
• Top-level:
• app.py and .streamlit/config.toml → optional Streamlit front end or Python service entry point
• pages/ → Next.js pages and serverless API routes (chat.js, generate-document.js, etc.)
• components/ → React UI modules (chat interface, document uploader, layout)
• utils/ → Python scripts for NLP, document processing, RAG, audio utilities, DB utilities
• uploads/ → file storage for user-uploaded documents
• setup_database.py → DB initialization script
• pyproject.toml & package.json → Python and Node.js dependencies
• Configuration: next.config.js, tailwind.config.js, postcss.config.js
Key Architecture Insights from README
• Frontend: Next.js + Tailwind CSS + Material UI, served on port 5000
• Backend: Next.js API routes that orchestrate calls to Python utilities and external AI APIs
• Python: LangChain-based RAG pipeline (FAISS vector store), PDF parsing, language detection, audio processing
• Data: PostgreSQL for user/session data, vector embeddings; uploads folder for raw documents
• External: OpenAI GPT, Anthropic Claude, xAI Grok
Main Components & Interactions
a. Frontend (Browser / React)
• Components: ChatInterface.js, DocumentUploader.js, Header/Layout
• Interacts via Axios/fetch with Next.js API endpoints under pages/api
b. API Gateway (Next.js serverless functions)
• Endpoints: /api/chat, /api/generate-document, /api/upload-document, /api/transcribe, /api/text-to-speech
• Responsibilities: input validation, session management, orchestrating Python utilities and external AI calls
c. Python NLP & RAG Service
• Modules: utils/process_document.py, rag_utils.py, retrieve_context.py, llm_utils.py
• Responsibilities:
• Document parsing (PDF → text)
• Chunking and embedding with LangChain + FAISS
• Retrieval of context for RAG
• Calling external LLM APIs via OpenAI/Anthropic/xAI SDKs
• Audio/text conversion via audio_utils.py
• Optionally exposed via app.py (Streamlit) or invoked via child-process/HTTP from Node.js
d. Data Layer
• PostgreSQL (via utils/db_utils.py and psycopg2) — stores user chats, embedding metadata, documents
• File storage (uploads/) — stores uploaded PDF/DOCX files temporarily
• Vector store (FAISS, in-memory or persisted on disk)
e. External AI Services
• OpenAI GPT-4o / GPT-3.5, Anthropic Claude, xAI Grok
• Accessed using API keys from environment variables
Architectural Patterns & Design Principles
• Separation of Concerns:
• UI vs. API vs. Python processing vs. Data store
• Serverless Functions: Next.js API routes handle stateless requests
• Microservice-style Python modules for specialized NLP and RAG tasks
• RAG Pattern: storing embeddings in FAISS, retrieving context dynamically
• Extensibility: support for multiple AI backends, modular utility scripts
• Security: environment-based API keys, PostgreSQL connection strings
Final Advice
• Be as detailed as possible: include every logical component (chat, transcription, TTS, document upload/generation).
• Integration points: UI ↔ API ↔ Python ↔ DB ↔ External APIs.
• RAG pipeline specifics (embedding storage, retrieval, context assembly) since it’s core to NyayaBot’s intelligence.