Class: DSPy::Teleprompt::GEPA::MutationEngine

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/teleprompt/gepa.rb

Overview

MutationEngine: Handles LLM-based prompt transformations for genetic evolution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ MutationEngine

Returns a new instance of MutationEngine.



1732
1733
1734
1735
# File 'lib/dspy/teleprompt/gepa.rb', line 1732

def initialize(config:)
  @config = config
  @instruction_proposer = InstructionProposer.new(config: config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



1726
1727
1728
# File 'lib/dspy/teleprompt/gepa.rb', line 1726

def config
  @config
end

#instruction_proposerObject (readonly)

Returns the value of attribute instruction_proposer.



1729
1730
1731
# File 'lib/dspy/teleprompt/gepa.rb', line 1729

def instruction_proposer
  @instruction_proposer
end

Instance Method Details

#batch_mutate(programs, execution_traces: [], failed_examples: []) ⇒ Object



1765
1766
1767
1768
1769
# File 'lib/dspy/teleprompt/gepa.rb', line 1765

def batch_mutate(programs, execution_traces: [], failed_examples: [])
  return [] if programs.empty?

  programs.map { |program| mutate_program(program, execution_traces: execution_traces, failed_examples: failed_examples) }
end

#emit_event(event_name, data = {}) ⇒ Object



1773
1774
1775
1776
# File 'lib/dspy/teleprompt/gepa.rb', line 1773

def emit_event(event_name, data = {})
  # For now, just a placeholder - could integrate with DSPy event system
  # In full implementation, this would emit events for monitoring
end

#mutate_program(program, execution_traces: [], failed_examples: []) ⇒ Object



1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
# File 'lib/dspy/teleprompt/gepa.rb', line 1739

def mutate_program(program, execution_traces: [], failed_examples: [])
  return program if rand > @config.mutation_rate

  begin
    original_instruction = extract_instruction(program)

    # Use LLM-based instruction proposal instead of hardcoded mutations
    improved_instruction = @instruction_proposer.propose_instruction(
      original_instruction: original_instruction,
      execution_traces: execution_traces,
      failed_examples: failed_examples
    )

    create_mutated_program(program, improved_instruction)
  rescue => e
    emit_event('mutation_error', {
      error: e.message,
      program_type: program.class.name
    })
    # Return original program on mutation failure
    program
  end
end