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.0.tar.gz (23.9 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.0-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file adaptive_rag_engine-1.0.0.tar.gz.

File metadata

  • Download URL: adaptive_rag_engine-1.0.0.tar.gz
  • Upload date:
  • Size: 23.9 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.0.tar.gz
Algorithm Hash digest
SHA256 062a9ad4117d4b9aaae0bd1fdb4939f87d41e1a7aaca5030ea25639f438e00d5
MD5 4475e2b5263cab999e8c2e2115ad9446
BLAKE2b-256 b5c5744617ef5dea0b76bccfb32fdfa95cab3a06652272d70442f3c7299cb8e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for adaptive_rag_engine-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6bd7f5a470a3d85de456aa7fa07a304bc32df4dff0ffbc37b8de5fe3527d97e6
MD5 99dcc9648f3f4596aa38fd621c4c306e
BLAKE2b-256 60253fe13878b98575f930b6751978d3606b13fdfaebe3c7309b32d855b25830

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