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.



36
37
38
39
40
# File 'lib/dspy/predict.rb', line 36

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.



33
34
35
# File 'lib/dspy/predict.rb', line 33

def prompt
  @prompt
end

#signature_classObject (readonly)

Returns the value of attribute signature_class.



30
31
32
# File 'lib/dspy/predict.rb', line 30

def signature_class
  @signature_class
end

Instance Method Details

#add_examples(examples) ⇒ Object



73
74
75
# File 'lib/dspy/predict.rb', line 73

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

#forward(**kwargs) ⇒ Object



78
79
80
81
# File 'lib/dspy/predict.rb', line 78

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

#forward_untyped(**input_values) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dspy/predict.rb', line 84

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



44
45
46
# File 'lib/dspy/predict.rb', line 44

def system_signature
  @prompt.render_system_prompt
end

#user_signature(input_values) ⇒ Object



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

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

#with_examples(examples) ⇒ Object



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

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

#with_instruction(instruction) ⇒ Object



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

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

#with_prompt(new_prompt) ⇒ Object



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

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