Class: DSPy::Predict
Instance Attribute Summary collapse
Class Method 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.
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
#prompt ⇒ Object
Returns the value of attribute prompt.
54
55
56
|
# File 'lib/dspy/predict.rb', line 54
def prompt
@prompt
end
|
#signature_class ⇒ Object
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
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)
if state[:instruction]
program = program.with_instruction(state[:instruction])
end
few_shot_examples = state[:few_shot_examples]
if few_shot_examples && !few_shot_examples.empty?
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(**kwargs) ⇒ Object
135
136
137
138
|
# File 'lib/dspy/predict.rb', line 135
def forward(**kwargs)
@last_input_values = kwargs.clone
T.cast(forward_untyped(**kwargs), T.type_parameter(:O))
end
|
#forward_untyped(**input_values) ⇒ Object
141
142
143
144
145
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
174
|
# File 'lib/dspy/predict.rb', line 141
def forward_untyped(**input_values)
DSPy::Context.with_span(
operation: "#{self.class.name}.forward",
'langfuse.observation.type' => 'span',
'langfuse.observation.input' => input_values.to_json,
'dspy.module' => self.class.name,
'dspy.signature' => @signature_class.name
) do |span|
validate_input_struct(input_values)
current_lm = lm
if current_lm.nil?
raise DSPy::ConfigurationError.missing_lm(self.class.name)
end
output_attributes = current_lm.chat(self, input_values)
processed_output = process_lm_output(output_attributes)
prediction_result = create_prediction_result(input_values, processed_output)
if span && prediction_result
output_hash = prediction_result.respond_to?(:to_h) ? prediction_result.to_h : prediction_result.to_s
span.set_attribute('langfuse.observation.output', DSPy::Utils::Serialization.to_json(output_hash))
end
prediction_result
end
end
|
#system_signature ⇒ Object
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)
instance = self.class.new(@signature_class)
instance.instance_variable_set(:@prompt, new_prompt)
instance
end
|