A blazingly fast FastAPI intelligent routing engine for text, voice, and multimodal Gemini & Groq with RAG capabilities.
The ChatVerse AI Backend is the powerful middleware orchestrating intelligent AI interactions between the React frontend and multiple LLM providers. Built with asynchronous Python using FastAPI and powered by Google Gemini, Groq, and RAG technologies, this server handles API key security, dynamic personas, multimodal processing, and vector-based document retrieval.
Key Mission: Provide a scalable, secure, and intelligent API layer that supports text chat, voice processing, image analysis, and intelligent document retrieval with fallback mechanisms.
Key Separation:
- P2P chat between users is direct Frontend-to-Firebase, bypassing the backend entirely for maximum latency optimization
- AI conversations flow through the backend for LLM processing and RAG context retrieval
- This decoupled architecture ensures efficient resource utilization and real-time user experiences
- Frontend: ChatVerse AI Frontend
┌─────────────────────────────────────────────────────────────┐
│ ChatVerse AI Backend │
│ Architecture Stack │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ FRONTEND INTERFACE │
│ (React + TypeScript Frontend) │
└────────────────────┬────────────────────────────────────────┘
│ HTTP/REST
┌────────────────────▼────────────────────────────────────────┐
│ FASTAPI SERVER (Async Python 3.11) │
│ ├─ CORS Middleware │
│ ├─ Authentication (Firebase) │
│ └─ Request Routing & Validation │
└────────────────────┬────────────────────────────────────────┘
│
┌────────────┼────────────┐
│ │ │
┌────▼──┐ ┌──────▼──┐ ┌──────▼──┐
│ GEMINI│ │ GROQ │ │ RAG │
│ 2.5 │ │ LLM │ │ PIPELINE│
│ Flash │ │ (Fback) │ │ │
└────┬──┘ └────┬────┘ └────┬────┘
│ │ │
└──────────┼────────────┘
│
┌──────────┴───────────┬─────────────┐
│ │ │
┌────▼──────┐ ┌───────▼────┐ ┌───▼──────┐
│ PINECONE │ │ SUPABASE │ │ FIREBASE │
│ Vector DB │ │ File Store │ │ Auth/DB │
│ (Embeddings) │ (Documents)│ │ (Data) │
└───────────┘ └────────────┘ └──────────┘
│ │ │
└──────────────────────┴─────────────┘
│
┌──────▼──────┐
│ EXTERNAL │
│ SERVICES │
├─ Speech2Text│
├─ PDF Parser │
└─ Embeddings │
└─────────────┘
| Component | Technology | Purpose |
|---|---|---|
| Framework | FastAPI | High-performance async web framework |
| Runtime | Python 3.11 | Server runtime |
| LLM #1 | Google Gemini 2.5 Flash | Primary AI model with vision capabilities |
| LLM #2 | Groq API | Fallback LLM for chat operations |
| Vector DB | Pinecone | Semantic search & embeddings storage |
| RAG Engine | Custom RAG Pipeline | Document-based context retrieval |
| File Storage | Supabase (PostgreSQL) | Document metadata & file storage |
| Authentication | Firebase Admin SDK | User verification & JWT validation |
| Audio Processing | SpeechRecognition | Audio-to-text transcription |
| PDF Processing | PyPDF2 + pdfplumber | PDF content extraction |
| Image Processing | Pillow | Image manipulation & analysis |
| HTTP Client | aiohttp + requests | Async HTTP calls |
| Embeddings | Gemini API | Text vectorization for RAG |
| Deployment | Render | Cloud hosting platform |
chatverse-ai-backend/
├── main.py # FastAPI application & endpoints
├── requirements.txt # Python dependencies
├── .env # Environment variables (not in git)
├── config/
│ ├── config.py # Configuration loader
│ └── __init__.py
├── auth/
│ ├── firebase_auth.py # Firebase token verification
│ └── __init__.py
├── services/
│ ├── chat_service.py # Chat operations
│ ├── rag_service.py # RAG pipeline
│ ├── document_service.py # Document management
│ ├── data_processor.py # Content processing
│ ├── embeddings.py # Vector generation
│ ├── pinecone_handler.py # Vector DB operations
│ ├── supabase_handler.py # File storage operations
│ └── __init__.py
├── test/
│ ├── test_models_google.py
│ ├── test_models_groq.py
│ └── __init__.py
└── README.md # This file
- Primary: Google Gemini 2.5 Flash with extended context
- Fallback: Groq API for redundancy
- Dynamic persona support (Assistant, Therapist, Study Buddy, Roast Bot)
- Upload documents (PDF, images, audio)
- Automatic embedding generation
- Semantic search using Pinecone vector database
- Context-aware responses based on document content
- Image Analysis: Upload images for Gemini vision analysis
- Voice Transcription: Real-time audio-to-text conversion from
.webmfiles - Multipart Payload Handling: Native binary file processing
- Upload and store documents securely
- Retrieve document embeddings from Pinecone
- Delete documents with cascading cleanup
- User-specific document isolation
- Chat history storage in Firestore
- Delete specific conversations
- Clean history of interactions
- Secure token-based authentication
- User session management
- Data persistence in Firestore
| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Server health check |
POST /api/chat
Purpose: Direct chat with LLM (Gemini → Groq fallback)
Authentication: No
Request Body:
{
"message": "Your message here",
"history": [
{"sender": "user", "text": "Previous user message"},
{"sender": "assistant", "text": "Previous AI response"}
],
"persona": "Assistant"
}Response:
{
"response": "AI generated response text"
}POST /api/rag-chat
Purpose: Query with document context retrieval
Authentication: Firebase Token Required ✅
Request Body:
{
"query": "What is mentioned about topic X in my documents?"
}Response:
{
"response": "Context-aware response from RAG pipeline",
"sources": [
{
"document_id": "doc_123",
"filename": "document.pdf",
"relevance": 0.95
}
]
}POST /api/upload-document
Purpose: Upload document for RAG indexing
Authentication: Firebase Token Required ✅
Content-Type: multipart/form-data
Request Parameters:
file(File): PDF, image, or audio file- Auto-extracted:
user_idfrom Firebase token
Response:
{
"success": true,
"filename": "document.pdf",
"message": "File is being processed. It will be available for queries shortly."
}GET /api/documents
Purpose: Retrieve all documents uploaded by user
Authentication: Firebase Token Required ✅
Response:
{
"documents": [
{
"doc_id": "doc_123",
"filename": "research.pdf",
"upload_date": "2024-01-15T10:30:00Z",
"file_size": 2048576,
"status": "processed"
}
]
}DELETE /api/documents/{doc_id}
Purpose: Delete single document from all storage systems
Authentication: Firebase Token Required ✅
Path Parameters:
doc_id: Document identifier
Response:
{
"success": true,
"message": "Document deleted successfully"
}DELETE /api/documents/delete-all
Purpose: Clear all documents for authenticated user
Authentication: Firebase Token Required ✅
Response:
{
"success": true,
"message": "All documents deleted successfully"
}POST /api/image-scan
Purpose: Analyze image with Gemini Vision
Authentication: No
Content-Type: multipart/form-data
Request Parameters:
file(File): Image file (.jpg, .png, etc.)prompt(string): Analysis prompt
Response:
{
"response": "Detailed image analysis from Gemini"
}POST /api/voice
Purpose: Convert audio to text
Authentication: No
Content-Type: multipart/form-data
Request Parameters:
file(File): Audio file (.webm, .mp3, .wav, etc.)
Response:
{
"success": true,
"transcript": "Transcribed text from audio",
"filename": "recording.webm"
}DELETE /api/chat/{conversation_id}
Purpose: Delete specific conversation and all messages
Authentication: Firebase Token Required ✅
Path Parameters:
conversation_id: Chat type (e.g., 'assistant', 'rag-analysis', 'therapist')
Response:
{
"success": true,
"message": "Chat conversation 'assistant' deleted successfully"
}Handles intelligent document retrieval and context augmentation:
- Query embedding generation
- Semantic similarity search in Pinecone
- Context ranking and selection
- Fallback handling between Gemini and Groq
Manages document lifecycle:
- Upload processing
- Storage in Supabase
- Metadata in Firestore
- Cascade deletion across systems
Conversation management:
- Message persistence
- History retrieval
- Conversation deletion
- User session tracking
Multimodal content processing:
- PDF text extraction
- Image preprocessing
- Audio transcription
- Data normalization
Vector generation:
- Gemini-powered text embeddings
- Chunk-based processing
- Embedding caching
Vector database operations:
- Embedding storage
- Semantic search
- Vector deletion
File and data storage:
- Document file upload
- Metadata persistence
- File retrieval
- Python 3.11+
- pip package manager
- Git
git clone https://github.com/Rahul-8283/chatverse-ai-backend.git
cd chatverse-ai-backend# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtCreate a .env file in the root directory:
GEMINI_API_KEY=your_gemini_api_key
GROQ_API_KEY=your_groq_api_key
PINECONE_INDEX_NAME=your_pinecone_index_name
PINECONE_API_KEY=your_pinecone_api_key
SUPABASE_URL=your_supabase_url
SUPABASE_BUCKET=your_supabase_bucket_name
SUPABASE_SECRET_KEY=your_supabase_secret_key
FIREBASE_PROJECT_ID=project_id
FIREBASE_CLIENT_ID=your_firebase_client_id
FIREBASE_CLIENT_EMAIL=your_firebase_client_email
FIREBASE_PRIVATE_KEY=your_firebase_private_key
FIREBASE_PRIVATE_KEY_ID=your_firebase_private_key_id- Go to Firebase Console
- Navigate to Project Settings → Service Accounts
- Click "Generate New Private Key"
- Save as
firebase-service-account-key.jsonin project root
uvicorn main:app --reload --host 0.0.0.0 --port 8000Access API Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Core Framework:
├─ FastAPI (Web framework)
├─ Uvicorn (ASGI server)
└─ Python-multipart (Form data parsing)
AI/ML:
├─ google-generativeai (Gemini API)
├─ groq (Groq LLM API)
├─ pinecone-client (Vector DB)
└─ numpy (Numerical computing)
Data Processing:
├─ PyPDF2 (PDF reading)
├─ pdfplumber (PDF extraction)
├─ Pillow (Image processing)
└─ pandas (Data manipulation)
Cloud Services:
├─ firebase-admin (Firebase integration)
├─ supabase (Backend as a Service)
└─ requests (HTTP client)
Audio:
└─ SpeechRecognition (Audio transcription)
This project is licensed under the MIT License. See LICENSE file for details.
