Class: DSPy::Predict

Inherits:
Module show all
Extended by:
T::Sig
Includes:
Mixins::StructBuilder, Mixins::TypeCoercion
Defined in:
lib/dspy/predict.rb

Direct Known Subclasses

ChainOfThought, CodeAct, ReAct

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#call, #call_untyped, #forward, #lm

Constructor Details

#initialize(signature_class) ⇒ Predict

Returns a new instance of Predict.



57
58
59
60
61
# File 'lib/dspy/predict.rb', line 57

def initialize(signature_class)
  super()
  @signature_class = signature_class
  @prompt = Prompt.from_signature(signature_class)
end

Instance Attribute Details

#promptObject (readonly)

Returns the value of attribute prompt.



54
55
56
# File 'lib/dspy/predict.rb', line 54

def prompt
  @prompt
end

#signature_classObject (readonly)

Returns the value of attribute signature_class.



51
52
53
# File 'lib/dspy/predict.rb', line 51

def signature_class
  @signature_class
end

Class Method Details

.from_h(data) ⇒ Object

Raises:

  • (ArgumentError)


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
92
93
94
95
96
97
# File 'lib/dspy/predict.rb', line 65

def self.from_h(data)
  state = data[:state]
  raise ArgumentError, "Missing state in serialized data" unless state

  signature_class_name = state[:signature_class]
  signature_class = Object.const_get(signature_class_name)
  program = new(signature_class)
  
  # Restore instruction if available
  if state[:instruction]
    program = program.with_instruction(state[:instruction])
  end
  
  # Restore examples if available
  few_shot_examples = state[:few_shot_examples]
  if few_shot_examples && !few_shot_examples.empty?
    # Convert hash examples back to FewShotExample objects
    examples = few_shot_examples.map do |ex|
      if ex.is_a?(Hash)
        DSPy::FewShotExample.new(
          input: ex[:input],
          output: ex[:output],
          reasoning: ex[:reasoning]
        )
      else
        ex
      end
    end
    program = program.with_examples(examples)
  end
  
  program
end

Instance Method Details

#add_examples(examples) ⇒ Object



130
131
132
# File 'lib/dspy/predict.rb', line 130

def add_examples(examples)
  with_prompt(@prompt.add_examples(examples))
end

#forward_untyped(**input_values) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dspy/predict.rb', line 137

def forward_untyped(**input_values)
  # Module#forward handles span creation, we just do the prediction logic
  
  # Store input values for optimization
  @last_input_values = input_values.clone
  
  # Validate input
  validate_input_struct(input_values)
  
  # Check if LM is configured
  current_lm = lm
  if current_lm.nil?
    raise DSPy::ConfigurationError.missing_lm(self.class.name)
  end
  
  # Call LM and process response
  output_attributes = current_lm.chat(self, input_values)
  processed_output = process_lm_output(output_attributes)
  
  # Create combined result struct
  prediction_result = create_prediction_result(input_values, processed_output)
  
  prediction_result
end

#system_signatureObject



101
102
103
# File 'lib/dspy/predict.rb', line 101

def system_signature
  @prompt.render_system_prompt
end

#user_signature(input_values) ⇒ Object



106
107
108
# File 'lib/dspy/predict.rb', line 106

def user_signature(input_values)
  @prompt.render_user_prompt(input_values)
end

#with_examples(examples) ⇒ Object



125
126
127
# File 'lib/dspy/predict.rb', line 125

def with_examples(examples)
  with_prompt(@prompt.with_examples(examples))
end

#with_instruction(instruction) ⇒ Object



120
121
122
# File 'lib/dspy/predict.rb', line 120

def with_instruction(instruction)
  with_prompt(@prompt.with_instruction(instruction))
end

#with_prompt(new_prompt) ⇒ Object



112
113
114
115
116
117
# File 'lib/dspy/predict.rb', line 112

def with_prompt(new_prompt)
  # Create a new instance with the same signature but updated prompt
  instance = self.class.new(@signature_class)
  instance.instance_variable_set(:@prompt, new_prompt)
  instance
end