Architecture Overview
High-level design
┌─────────────────────────────────────────────────┐
│ User │
│ (Cloudflare Pages — static files) │
│ ┌──────────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Landing page │ │ Chat │ │ Gurukula │ │ Maharshi │ │
│ │ index.html │ │ chat.html│ │learning. │ │maharshi. │ │
│ │ (/) │ │ (/chat) │ │html(/lrn)│ │html(/... │ │
│ └──────────────┘ └─────┬────┘ └──────────┘ └──────────┘ │
└───────────────────────────┼──────────────────────┘
│ POST /api/chat
▼
┌─────────────────────────────────────────────────┐
│ API Layer (GCP Cloud Run) │
│ ┌─────────────────────────────────────────┐ │
│ │ FastAPI (Python) │ │
│ │ - Rate limiter (30/min/IP) │ │
│ │ - CORS locked to frontend domain │ │
│ │ - Request validation │ │
│ │ - Auth (JWT, email/password) │ │
│ └────────────────┬────────────────────────┘ │
│ ┌────────────────┴────────────────────────┐ │
│ │ Vedic Knowledge Base (BM25 search) │ │
│ │ DharmicData: Rig/Yajur/Atharva Vedas │ │
│ │ HuggingFace: Sama Veda, Krishna Yajur │ │
│ │ (Griffith/Keith translations) │ │
│ │ Memory-optimized BM25, caps @ 10K/3K │ │
│ │ ✓ Tried first before web search │ │
│ └────────────────┬────────────────────────┘ │
│ ┌────────────────┴────────────────────────┐ │
│ │ Web Search (Tavily → Wiki → DDG) │ │
│ │ Tavily (API key) as primary fallback │ │
│ └────────────────┬────────────────────────┘ │
│ ┌────────────────┴────────────────────────┐ │
│ │ Context + Prompt + Question │ │
│ │ sent to Sarvam AI for synthesis │ │
│ └────────────────┬────────────────────────┘ │
│ ┌────────────────┴────────────────────────┐ │
│ │ Database (Neon Postgres) │ │
│ │ Users, conversations, history │ │
│ └─────────────────────────────────────────┘ │
└──────────────────┬──────────────────────────────┘
│ Sarvam AI API
▼
┌─────────────────────────────────────────────────┐
│ Sarvam AI (sarvam-105b) │
│ 128K context, hosted by Sarvam AI │
└─────────────────────────────────────────────────┘
Key components
1. Frontend (veda-guru-ai-ui)
- Static site deployed on Cloudflare Pages
- Custom domain:
veda-guru.com - Routing handled via Cloudflare Pages (
_redirects+_headersfiles): /— Landing page (index.html, dharmic-themed entry with Om, CTAs)/chat— Chat interface (chat.html, main Q&A)/learn— Gurukula learning paths (learning.html, 4 margs with progress tracking)/maharshi— Maharshi voice page (maharshi.html, mic + TTS)/*catch-all — Landing page
- Vanilla HTML/CSS/JS with
marked.jsfor markdown rendering - Responsive design with Vedic-themed styling
- Auth modal (login/register), history panel, profile modal
- Invocation overlay (handles backend cold start)
2. API Service (veda-guru-ai-api)
- FastAPI (Python) deployed on GCP Cloud Run
- Endpoints:
/api/chat,/api/sage,/api/tts,/api/image-terms,/api/suggest - Vedic KB endpoints:
/api/vedic/search,/api/vedic/stats,/api/vedic/translations - Learning endpoints:
/api/learning/courses,/api/learning/courses/{id}/{idx},/api/learning/progress,/api/learning/daily-reading - Pronunciation endpoint:
/api/pronunciation/terms - Auth endpoints:
/api/auth/register,/api/auth/login,/api/auth/me,/api/auth/profile - History endpoints:
/api/history(CRUD for conversations) - Feedback endpoint:
/api/feedback - Rate limited: 30 requests/minute per IP via
slowapi - CORS: Restricted to the frontend domain only
- Request timeout: 120s (handles Render cold starts)
- Corpus downloaded at startup: DharmicData (Rig/Yajur/Atharva) + HuggingFace IndianVedasOriginal
3. Database
- Neon Postgres (free tier, persistent) or SQLite fallback
- Stores: user accounts (email, hashed password, name), conversation history (JSON)
- Feedback stored in
feedbacktable (upsert by user+message) - Token-based auth (HMAC-signed, 30-day expiry)
4. Vedic Knowledge Base
- Primary: DharmicData JSON corpus (Rigveda, Yajurveda Shukla Madhyandina+Kanva, Atharvaveda)
- Secondary: HuggingFace IndianVedasOriginal (Sama Veda, Krishna Yajur Veda, additional Rig/Atharva — Griffith/Keith translations)
- Search: BM25 (in-memory, memory-optimized — no redundant token lists, precomputed
dl) - Caps:
MAX_CORPUS_ENTRIES=10_000,HF_MAX_ENTRIES=3_000to stay within 512MB - Version tracking: GitHub commit SHA stored in
.versionfile; only re-downloads when upstream changes - Filtering: Only Vedic collections loaded; non-Vedic HF entries (Charaka, Sushruta, etc.) skipped
5. Web Search
- Primary: Tavily (API key, best results)
- Fallback: Wikipedia API (free, always available, great for Vedic topics)
- Second fallback: DuckDuckGo via
duckduckgo-searchlibrary - Last resort: Google via
googlesearch-python
6. LLM — Sarvam AI
- Model:
sarvam-105b(128K context window) - Auth:
api-subscription-keyheader - Endpoint:
POST https://api.sarvam.ai/v1/chat/completions
7. TTS — Sarvam AI
- Model:
bulbul:v3 - Voice:
shubh(en-IN) - Returns base64 audio, decoded and played via Web Audio API
Data flow (chat query)
User: "What does Rig Veda say about truth?"
1. POST /api/chat { message, history }
2. Check rate limit → reject if over quota
3. Search Vedic corpus (BM25 over DharmicData + HF Vedas)
4. If corpus results found → use as context (skip web search)
5. If not → search web (Tavily → Wikipedia → DuckDuckGo)
6. Format results + question into prompt
7. Send prompt to Sarvam AI /v1/chat/completions
8. Parse response, attach source references
9. If authenticated, auto-save to conversation history
10. Return { reply, sources } to frontend
11. Frontend renders markdown with code block styling
12. Non-blocking: fetch follow-up suggestions via /api/suggest
Data flow (learning content)
User opens /learn in browser
1. GET /api/learning/courses
2. Returns 3 tiers: beginner(10), intermediate(12), advanced(14)
3. User clicks an adhyaya
4. GET /api/learning/courses/{id}/{idx}?top_k=5
5. Backend runs BM25 queries from course definition against Vedic corpus
6. Returns lesson content + real verses with source labels
7. Frontend renders lesson, objectives, verses, reflection questions
8. GET /api/pronunciation/terms — key Sanskrit terms with IAST + audio
9. Quiz generated client-side from lesson content (3-5 questions)
10. Progress saved to localStorage + syncs to server via PUT /api/learning/progress
11. Daily reading: GET /api/learning/daily-reading (random corpus verse)
Security
- Rate limiting: 30 requests/minute per IP
- CORS: Only the frontend domain is allowed
- LLM key:
SARVAM_API_KEYstored as GCP Cloud Run env var, never in code - JWT auth: HMAC-signed tokens with 30-day expiry
- Passwords: SHA-256 with per-user salt, never stored in plaintext
- Database:
DATABASE_URLenv var, never in code - No PII stored beyond email: Email used only for login identification
- Cold start: GCP Cloud Run scales to zero after 5min idle — first request may take 10-30s
Repositories
| Repo | Purpose | URL |
|---|---|---|
veda-guru-ai-docs |
Documentation site | GitHub |
veda-guru-ai-api |
FastAPI chatbot backend | GitHub |
veda-guru-ai-ui |
Chat frontend | GitHub |