A Retrieval-Augmented Generation API built with FastAPI, ChromaDB, and Google Gemini.
Upload PDF documents, ask questions in natural language, get answers grounded in your documents, with source citations showing exactly which page and document the answer came from.
- FastAPI : REST API framework
- PyMuPDF : PDF text extraction
- LangChain Text Splitters : Recursive character chunking
- ChromaDB : Vector store (persistent)
- Google Gemini : Embeddings (
gemini-embedding-001) and generation (gemini-2.5-flash)
- Python 3.11+
- Docker
- Google Gemini API key (free tier — get it at aistudio.google.com)
git clone <your-repo-url>
cd rag-api
python -m venv venv
venv\Scripts\activate # Windows
pip install -r requirements.txtCreate a .env file:
GEMINI_API_KEY=your_api_key_here
Run the server:
cd app
uvicorn main:app --reloadVisit http://localhost:8000/docs for the interactive API.
docker-compose up --buildVisit http://localhost:8000/docs
| Endpoint | Method | Description |
|---|---|---|
/upload |
POST | Upload a PDF document |
/query |
POST | Ask a question, get answer + sources |
/documents |
GET | List all uploaded documents |
/health |
GET | Health check |
Upload a document:
curl -X POST http://localhost:8000/upload \
-F "file=@your_document.pdf"Ask a question:
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{"question": "What is the main topic of the document?"}'Response includes answer + sources:
{
"answer": "The document covers...",
"sources": [
{
"filename": "document.pdf",
"page": 3,
"relevance_score": 0.87,
"excerpt": "..."
}
]
}Ingestion: PDF → Text Extraction → Chunking (500 tokens, 50 overlap) → Embedding → ChromaDB
Query: Question → Embedding → Semantic Search (top-5) → Prompt Construction → Gemini → Answer + Sources
- Launch EC2 instance (t3.small, Ubuntu 22.04)
- Open port 8000 in security group
- Install Docker:
sudo apt update && sudo apt install -y docker.io docker-compose
sudo systemctl start docker- Clone repo and deploy:
git clone <your-repo-url>
cd rag-api
echo "GEMINI_API_KEY=your_key" > .env
docker-compose up -dAPI will be available at http://<ec2-public-ip>:8000