Module: Parse::Retrieval::Reranker

Defined in:
lib/parse/retrieval/reranker.rb,
lib/parse/retrieval/reranker/cohere.rb

Overview

Cross-encoder reranking for retrieved documents.

A reranker takes a query and a list of candidate document texts and returns a relevance-ordered scoring. It runs AFTER the (vector, lexical, or hybrid) retrieval step and BEFORE chunking, reordering the retrieved documents by a more expensive cross-encoder relevance model than the first-stage similarity score.

== Protocol

A reranker is any object that responds to:

#rerank(query:, documents:, top_n: nil) -> Array

where documents is an Array and the return is an Array of Result (index into documents, plus relevance_score), descending by relevance. Implementations MUST:

  • Return at most documents.length results (and at most top_n when given).
  • Use 0-based index values that are valid positions in the input.
  • Never raise for an empty documents list — return [].

Base provides input validation and result normalization so adapters only implement the network call (Base#rerank_scores).

Examples:

wiring into retrieve

reranker = Parse::Retrieval::Reranker::Cohere.new(api_key: ENV.fetch("COHERE_API_KEY"))
chunks = Parse::Retrieval.retrieve(query: q, klass: Article, k: 30,
                                   rerank: reranker, rerank_top_n: 5)

Defined Under Namespace

Classes: Base, Cohere, Error, Fixture, InvalidResponseError, Result