A modern full-stack chat application built with Next.js and FastAPI, featuring AI-powered responses using RAG (Retrieval Augmented Generation) for domestic worker rights information. The system processes government documents and provides accurate, context-aware answers based on legal regulations.
- AI-Powered Chat: Intelligent responses using Google Gemini 2.0 Flash Exp and Pinecone vector search
- RAG System: 3-layer search engine for accurate, context-aware responses from legal documents
- Jurisdiction-Aware: Remembers user's jurisdiction preference across conversations
- Modern UI: Clean, Gemini-inspired design with customizable themes (Caffeine, Neo Brutalism) and dark/light modes
- Full-Stack: Complete REST API backend with MongoDB persistence
- Type-Safe: Full TypeScript support on frontend, Pydantic models on backend
- No Auth Required: Works immediately without authentication (optional auth available)
- Document-Based: Answers sourced from verified government documents and legal regulations
Architecture diagram showing data ingestion pipeline (top) and user request processing pipeline (bottom)
Fairly uses a two-pipeline architecture: Data Ingestion & Preprocessing and User Request Processing.
Government Websites (PDFs)
β
Local PC (Download & Store)
β
Python Script (Preprocessing & Semantic Matching)
βββ Parse & Preprocess PDFs
βββ Generate Vector Embeddings (Google Gemini Embedding Model)
β βββ Model: gemini-embedding-exp-03-07 (3072 dimensions)
βββ Store Data β MongoDB
βββ Store Vector Embeddings β Pinecone
Next.js Frontend
β (User Request)
FastAPI Backend
β (Generate Query Embedding)
Google Gemini Embedding Model
βββ Model: gemini-embedding-exp-03-07 (3072 dimensions)
β (Vector Embedding)
Pinecone (Vector Database - Similarity Search)
β (Retrieved Document Chunks)
Gemini 2.0 Flash Exp (AI Model)
β (Validated Output)
FastAPI Backend
β (Response)
Next.js Frontend (Display to User)
- User Query β FastAPI receives user message
- Query Embedding β Google Gemini Embedding Model (gemini-embedding-exp-03-07) converts query to 3072-dimensional vector
- Vector Search β FastAPI queries Pinecone for semantically similar document chunks using the query embedding
- Context Retrieval β Pinecone returns relevant document chunks from MongoDB
- AI Generation β Gemini 2.0 Flash Exp generates response using retrieved context
- Validation β Response is validated and returned to user
HACK-RUC/
βββ frontend/ # Next.js frontend application
β βββ app/ # Next.js app router pages
β βββ components/ # React components (UI, auth, chat)
β βββ lib/ # API clients, contexts, types, utils
β βββ config/ # Configuration files
βββ backend/ # FastAPI backend application
β βββ api/ # FastAPI application
β β βββ routes/ # API endpoints (chats, messages)
β β βββ services/ # Business logic (chat, message, AI)
β β βββ models.py # Pydantic models
β βββ search engine/ # RAG search engine (3-layer architecture)
β βββ data-preprocessing/ # PDF parsing & preprocessing scripts
β βββ embdedding/ # Vector embedding generation scripts
βββ README.md # This file
- Node.js 18+ and npm
- Python 3.8+
- MongoDB Atlas account (or local MongoDB)
- Google Gemini API key
- Pinecone account and API key
cd backend
# Install dependencies
pip install -r requirements.txt
# Configure environment variables
# Copy .env.example to .env and fill in your credentials
# Required variables:
# - MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_CLUSTER, MONGODB_APP_NAME
# - MONGODB_DATABASE, MONGODB_COLLECTION
# - GEMINI_API_KEY
# - PINECONE_API_KEY, PINECONE_INDEX_NAME
# - API_PORT, API_HOST, CORS_ORIGINS
# Create database indexes
python api/create_indexes.py
# Start the server
python run_api.pyThe API will be available at http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
cd frontend
# Install dependencies
npm install
# Configure environment variables (optional)
# Create .env.local with:
# NEXT_PUBLIC_API_URL=http://localhost:8000
# NEXT_PUBLIC_USE_MOCK_API=false
# Start development server
npm run devThe frontend will be available at http://localhost:3000
# MongoDB Configuration
MONGODB_USERNAME=your_username
MONGODB_PASSWORD=your_password
MONGODB_CLUSTER=your_cluster.mongodb.net
MONGODB_APP_NAME=your_app_name
MONGODB_DATABASE=fairly
MONGODB_COLLECTION=fairly_chunks
# Google Gemini API
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-2.0-flash-exp
EMBEDDING_MODEL=gemini-embedding-exp-03-07
EMBEDDING_DIMENSION=3072
# Pinecone Configuration
PINECONE_API_KEY=your_pinecone_api_key
PINECONE_INDEX_NAME=domestic-worker-rights
# Search Engine Configuration
TOP_K_RESULTS=8
SIMILARITY_THRESHOLD=0.65
# API Configuration
API_PORT=8000
API_HOST=0.0.0.0
CORS_ORIGINS=http://localhost:3000NEXT_PUBLIC_API_URL=http://localhost:8000
NEXT_PUBLIC_USE_MOCK_API=false# Clear Python cache
Get-ChildItem -Path . -Recurse -Directory -Filter "__pycache__" | Remove-Item -Recurse -Force
# Clear Next.js cache
Remove-Item -Path "frontend\.next" -Recurse -Force -ErrorAction SilentlyContinuecd backend
python run_api.py # Start with auto-reload
python test_api.py # Run API testsBackend runs on: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
cd frontend
npm run dev # Start development server
npm run build # Build for production
npm run lint # Run linterFrontend runs on: http://localhost:3000
Terminal 1 (Backend):
cd backend
python run_api.pyTerminal 2 (Frontend):
cd frontend
npm run devGET /api/chats- Get all chatsGET /api/chats/{chat_id}- Get specific chatPOST /api/chats- Create new chatPATCH /api/chats/{chat_id}- Update chatDELETE /api/chats/{chat_id}- Delete chat
GET /api/chats/{chat_id}/messages- Get all messagesPOST /api/chats/{chat_id}/messages- Create message (triggers AI response)PATCH /api/chats/{chat_id}/messages/{message_id}- Update messageDELETE /api/chats/{chat_id}/messages/{message_id}- Delete message
The application uses a sophisticated 3-layer RAG (Retrieval Augmented Generation) system powered by Google Gemini 2.0 Flash Exp:
-
Layer 1: Query Processing & Vectorization
- Jurisdiction detection and normalization
- Query refinement using AI
- Vector embedding generation using Google Gemini Embedding Model
- Model:
gemini-embedding-exp-03-07 - Dimensions: 3072
- Model:
-
Layer 2: Vector Similarity Search
- Semantic search in Pinecone vector database
- Retrieves top-k most relevant document chunks
- Filters by jurisdiction when specified
-
Layer 3: Response Generation
- Context-aware response generation using Gemini 2.0 Flash Exp
- Validates response relevance to query
- Returns human-readable answers based on retrieved documents
When a user sends a message:
- User Message Saved β Stored in MongoDB immediately
- Jurisdiction Detection β If message contains only jurisdiction (e.g., "US federal"), it's stored for future queries
- Background AI Processing β FastAPI triggers asynchronous AI response generation
- Query Embedding β Google Gemini Embedding Model (
gemini-embedding-exp-03-07) converts query to 3072-dimensional vector - Vector Search β Pinecone searches for similar document chunks using the query embedding
- AI Generation β Gemini 2.0 Flash Exp generates response using retrieved context
- Response Saved β Assistant message stored in MongoDB
- Frontend Polling β Frontend polls for AI response (every 2 seconds)
- UI Update β Response displayed when ready
Part of the HACK-RUC project.
This is a hackathon project. For questions or issues, refer to the documentation in each subdirectory.
