Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Add assert_transform to teleprompter

Shangyint91e1764b5efa
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.py
index 9f25a67a3..bd6cdde9d 100644
--- a/dspy/primitives/assertions.py
+++ b/dspy/primitives/assertions.py
@@ -1,3 +1,4 @@
+import inspect
from typing import Any, Callable
import dsp
import dspy
@@ -167,3 +168,31 @@ def assert_backtrack_policy(max_backtracks=2):
return inner
return wrapper
+
+
+def handle_assert(self, *args, **kwargs):
+
+ args_to_vals = inspect.getcallargs(self._forward, *args, **kwargs)
+
+ # if user has specified a bypass_assert flag, set it
+ if "bypass_assert" in args_to_vals:
+ dspy.settings.configure(bypass_assert=args_to_vals["bypass_assert"])
+
+ wrapped_forward = assert_backtrack_policy()(self._forward)
+ return wrapped_forward(*args, **kwargs)
+
+
+def assert_transform_module(module):
+ """
+ Transform a module to handle assertions.
+ TODO: arguments to specify the actual assertion handler
+ """
+ if not getattr(module, "forward", False):
+ raise ValueError("Module must have a forward method to have assertions handled.")
+ if getattr(module, "_forward", False):
+ pass # TODO warning: might be overwriting a previous _forward method
+
+ module._forward = module.forward
+ module.forward = handle_assert.__get__(module)
+
+ return module
\ No newline at end of file
diff --git a/dspy/primitives/program.py b/dspy/primitives/program.py
index 024eef929..9797a95a5 100644
--- a/dspy/primitives/program.py
+++ b/dspy/primitives/program.py
@@ -24,19 +24,7 @@ class Module(BaseModule, metaclass=ProgramMeta):
self._compiled = False
def __call__(self, *args, **kwargs):
- if getattr(self, "forward", False) and not getattr(
- self.forward, "_decorated", False
- ):
- args_to_vals = inspect.getcallargs(self.forward, *args, **kwargs)
-
- # if user has specified a bypass_assert flag, set it
- if "bypass_assert" in args_to_vals:
- dspy.settings.configure(bypass_assert=args_to_vals["bypass_assert"])
-
- wrapped_forward = assert_backtrack_policy()(self.forward)
- return wrapped_forward(*args, **kwargs)
- else:
- return self.forward(*args, **kwargs)
+ return self.forward(*args, **kwargs)
def named_predictors(self):
from dspy.predict.predict import Predict
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.py
index 57be40e9a..ed8e25899 100644
--- a/dspy/teleprompt/bootstrap.py
+++ b/dspy/teleprompt/bootstrap.py
@@ -4,6 +4,7 @@ import random
import threading
from dspy.primitives import Example
+from dspy.primitives.assertions import assert_transform_module
from .teleprompt import Teleprompter
from .vanilla import LabeledFewShot
@@ -61,6 +62,9 @@ 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)
+
if self.max_labeled_demos and self.teacher._compiled is False:
teleprompter = LabeledFewShot(k=self.max_labeled_demos)
self.teacher = teleprompter.compile(self.teacher.reset_copy(), trainset=self.trainset)