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

Instance Method Summary collapse

Methods inherited from Module

#call, #call_untyped, #lm

Constructor Details

#initialize(signature_class) ⇒ Predict

Returns a new instance of Predict.



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

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.



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

def prompt
  @prompt
end

#signature_classObject (readonly)

Returns the value of attribute signature_class.



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

def signature_class
  @signature_class
end

Instance Method Details

#add_examples(examples) ⇒ Object



93
94
95
# File 'lib/dspy/predict.rb', line 93

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

#forward(**kwargs) ⇒ Object



98
99
100
101
# File 'lib/dspy/predict.rb', line 98

def forward(**kwargs)
  @last_input_values = kwargs.clone
  T.cast(forward_untyped(**kwargs), T.type_parameter(:O))
end

#forward_untyped(**input_values) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dspy/predict.rb', line 104

def forward_untyped(**input_values)
  # Wrap prediction in span tracking
  DSPy::Context.with_span(
    operation: "#{self.class.name}.forward",
    'dspy.module' => self.class.name,
    'dspy.signature' => @signature_class.name
  ) do
    # 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
    create_prediction_result(input_values, processed_output)
  end
end

#system_signatureObject



64
65
66
# File 'lib/dspy/predict.rb', line 64

def system_signature
  @prompt.render_system_prompt
end

#user_signature(input_values) ⇒ Object



69
70
71
# File 'lib/dspy/predict.rb', line 69

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

#with_examples(examples) ⇒ Object



88
89
90
# File 'lib/dspy/predict.rb', line 88

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

#with_instruction(instruction) ⇒ Object



83
84
85
# File 'lib/dspy/predict.rb', line 83

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

#with_prompt(new_prompt) ⇒ Object



75
76
77
78
79
80
# File 'lib/dspy/predict.rb', line 75

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