Architecture Overview
GitaChat follows a clean separation between frontend and backend services:Frontend Architecture
Server-side rendered React application with client-side query caching and optimistic updates
Backend Architecture
RESTful API with async processing, rate limiting, and connection pooling
Vector Search
Semantic search powered by 768-dimensional embeddings and Pinecone
AI Commentary
Context-aware responses generated by OpenAI GPT-4o-mini
Frontend Stack
Next.js 15 + React 19
Framework: Next.js with App Router for server-side rendering and routingKey Features:
- Server and client components for optimal performance
- API routes for backend integration (
/app/api/route.ts) - Static generation for verse pages
- Image optimization and lazy loading
frontend/app/page.tsx handles the main search interfaceTypeScript
Type Safety: Comprehensive type definitions in
app/lib/types.tsTanStack Query (React Query)
State Management: Handles server state with caching and optimistic updatesImplementation (
frontend/app/page.tsx:30-36):Tailwind CSS
Styling: Utility-first CSS framework with custom themeCustom Colors:
- Saffron accent color (
#FF6B35) - Dark mode optimized palette
- Responsive design breakpoints
Clerk Authentication
Auth Provider: User authentication and session managementIntegration:
@clerk/nextjs for seamless auth in API routes- User ID tracking for history
- Protected routes for bookmarks and notes
Supabase
Database: PostgreSQL for user data persistenceTables:
query_history- Search history trackingbookmarks- Saved versesnotes- User notes on versesemail_subscriptions- Daily email preferences
Backend Stack
FastAPI
Python Framework: High-performance async API frameworkMain Application (Key Features:
backend/main.py:77):- Async request handling
- Pydantic validation
- Auto-generated OpenAPI docs
- CORS configuration for web clients
Sentence Transformers (BGE)
Embedding Model: BAAI/bge-base-en-v1.5 for semantic searchConfiguration (Initialization (Performance: Top MTEB (Massive Text Embedding Benchmark) scores for retrieval
backend/config.py:32-33):backend/clients.py:24):Pinecone
Vector Database: Managed vector search with 768-dimensional embeddingsClient Setup (Storage: 700+ verses with metadata (chapter, verse, translation, commentary)
backend/clients.py:16-18):OpenAI GPT-4o-mini
AI Model: Contextual commentary generationClient Configuration (Use Cases:
backend/clients.py:21):- Generate contextual commentary (
utils.py:34) - Summarize traditional commentary (
utils.py:12)
SlowAPI
Rate Limiting: Request throttling to prevent abuseImplementation (
backend/main.py:16,116):Data Flow
Search Query Flow
Step-by-Step Process:
- User Input: User submits query in Next.js frontend
- Frontend API Route:
/app/api/route.tsvalidates and proxies request - Rate Limiting: Frontend checks rate limit (20/min per client)
- Backend Endpoint: FastAPI receives at
/api/query(30/min limit) - Embedding: Query encoded with BGE model + instruction prefix
- Vector Search: Pinecone returns top 8 semantic matches
- Hybrid Ranking: Combines semantic score + keyword matching
- AI Commentary: OpenAI generates contextual response
- Response: Returns best match + 3 related verses
- History: Saves to Supabase if user authenticated
Verse Retrieval Flow
Direct Verse Access:
- Request:
/api/versewith chapter and verse numbers - Metadata Filter: Pinecone query with exact chapter/verse filter
- Response: Returns verse with pre-computed summary
- No AI Call: Uses cached commentary for fast response
Configuration & Environment
Backend Environment
Required Variables (
backend/config.py):PINECONE_API_KEY- Pinecone authenticationPINECONE_INDEX- Index name (e.g., “gitachat”)GPT_KEY- OpenAI API key
Frontend Environment
Required Variables:
BACKEND_URL- Python API endpointNEXT_PUBLIC_CLERK_*- Clerk auth keysNEXT_PUBLIC_SUPABASE_*- Supabase configRESEND_API_KEY- Email serviceREPLICATE_API_TOKEN- Image generation
Performance Optimizations
Frontend Optimizations
- TanStack Query caching reduces redundant API calls
- LocalStorage persists last search for instant back navigation
- Suspense boundaries for progressive loading
- Optimistic updates for bookmark/note actions
Backend Optimizations
- Model pre-loading on startup prevents cold starts
- Connection pooling for Pinecone and OpenAI
- Async request handling with FastAPI
- In-memory cache for all verses (
all_verses_cache)
Database Optimizations
- Pinecone indexes for sub-100ms vector search
- Supabase connection pooling
- Metadata filtering reduces query time
- Pre-computed summaries cached in vectors
AI Optimizations
- GPT-4o-mini selected for speed/cost balance
- 500 token limit for commentary generation
- Temperature 0.7 for consistent quality
- Timeout protection (30s) prevents hanging requests
Deployment Architecture
Frontend Deployment
Platform: Vercel (Next.js optimized)
- Automatic HTTPS and CDN
- Edge caching for static assets
- Serverless API routes
- Preview deployments for PRs
Backend Deployment
Platform: Railway / Render / Fly.io
- Container-based deployment
- Auto-scaling based on traffic
- Health checks at
/health - CORS configured for frontend domain
Security Considerations
Authentication
- Clerk handles all auth flows
- JWT tokens for API authentication
- Secure session management
- User data isolation in Supabase
Rate Limiting
- Frontend: 20 requests/min per client
- Backend: 30 requests/min per IP
- 429 status codes for limit exceeded
- Exponential backoff recommended
Input Validation
- Pydantic models validate all inputs
- 500 character query limit
- Chapter (1-18) and verse (1-78) bounds
- SQL injection protection via Supabase RLS
API Security
- Environment variables for all secrets
- CORS whitelist for allowed origins
- Timeout protection on external calls
- Error messages don’t expose internals