Module: DSPy::Teleprompt::Utils
- Extended by:
- T::Sig
- Defined in:
- lib/dspy/teleprompt/utils.rb
Overview
Bootstrap utilities for MIPROv2 optimization Handles few-shot example generation and candidate program evaluation
Defined Under Namespace
Classes: BootstrapConfig, BootstrapResult
Class Method Summary collapse
- .create_n_fewshot_demo_sets(program, trainset, config: BootstrapConfig.new, metric: nil) ⇒ Object
- .eval_candidate_program(program, examples, config: BootstrapConfig.new, metric: nil) ⇒ Object
- .eval_candidate_program_full(program, examples, config, metric) ⇒ Object
- .eval_candidate_program_minibatch(program, examples, config, metric) ⇒ Object
Class Method Details
.create_n_fewshot_demo_sets(program, trainset, config: BootstrapConfig.new, metric: nil) ⇒ Object
105 106 107 108 109 110 111 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 |
# File 'lib/dspy/teleprompt/utils.rb', line 105 def self.create_n_fewshot_demo_sets(program, trainset, config: BootstrapConfig.new, metric: nil) DSPy::Context.with_span( operation: 'optimization.bootstrap_start', 'dspy.module' => 'Bootstrap', 'bootstrap.trainset_size' => trainset.size, 'bootstrap.max_examples' => config.max_bootstrapped_examples, 'bootstrap.num_candidate_sets' => config.num_candidate_sets ) do # Convert to typed examples if needed typed_examples = ensure_typed_examples(trainset) # Generate successful examples through bootstrap successful_examples, failed_examples = generate_successful_examples( program, typed_examples, config, metric ) # Create candidate sets from successful examples candidate_sets = create_candidate_sets(successful_examples, config) # Gather statistics statistics = { total_trainset: trainset.size, successful_count: successful_examples.size, failed_count: failed_examples.size, success_rate: successful_examples.size.to_f / (successful_examples.size + failed_examples.size), candidate_sets_created: candidate_sets.size, average_set_size: candidate_sets.empty? ? 0 : candidate_sets.map(&:size).sum.to_f / candidate_sets.size } emit_bootstrap_complete_event(statistics) BootstrapResult.new( candidate_sets: candidate_sets, successful_examples: successful_examples, failed_examples: failed_examples, statistics: statistics ) end end |
.eval_candidate_program(program, examples, config: BootstrapConfig.new, metric: nil) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/dspy/teleprompt/utils.rb', line 157 def self.eval_candidate_program(program, examples, config: BootstrapConfig.new, metric: nil) # Use minibatch evaluation for large datasets if examples.size > config.minibatch_size eval_candidate_program_minibatch(program, examples, config, metric) else eval_candidate_program_full(program, examples, config, metric) end end |
.eval_candidate_program_full(program, examples, config, metric) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/dspy/teleprompt/utils.rb', line 200 def self.eval_candidate_program_full(program, examples, config, metric) # Create evaluator with proper configuration evaluator = DSPy::Evaluate.new( program, metric: metric || default_metric_for_examples(examples), num_threads: config.num_threads, max_errors: config.max_errors ) # Run evaluation evaluator.evaluate(examples, display_progress: false) end |
.eval_candidate_program_minibatch(program, examples, config, metric) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dspy/teleprompt/utils.rb', line 175 def self.eval_candidate_program_minibatch(program, examples, config, metric) DSPy::Context.with_span( operation: 'optimization.minibatch_evaluation', 'dspy.module' => 'Bootstrap', 'minibatch.total_examples' => examples.size, 'minibatch.size' => config.minibatch_size, 'minibatch.num_batches' => (examples.size.to_f / config.minibatch_size).ceil ) do # Randomly sample a minibatch for evaluation sample_size = [config.minibatch_size, examples.size].min sampled_examples = examples.sample(sample_size) eval_candidate_program_full(program, sampled_examples, config, metric) end end |