DSPy: The framework for programming—not prompting—language models
Python
each decorator is a backtrack policy; add simple/effective backtrack to latest predictor policy--uses prefix caching
manishshettym41b2f78f4fb5
diff --git a/assert_example.py b/assert_example.pyindex 43dc51750..8312df2f9 100644--- a/assert_example.py+++ b/assert_example.py@@ -71,7 +71,8 @@ class SimplifiedBaleen(dspy.Module):query = self.generate_query[hop](context=context, question=question).query# assertion- dspy.Assert(lambda x: len(x) <= 1, query)+ print(f"Query length @ {id(self.generate_query[hop])} w/ config {self.generate_query[hop].get_config()}: {len(query)}")+ dspy.Assert(lambda x: len(x) <= 50, query)passages = self.retrieve(query).passagescontext = deduplicate(context + passages)diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.pyindex 3992c1c55..93b27c05b 100644--- a/dspy/primitives/assertions.py+++ b/dspy/primitives/assertions.py@@ -42,8 +42,11 @@ class Assert:# class RuntimeAssert(Assert): ...+############################# ASSERTION AND BACKTRACKING POLICIES #############################+def assert_transform(backtrack=2):- """Decorator for transforming a function into a rewindable function."""+ """Decorator that simply re-runs the function if assertion fails,+ up to [backtrack] times."""def wrapper(func):def inner(*args, **kwargs):@@ -62,3 +65,80 @@ def assert_transform(backtrack=2):return innerreturn wrapper+++def assert_update_transform(backtrack=2):+ """Decorator that simply re-runs the function with updated temperature+ if assertion fails, up to [backtrack] times."""++ def wrapper(func):+ def inner(*args, **kwargs):+ for i in range(backtrack):+ lm = dsp.settings.lm++ if i > 0 and backtrack_to is not None:+ print(f"updating temperature to {0.7 + 0.001 * i}")+ lm = lm.copy(temperature=0.7 + 0.001 * i)++ new_settings = dict(lm=lm) if i > 0 else {}++ try:+ with dsp.settings.context(**new_settings):+ return func(*args, **kwargs)+ except DSPyAssertionError as e:+ print(f"{e.msg}")++ if dsp.settings.trace:+ backtrack_to = id(dsp.settings.trace[-1][0])+ else:+ print(+ "UNREACHABLE: No trace available, this should not happen. Is this run time?"+ )++ return inner++ return wrapper+++def assert_latest_transform(backtrack=3):+ """Decorator that simply re-runs the function but updates the temperature+ only for the latest predictor in ther trace if assertion fails, up to [backtrack] times.++ # NOTE (@manish): the previous assert_update_transform applies temperature change to all predictors in the pipeline.+ # Here, we udpate/pass the new temperature to the *backtrack_to* predictor *only* --> cause a cache miss --> re-run+ # For other predictors, settings remain the same --> cache hit --> no re-run+ """++ def wrapper(func):+ def inner(*args, **kwargs):+ backtrack_to = None++ for i in range(backtrack):++ if i > 0 and backtrack_to is not None:+ print(f"rewinding to {id(backtrack_to)}")++ # udpate temperature via "config" attribute of the backtrack_to predictor++ print(f"prev temp @ {id(backtrack_to)}: {backtrack_to.get_config().get('temperature', 'default')}")+ backtrack_to.update_config(temperature=0.7 + 0.001 * i)+ print(f"new temp @ {id(backtrack_to)}: {backtrack_to.get_config()['temperature']}")++ # @manish: works quite well w/ current example!!!+ # TODO (@manish): check if this makes sense w/ more examples++ try:+ return func(*args, **kwargs)+ except DSPyAssertionError as e:+ print(f"{e.msg}")++ if dsp.settings.trace:+ backtrack_to = dsp.settings.trace[-1][0]+ else:+ print(+ "UNREACHABLE: No trace available, this should not happen. Is this run time?"+ )++ return inner++ return wrapper\ No newline at end of filediff --git a/dspy/primitives/program.py b/dspy/primitives/program.pyindex 886555858..0844f60fa 100644--- a/dspy/primitives/program.py+++ b/dspy/primitives/program.py@@ -1,7 +1,7 @@import copyfrom dspy.primitives.module import BaseModule-from dspy.primitives.assertions import assert_transform+from dspy.primitives.assertions import assert_transform, assert_update_transform, assert_latest_transformclass ProgramMeta(type):@@ -26,7 +26,7 @@ class Module(BaseModule, metaclass=ProgramMeta):def __call__(self, *args, **kwargs):if getattr(self, "forward", False) and not getattr(self.forward, "_decorated", False):- wrapped_forward = assert_transform()(self.forward)+ wrapped_forward = assert_latest_transform()(self.forward)return wrapped_forward(*args, **kwargs)else:return self.forward(*args, **kwargs)