DSPy: The framework for programming—not prompting—language models
Python
added stop_at_score kwarg to random_search
Arnav Singhvi0df45c709a61
diff --git a/dspy/teleprompt/random_search.py b/dspy/teleprompt/random_search.pyindex be04411d6..9aec12612 100644--- a/dspy/teleprompt/random_search.py+++ b/dspy/teleprompt/random_search.py@@ -28,13 +28,13 @@ from dspy.evaluate.evaluate import Evaluateclass BootstrapFewShotWithRandomSearch(Teleprompter):- def __init__(self, metric, teacher_settings={}, max_bootstrapped_demos=4, max_labeled_demos=16, max_rounds=1, num_candidate_programs=16, num_threads=6):+ def __init__(self, metric, teacher_settings={}, max_bootstrapped_demos=4, max_labeled_demos=16, max_rounds=1, num_candidate_programs=16, num_threads=6, stop_at_score=None):self.metric = metricself.teacher_settings = teacher_settingsself.max_rounds = max_roundsself.num_threads = num_threads-+ self.stop_at_score = stop_at_scoreself.min_num_samples = 1self.max_num_samples = max_bootstrapped_demosself.num_candidate_sets = num_candidate_programs@@ -100,6 +100,10 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):print(f"Scores so far: {scores}")+ if self.stop_at_score is not None and score >= self.stop_at_score:+ print(f"Stopping early because score {score} is >= stop_at_score {self.stop_at_score}")+ break+if score >= max(scores):print('New best score:', score, 'for seed', seed)best_program = program2