Skip to main content

dspy.ChainOfThoughtWithHint

Constructor

The constructor initializes the ChainOfThoughtWithHint class and sets up its attributes, inheriting from the Predict class. This class enhances the ChainOfThought class by offering an additional option to provide hints for reasoning. Two distinct signature templates are created internally depending on the presence of the hint.

class ChainOfThoughtWithHint(Predict):
def __init__(self, signature, rationale_type=None, activated=True, **config):
super().__init__(signature, **config)
self.activated = activated
signature = self.signature

*keys, last_key = signature.fields.keys()
rationale_type = rationale_type or dspy.OutputField(
prefix="Reasoning: Let's think step by step in order to",
desc="${produce the " + last_key + "}. We ...",
)
self.extended_signature1 = self.signature.insert(-2, "rationale", rationale_type, type_=str)

DEFAULT_HINT_TYPE = dspy.OutputField()
self.extended_signature2 = self.extended_signature1.insert(-2, "hint", DEFAULT_HINT_TYPE, type_=str)

Parameters:

  • signature (Any): Signature of predictive model.
  • rationale_type (dspy.OutputField, optional): Rationale type for reasoning steps. Defaults to None.
  • activated (bool, optional): Flag for activated chain of thought processing. Defaults to True.
  • **config (dict): Additional configuration parameters for model.

Method

forward(self, **kwargs)

This method extends the parent Predict class's forward pass, updating the signature dynamically based on the presence of hint in the keyword arguments and the activated attribute.

Parameters:

  • **kwargs: Keyword arguments required for prediction.

Returns:

  • The result of the forward method in the parent Predict class.

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 ChainOfThought module
generate_answer = dspy.ChainOfThoughtWithHint(BasicQA)

# Call the predictor on a particular input alongside a hint.
question='What is the color of the sky?'
hint = "It's what you often see during a sunny day."
pred = generate_answer(question=question, hint=hint)

print(f"Question: {question}")
print(f"Predicted Answer: {pred.answer}")