Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

[running] transfered signature updates to a new module: dspy.Retry

manishshettymd2a3381acc2a
diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.py
index 52d3ffa82..83bb74f3a 100644
--- a/dspy/predict/chain_of_thought.py
+++ b/dspy/predict/chain_of_thought.py
@@ -3,7 +3,6 @@ import dsp
from .predict import Predict
-
# TODO: FIXME: Insert this right before the *first* output field. Also rewrite this to use the new signature system.
# TODO: This shouldn't inherit from Predict. It should be a module that has one or two predictors.
@@ -27,6 +26,7 @@ class ChainOfThought(dspy.Module):
# How this should look like. But with also passing signature=simpler_signature to the predict module *if* deactivated.
"""
+
class ChainOfThought(Predict):
def __init__(self, signature, rationale_type=None, activated=True, **config):
super().__init__(signature, **config)
@@ -36,20 +36,28 @@ class ChainOfThought(Predict):
signature = self.signature
*keys, last_key = signature.kwargs.keys()
- DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
- desc="${produce the " + last_key + "}. We ...")
+ DEFAULT_RATIONALE_TYPE = dsp.Type(
+ prefix="Reasoning: Let's think step by step in order to",
+ desc="${produce the " + last_key + "}. We ...",
+ )
rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE
-
+
extended_kwargs = {key: signature.kwargs[key] for key in keys}
- extended_kwargs.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})
-
- self.extended_signature = dsp.Template(signature.instructions, **extended_kwargs)
-
+ extended_kwargs.update(
+ {"rationale": rationale_type, last_key: signature.kwargs[last_key]}
+ )
+
+ self.extended_signature = dsp.Template(
+ signature.instructions, **extended_kwargs
+ )
+
def forward(self, **kwargs):
- signature_kwargs = kwargs.pop('signature', None)
+ signature_kwargs = kwargs.pop("signature", None)
if signature_kwargs is None:
- if self.activated is True or (self.activated is None and isinstance(dsp.settings.lm, dsp.GPT3)):
+ if self.activated is True or (
+ self.activated is None and isinstance(dsp.settings.lm, dsp.GPT3)
+ ):
signature = self.extended_signature
else:
signature = self.signature
diff --git a/dspy/predict/retry.py b/dspy/predict/retry.py
index a4ffaa46b..8a6ee49d2 100644
--- a/dspy/predict/retry.py
+++ b/dspy/predict/retry.py
@@ -9,6 +9,7 @@ class Retry(Predict):
super().__init__(module.signature)
self.module = module
self.original_signature = module.signature.signature
+ self.original_forward = module.forward
self.new_signature = self._create_new_signature(self.original_signature)
def _create_new_signature(self, original_signature):
@@ -16,24 +17,32 @@ class Retry(Predict):
input_fields = original_signature.input_fields()
output_fields = original_signature.output_fields()
modified_output_fields = {}
- for key, value in output_fields.items():
- modified_output_fields[f"past_{key}"] = dspy.OutputField(
- prefix="Past " + value.prefix, desc=value.desc, format=value.format
- )
+
+ # @manish: TODO: verify this and remove past_outputs from traces
+ # for key, value in output_fields.items():
+ # modified_output_fields[f"past_{key}"] = dspy.OutputField(
+ # prefix="Past " + value.prefix, desc=value.desc, format=value.format
+ # )
+
extended_signature.update(input_fields)
- extended_signature.update(modified_output_fields)
+ # extended_signature.update(modified_output_fields)
+
+ extended_signature["feedback"] = dspy.InputField(
+ prefix="Instructions:",
+ desc="Some instructions you must satisfy",
+ format=str,
+ )
+
extended_signature["traces"] = dspy.InputField(
- prefix="Trace:",
- desc="Traces from your past attempts",
+ prefix="Traces:",
+ desc="traces of failures from your past attempts",
format=dsp.passages2text,
)
- extended_signature["feedback"] = dspy.InputField(
- prefix="Instruction:", desc="Some instructions you must satisfy", format=str
- )
extended_signature.update(output_fields)
+
return extended_signature
def forward(self, *args, **kwargs):
kwargs["signature"] = self.new_signature
- return self.module.forward(**kwargs)
+ return self.original_forward(**kwargs)
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.py
index 0c741bfbd..87c8bb845 100644
--- a/dspy/primitives/assertions.py
+++ b/dspy/primitives/assertions.py
@@ -73,6 +73,37 @@ 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(*args, **kwargs):
+ for arg, value in default_args.items():
+ kwargs.setdefault(arg, value)
+
+ return original_forward(**kwargs)
+
+ predictor.forward = new_forward
+
+
+def _pprintdict(d):
+ """Pretty print a dictionary"""
+ import json
+ from termcolor import colored
+
+ formatted_dict = json.dumps(d, indent=4)
+ colored_lines = [
+ colored(line, "blue")
+ if ":" not in line
+ else ": ".join(
+ colored(part.strip(), "blue" if i == 0 else "green")
+ for i, part in enumerate(line.split(":", 1))
+ )
+ for line in formatted_dict.splitlines()
+ ]
+ return "\n".join(colored_lines) + "\n"
+
+
#################### Assertion Exceptions ####################
@@ -213,40 +244,40 @@ def suggest_backtrack_handler(func, max_backtracks=10, **handler_args):
from ..predict.retry import Retry
def wrapper(*args, **kwargs):
- nonlocal func
target_module = handler_args.get("target_module", None)
- backtrack_to = None
- error_msg = None
- result = None
+ backtrack_to, error_msg, result = None, None, None
predictors_to_original_forward = {}
- # predictor_feedback: Predictor -> List[feedback_msg]
- predictor_feedbacks = {}
-
- # failure_traces: Predictor -> Dict[assertion, (ip, op, msg)]
- failure_traces = {}
+ predictor_feedbacks = {} # Predictor -> List[feedback_msg]
+ failure_traces = {} # Predictor -> Dict[assertion, (ip, op, msg)]
for i in range(max_backtracks + 1):
if i > 0 and backtrack_to is not None:
# create a new retry module that wraps backtrack_to
retry_module = Retry(backtrack_to)
- # save the original forward function to revert back to
+ # save original forward function to revert back to
predictors_to_original_forward[backtrack_to] = backtrack_to.forward
- # set the new fields
- # TODO some handling of the past_outputs fields within Retry module?
+ # generate values for 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
+ # TODO separate past_outputs as a field from traces
+
+ # set values as default for the new fields
+ _wrap_forward_with_set_fields(
+ retry_module,
+ default_args={
+ "feedback": feedback_msg,
+ "traces": trace_passages,
+ },
+ )
- # TODO: verify if this correctly - replace backtrack_to.forward in the user's program with retry_module.forward
- func = retry_module.forward.__get__(
- backtrack_to, backtrack_to.__class__
+ # point backtrack_to.forward to retry_module.forward
+ # TODO: verify and make sure is thread safe
+ backtrack_to.forward = retry_module.forward.__get__(
+ retry_module, retry_module.__class__
)
- # setattr(backtrack_to, 'forward', func)
- # NOTE: this replacement needs to be thread safe if possible
# if last backtrack: ignore suggestion errors
if i == max_backtracks: