Class: RubyLLM::Contract::Eval::RetryOptimizer
- Inherits:
-
Object
- Object
- RubyLLM::Contract::Eval::RetryOptimizer
- Defined in:
- lib/ruby_llm/contract/eval/retry_optimizer.rb
Overview
Runs compare_models on ALL evals for a step, builds a score matrix, identifies the constraining eval, and suggests an escalation chain.
optimizer = RetryOptimizer.new(step: MyStep, candidates: [...], context: {})
result = optimizer.call
result.print_summary
result.to_dsl # => copy-paste retry_policy
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(step:, candidates:, context: {}, min_score: DEFAULT_MIN_SCORE, runs: 1, production_mode: nil) ⇒ RetryOptimizer
constructor
A new instance of RetryOptimizer.
Constructor Details
#initialize(step:, candidates:, context: {}, min_score: DEFAULT_MIN_SCORE, runs: 1, production_mode: nil) ⇒ RetryOptimizer
Returns a new instance of RetryOptimizer.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ruby_llm/contract/eval/retry_optimizer.rb', line 101 def initialize(step:, candidates:, context: {}, min_score: DEFAULT_MIN_SCORE, runs: 1, production_mode: nil) @step = step @candidates = candidates @context = context @min_score = min_score @runs = runs @production_mode = production_mode end |
Instance Method Details
#call ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ruby_llm/contract/eval/retry_optimizer.rb', line 112 def call evals = @step.eval_names return empty_result(evals) if evals.empty? score_matrix = {} evals.each do |eval_name| # `retry_policy_override: nil` in context disables the step's # class-level retry policy for this comparison run — see # step/base.rb#runtime_settings, which honours the key when # present (even when value is nil). Replaces the prior # `define_singleton_method(:retry_policy)` mutation, which was # not thread-safe across concurrent optimizer calls. comparison = @step.compare_models( eval_name, candidates: @candidates, context: @context.merge(retry_policy_override: nil), runs: @runs, production_mode: @production_mode ) score_matrix[eval_name] = extract_scores(comparison) end labels = score_matrix.values.flat_map(&:keys).uniq constraining = find_constraining_eval(score_matrix, labels) chain, details = build_chain(score_matrix, labels, evals) Result.new( step_name: @step.name || @step.to_s, eval_names: evals, candidate_labels: labels, score_matrix: score_matrix, constraining_eval: constraining, chain: chain, chain_details: details ) end |