Class: Parse::Retrieval::Reranker::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/retrieval/reranker.rb

Overview

Common superclass: validates inputs, bounds top_n, and normalizes raw (index, score) pairs into sorted Results. Concrete adapters implement #rerank_scores.

Direct Known Subclasses

Cohere, Fixture

Constant Summary collapse

MAX_DOCUMENTS =

Hard cap on the number of documents a single rerank call may carry, to bound provider cost / payload size. Providers typically cap around 1000; we stay conservative.

1000

Instance Method Summary collapse

Instance Method Details

#rerank(query:, documents:, top_n: nil) ⇒ Array<Result>

Rerank documents against query.

Parameters:

  • query (String)

    the natural-language query.

  • documents (Array<String>)

    candidate document texts.

  • top_n (Integer, nil) (defaults to: nil)

    return at most this many results.

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/parse/retrieval/reranker.rb', line 68

def rerank(query:, documents:, top_n: nil)
  unless query.is_a?(String) && !query.strip.empty?
    raise ArgumentError, "#{self.class}#rerank: query must be a non-empty String."
  end
  docs = Array(documents).map(&:to_s)
  return [] if docs.empty?
  if docs.length > MAX_DOCUMENTS
    raise ArgumentError,
          "#{self.class}#rerank: #{docs.length} documents exceeds MAX_DOCUMENTS=#{MAX_DOCUMENTS}."
  end
  n = top_n.nil? ? docs.length : [Integer(top_n), docs.length].min
  n = docs.length if n <= 0

  pairs = rerank_scores(query, docs, n)
  normalize_results(pairs, docs.length, n)
end

#rerank_scores(query, documents, top_n) ⇒ Array<Array(Integer, Numeric)>, Array<Result> (protected)

Adapter hook: return an Array of [index, score] pairs (or Results) for documents. top_n is a hint; the base class re-bounds and re-sorts regardless.

Parameters:

Returns:

Raises:

  • (NotImplementedError)


95
96
97
# File 'lib/parse/retrieval/reranker.rb', line 95

def rerank_scores(query, documents, top_n)
  raise NotImplementedError, "#{self.class}#rerank_scores must be implemented."
end