Class: DSPy::Propose::GroundedProposer

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

Overview

Grounded Proposer for generating better instructions based on training data Analyzes task patterns and creates contextually appropriate instructions

Defined Under Namespace

Classes: Config, ProposalResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ GroundedProposer



92
93
94
# File 'lib/dspy/propose/grounded_proposer.rb', line 92

def initialize(config: nil)
  @config = config || Config.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



89
90
91
# File 'lib/dspy/propose/grounded_proposer.rb', line 89

def config
  @config
end

Instance Method Details

#propose_instructions(signature_class, examples, few_shot_examples: nil, current_instruction: 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
# File 'lib/dspy/propose/grounded_proposer.rb', line 105

def propose_instructions(signature_class, examples, few_shot_examples: nil, current_instruction: nil)
  DSPy::Context.with_span(
    operation: 'optimization.instruction_proposal',
    'dspy.module' => 'GroundedProposer',
    'proposal.signature' => signature_class.name,
    'proposal.num_examples' => examples.size,
    'proposal.has_few_shot' => !few_shot_examples.nil?,
    'proposal.has_current_instruction' => !current_instruction.nil?
  ) do
    # Analyze the task and training data
    analysis = analyze_task(signature_class, examples, few_shot_examples)
    
    # Generate instruction candidates
    candidates = generate_instruction_candidates(
      signature_class, 
      analysis, 
      current_instruction
    )

    # Filter and rank candidates
    filtered_candidates = filter_and_rank_candidates(candidates, analysis)

     = {
      generation_timestamp: Time.now.iso8601,
      model_used: @config.proposal_model,
      num_examples_analyzed: [examples.size, @config.max_examples_for_analysis].min,
      original_instruction: current_instruction
    }

    result = ProposalResult.new(
      candidate_instructions: filtered_candidates,
      analysis: analysis,
      metadata: 
    )

    emit_proposal_complete_event(result)
    result
  end
end