Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

fix teacher: assertion will not fail

Shangyint475fe5589320
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.py
index b07af0b46..42f2f306b 100644
--- a/dspy/primitives/assertions.py
+++ b/dspy/primitives/assertions.py
@@ -114,11 +114,25 @@ def bypass_suggest_handler(func):
return wrapper
-def assert_backtrack_handler(func, max_backtracks=2):
- """Handler for backtracking assertions.
+def bypass_assert_handler(func):
+ """Handler to bypass suggest only.
+
+ If a suggestion fails, it will be logged but not raised.
+ And If an assertion fails, it will be raised.
+ """
+
+ def wrapper(*args, **kwargs):
+ with dspy.settings.context(bypass_assert=True):
+ return func(*args, **kwargs)
+
+ return wrapper
+
+
+def suggest_backtrack_handler(func, max_backtracks=2):
+ """Handler for backtracking suggestion.
Re-run the latest predictor up to `max_backtracks` times,
- with updated signature if assertion fails. updated signature adds a new
+ with updated signature if a suggestion fails. updated signature adds a new
input field to the signature, which is the feedback.
"""
@@ -251,7 +265,7 @@ def handle_assert_forward(assertion_handler):
return forward
-default_assertion_handler = assert_backtrack_handler
+default_assertion_handler = suggest_backtrack_handler
def assert_transform_module(module, assertion_handler=default_assertion_handler):
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.py
index 441a16a5c..c908b408c 100644
--- a/dspy/teleprompt/bootstrap.py
+++ b/dspy/teleprompt/bootstrap.py
@@ -4,7 +4,7 @@ import random
import threading
from dspy.primitives import Example
-from dspy.primitives.assertions import assert_transform_module
+from dspy.primitives.assertions import assert_transform_module, bypass_assert_handler, suggest_backtrack_handler
from .teleprompt import Teleprompter
from .vanilla import LabeledFewShot
@@ -63,7 +63,8 @@ class BootstrapFewShot(Teleprompter):
assert self.student._compiled is False, "Student must be uncompiled."
self.student = assert_transform_module(self.student)
- self.teacher = assert_transform_module(self.teacher)
+ self.teacher = assert_transform_module(self.teacher,
+ assertion_handler=lambda fun: bypass_assert_handler(suggest_backtrack_handler(fun)))
if self.max_labeled_demos and self.teacher._compiled is False:
teleprompter = LabeledFewShot(k=self.max_labeled_demos)