Class: DSPy::Propose::GroundedProposer
- Inherits:
-
Object
- Object
- DSPy::Propose::GroundedProposer
- 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
Constant Summary collapse
- MAX_HISTORY_INSTRUCTIONS =
5- TIPS =
Python-compatible TIPS dictionary for instruction generation
{ "none" => "", "creative" => "Don't be afraid to be creative when creating the new instruction!", "simple" => "Keep the instruction clear and concise.", "description" => "Make sure your instruction is very informative and descriptive.", "high_stakes" => "The instruction should include a high stakes scenario in which the LM must solve the task!", "persona" => 'Include a persona that is relevant to the task in the instruction (ie. "You are a ...")' }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize(config: nil, program: nil, trainset: nil) ⇒ GroundedProposer
constructor
A new instance of GroundedProposer.
- #propose_instructions(signature_class, examples, few_shot_examples: nil, current_instruction: nil, trial_logs: nil) ⇒ Object
- #propose_instructions_for_program(trainset:, program:, demo_candidates:, trial_logs: nil, num_instruction_candidates: nil) ⇒ Object
Constructor Details
#initialize(config: nil, program: nil, trainset: nil) ⇒ GroundedProposer
Returns a new instance of GroundedProposer.
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/propose/grounded_proposer.rb', line 146 def initialize(config: nil, program: nil, trainset: nil) @config = config || Config.new @program = program @trainset = trainset @dataset_summary = nil @program_code_string = nil # Generate dataset summary if data-aware mode enabled (Python: use_dataset_summary) if @config.use_dataset_summary && trainset && !trainset.empty? begin require_relative 'dataset_summary_generator' @dataset_summary = DatasetSummaryGenerator.create_dataset_summary( trainset, @config.view_data_batch_size, DSPy.current_lm, verbose: @config.verbose ) rescue => e DSPy.logger.warn("Failed to generate dataset summary: #{e.}") @dataset_summary = nil end end # Extract program source code if program-aware mode enabled if @config.program_aware && program @program_code_string = extract_program_source(program) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
137 138 139 |
# File 'lib/dspy/propose/grounded_proposer.rb', line 137 def config @config end |
Instance Method Details
#propose_instructions(signature_class, examples, few_shot_examples: nil, current_instruction: nil, trial_logs: nil) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/dspy/propose/grounded_proposer.rb', line 209 def propose_instructions(signature_class, examples, few_shot_examples: nil, current_instruction: nil, trial_logs: 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, few_shot_examples: few_shot_examples, trial_logs: trial_logs ) # Filter and rank candidates filtered_candidates = filter_and_rank_candidates(candidates, analysis) = { generation_timestamp: Time.now.iso8601, model_used: DSPy.current_lm.model, num_examples_analyzed: [examples.size, @config.view_data_batch_size].min, original_instruction: current_instruction } result = ProposalResult.new( candidate_instructions: filtered_candidates, analysis: analysis, metadata: ) emit_proposal_complete_event(result) result end end |
#propose_instructions_for_program(trainset:, program:, demo_candidates:, trial_logs: nil, num_instruction_candidates: 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 |
# File 'lib/dspy/propose/grounded_proposer.rb', line 260 def propose_instructions_for_program(trainset:, program:, demo_candidates:, trial_logs: nil, num_instruction_candidates: nil) num_candidates = num_instruction_candidates || @config.num_instruction_candidates current_instruction = if program.respond_to?(:prompt) && program.prompt.respond_to?(:instruction) program.prompt.instruction else nil end few_shot_examples = demo_candidates[0]&.flatten&.take(@config.num_demos_in_context) || [] signature_class = if program.respond_to?(:signature_class) program.signature_class else raise ArgumentError, "Program must expose signature_class for instruction proposal" end base_result = propose_instructions( signature_class, trainset, few_shot_examples: few_shot_examples, current_instruction: current_instruction, trial_logs: trial_logs ) predictor_instructions = { 0 => base_result.candidate_instructions.take(num_candidates) } ProposalResult.new( candidate_instructions: base_result.candidate_instructions, analysis: base_result.analysis, metadata: base_result., predictor_instructions: predictor_instructions ) end |