Module: Parse::Retrieval::Profiles

Defined in:
lib/parse/retrieval/profiles.rb

Overview

Server-configured retrieval profiles for the semantic_search agent tool.

A profile composes the library's existing retrieval paths (vector, hybrid, reranking) with explicit budgets, so an application can offer an agent a few named strategies (for example fast, balanced, precise) without letting the model pick providers, endpoints, or credentials. The agent only ever names a profile; everything else is fixed on the server at registration time.

Registration validates the whole configuration, so a misconfigured profile (unknown option, unregistered reranker, non-positive budget) fails at boot rather than mid-request. Without a profile: argument semantic_search behaves exactly as before.

Examples:

Parse::Retrieval.register_reranker(:voyage,
  Parse::Retrieval::Reranker::Voyage.new(api_key: ENV.fetch("VOYAGE_API_KEY"), model: "rerank-3-lite"))

Parse::Retrieval::Profiles.register(:fast, k: 5, max_k: 10)
Parse::Retrieval::Profiles.register(:precise,
  reranker: :voyage, rerank_candidates: 30, rerank_top_n: 8,
  rerank_max_document_chars: 4_000, rerank_timeout: 5,
  on_rerank_failure: :fallback, max_total_tokens: 8_000)

agent.execute(:semantic_search, class_name: "Article", query: "refund policy", profile: "precise")

Defined Under Namespace

Classes: Profile

Constant Summary collapse

ALLOWED_OPTIONS =
%i[
  k max_k hybrid reranker rerank_candidates rerank_top_n
  rerank_max_document_chars rerank_timeout on_rerank_failure max_total_tokens
].freeze
DEFAULTS =

Defaults applied to an option the profile does not set.

{
  k: 10,
  max_k: 20,
  hybrid: nil,
  rerank_candidates: 30,
  rerank_top_n: nil,
  rerank_max_document_chars: 4_000,
  rerank_timeout: 10,
  on_rerank_failure: :fallback,
  max_total_tokens: nil,
}.freeze
MAX_RERANK_CANDIDATES =

Hard ceilings no profile may exceed, so a misconfigured profile still cannot fan out unbounded provider work.

100
MAX_K =

semantic_search returns at most this many documents, so a larger max_k could never take effect; it is refused rather than clamped.

20
MAX_RERANK_DOCUMENT_CHARS =
32_000
FAILURE_MODES =
%i[fallback raise].freeze
RERANK_OPTIONS =

Options that only have an effect when the profile names a reranker.

%i[rerank_candidates rerank_top_n rerank_max_document_chars rerank_timeout on_rerank_failure].freeze
HYBRID_KEYS =
%i[lexical vector fusion].freeze
NAME_RE =
/\A[a-z][a-z0-9_]{0,39}\z/.freeze

Class Method Summary collapse

Class Method Details

.fetch!(name) ⇒ Profile

Returns the registered profile.

Returns:

  • (Profile) —

    the registered profile.

Raises:



113
114
115
116
117
118
119
120
# File 'lib/parse/retrieval/profiles.rb', line 113

def fetch!(name)
  key = name.to_s
  profile = @mutex.synchronize { @registry[key] }
  return profile if profile

  raise Parse::Agent::ValidationError,
        "Unknown retrieval profile #{key.inspect}. Available: #{names.inspect}."
end

.names ⇒ Array<String>

Returns registered profile names, sorted.

Returns:

  • (Array<String>) —

    registered profile names, sorted.



123
124
125
# File 'lib/parse/retrieval/profiles.rb', line 123

def names
  @mutex.synchronize { @registry.keys.sort }
end

.register(name, **options) ⇒ Profile

Register (or replace) a profile.

Parameters:

  • name (Symbol, String) —

    lowercase identifier.

  • options (Hash) —

    see ALLOWED_OPTIONS.

    • k, max_k [Integer]: default and maximum results.
    • hybrid [true, Hash]: run lexical + vector fusion; a Hash carries server-side lexical:, vector:, fusion: settings.
    • reranker [Symbol, String]: a name registered with Parse::Retrieval.register_reranker.
    • rerank_candidates [Integer]: documents retrieved and sent to the reranker (capped at MAX_RERANK_CANDIDATES).
    • rerank_top_n [Integer, nil]: documents kept after reranking (defaults to the effective k).
    • rerank_max_document_chars [Integer]: each document's text is cut to this length before it is sent to the reranker.
    • rerank_timeout [Numeric]: seconds allowed for reranking.
    • on_rerank_failure [:fallback, :raise]: on timeout or provider failure, keep the retrieval order (observable) or fail the call.
    • max_total_tokens [Integer, nil]: default response budget.

Returns:

Raises:

  • (ArgumentError) —

    on any invalid option.



104
105
106
107
108
# File 'lib/parse/retrieval/profiles.rb', line 104

def register(name, **options)
  profile = build(name, options)
  @mutex.synchronize { @registry[profile.name] = profile }
  profile
end

.reset! ⇒ Object



131
132
133
# File 'lib/parse/retrieval/profiles.rb', line 131

def reset!
  @mutex.synchronize { @registry.clear }
end

.unregister(name) ⇒ Object



127
128
129
# File 'lib/parse/retrieval/profiles.rb', line 127

def unregister(name)
  @mutex.synchronize { @registry.delete(name.to_s) }
end