Skip to content

dibyo10/KrishnaSaarthi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Krishna Saarthi — Backend

Go backend for Krishna Saarthi, a Bhagavad Gita-inspired philosophical guide powered by Sarvam AI.

Stack

  • Go — HTTP server (net/http)
  • Sarvam AI (sarvam-105b) — LLM for response generation
  • Supermemory — Long-term memory per user across sessions
  • MongoDB Atlas — Persists users, sessions, and messages
  • Clerk — JWT-based authentication
  • Pinecone — Vector database for Gita verse retrieval (RAG)
  • Ollama (nomic-embed-text) — Local embeddings for RAG queries (768 dimensions)

Project Structure

backend/
├── cmd/
│   ├── server/main.go          # Entry point, route registration
│   └── ingest/main.go          # One-time script to ingest Gita verses into Pinecone
├── constants/                  # System prompt
├── data/
│   ├── verse.json              # 701 Gita verses (Sanskrit + transliteration)
│   ├── translation.json        # English translations (Swami Gambirananda)
│   └── chapters.json           # 18 chapter summaries
├── internal/
│   ├── db/mongo.go             # MongoDB connection
│   ├── handlers/
│   │   ├── chat.go             # POST /chat handler
│   │   └── health.go           # GET /health handler
│   ├── models/                 # Request/response structs
│   ├── services/
│   │   ├── sarvam.go           # Sarvam AI client
│   │   ├── supermemory.go      # Supermemory client
│   │   ├── chat_store.go       # MongoDB read/write logic
│   │   └── rag.go              # Pinecone + Ollama RAG service
│   └── session/                # In-memory session store (legacy)
└── middlewares/
    ├── auth.go                 # Clerk JWT verification
    └── cors_middleware.go      # CORS headers

Request Flow

POST /chat
  → CORS middleware
  → Auth middleware (verify Clerk JWT, extract userID)
  → Search Supermemory for relevant long-term user context
  → Embed user message via Ollama (nomic-embed-text)
  → Query Pinecone for top 3 relevant Gita verses (RAG)
  → Load last 10 messages for session from MongoDB
  → Build prompt: system + memory context + verse context + history
  → Call Sarvam AI
  → Save user + assistant messages to MongoDB
  → Store exchange in Supermemory (async goroutine)
  → Return response

Routes

Method Path Auth Description
GET /health No Health check
POST /chat Yes Send a message
POST /auth/sync Yes Upsert user on first sign-in
GET /sessions Yes List all sessions for a user
GET /sessions/{id}/messages Yes Load messages for a session

Headers (POST /chat)

Header Required Description
X-Session-ID Yes UUID per browser tab/session
Authorization Yes Bearer <clerk_jwt>

Environment Variables

Create a .env file in the project root:

SARVAM_API_KEY=
SUPERMEMORY_API_KEY=
CLERK_SECRET_KEY=sk_test_
MONGO_DB_URI=mongodb+srv://user:password@cluster.mongodb.net/krishna-saarthi
PINECONE_API_KEY=
PINECONE_INDEX_URL=https://bhagavad-gita-xxxx.svc.us-east-1-aws.pinecone.io
OLLAMA_URL=http://localhost:11434

Getting Started

# Install dependencies
go mod tidy

# Start Ollama (required for RAG embeddings)
ollama serve
ollama pull nomic-embed-text

# Run the server
go run cmd/server/main.go

Server starts on :8080.

RAG — Gita Verse Ingestion

The ingestion script embeds all 701 verses + 18 chapter summaries into Pinecone. This is a one-time setup:

# Copy data files to backend/data/
# Then run:
go run cmd/ingest/main.go

This ingests 719 vectors total (701 verses + 18 chapters) using nomic-embed-text (768 dimensions) via Ollama. Each verse is stored with its translation (Swami Gambirananda), transliteration, word meanings, chapter number, and verse reference (e.g. BG 2.47).

At query time, the user's message is embedded with the same model and the top 3 most semantically similar verses are retrieved and injected into the system prompt.

Memory Architecture

Krishna Saarthi uses a three-layer memory system:

Layer Storage Scope Purpose
Short-term MongoDB Per session (last 10 messages) In-conversation context
Long-term Supermemory Per user (across all sessions) User identity, patterns, history
Knowledge Pinecone Global (all users) Verified Gita verses for RAG

MongoDB Collections

Collection Description
users Created on first sign-in via /auth/sync
sessions One per browser session, keyed by sessionId
messages All chat messages with userId, sessionId, role, content

Session Persistence

  • Each browser tab generates a unique X-Session-ID (UUID)
  • The active session ID is persisted in localStorage so refreshing restores the last conversation
  • Draft input is also persisted in localStorage and restored on refresh
  • Past sessions are listed in the sidebar and fully reloadable

Adding a New Route

  1. Add the handler in internal/handlers/
  2. Register it in cmd/server/main.go
  3. Wrap with AuthMiddleware if it requires authentication
  4. Add any new request/response structs to internal/models/

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages