def __init__(self, signature, max_iters=3):
super().__init__()
self.signature = signature = ensure_signature(signature)
self.max_iters = max_iters
self.input_fields = signature.input_fields
self.output_fields = signature.output_fields
assert len(self.output_fields) == 1, "PoT only supports one output field."
self.output_field_name = next(iter(self.output_fields))
inputs_ = ", ".join(
[f"`{field_name}`" for field_name in self.input_fields.keys()],
)
outputs_ = f"`{self.output_field_name}`"
assert len(self.output_fields) == 1, "PoT only supports one output field."
instr = []
instr.append(
f"You will be given {inputs_} and you will respond with {outputs_}.",
)
instr.append(
f"Generating executable Python code that programmatically computes the correct {outputs_}.",
)
instr.append(
f"After you're done with the computation, make sure the last line in your code evaluates to the correct value for {outputs_}.",
)
instr = "\n".join(instr)
self.code_generate = dspy.ChainOfThought(
dspy.Signature(
self._generate_signature("generate").fields,
self._generate_instruction("generate"),
),
)
self.code_regenerate = dspy.ChainOfThought(
dspy.Signature(
self._generate_signature("regenerate").fields,
self._generate_instruction("regenerate"),
),
)
self.generate_answer = dspy.ChainOfThought(
dspy.Signature(
self._generate_signature("answer").fields,
self._generate_instruction("answer"),
),
)