Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fixing commits + adding FIXMEs for a few the new retry module

manishshettym759d62ae604e
diff --git a/dspy/predict/__init__.py b/dspy/predict/__init__.py
index 2dec22fe5..f646ab69f 100644
--- a/dspy/predict/__init__.py
+++ b/dspy/predict/__init__.py
@@ -3,5 +3,6 @@ from .chain_of_thought import ChainOfThought
from .multi_chain_comparison import MultiChainComparison
from .chain_of_thought_with_hint import ChainOfThoughtWithHint
from .react import ReAct
-from .aggregation import majority
-from .program_of_thought import ProgramOfThought
\ No newline at end of file
+from .aggregation import majority
+from .program_of_thought import ProgramOfThought
+from .retry import Retry
diff --git a/dspy/predict/retry.py b/dspy/predict/retry.py
new file mode 100644
index 000000000..675313e43
--- /dev/null
+++ b/dspy/predict/retry.py
@@ -0,0 +1,37 @@
+import dspy
+import dsp
+
+from .predict import Predict
+
+
+class Retry(Predict):
+ def __init__(self, module):
+ super().__init__(module.signature)
+ self.module = module
+ self.original_signature = module.signature.signature
+ self.new_signature = self._create_new_signature(self.original_signature)
+
+ def _create_new_signature(self, original_signature):
+ extended_signature = {}
+
+ # FIXME: AttributeError: '_SignatureNamespace' object
+ # has no attribute 'items'.
+ # FIXME: the order of the input fields might be incorrect
+ # the new fields should be after the old input fields,
+ # but before the output fields.
+ for key, value in original_signature.items():
+ extended_signature[key] = value
+
+ extended_signature["traces"] = dspy.InputField(
+ prefix="Trace:",
+ desc="Traces from your past attempts",
+ format=dsp.passages2text,
+ )
+
+ extended_signature["feedback"] = dspy.InputField(
+ prefix="Instruction:", desc="Some instructions you must satisfy", format=str
+ )
+ return extended_signature
+
+ def forward(self, **kwargs):
+ return self.module.forward(signature=self.new_signature, **kwargs)
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.py
index 0407349fe..7824c0d4b 100644
--- a/dspy/primitives/assertions.py
+++ b/dspy/primitives/assertions.py
@@ -74,19 +74,6 @@ def _revert_predictor_signature(predictor, *args):
predictor.extended_signature = new_signature
-def _wrap_forward_with_set_fields(predictor, default_args):
- """Wrap the forward method of a predictor instance to enforce default arguments."""
- original_forward = predictor.forward
-
- def new_forward(**kwargs):
- for arg, value in default_args.items():
- kwargs.setdefault(arg, value)
-
- return original_forward(**kwargs)
-
- predictor.forward = new_forward
-
-
#################### Assertion Exceptions ####################
@@ -248,41 +235,21 @@ def suggest_backtrack_handler(func, max_backtracks=10, **handler_args):
for i in range(max_backtracks + 1):
if i > 0 and backtrack_to is not None:
- # create a new traces field
- traces_field = dspy.InputField(
- prefix="Counter Examples:",
- desc="Traces of some incorrect outputs that violate instructions",
- format=dsp.passages2text,
- )
-
- # create a new feedback field
- feedback_field = dspy.InputField(
- prefix="Instruction:",
- desc="Some instructions you must satisfy",
- format=str,
- )
+ # create a new retry module that wraps backtrack_to
+ retry_module = dspy.Retry(backtrack_to)
# save the original forward function to revert back to
predictors_to_original_forward[backtrack_to] = backtrack_to.forward
- # extend signature with new fields
- _extend_predictor_signature(
- backtrack_to,
- _assert_feedback=feedback_field,
- _assert_traces=traces_field,
- )
-
# set the new fields
feedback_msg = _build_error_msg(predictor_feedbacks[backtrack_to])
trace_passages = _build_trace_passages(failure_traces[backtrack_to])
+ kwargs["feedback"] = feedback_msg
+ kwargs["traces"] = trace_passages
- _wrap_forward_with_set_fields(
- backtrack_to,
- default_args={
- "_assert_feedback": feedback_msg,
- "_assert_traces": trace_passages,
- },
- )
+ # FIXME: need to replace backtrack_to.forward
+ # in the user's program with retry_module.forward
+ # NOTE: this replacement needs to be thread safe if possible
# if last backtrack: ignore suggestion errors
if i == max_backtracks: