DSPy: The framework for programming—not prompting—language models
Python
remove legacy assertion code (#8546)
Chen Qianf0e5a41df2c1
diff --git a/dspy/evaluate/evaluate.py b/dspy/evaluate/evaluate.pyindex 3203b7293..3a62ed353 100644--- a/dspy/evaluate/evaluate.py+++ b/dspy/evaluate/evaluate.py@@ -51,6 +51,7 @@ class EvaluationResult(Prediction):- score: An float value (e.g., 67.30) representing the overall performance- results: a list of (example, prediction, score) tuples for each example in devset"""+def __init__(self, score: float, results: list[tuple["dspy.Example", "dspy.Example", Any]]):super().__init__(score=score, results=results)@@ -126,9 +127,9 @@ class Evaluate:Returns:The evaluation results are returned as a dspy.EvaluationResult object containing the following attributes:-+- score: A float percentage score (e.g., 67.30) representing overall performance-+- results: a list of (example, prediction, score) tuples for each example in devset"""metric = metric if metric is not None else self.metric@@ -145,11 +146,7 @@ class Evaluate:executor = ParallelExecutor(num_threads=num_threads,disable_progress_bar=not display_progress,- max_errors=(- self.max_errors- if self.max_errors is not None- else dspy.settings.max_errors- ),+ max_errors=(self.max_errors if self.max_errors is not None else dspy.settings.max_errors),provide_traceback=self.provide_traceback,compare_results=True,)@@ -157,13 +154,6 @@ class Evaluate:def process_item(example):prediction = program(**example.inputs())score = metric(example, prediction)-- # Increment assert and suggest failures to program's attributes- if hasattr(program, "_assert_failures"):- program._assert_failures += dspy.settings.get("assert_failures")- if hasattr(program, "_suggest_failures"):- program._suggest_failures += dspy.settings.get("suggest_failures")-return prediction, scoreresults = executor.execute(process_item, devset)@@ -191,7 +181,6 @@ class Evaluate:results=results,)-def _construct_result_table(self, results: list[tuple["dspy.Example", "dspy.Example", Any]], metric_name: str) -> "pd.DataFrame":diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.pyindex bc75d3efe..2b2fd8ec2 100644--- a/dspy/teleprompt/bootstrap.py+++ b/dspy/teleprompt/bootstrap.py@@ -85,10 +85,6 @@ class BootstrapFewShot(Teleprompter):self.student = self._train()self.student._compiled = True- # set assert_failures and suggest_failures as attributes of student w/ value 0- self.student._assert_failures = 0- self.student._suggest_failures = 0-return self.studentdef _prepare_student_and_teacher(self, student, teacher):@@ -111,7 +107,9 @@ class BootstrapFewShot(Teleprompter):teacher.predictors(),), "Student and teacher must have the same number of predictors."- for (name1, predictor1), (name2, predictor2) in zip(student.named_predictors(), teacher.named_predictors(), strict=False):+ for (name1, predictor1), (name2, predictor2) in zip(+ student.named_predictors(), teacher.named_predictors(), strict=False+ ):assert name1 == name2, "Student and teacher must have the same program structure."if hasattr(predictor1.signature, "equals"):assert predictor1.signature.equals(@@ -210,11 +208,7 @@ class BootstrapFewShot(Teleprompter):with self.error_lock:self.error_count += 1current_error_count = self.error_count- effective_max_errors = (- self.max_errors- if self.max_errors is not None- else dspy.settings.max_errors- )+ effective_max_errors = self.max_errors if self.max_errors is not None else dspy.settings.max_errorsif current_error_count >= effective_max_errors:raise elogger.error(f"Failed to run or to evaluate example {example} with {self.metric} due to {e}.")@@ -244,7 +238,6 @@ class BootstrapFewShot(Teleprompter):# Update the tracesfor name, demos in name2traces.items():-# If there are multiple traces for the same predictor in the sample example,# sample 50/50 from the first N-1 traces or the last trace.if len(demos) > 1:diff --git a/dspy/teleprompt/random_search.py b/dspy/teleprompt/random_search.pyindex e189b07bd..c6447e833 100644--- a/dspy/teleprompt/random_search.py+++ b/dspy/teleprompt/random_search.py@@ -58,11 +58,7 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):self.trainset = trainsetself.valset = valset or trainset # TODO: FIXME: Note this choice.- effective_max_errors = (- self.max_errors- if self.max_errors is not None- else dspy.settings.max_errors- )+ effective_max_errors = self.max_errors if self.max_errors is not None else dspy.settings.max_errorsscores = []all_subscores = []@@ -129,13 +125,6 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):all_subscores.append(subscores)- ############ Assertion-aware Optimization ############- if hasattr(program, "_suggest_failures"):- score = score - program._suggest_failures * 0.2- if hasattr(program, "_assert_failures"):- score = 0 if program._assert_failures > 0 else score- ######################################################-if len(scores) == 0 or score > max(scores):print("New best score:", score, "for seed", seed)best_program = program