DSPy: The framework for programming—not prompting—language models
Python
added proper error handling within evaluate and bootstrap
Arnav Singhvi860ad9116b69
diff --git a/dspy/evaluate/evaluate.py b/dspy/evaluate/evaluate.pyindex 688e0ea30..ee9a8b2ac 100644--- a/dspy/evaluate/evaluate.py+++ b/dspy/evaluate/evaluate.py@@ -28,6 +28,8 @@ class Evaluate:self.display_table = display_tableself.display = displayself.max_errors = max_errors+ self.error_count = 0+ self.error_lock = threading.Lock()def _execute_single_thread(self, wrapped_program, devset, display_progress):ncorrect = 0@@ -95,6 +97,11 @@ class Evaluate:score = metric(example, prediction) # FIXME: TODO: What's the right order? Maybe force name-based kwargs!return example_idx, example, prediction, scoreexcept Exception as e:+ with self.error_lock:+ self.error_count += 1+ current_error_count = self.error_count+ if current_error_count >= self.max_errors:+ raise eprint(f"Error for example in dev set: \t\t {e}")return example_idx, example, dict(), 0.0finally:diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.pyindex 14588592d..57be40e9a 100644--- a/dspy/teleprompt/bootstrap.py+++ b/dspy/teleprompt/bootstrap.py@@ -1,6 +1,7 @@import dspimport tqdmimport random+import threadingfrom dspy.primitives import Example@@ -30,13 +31,16 @@ from dspy.evaluate.evaluate import Evaluateclass BootstrapFewShot(Teleprompter):- def __init__(self, metric=None, teacher_settings={}, max_bootstrapped_demos=4, max_labeled_demos=16, max_rounds=1):+ def __init__(self, metric=None, teacher_settings={}, max_bootstrapped_demos=4, max_labeled_demos=16, max_rounds=1, max_errors=5):self.metric = metricself.teacher_settings = teacher_settingsself.max_bootstrapped_demos = max_bootstrapped_demosself.max_labeled_demos = max_labeled_demosself.max_rounds = max_rounds+ self.max_errors= max_errors+ self.error_count = 0+ self.error_lock = threading.Lock()def compile(self, student, *, teacher=None, trainset, valset=None):self.trainset = trainset@@ -135,7 +139,11 @@ class BootstrapFewShot(Teleprompter):# print(success, example, prediction)except Exception as e:success = False- # FIXME: remove the reliance on uuid here so the error is printed+ with self.error_lock:+ self.error_count += 1+ current_error_count = self.error_count+ if current_error_count >= self.max_errors:+ raise eprint(f'Failed to run or to evaluate example {example} with {self.metric} due to {e}.')if success: