Skip to content
deepshelkePublic

About

Fairly is an AI-powered chat app helping domestic workers understand legal rights who provides instant, accurate answers from verified legal documents!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fairly - Domestic Worker Rights Chat Application

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.

🎯 Features

  • 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

πŸ—οΈ System Architecture

System Architecture Diagram

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.

Data Ingestion Pipeline

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

User Request Pipeline

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)

RAG (Retrieval Augmented Generation) Flow

  1. User Query β†’ FastAPI receives user message
  2. Query Embedding β†’ Google Gemini Embedding Model (gemini-embedding-exp-03-07) converts query to 3072-dimensional vector
  3. Vector Search β†’ FastAPI queries Pinecone for semantically similar document chunks using the query embedding
  4. Context Retrieval β†’ Pinecone returns relevant document chunks from MongoDB
  5. AI Generation β†’ Gemini 2.0 Flash Exp generates response using retrieved context
  6. Validation β†’ Response is validated and returned to user

πŸ“ Project Structure

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

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ and npm
  • Python 3.8+
  • MongoDB Atlas account (or local MongoDB)
  • Google Gemini API key
  • Pinecone account and API key

1. Backend Setup

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.py

The API will be available at http://localhost:8000

2. Frontend Setup

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 dev

The frontend will be available at http://localhost:3000

Backend (.env)

# 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:3000

Frontend (.env.local)

NEXT_PUBLIC_API_URL=http://localhost:8000
NEXT_PUBLIC_USE_MOCK_API=false

πŸ› οΈ Development

Starting Fresh (Clear Caches)

# 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 SilentlyContinue

Backend Development

cd backend
python run_api.py          # Start with auto-reload
python test_api.py          # Run API tests

Backend runs on: http://localhost:8000

Frontend Development

cd frontend
npm run dev                 # Start development server
npm run build               # Build for production
npm run lint                # Run linter

Frontend runs on: http://localhost:3000

Running Both Servers

Terminal 1 (Backend):

cd backend
python run_api.py

Terminal 2 (Frontend):

cd frontend
npm run dev

πŸ“‘ API Endpoints

Chat Endpoints

  • GET /api/chats - Get all chats
  • GET /api/chats/{chat_id} - Get specific chat
  • POST /api/chats - Create new chat
  • PATCH /api/chats/{chat_id} - Update chat
  • DELETE /api/chats/{chat_id} - Delete chat

Message Endpoints

  • GET /api/chats/{chat_id}/messages - Get all messages
  • POST /api/chats/{chat_id}/messages - Create message (triggers AI response)
  • PATCH /api/chats/{chat_id}/messages/{message_id} - Update message
  • DELETE /api/chats/{chat_id}/messages/{message_id} - Delete message

🧠 AI Integration & RAG System

The application uses a sophisticated 3-layer RAG (Retrieval Augmented Generation) system powered by Google Gemini 2.0 Flash Exp:

3-Layer Architecture

  1. 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
  2. Layer 2: Vector Similarity Search

    • Semantic search in Pinecone vector database
    • Retrieves top-k most relevant document chunks
    • Filters by jurisdiction when specified
  3. 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

Request Processing Flow

When a user sends a message:

  1. User Message Saved β†’ Stored in MongoDB immediately
  2. Jurisdiction Detection β†’ If message contains only jurisdiction (e.g., "US federal"), it's stored for future queries
  3. Background AI Processing β†’ FastAPI triggers asynchronous AI response generation
  4. Query Embedding β†’ Google Gemini Embedding Model (gemini-embedding-exp-03-07) converts query to 3072-dimensional vector
  5. Vector Search β†’ Pinecone searches for similar document chunks using the query embedding
  6. AI Generation β†’ Gemini 2.0 Flash Exp generates response using retrieved context
  7. Response Saved β†’ Assistant message stored in MongoDB
  8. Frontend Polling β†’ Frontend polls for AI response (every 2 seconds)
  9. UI Update β†’ Response displayed when ready

πŸ“ License

Part of the HACK-RUC project.

🀝 Contributing

This is a hackathon project. For questions or issues, refer to the documentation in each subdirectory.

About

Fairly is an AI-powered chat app helping domestic workers understand legal rights who provides instant, accurate answers from verified legal documents!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages