Module: Parse::Retrieval::Benchmark
- Defined in:
- lib/parse/retrieval/benchmark.rb
Overview
A small evaluation harness for comparing retrieval profiles.
Run a labeled case set through each profile and report quality
(recall@k, MRR, hit rate), latency (mean and p95), and estimated rerank
usage, overall and per tag. Tags group the cases a deployment cares
about: exact names, semantic questions, long documents, restrictive
ACLs, tenant boundaries. For ACL and tenant cases, relevant lists only
what the caller is ALLOWED to retrieve, and forbidden lists ids that
must never appear; any forbidden hit is reported as a violation.
The harness is runner-agnostic. Benchmark.semantic_search_runner drives the
real semantic_search tool through an agent, so measurements reflect
the access policy, budgets, and fallbacks that production uses.
Defined Under Namespace
Classes: Case
Class Method Summary collapse
- .case_from(hash) ⇒ Case
- .load_cases(path) ⇒ Array<Case>
-
.run(cases:, profiles:, runner:, k: 10) ⇒ Hash{String => Hash}
Run every case through every profile.
-
.semantic_search_runner(agent, class_name:, **options) ⇒ Proc
A runner that executes the
semantic_searchtool throughagentand ranks parent documents by their first chunk.
Class Method Details
.case_from(hash) ⇒ Case
42 43 44 45 46 47 48 49 |
# File 'lib/parse/retrieval/benchmark.rb', line 42 def case_from(hash) h = hash.transform_keys(&:to_s) Case.new( id: h.fetch("id").to_s, query: h.fetch("query").to_s, relevant: Array(h["relevant"]).map(&:to_s), forbidden: Array(h["forbidden"]).map(&:to_s), tags: Array(h["tags"]).map(&:to_s), ) end |
.load_cases(path) ⇒ Array<Case>
36 37 38 |
# File 'lib/parse/retrieval/benchmark.rb', line 36 def load_cases(path) Array(JSON.parse(::File.read(path))).map { |h| case_from(h) } end |
.run(cases:, profiles:, runner:, k: 10) ⇒ Hash{String => Hash}
Run every case through every profile.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/parse/retrieval/benchmark.rb', line 59 def run(cases:, profiles:, runner:, k: 10) profiles.each_with_object({}) do |profile, report| rows = cases.map { |c| score_case(c, profile, runner, k) } report[profile.nil? ? "default" : profile.to_s] = summarize(rows).merge( by_tag: rows.flat_map { |r| r[:tags].map { |t| [t, r] } } .group_by(&:first) .transform_values { |pairs| summarize(pairs.map(&:last)) }, ) end end |
.semantic_search_runner(agent, class_name:, **options) ⇒ Proc
A runner that executes the semantic_search tool through agent
and ranks parent documents by their first chunk.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/parse/retrieval/benchmark.rb', line 77 def semantic_search_runner(agent, class_name:, **) lambda do |bench_case, profile| tokens = 0 # Notifications are delivered on the instrumenting thread, so only # events from THIS thread belong to this case; other threads' # searches are ignored. runner_thread = Thread.current sub = if defined?(ActiveSupport::Notifications) ActiveSupport::Notifications.subscribe("parse.retrieval.search") do |*args| next unless Thread.current.equal?(runner_thread) payload = args.last tokens += payload.dig(:rerank, :tokens_estimated).to_i if payload.is_a?(Hash) end end begin args = { class_name: class_name, query: bench_case.query }.merge() args[:profile] = profile unless profile.nil? result = agent.execute(:semantic_search, **args) data = result[:success] ? (result[:data] || {}) : {} chunks = data[:chunks] || data["chunks"] || [] ids = chunks.map { |c| (c[:metadata] || c["metadata"] || {})[:object_id] || c.dig("metadata", "object_id") } .compact.map(&:to_s).uniq { ids: ids, tokens_estimated: tokens, error: result[:success] ? nil : result[:error_code] } ensure ActiveSupport::Notifications.unsubscribe(sub) if sub end end end |