Class: DSPy::Predict
Constant Summary
Constants inherited
from Module
Module::DEFAULT_MODULE_SUBSCRIPTION_SCOPE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
deserialize_enum, enum_type?, extract_enum_class
Methods inherited from Module
#call, #call_untyped, #configure_predictor, #dup_for_thread, #forward, inherited, #lm, #module_scope_id, #module_scope_label, #module_scope_label=, module_subscription_specs, #registered_module_subscriptions, #save, subscribe, #to_h, #unsubscribe_module_events
Methods included from Callbacks
included
Constructor Details
#initialize(signature_class) ⇒ Predict
Returns a new instance of Predict.
63
64
65
66
67
68
69
|
# File 'lib/dspy/predict.rb', line 63
def initialize(signature_class)
super()
@signature_class = signature_class
@prompt = build_prompt_from_signature
@demos = nil
end
|
Instance Attribute Details
#demos ⇒ Object
Returns the value of attribute demos.
60
61
62
|
# File 'lib/dspy/predict.rb', line 60
def demos
@demos
end
|
#prompt ⇒ Object
Returns the value of attribute prompt.
56
57
58
|
# File 'lib/dspy/predict.rb', line 56
def prompt
@prompt
end
|
#signature_class ⇒ Object
Returns the value of attribute signature_class.
53
54
55
|
# File 'lib/dspy/predict.rb', line 53
def signature_class
@signature_class
end
|
Class Method Details
.from_h(data) ⇒ Object
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
98
99
100
101
102
103
104
105
|
# File 'lib/dspy/predict.rb', line 73
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
141
142
143
144
145
146
|
# File 'lib/dspy/predict.rb', line 141
def add_examples(examples)
instance = with_prompt(@prompt.add_examples(examples))
combined = instance.prompt.few_shot_examples
instance.demos = combined.map { |example| example }
instance
end
|
149
150
151
152
153
|
# File 'lib/dspy/predict.rb', line 149
def configure(&block)
super(&block)
sync_prompt_formats_from_lm(config.lm) if config.lm
self
end
|
#forward_untyped(**input_values) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/dspy/predict.rb', line 168
def forward_untyped(**input_values)
input_props = @signature_class.input_struct_class.props
coerced_input_values = coerce_output_attributes(input_values, input_props)
validate_input_struct(coerced_input_values)
current_lm = lm
if current_lm.nil?
raise DSPy::ConfigurationError.missing_lm(self.class.name)
end
output_attributes = current_lm.chat(self, coerced_input_values)
processed_output = process_lm_output(output_attributes)
prediction_result = create_prediction_result(coerced_input_values, processed_output)
prediction_result
end
|
#named_predictors ⇒ Object
156
157
158
|
# File 'lib/dspy/predict.rb', line 156
def named_predictors
[["self", self]]
end
|
#predictors ⇒ Object
161
162
163
|
# File 'lib/dspy/predict.rb', line 161
def predictors
[self]
end
|
#system_signature ⇒ Object
109
110
111
|
# File 'lib/dspy/predict.rb', line 109
def system_signature
@prompt.render_system_prompt
end
|
#user_signature(input_values) ⇒ Object
114
115
116
|
# File 'lib/dspy/predict.rb', line 114
def user_signature(input_values)
@prompt.render_user_prompt(input_values)
end
|
#with_examples(examples) ⇒ Object
134
135
136
137
138
|
# File 'lib/dspy/predict.rb', line 134
def with_examples(examples)
instance = with_prompt(@prompt.with_examples(examples))
instance.demos = examples.map { |example| example }
instance
end
|
#with_instruction(instruction) ⇒ Object
129
130
131
|
# File 'lib/dspy/predict.rb', line 129
def with_instruction(instruction)
with_prompt(@prompt.with_instruction(instruction))
end
|
#with_prompt(new_prompt) ⇒ Object
120
121
122
123
124
125
126
|
# File 'lib/dspy/predict.rb', line 120
def with_prompt(new_prompt)
instance = self.class.new(@signature_class)
instance.instance_variable_set(:@prompt, new_prompt)
instance.instance_variable_set(:@demos, @demos&.map { |demo| demo })
instance
end
|