Skip to content

Commit

Permalink
Easier api for transforming assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shangyint committed Feb 8, 2024
1 parent 879e956 commit 776d734
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions dspy/primitives/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ def wrapper(*args, **kwargs):


def backtrack_handler(func, bypass_suggest=True, max_backtracks=2):
"""Handler for backtracking suggestion.
"""Handler for backtracking suggestion and assertion.
Re-run the latest predictor up to `max_backtracks` times,
with updated signature if a suggestion fails. updated signature adds a new
with updated signature if an assertion fails. updated signature adds a new
input field to the signature, which is the feedback.
"""

Expand Down Expand Up @@ -336,6 +336,13 @@ def assert_transform_module(
module
)

if all(map(lambda p: isinstance(p, dspy.retry.Retry), module.named_predictors())):
pass # we already applied the Retry mapping outside
elif all(map(lambda p: not isinstance(p, dspy.retry.Retry), module.named_predictors())):
module.map_named_predictors(dspy.retry.Retry)
else:
raise RuntimeError("Module has mixed predictors, can't apply Retry mapping.")

setattr(module, "_assert_transformed", True)

return module

0 comments on commit 776d734

Please sign in to comment.