Class: DSPy::Teleprompt::MIPROv2

Inherits:
Teleprompter show all
Extended by:
T::Sig
Defined in:
lib/dspy/teleprompt/mipro_v2.rb

Overview

MIPROv2: Multi-prompt Instruction Proposal with Retrieval Optimization State-of-the-art prompt optimization combining bootstrap sampling, instruction generation, and Bayesian optimization

Defined Under Namespace

Modules: AutoMode Classes: CandidateConfig, MIPROv2Config, MIPROv2Result

Instance Attribute Summary collapse

Attributes inherited from Teleprompter

#config, #evaluator, #metric

Instance Method Summary collapse

Methods inherited from Teleprompter

#create_evaluator, #ensure_typed_examples, #evaluate_program, #save_results, #validate_inputs

Constructor Details

#initialize(metric: nil, config: nil) ⇒ MIPROv2

Returns a new instance of MIPROv2.



243
244
245
246
247
248
249
250
# File 'lib/dspy/teleprompt/mipro_v2.rb', line 243

def initialize(metric: nil, config: nil)
  @mipro_config = config || MIPROv2Config.new
  super(metric: metric, config: @mipro_config)
  
  @proposer = DSPy::Propose::GroundedProposer.new(config: @mipro_config.proposer_config)
  @optimization_trace = []
  @evaluated_candidates = []
end

Instance Attribute Details

#mipro_configObject (readonly)

Returns the value of attribute mipro_config.



232
233
234
# File 'lib/dspy/teleprompt/mipro_v2.rb', line 232

def mipro_config
  @mipro_config
end

#proposerObject (readonly)

Returns the value of attribute proposer.



235
236
237
# File 'lib/dspy/teleprompt/mipro_v2.rb', line 235

def proposer
  @proposer
end

Instance Method Details

#compile(program, trainset:, valset: nil) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/dspy/teleprompt/mipro_v2.rb', line 260

def compile(program, trainset:, valset: nil)
  validate_inputs(program, trainset, valset)

  instrument_step('miprov2_compile', {
    trainset_size: trainset.size,
    valset_size: valset&.size || 0,
    num_trials: @mipro_config.num_trials,
    optimization_strategy: @mipro_config.optimization_strategy,
    mode: infer_auto_mode
  }) 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([typed_trainset.size / 3, 10].max)

    # Phase 1: Bootstrap few-shot examples
    emit_event('phase_start', { phase: 1, name: 'bootstrap' })
    bootstrap_result = phase_1_bootstrap(program, typed_trainset)
    emit_event('phase_complete', { 
      phase: 1, 
      success_rate: bootstrap_result.statistics[:success_rate],
      candidate_sets: bootstrap_result.candidate_sets.size
    })

    # Phase 2: Generate instruction candidates
    emit_event('phase_start', { phase: 2, name: 'instruction_proposal' })
    proposal_result = phase_2_propose_instructions(program, typed_trainset, bootstrap_result)
    emit_event('phase_complete', { 
      phase: 2, 
      num_candidates: proposal_result.num_candidates,
      best_instruction_preview: proposal_result.best_instruction[0, 50]
    })

    # Phase 3: Bayesian optimization
    emit_event('phase_start', { phase: 3, name: 'optimization' })
    optimization_result = phase_3_optimize(
      program,
      evaluation_set,
      proposal_result,
      bootstrap_result
    )
    emit_event('phase_complete', { 
      phase: 3, 
      best_score: optimization_result[:best_score],
      trials_completed: optimization_result[:trials_completed]
    })

    # Build final result
    final_result = build_miprov2_result(
      optimization_result,
      bootstrap_result,
      proposal_result
    )

    save_results(final_result)
    final_result
  end
end