DSPy: The framework for programming—not prompting—language models
Python
Add retry capabilities to Predict
Shangyint879e956e4329
diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.pyindex 83bb74f3a..c153ef57c 100644--- a/dspy/predict/chain_of_thought.py+++ b/dspy/predict/chain_of_thought.py@@ -53,8 +53,8 @@ class ChainOfThought(Predict):)def forward(self, **kwargs):- signature_kwargs = kwargs.pop("signature", None)- if signature_kwargs is None:+ new_signature = kwargs.pop("new_signature", None)+ if new_signature is None:if self.activated is True or (self.activated is None and isinstance(dsp.settings.lm, dsp.GPT3)):@@ -62,7 +62,7 @@ class ChainOfThought(Predict):else:signature = self.signatureelse:- signature = dsp.Template(self.signature.instructions, **signature_kwargs)+ signature = dsp.Template(signature.instructions, **new_signature)return super().forward(signature=signature, **kwargs)diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex 930da5868..5285f09fa 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -61,6 +61,7 @@ class Predict(Parameter):def forward(self, **kwargs):# Extract the three privileged keyword arguments.+ new_signature = kwargs.pop("new_signature", None)signature = kwargs.pop("signature", self.signature)demos = kwargs.pop("demos", self.demos)config = dict(**self.config, **kwargs.pop("config", {}))@@ -84,6 +85,9 @@ class Predict(Parameter):x = dsp.Example(demos=demos, **kwargs)+ if new_signature is not None:+ signature = dsp.Template(signature.instructions, **new_signature)+if self.lm is None:x, C = dsp.generate(signature, **config)(x, stage=self.stage)else:diff --git a/dspy/predict/retry.py b/dspy/predict/retry.pyindex a9b652c05..b8f06633b 100644--- a/dspy/predict/retry.py+++ b/dspy/predict/retry.py@@ -44,7 +44,7 @@ class Retry(Predict):if past_key in self.new_signature:kwargs[past_key] = valuedel kwargs["past_outputs"]- kwargs["signature"] = self.new_signature+ kwargs["new_signature"] = self.new_signaturereturn self.original_forward(**kwargs)def __call__(self, **kwargs):