Class: Parse::Retrieval::BudgetedReranker

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

Overview

Wraps a registered reranker with a profile's budgets. Every document is cut to rerank_max_document_chars, the call is charged to the caller's spend budget through charge, and the provider call is bounded by rerank_timeout. On a timeout or provider failure it either returns nil (the retriever then keeps the retrieval order) and records the reason, or re-raises, per on_rerank_failure.

The text it receives has already passed the semantic_search text source check, so it never contains a field outside the agent's effective agent_fields.

Constant Summary collapse

MAX_QUERY_CHARS =

Longest query sent to a reranker. The query is paired with every document in the provider call, so an unbounded query multiplies the cost of every rerank.

2_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inner, profile, charge: nil) ⇒ BudgetedReranker

Returns a new instance of BudgetedReranker.

Parameters:

  • inner (#rerank) —

    the registered reranker.

  • profile (Profiles::Profile)
  • charge (Proc, nil) (defaults to: nil) —

    charge.call(tokens); may raise to refuse.



251
252
253
254
255
256
257
# File 'lib/parse/retrieval/profiles.rb', line 251

def initialize(inner, profile, charge: nil)
  @inner = inner
  @profile = profile
  @charge = charge
  @stats = { used: false, documents: 0, chars: 0, tokens_estimated: 0,
             duration_ms: 0.0, fallback: false, fallback_reason: nil }
end

Instance Attribute Details

#stats ⇒ Object (readonly)

Returns the value of attribute stats.



246
247
248
# File 'lib/parse/retrieval/profiles.rb', line 246

def stats
  @stats
end

Instance Method Details

#rerank(query:, documents:, top_n: nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/parse/retrieval/profiles.rb', line 264

def rerank(query:, documents:, top_n: nil)
  limit = @profile.rerank_max_document_chars
  query = query.to_s[0, MAX_QUERY_CHARS]
  docs = Array(documents).map { |d| d.to_s[0, limit] }
  tokens = Parse::Embeddings::SpendCap.estimate_tokens(query) * [docs.length, 1].max +
           docs.sum { |d| Parse::Embeddings::SpendCap.estimate_tokens(d) }
  @stats.merge!(used: true, documents: docs.length, chars: docs.sum(&:length), tokens_estimated: tokens)
  @charge&.call(tokens)

  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  begin
    Timeout.timeout(@profile.rerank_timeout) do
      @inner.rerank(query: query, documents: docs, top_n: top_n)
    end
  rescue Timeout::Error, StandardError => e
    raise if @profile.on_rerank_failure == :raise
    @stats[:fallback] = true
    @stats[:fallback_reason] = e.is_a?(Timeout::Error) ? "timeout" : e.class.name
    nil
  ensure
    @stats[:duration_ms] = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round(1)
  end
end