An intelligent document-based question answering API built with FastAPI, Google Gemini 2.0, and MongoDB. The system uses Retrieval-Augmented Generation (RAG) to provide AI-powered answers from uploaded documents with real-time WebSocket streaming.
- Multi-User Support: User authentication with MongoDB and JWT-based sessions
- Document Processing: Upload and process PDF, TXT, Markdown, and JSON documents
- Semantic Search: FAISS vector similarity search with per-user data isolation
- AI-Powered Q&A: Google Gemini for answer generation with source attribution
- Real-time Streaming: WebSocket support for streaming responses
- Dual Intelligence Modes: RAG mode (document-based) and general chat mode
- User Management: Secure registration, login, and JWT authentication with MongoDB storage
- Document Upload: Support for PDF, TXT, Markdown, and JSON with automatic text extraction and chunking
- Vector Search: FAISS for efficient similarity search using Gemini text embedding model
- Question Answering: Context-aware responses using Google Gemini with source citations
- Per-User Isolation: Complete data separation with user-scoped vector stores and documents
- WebSocket Streaming: Real-time bidirectional communication with authentication
- Document Management: Full CRUD operations for document lifecycle
- Python 3.11 or 3.12
- MongoDB Atlas account
- Google Gemini API key (Get one here)
1. Clone and Setup Environment
# Clone repository
git clone <your-repo-url>
cd document-qa-chatbot
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt2. Configure MongoDB Atlas
- Create a free account at MongoDB Atlas
- Create a new cluster
- Configure network access (add your IP address or allow from anywhere: 0.0.0.0/0)
- Create a database user with credentials
- Get your connection string (Format:
mongodb+srv://<username>:<password>@cluster.mongodb.net)
Create a .env file in the project root:
# Application Settings
APP_NAME=Document QA API
APP_VERSION=3.0.0
API_PREFIX=/api/v1
DEBUG=true
# Gemini API Configuration (Required)
GEMINI_API_KEY=your_gemini_api_key_here
# MongoDB Atlas Configuration (Required)
MONGODB_URL=mongodb+srv://username:password@cluster.mongodb.net
MONGODB_DB_NAME=document_qa_db
# Authentication Configuration (Required)
# Generate SECRET_KEY with: openssl rand -hex 32
SECRET_KEY=your_secure_random_secret_key_here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=1440
# Document Processing Configuration
CHUNK_SIZE=500
CHUNK_OVERLAP=50
MAX_FILE_SIZE=10485760
# Storage Paths
VECTOR_STORE_PATH=./data/vector_store
DOCUMENTS_DATA_PATH=./data/documents
UPLOADS_PATH=./data/uploads
# CORS Settings
ALLOWED_ORIGINS=*Important: Generate a secure SECRET_KEY using:
openssl rand -hex 323. Run the Application
# Development mode
python main.py
# Or using uvicorn directly
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000Access the application:
- Web UI: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
document-qa-chatbot/
├── main.py # Application entry point
├── requirements.txt # Python dependencies
├── .env # Environment configuration
│
├── src/ # Application source code
│ ├── main.py # FastAPI app instance
│ ├── core/ # Core functionality
│ │ ├── config.py # Configuration management
│ │ └── security.py # Authentication & JWT
│ ├── models/ # Data models
│ │ └── schemas.py # Pydantic schemas
│ ├── services/ # Business logic
│ │ ├── database.py # MongoDB service
│ │ ├── vector_store.py # FAISS vector operations
│ │ ├── llm_service.py # Gemini LLM integration
│ │ └── file_processor.py # File processing
│ └── api/routes/ # API endpoints
│ ├── auth.py # Authentication
│ ├── documents.py # Document management
│ ├── query.py # Query endpoint
│ ├── websocket.py # WebSocket streaming
│ └── health.py # Health check
│
├── static/ # Frontend assets
│ └── index.html # Web interface
│
└── sample_documents/ # Test data
├── ai_basics.txt
├── machine_learning.md
├── python_guide.txt
└── test_sample.pdf