dspy.Predict
Constructor
The constructor initializes the Predict
class and sets up its attributes, taking in the signature
and additional config options. If the signature
is a string, it processes the input and output fields, generates instructions, and creates a template for the specified signature
type.
class Predict(Parameter):
def __init__(self, signature, **config):
self.stage = random.randbytes(8).hex()
self.signature = signature
self.config = config
self.reset()
if isinstance(signature, str):
inputs, outputs = signature.split("->")
inputs, outputs = inputs.split(","), outputs.split(",")
inputs, outputs = [field.strip() for field in inputs], [field.strip() for field in outputs]
assert all(len(field.split()) == 1 for field in (inputs + outputs))
inputs_ = ', '.join([f"`{field}`" for field in inputs])
outputs_ = ', '.join([f"`{field}`" for field in outputs])
instructions = f"""Given the fields {inputs_}, produce the fields {outputs_}."""
inputs = {k: InputField() for k in inputs}
outputs = {k: OutputField() for k in outputs}
for k, v in inputs.items():
v.finalize(k, infer_prefix(k))
for k, v in outputs.items():
v.finalize(k, infer_prefix(k))
self.signature = dsp.Template(instructions, **inputs, **outputs)
Parameters:
- signature
(Any): Signature of predictive model.
- **config
(dict): Additional configuration parameters for model.
Method
__call__(self, **kwargs)
This method serves as a wrapper for the forward
method. It allows making predictions using the Predict
class by providing keyword arguments.
Parameters:
- **kwargs
: Keyword arguments required for prediction.
Returns:
- The result of forward
method.
Examples
#Define a simple signature for basic question answering
class BasicQA(dspy.Signature):
"""Answer questions with short factoid answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
#Pass signature to Predict module
generate_answer = dspy.Predict(BasicQA)
# Call the predictor on a particular input.
question='What is the color of the sky?'
pred = generate_answer(question=question)
print(f"Question: {question}")
print(f"Predicted Answer: {pred.answer}")