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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adaptive_rag_engine-1.0.1.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adaptive_rag_engine-1.0.1-py3-none-any.whl (36.5 kB view details)

Uploaded Python 3

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

Hashes for adaptive_rag_engine-1.0.1.tar.gz
Algorithm Hash digest
SHA256 eb9028635afa0d95ce7798a487e0eefd568a8e8fe7a9ed705df5ecb8d113ae08
MD5 956bc9cb59d604f2c64fc824a9c2ac92
BLAKE2b-256 8a3ac5273ae4c6001036fedc70b5d078585e826b979ff500ce275f9258529537

See more details on using hashes here.

File details

Details for the file adaptive_rag_engine-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for adaptive_rag_engine-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f749de9d2d2209905aa98b598c9c0c5dc43454f6334364a94a1cdc4dddf2e12
MD5 986fff177cf4b3968a408f4a3a16cd1e
BLAKE2b-256 5cf1cf0a3f9d4f86f8d153a57b352fa25b9adcffce5fe966f830cd78e5b5a6e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page