A high-performance reranking service designed for Retrieval-Augmented Generation (RAG) pipelines. This service uses Cross-Encoder models to provide precise relevance scores for a set of documents relative to a query, significantly improving retrieval accuracy over standard vector search or BM25.
- Cross-Encoder Reranking: Utilizes state-of-the-art transformer models (defaults to
cross-encoder/ms-marco-MiniLM-L-6-v2). - Hardware Acceleration: Automatic detection and utilization of CUDA (NVIDIA), MPS (Apple Silicon), or CPU.
- Score Fusion: Integrates prior retrieval scores (e.g., from vector search) with model scores using weighted fusion.
- Dynamic Truncation: Handles long documents and large batches efficiently with configurable limits.
- FastAPI Core: Low-latency, asynchronous API handling.
- Batch Processing: Optimized inference through batching.
- Python 3.14+
- FastAPI & Uvicorn
- PyTorch
- Hugging Face Transformers
- NumPy
- Pydantic
-
Clone the repository:
git clone <repository-url> cd rag-reranker
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Environment Configuration: Create a
.envfile (or use the provided one) to configure the service:RERANK_MODEL=cross-encoder/ms-marco-MiniLM-L-6-v2 RERANK_MAX_LENGTH=512 RERANK_BATCH_SIZE=16 RERANK_MAX_DOCUMENTS=100 RERANK_MAX_DOC_CHARS=5000 RERANK_MODEL_WEIGHT=0.8 RERANK_PRIOR_WEIGHT=0.2 RERANK_OUTPUT_ACTIVATION=sigmoid RERANK_LOG_LEVEL=INFO
Run the service using Uvicorn:
uvicorn app:app --port 8001The service will be available at http://0.0.0.0:8001.
POST /rerank
Refines the ranking of a list of documents based on a query.
Request Body:
{
"query": "What is the capital of France?",
"documents": [
"Paris is the capital and most populous city of France.",
"The Eiffel Tower is located in Paris.",
"Lyon is a major city in France."
],
"prior_scores": [0.9, 0.8, 0.7],
"use_score_fusion": true,
"top_k": 5
}Response:
{
"scores": [0.98, 0.45, 0.12],
"raw_scores": [0.97, 0.4, 0.1],
"ranked_indices": [0, 1, 2],
"count": 3,
"trimmed": false,
"time_ms": 45.2,
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"device": "cuda"
}GET /health
Returns the current status and basic configuration.
GET /meta
Returns detailed configuration including model parameters and fusion weights.
| Variable | Description | Default |
|---|---|---|
RERANK_MODEL |
Hugging Face model identifier | cross-encoder/ms-marco-MiniLM-L-6-v2 |
RERANK_MAX_LENGTH |
Max sequence length for the model | 512 |
RERANK_BATCH_SIZE |
Batch size for inference | 16 |
RERANK_MAX_DOCUMENTS |
Max number of documents per request | 100 |
RERANK_MAX_DOC_CHARS |
Max characters per document (truncated if exceeded) | 5000 |
RERANK_MODEL_WEIGHT |
Weight for model score in fusion | 0.8 |
RERANK_PRIOR_WEIGHT |
Weight for prior score in fusion | 0.2 |
RERANK_OUTPUT_ACTIVATION |
sigmoid or logit |
sigmoid |
When use_score_fusion is enabled, the final score is calculated as:
Final Score = (RERANK_MODEL_WEIGHT * model_score) + (RERANK_PRIOR_WEIGHT * normalized_prior_score)
Prior scores are automatically normalized to a 0-1 range before fusion.