The backend integrates with the notebooks' rag_utils module for a unified implementation:
backend/
├── main.py # FastAPI app initialization + startup hooks
├── api/
│ └── routes.py # API endpoint definitions
├── rag/
│ ├── neural_rag.py # Neural RAG (uses rag_utils)
│ └── internet_rag.py # Internet RAG (uses rag_utils)
├── services/ # Legacy services (optional, can be deprecated)
├── utils/
│ ├── logger.py # Logging + .env loader
│ └── metrics.py # Performance timing utilities
└── __init__.py
- Shared RAG Utils: Both notebooks and backend import from
notebooks/rag_utils.py - Neural RAG: Uses FAISS vector index + SentenceTransformers embeddings
- Internet RAG: Uses Tavily Search API + BeautifulSoup web scraping
- LLM: Mistral-7B with 4-bit quantization (when
RAG_USE_REAL_MODELS=1)
GET / - Health check + system info
POST /api/rag/neural - Query neural RAG
POST /api/rag/internet - Query internet RAG
POST /api/rag/compare - Compare both methods
GET /docs - Interactive API documentation
{
"query": "What is machine learning?",
"k": 3,
"answer_len": 50
}{
"method": "neural",
"query": "What is machine learning?",
"answer": "Machine learning is a subset of artificial intelligence...",
"contexts": ["passage1", "passage2", "passage3"],
"metrics": {
"retrieval_time": 0.123,
"llm_time": 0.456,
"context_size": 1200.0
}
}# Install full dev dependencies
pip install -r requirements-dev.txt
# Run with auto-reload
python -m uvicorn backend.main:app --reloadVisit: http://localhost:8000/docs
# Install minimal backend dependencies
pip install -r requirements.txt
# Build image
docker build -f docker/Dockerfile -t cloud-rag-api .
# Run container
docker run -p 8000:8000 \
-e RAG_USE_REAL_MODELS=0 \
-e TAVILY_API_KEY=your_key_here \
cloud-rag-apiCreate .env from .env_example:
# Enable real SentenceTransformers/Transformers (slower, more accurate)
RAG_USE_REAL_MODELS=0
# Enable real MS MARCO dataset (large, ~2GB download)
RAG_USE_REAL_DATASET=0
# Tavily Search API key (for internet RAG)
TAVILY_API_KEY=your_tavily_api_key
# Hugging Face token (optional, for gated models)
HF_TOKEN=your_hf_token
# Logging level
LOG_LEVEL=INFO- main.py: Loads
.envat startup, initializes logger - routes.py: Creates cached instances of NeuralRag/InternetRag on first request
- neural_rag.py: Calls
build_neural_rag_runtime()from rag_utils once - rag_utils.py: Loads dataset, embedder, FAISS index based on env flags
RAG instances are cached using @lru_cache to avoid reinitializing expensive resources:
- FAISS index stays in memory across requests
- SentenceTransformers model cached
- MS MARCO dataset cached (if enabled)
curl http://localhost:8000/curl -X POST http://localhost:8000/api/rag/neural \
-H "Content-Type: application/json" \
-d '{"query": "What is FAISS?"}'curl -X POST http://localhost:8000/api/rag/internet \
-H "Content-Type: application/json" \
-d '{"query": "Latest AI news"}'curl -X POST http://localhost:8000/api/rag/compare \
-H "Content-Type: application/json" \
-d '{"query": "Explain machine learning"}'- If using fallback embedder, FAISS is optional
- For real models:
pip install faiss-cpu
- Reduce dataset size:
num_samples=20inbuild_neural_rag_runtime() - Disable 4-bit quantization: set
RAG_USE_REAL_MODELS=0
- Check
TAVILY_API_KEYin.env - Internet RAG falls back to mock results if API fails
- Ensure
notebooks/rag_utils.pyexists - Verify
notebooks/is added tosys.pathinmain.py
- Add caching decorator for query results
- Implement request rate limiting
- Add authentication (API key or OAuth)
- Create dashboard for performance comparison
- Support for custom vector indexes (Pinecone, Weaviate, etc.)