Class: DSPy::Teleprompt::SimpleOptimizer
- Inherits:
-
Teleprompter
- Object
- Teleprompter
- DSPy::Teleprompt::SimpleOptimizer
- Extended by:
- T::Sig
- Defined in:
- lib/dspy/teleprompt/simple_optimizer.rb
Overview
Simple optimization algorithm using random/grid search Uses grounded proposer for instruction generation and bootstrap for examples
Defined Under Namespace
Classes: OptimizerConfig, TrialResult
Instance Attribute Summary collapse
-
#optimizer_config ⇒ Object
readonly
Returns the value of attribute optimizer_config.
-
#proposer ⇒ Object
readonly
Returns the value of attribute proposer.
Attributes inherited from Teleprompter
Instance Method Summary collapse
- #compile(program, trainset:, valset: nil) ⇒ Object
-
#initialize(metric: nil, config: nil) ⇒ SimpleOptimizer
constructor
A new instance of SimpleOptimizer.
Methods inherited from Teleprompter
#create_evaluator, #ensure_typed_examples, #evaluate_program, #save_results, #validate_inputs
Constructor Details
#initialize(metric: nil, config: nil) ⇒ SimpleOptimizer
Returns a new instance of SimpleOptimizer.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dspy/teleprompt/simple_optimizer.rb', line 109 def initialize(metric: nil, config: nil) @optimizer_config = config || OptimizerConfig.new super(metric: metric, config: @optimizer_config) @proposer = if @optimizer_config.use_instruction_optimization DSPy::Propose::GroundedProposer.new(config: @optimizer_config.proposer_config) else nil end end |
Instance Attribute Details
#optimizer_config ⇒ Object (readonly)
Returns the value of attribute optimizer_config.
98 99 100 |
# File 'lib/dspy/teleprompt/simple_optimizer.rb', line 98 def optimizer_config @optimizer_config end |
#proposer ⇒ Object (readonly)
Returns the value of attribute proposer.
101 102 103 |
# File 'lib/dspy/teleprompt/simple_optimizer.rb', line 101 def proposer @proposer end |
Instance Method Details
#compile(program, trainset:, valset: nil) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/dspy/teleprompt/simple_optimizer.rb', line 128 def compile(program, trainset:, valset: nil) validate_inputs(program, trainset, valset) instrument_step('compile', { trainset_size: trainset.size, valset_size: valset&.size || 0, num_trials: @optimizer_config.num_trials, search_strategy: @optimizer_config.search_strategy }) do # Convert examples to typed format typed_trainset = ensure_typed_examples(trainset) typed_valset = valset ? ensure_typed_examples(valset) : nil # Use validation set if available, otherwise use part of training set evaluation_set = typed_valset || typed_trainset.take(10) # Bootstrap few-shot examples if enabled bootstrap_result = nil if @optimizer_config.use_few_shot_optimization bootstrap_result = bootstrap_examples(program, typed_trainset) end # Generate instruction candidates if enabled instruction_candidates = [] if @optimizer_config.use_instruction_optimization && @proposer instruction_candidates = generate_instruction_candidates(program, typed_trainset, bootstrap_result) end # Run optimization trials trials = run_optimization_trials( program, evaluation_set, instruction_candidates, bootstrap_result ) # Find best trial best_trial = find_best_trial(trials) # Build optimization result optimization_result = build_optimization_result(best_trial, trials) save_results(optimization_result) optimization_result end end |