Reranker Module

Document reranking implementations for improved search relevance

Reranker Module

The Reranker module provides concrete implementations for reranking search results to improve relevance.

Import

python
from praisonai.adapters import LLMReranker
from praisonai.adapters.rerankers import CrossEncoderReranker, CohereReranker

Quick Example

python
from praisonai.adapters import LLMReranker

# Create LLM-based reranker
reranker = LLMReranker(model="gpt-4o-mini")

# Rerank documents
documents = ["Python is a language", "Java is compiled", "Python uses indentation"]
results = reranker.rerank(
    query="What is Python?",
    documents=documents,
    top_k=2
)

for r in results:
    print(f"Score: {r.score:.3f} - {r.text}")

Features

  • LLM-based relevance scoring with any model
  • Cross-encoder neural reranking
  • Cohere Rerank API integration
  • Batch processing for efficiency
  • Async support

Classes

LLMReranker

Uses an LLM to score document relevance to a query.

python
from praisonai.adapters import LLMReranker

reranker = LLMReranker(
    model="gpt-4o-mini",
    batch_size=5
)
results = reranker.rerank("query", documents)

Parameters:

ParameterTypeDefaultDescription
llmAnyNoneCustom LLM instance (Agent)
modelstr"gpt-4o-mini"Model for scoring
batch_sizeint5Documents per batch

How it works:

  1. Prompts the LLM to rate relevance on a 0-10 scale
  2. Normalizes scores to 0-1 range
  3. Sorts by score descending

CrossEncoderReranker

Uses sentence-transformers cross-encoder for accurate relevance scoring.

python
from praisonai.adapters.rerankers import CrossEncoderReranker

reranker = CrossEncoderReranker(
    model_name="cross-encoder/ms-marco-MiniLM-L-6-v2"
)
results = reranker.rerank("query", documents, top_k=5)

Parameters:

ParameterTypeDefaultDescription
model_namestr"cross-encoder/ms-marco-MiniLM-L-6-v2"Cross-encoder model

CohereReranker

Uses Cohere’s rerank API for high-quality reranking.

python
from praisonai.adapters.rerankers import CohereReranker

reranker = CohereReranker(
    api_key="your-api-key",  # Or set COHERE_API_KEY env
    model="rerank-english-v3.0"
)
results = reranker.rerank("query", documents, top_k=5)

Parameters:

ParameterTypeDefaultDescription
api_keystrCOHERE_API_KEY envCohere API key
modelstr"rerank-english-v3.0"Rerank model

Methods

rerank(query, documents, top_k=None)

Rerank documents by relevance to query.

Parameters:

  • query (str): Search query
  • documents (List[str]): Documents to rerank
  • top_k (int, optional): Number of results to return

Returns: List[RerankResult] – Reranked documents with scores

arerank(query, documents, top_k=None)

Async reranking that offloads the blocking body via asyncio.to_thread(...), so calling it from an event loop no longer freezes it. Authors of custom rerankers on a natively-async provider should override arerank and await the native call directly — see Native async in custom retrievers / rerankers.

Example: Full RAG Pipeline

python
from praisonai.adapters import (
    ChromaVectorStore, 
    BasicRetriever, 
    LLMReranker
)

# Setup
store = ChromaVectorStore(namespace="docs")
retriever = BasicRetriever(
    vector_store=store,
    embedding_fn=get_embedding,
    top_k=20  # Retrieve more for reranking
)
reranker = LLMReranker(model="gpt-4o-mini")

# Retrieve
query = "How to deploy Python apps?"
results = retriever.retrieve(query)

# Rerank
documents = [r.text for r in results]
reranked = reranker.rerank(query, documents, top_k=5)

# Use top results
for r in reranked:
    print(f"Score: {r.score:.3f}")
    print(f"Text: {r.text[:100]}...")

Example: Using with Agent

python
from praisonai.adapters import LLMReranker
from praisonaiagents import Agent

# Use an agent for reranking
agent = Agent(
    instructions="You are a relevance scoring assistant",
    llm="gpt-4o-mini"
)

reranker = LLMReranker(llm=agent)
results = reranker.rerank("Python basics", documents)

Reranker Selection Guide

Use CaseRecommended
Best accuracyCohereReranker
Local/offlineCrossEncoderReranker
Already using LLMLLMReranker
Fast, no dependenciesSkip reranking

Environment Variables

bash
# For LLM reranker (uses litellm)
export OPENAI_API_KEY=sk-xxx

# For Cohere reranker
export COHERE_API_KEY=xxx

CLI Usage

bash
# LLM reranker
praisonai knowledge query "What is Python?" --reranker llm

# Cross-encoder reranker
praisonai knowledge query "What is Python?" --reranker cross_encoder

# Cohere reranker
praisonai knowledge query "What is Python?" --reranker cohere

Related