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.
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
#prompt ⇒ Object
Returns the value of attribute prompt.
53
54
55
|
# File 'lib/dspy/predict.rb', line 53
def prompt
@prompt
end
|
#signature_class ⇒ Object
Returns the value of attribute signature_class.
50
51
52
|
# File 'lib/dspy/predict.rb', line 50
def signature_class
@signature_class
end
|
Class Method Details
.from_h(data) ⇒ Object
64
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
|
# File 'lib/dspy/predict.rb', line 64
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
129
130
131
|
# File 'lib/dspy/predict.rb', line 129
def add_examples(examples)
with_prompt(@prompt.add_examples(examples))
end
|
#forward(**kwargs) ⇒ Object
134
135
136
137
|
# File 'lib/dspy/predict.rb', line 134
def forward(**kwargs)
@last_input_values = kwargs.clone
T.cast(forward_untyped(**kwargs), T.type_parameter(:O))
end
|
#forward_untyped(**input_values) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/dspy/predict.rb', line 140
def forward_untyped(**input_values)
DSPy::Context.with_span(
operation: "#{self.class.name}.forward",
'dspy.module' => self.class.name,
'dspy.signature' => @signature_class.name
) do
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)
create_prediction_result(input_values, processed_output)
end
end
|
#system_signature ⇒ Object
100
101
102
|
# File 'lib/dspy/predict.rb', line 100
def system_signature
@prompt.render_system_prompt
end
|
#user_signature(input_values) ⇒ Object
105
106
107
|
# File 'lib/dspy/predict.rb', line 105
def user_signature(input_values)
@prompt.render_user_prompt(input_values)
end
|
#with_examples(examples) ⇒ Object
124
125
126
|
# File 'lib/dspy/predict.rb', line 124
def with_examples(examples)
with_prompt(@prompt.with_examples(examples))
end
|
#with_instruction(instruction) ⇒ Object
119
120
121
|
# File 'lib/dspy/predict.rb', line 119
def with_instruction(instruction)
with_prompt(@prompt.with_instruction(instruction))
end
|
#with_prompt(new_prompt) ⇒ Object
111
112
113
114
115
116
|
# File 'lib/dspy/predict.rb', line 111
def with_prompt(new_prompt)
instance = self.class.new(@signature_class)
instance.instance_variable_set(:@prompt, new_prompt)
instance
end
|