Skip to main content

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.

Python FastAPI LangChain FAISS License


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

Metadata

Release files for adaptive-rag-engine 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for adaptive-rag-engine 1.0.1
File Size Uploaded
adaptive_rag_engine-1.0.1.tar.gz 24.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for adaptive-rag-engine 1.0.1
File Interpreter ABI Platform
adaptive_rag_engine-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 61.3 kB

Release files / adaptive_rag_engine-1.0.1.tar.gz

Download URL adaptive_rag_engine-1.0.1.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
eb9028635afa0d95ce7798a487e0eefd568a8e8fe7a9ed705df5ecb8d113ae08
BLAKE2b-256 checksum
How to use checksums
8a3ac5273ae4c6001036fedc70b5d078585e826b979ff500ce275f9258529537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / adaptive_rag_engine-1.0.1-py3-none-any.whl

Download URL adaptive_rag_engine-1.0.1-py3-none-any.whl
Size 36.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f749de9d2d2209905aa98b598c9c0c5dc43454f6334364a94a1cdc4dddf2e12
BLAKE2b-256 checksum
How to use checksums
5cf1cf0a3f9d4f86f8d153a57b352fa25b9adcffce5fe966f830cd78e5b5a6e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page