Adaptive RAG Engine
Production-ready Adaptive Retrieval-Augmented Generation (Adaptive RAG) Engine built using FastAPI, LangChain, FAISS, SentenceTransformers, BM25, Cross-Encoder Reranking and Google Gemini.
Overview
Adaptive RAG Engine is a modular Retrieval-Augmented Generation framework designed to improve retrieval quality through adaptive query routing, hybrid retrieval, reciprocal rank fusion (RRF), cross-encoder reranking, confidence estimation, and optimized context construction before passing relevant information to a Large Language Model (LLM).
Unlike conventional RAG systems that rely on a single retrieval strategy, Adaptive RAG dynamically selects Dense, Sparse, or Hybrid Retrieval based on query analysis to improve retrieval precision while maintaining low latency.
Key Features
- Adaptive Query Analysis
- Intelligent Query Routing
- Dense Retrieval (SentenceTransformers)
- Sparse Retrieval (BM25)
- Hybrid Retrieval
- Reciprocal Rank Fusion (RRF)
- Cross-Encoder Re-ranking
- Confidence Estimation
- Context Optimization
- LangChain Retriever
- FastAPI REST API
- FAISS Vector Database
- Google Gemini Integration
- Modular Production Architecture
- Docker Ready
- PyPI Ready
System Architecture
flowchart TD
A[User Query]
A --> B[FastAPI REST API]
B --> C[Adaptive RAG Pipeline]
C --> D[Query Analyzer]
D --> E[Adaptive Query Router]
E -->|Semantic Query| F[Dense Retriever]
E -->|Keyword Query| G[BM25 Retriever]
E -->|Mixed Query| H[Hybrid Retrieval]
F --> I[SentenceTransformer Embeddings]
I --> J[FAISS Vector Store]
G --> K[BM25 Index]
J --> H
K --> H
H --> L[Reciprocal Rank Fusion]
L --> M[Cross Encoder Reranker]
M --> N[Confidence Estimator]
N --> O[Context Optimizer]
O --> P[LangChain Retriever Adapter]
P --> Q[Prompt Builder]
Q --> R[Gemini LLM]
R --> S[Generated Answer]
S --> T[FastAPI Response]
Retrieval Pipeline
User Query
↓
FastAPI REST API
↓
Query Analyzer
↓
Adaptive Query Router
↓
Dense Retriever (MiniLM)
BM25 Retriever
↓
Hybrid Retriever
↓
RRF Fusion
↓
Cross Encoder Reranker
↓
Confidence Estimator
↓
Context Optimizer
↓
LangChain Retriever
↓
Prompt Builder
↓
Gemini
↓
Generated Response
Technology Stack
| Category | Technologies |
|---|---|
| Backend | FastAPI |
| LLM | Google Gemini |
| Framework | LangChain |
| Vector Search | FAISS |
| Embeddings | SentenceTransformers |
| Sparse Retrieval | BM25 |
| Reranking | Cross Encoder |
| Programming Language | Python |
| Packaging | Docker, PyPI |
Project Structure
app/
src/
tests/
docs/
data/
logs/
Installation
git clone ...
cd adaptive-rag-engine
pip install -r requirements.txt
Environment Variables
GEMINI_API_KEY=
GEMINI_MODEL=
HF_TOKEN=
Running
uvicorn app.main:app --reload
API
POST /rag/ask
Example
{
"question":"Explain Retrieval Augmented Generation."
}
Docker
docker build -t adaptive-rag-engine .
docker run -p 8000:8000 adaptive-rag-engine
PyPI
pip install adaptive-rag-engine
Future Scope
- Redis Cache
- Streaming Responses
- Multi-modal Retrieval
- Kubernetes Deployment
- Multi-Agent RAG
- GraphRAG
- Knowledge Graph Integration
License
MIT License
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file adaptive_rag_engine-1.0.1.tar.gz.
File metadata
- Download URL: adaptive_rag_engine-1.0.1.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb9028635afa0d95ce7798a487e0eefd568a8e8fe7a9ed705df5ecb8d113ae08
|
|
| MD5 |
956bc9cb59d604f2c64fc824a9c2ac92
|
|
| BLAKE2b-256 |
8a3ac5273ae4c6001036fedc70b5d078585e826b979ff500ce275f9258529537
|
File details
Details for the file adaptive_rag_engine-1.0.1-py3-none-any.whl.
File metadata
- Download URL: adaptive_rag_engine-1.0.1-py3-none-any.whl
- Upload date:
- Size: 36.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f749de9d2d2209905aa98b598c9c0c5dc43454f6334364a94a1cdc4dddf2e12
|
|
| MD5 |
986fff177cf4b3968a408f4a3a16cd1e
|
|
| BLAKE2b-256 |
5cf1cf0a3f9d4f86f8d153a57b352fa25b9adcffce5fe966f830cd78e5b5a6e4
|