DSPy: The framework for programming—not prompting—language models
Python
Minor fix for assertions - remove break
Shangyintdddf461dadf2
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.pyindex a6a5fee16..ab669e96d 100644--- a/dspy/primitives/assertions.py+++ b/dspy/primitives/assertions.py@@ -44,8 +44,9 @@ class Assert:############################# ASSERTION AND BACKTRACKING POLICIES #############################+def assert_transform(backtrack=2):- """Decorator that simply re-runs the function if assertion fails,+ """Decorator that simply re-runs the function if assertion fails,up to [backtrack] times."""def wrapper(func):@@ -61,8 +62,6 @@ def assert_transform(backtrack=2):print("UNREACHABLE: No trace available, this should not happen. Is this run time?")- else:- breakreturn inner@@ -70,18 +69,18 @@ def assert_transform(backtrack=2):def assert_update_transform(backtrack=2):- """Decorator that simply re-runs the function with updated temperature+ """Decorator that simply re-runs the function with updated temperatureif 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)-+ print(f"updating temperature to {0.71 + 0.002 * i}")+ lm = lm.copy(temperature=0.71 + 0.002 * i)+new_settings = dict(lm=lm) if i > 0 else {}try:@@ -89,15 +88,13 @@ def assert_update_transform(backtrack=2):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?")- else:- breakreturn inner@@ -107,7 +104,7 @@ def assert_update_transform(backtrack=2):def assert_latest_transform(backtrack=3):"""Decorator that simply re-runs the function but updates the temperatureonly 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@@ -118,16 +115,19 @@ def assert_latest_transform(backtrack=3):backtrack_to = Nonefor 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')}")+ 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']}")-+ 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@@ -135,7 +135,7 @@ def assert_latest_transform(backtrack=3):return func(*args, **kwargs)except DSPyAssertionError as e:print(f"{e.msg}")-+if dsp.settings.trace:backtrack_to = dsp.settings.trace[-1][0]else:@@ -145,4 +145,4 @@ def assert_latest_transform(backtrack=3):return inner- return wrapper\ No newline at end of file+ return wrapper