Module: DSPy::Teleprompt::InstructionUpdates
- Extended by:
- T::Sig
- Defined in:
- lib/dspy/teleprompt/instruction_updates.rb
Class Method Summary collapse
- .apply_examples(owner, predictor, examples) ⇒ Object
- .apply_instruction(owner, predictor, instruction) ⇒ Object
- .ensure_examples_capability!(predictor) ⇒ Object
- .ensure_instruction_capability!(predictor) ⇒ Object
- .replace_in_object(container, target, replacement, visited) ⇒ Object
- .replace_reference(owner, target, replacement) ⇒ Object
Class Method Details
.apply_examples(owner, predictor, examples) ⇒ Object
34 35 36 37 38 |
# File 'lib/dspy/teleprompt/instruction_updates.rb', line 34 def apply_examples(owner, predictor, examples) ensure_examples_capability!(predictor) updated = predictor.with_examples(examples) [replace_reference(owner, predictor, updated), updated] end |
.apply_instruction(owner, predictor, instruction) ⇒ Object
27 28 29 30 31 |
# File 'lib/dspy/teleprompt/instruction_updates.rb', line 27 def apply_instruction(owner, predictor, instruction) ensure_instruction_capability!(predictor) updated = predictor.with_instruction(instruction) [replace_reference(owner, predictor, updated), updated] end |
.ensure_examples_capability!(predictor) ⇒ Object
21 22 23 24 |
# File 'lib/dspy/teleprompt/instruction_updates.rb', line 21 def ensure_examples_capability!(predictor) return if predictor.respond_to?(:with_examples) raise DSPy::InstructionUpdateError.missing_examples_capability(predictor.class) end |
.ensure_instruction_capability!(predictor) ⇒ Object
15 16 17 18 |
# File 'lib/dspy/teleprompt/instruction_updates.rb', line 15 def ensure_instruction_capability!(predictor) return if predictor.respond_to?(:with_instruction) raise DSPy::InstructionUpdateError.missing_instruction_capability(predictor.class) end |
.replace_in_object(container, target, replacement, visited) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dspy/teleprompt/instruction_updates.rb', line 65 def replace_in_object(container, target, replacement, visited) return replacement if container.equal?(target) return container if visited.include?(container.object_id) visited.add(container.object_id) case container when Array modified = false new_array = container.map do |value| new_value = replace_in_object(value, target, replacement, visited) modified ||= !new_value.equal?(value) new_value end modified ? new_array : container when Hash modified = false new_hash = container.each_with_object({}) do |(key, value), memo| new_value = replace_in_object(value, target, replacement, visited) modified ||= !new_value.equal?(value) memo[key] = new_value end modified ? new_hash : container else container end end |
.replace_reference(owner, target, replacement) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dspy/teleprompt/instruction_updates.rb', line 41 def replace_reference(owner, target, replacement) return replacement if owner.equal?(target) Array(owner.instance_variables).each do |ivar| value = owner.instance_variable_get(ivar) next if value.nil? new_value = replace_in_object(value, target, replacement, ::Set.new) unless new_value.equal?(value) owner.instance_variable_set(ivar, new_value) end end owner end |