localvectordb.reranking module
Reranking providers for LocalVectorDB.
This module provides cross-encoder and API-based reranking to improve search result quality by re-scoring candidates with more powerful models.
- class localvectordb.reranking.Reranker(model: str, *, timeout: int = 90, max_retries: int = 3, **kwargs: Any)
Bases:
ABCAbstract base class for reranking providers.
- abstractmethod rerank(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results synchronously.
- Parameters:
query (str) – The original query text.
results (List[QueryResult]) – Search results to rerank.
top_k (int, optional) – Maximum number of results to return. If None, returns all.
- Returns:
Reranked results with updated scores, best first.
- Return type:
List[QueryResult]
Notes
Every provider writes two metadata keys and leaves
result.scoreholding an absolute[0, 1]relevance score – one that means the same thing regardless of the other candidates in the batch, so it survivesscore_thresholdfiltering and cross-query comparison:metadata["original_score"]– the pre-rerank search score.metadata["rerank_raw_score"]– the reranker model’s raw output before the provider’s[0, 1]mapping.
The mapping is provider-specific because the raw scores are:
Jina – the API returns a native
[0, 1]relevance score; used as-is.SentenceTransformers / HuggingFace – a cross-encoder logit, squashed with a logistic sigmoid. This is deliberately not a per-batch min-max: min-max is pool-relative (top always 1.0, bottom always 0.0) and would break
score_thresholdexactly as the pre-T1.1 hybrid fusion did.Mock – word-overlap fraction, already in
[0, 1].
- async rerank_async(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results asynchronously. Default delegates to sync.
- class localvectordb.reranking.JinaReranker(model: str = 'jina-reranker-v2-base-multilingual', *, api_key: str | None = None, timeout: int = 90, max_retries: int = 3, **kwargs: Any)
Bases:
RerankerJina AI reranker using the Jina Reranker API.
- Parameters:
- __init__(model: str = 'jina-reranker-v2-base-multilingual', *, api_key: str | None = None, timeout: int = 90, max_retries: int = 3, **kwargs: Any) None
- rerank(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results synchronously.
- Parameters:
query (str) – The original query text.
results (List[QueryResult]) – Search results to rerank.
top_k (int, optional) – Maximum number of results to return. If None, returns all.
- Returns:
Reranked results with updated scores, best first.
- Return type:
List[QueryResult]
Notes
Every provider writes two metadata keys and leaves
result.scoreholding an absolute[0, 1]relevance score – one that means the same thing regardless of the other candidates in the batch, so it survivesscore_thresholdfiltering and cross-query comparison:metadata["original_score"]– the pre-rerank search score.metadata["rerank_raw_score"]– the reranker model’s raw output before the provider’s[0, 1]mapping.
The mapping is provider-specific because the raw scores are:
Jina – the API returns a native
[0, 1]relevance score; used as-is.SentenceTransformers / HuggingFace – a cross-encoder logit, squashed with a logistic sigmoid. This is deliberately not a per-batch min-max: min-max is pool-relative (top always 1.0, bottom always 0.0) and would break
score_thresholdexactly as the pre-T1.1 hybrid fusion did.Mock – word-overlap fraction, already in
[0, 1].
- async rerank_async(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results asynchronously. Default delegates to sync.
- class localvectordb.reranking.SentenceTransformersReranker(model: str = 'cross-encoder/ms-marco-MiniLM-L-6-v2', *, device: str | None = None, timeout: int = 90, max_retries: int = 3, **kwargs: Any)
Bases:
RerankerCross-encoder reranker using sentence-transformers.
- Parameters:
- __init__(model: str = 'cross-encoder/ms-marco-MiniLM-L-6-v2', *, device: str | None = None, timeout: int = 90, max_retries: int = 3, **kwargs: Any) None
- rerank(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results synchronously.
- Parameters:
query (str) – The original query text.
results (List[QueryResult]) – Search results to rerank.
top_k (int, optional) – Maximum number of results to return. If None, returns all.
- Returns:
Reranked results with updated scores, best first.
- Return type:
List[QueryResult]
Notes
Every provider writes two metadata keys and leaves
result.scoreholding an absolute[0, 1]relevance score – one that means the same thing regardless of the other candidates in the batch, so it survivesscore_thresholdfiltering and cross-query comparison:metadata["original_score"]– the pre-rerank search score.metadata["rerank_raw_score"]– the reranker model’s raw output before the provider’s[0, 1]mapping.
The mapping is provider-specific because the raw scores are:
Jina – the API returns a native
[0, 1]relevance score; used as-is.SentenceTransformers / HuggingFace – a cross-encoder logit, squashed with a logistic sigmoid. This is deliberately not a per-batch min-max: min-max is pool-relative (top always 1.0, bottom always 0.0) and would break
score_thresholdexactly as the pre-T1.1 hybrid fusion did.Mock – word-overlap fraction, already in
[0, 1].
- class localvectordb.reranking.HuggingFaceReranker(model: str = 'BAAI/bge-reranker-v2-m3', *, api_key: str | None = None, base_url: str | None = None, timeout: int = 90, max_retries: int = 3, **kwargs: Any)
Bases:
RerankerHuggingFace Inference API reranker.
- Parameters:
model (str) – HuggingFace model ID. Default: “BAAI/bge-reranker-v2-m3”
api_key (str, optional) – API key. Falls back to HF_TOKEN / HUGGINGFACE_TOKEN env vars.
base_url (str, optional) – API base URL. Default: https://api-inference.huggingface.co
- __init__(model: str = 'BAAI/bge-reranker-v2-m3', *, api_key: str | None = None, base_url: str | None = None, timeout: int = 90, max_retries: int = 3, **kwargs: Any) None
- rerank(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results synchronously.
- Parameters:
query (str) – The original query text.
results (List[QueryResult]) – Search results to rerank.
top_k (int, optional) – Maximum number of results to return. If None, returns all.
- Returns:
Reranked results with updated scores, best first.
- Return type:
List[QueryResult]
Notes
Every provider writes two metadata keys and leaves
result.scoreholding an absolute[0, 1]relevance score – one that means the same thing regardless of the other candidates in the batch, so it survivesscore_thresholdfiltering and cross-query comparison:metadata["original_score"]– the pre-rerank search score.metadata["rerank_raw_score"]– the reranker model’s raw output before the provider’s[0, 1]mapping.
The mapping is provider-specific because the raw scores are:
Jina – the API returns a native
[0, 1]relevance score; used as-is.SentenceTransformers / HuggingFace – a cross-encoder logit, squashed with a logistic sigmoid. This is deliberately not a per-batch min-max: min-max is pool-relative (top always 1.0, bottom always 0.0) and would break
score_thresholdexactly as the pre-T1.1 hybrid fusion did.Mock – word-overlap fraction, already in
[0, 1].
- class localvectordb.reranking.MockReranker(model: str = 'mock-reranker', *, timeout: int = 90, max_retries: int = 3, **kwargs: Any)
Bases:
RerankerMock reranker for testing. Uses word-overlap scoring.
- __init__(model: str = 'mock-reranker', *, timeout: int = 90, max_retries: int = 3, **kwargs: Any) None
- rerank(query: str, results: List[QueryResult], top_k: int | None = None) List[QueryResult]
Rerank search results synchronously.
- Parameters:
query (str) – The original query text.
results (List[QueryResult]) – Search results to rerank.
top_k (int, optional) – Maximum number of results to return. If None, returns all.
- Returns:
Reranked results with updated scores, best first.
- Return type:
List[QueryResult]
Notes
Every provider writes two metadata keys and leaves
result.scoreholding an absolute[0, 1]relevance score – one that means the same thing regardless of the other candidates in the batch, so it survivesscore_thresholdfiltering and cross-query comparison:metadata["original_score"]– the pre-rerank search score.metadata["rerank_raw_score"]– the reranker model’s raw output before the provider’s[0, 1]mapping.
The mapping is provider-specific because the raw scores are:
Jina – the API returns a native
[0, 1]relevance score; used as-is.SentenceTransformers / HuggingFace – a cross-encoder logit, squashed with a logistic sigmoid. This is deliberately not a per-batch min-max: min-max is pool-relative (top always 1.0, bottom always 0.0) and would break
score_thresholdexactly as the pre-T1.1 hybrid fusion did.Mock – word-overlap fraction, already in
[0, 1].
- class localvectordb.reranking.RerankerRegistry
Bases:
objectRegistry for reranker providers with plugin discovery.
- classmethod register(name: str, provider_class: Type[Reranker]) None
Register a new reranker provider.