fastrag.retrieval.reranking.base#

The Reranker contract: scoring documents against a query.

Classes

Reranker()

Scores documents against a query with a cross-encoder.

class fastrag.retrieval.reranking.base.Reranker[source]#

Bases: ABC

Scores documents against a query with a cross-encoder.

A cross-encoder reads the query and a document together, rather than comparing precomputed embeddings, so it is far more accurate than vector similarity — and far more expensive, which is why it narrows a retriever’s over-fetch instead of searching a whole collection; see RerankingRetriever.

abstract property model_id: str#

Stable identifier of the model, for logging and events.

abstractmethod async rerank(query, documents, *, top_k)[source]#

Score documents against query and return them best-first.

Results are best-first, number at most top_k, and each carries the reserved metadata keys score (the model’s own relevance score, on the model’s own scale — never comparable with a vector store’s similarity score) and rank (0-based, contiguous) restated from this reranker — any score/rank a document already carried is overwritten, not merged. An empty documents returns [] without a network call.

Parameters:
  • query (str) – The same query text the documents were retrieved for.

  • documents (Sequence[Document]) – Candidates to score, already retrieved by some other means (typically a Retriever’s over-fetch).

  • top_k (int) – Maximum number of documents to return, best first.

Return type:

list[Document]