DSPy: The framework for programming—not prompting—language models
Python
chore: moved print logging to dspy.logger for bootstrap and random search
KCaverly1088ea05c62a
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.pyindex de10a74a3..26d625d22 100644--- a/dspy/teleprompt/bootstrap.py+++ b/dspy/teleprompt/bootstrap.py@@ -124,7 +124,9 @@ class BootstrapFewShot(Teleprompter):if success:bootstrapped[example_idx] = True- print(f"Bootstrapped {len(bootstrapped)} full traces after {example_idx+1} examples in round {round_idx}.")+ dspy.logger.info(+ f"Bootstrapped {len(bootstrapped)} full traces after {example_idx + 1} examples in round {round_idx}."+ )# Unbootstrapped training examples@@ -167,7 +169,6 @@ class BootstrapFewShot(Teleprompter):success = metric_valelse:success = True- # print(success, example, prediction)except Exception as e:success = Falsewith self.error_lock:@@ -175,7 +176,7 @@ class BootstrapFewShot(Teleprompter):current_error_count = self.error_countif current_error_count >= self.max_errors:raise e- print(f"Failed to run or to evaluate example {example} with {self.metric} due to {e}.")+ dspy.logger.error(f"Failed to run or to evaluate example {example} with {self.metric} due to {e}.")if success:for step in trace:@@ -192,15 +193,15 @@ class BootstrapFewShot(Teleprompter):except KeyError as e:continue # FIXME: !- # TODO: Look closer into this. It's a bit tricky to reproduce.- print(f"Failed to find predictor {predictor} in {self.predictor2name}.")- print(- "Are you doing this in a notebook (Jupyter)? This might be caused by redefining values by rerunning cells.",- )- print("Try restarting the notebook, or open an issue.")- raise KeyError(- f"Failed to find predictor {id(predictor)} {predictor} in {self.predictor2name}.",- ) from e+ # # TODO: Look closer into this. It's a bit tricky to reproduce.+ # print(f"Failed to find predictor {predictor} in {self.predictor2name}.")+ # print(+ # "Are you doing this in a notebook (Jupyter)? This might be caused by redefining values by rerunning cells.",+ # )+ # print("Try restarting the notebook, or open an issue.")+ # raise KeyError(+ # f"Failed to find predictor {id(predictor)} {predictor} in {self.predictor2name}.",+ # ) from ename2traces[predictor_name].append(demo)diff --git a/dspy/teleprompt/random_search.py b/dspy/teleprompt/random_search.pyindex 7958bc471..bbeaa90af 100644--- a/dspy/teleprompt/random_search.py+++ b/dspy/teleprompt/random_search.py@@ -1,5 +1,6 @@import random+import dspyfrom dspy.evaluate.evaluate import Evaluatefrom dspy.teleprompt.teleprompt import Teleprompter@@ -54,9 +55,10 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):# self.max_bootstrapped_demos = self.max_num_tracesself.max_labeled_demos = max_labeled_demos- print("Going to sample between", self.min_num_samples, "and", self.max_num_samples, "traces per predictor.")- # print("Going to sample", self.max_num_traces, "traces in total.")- print("Will attempt to train", self.num_candidate_sets, "candidate sets.")+ dspy.logger.info(+ "Going to sample between", self.min_num_samples, "and", self.max_num_samples, "traces per predictor."+ )+ dspy.logger.info("Will attempt to train", self.num_candidate_sets, "candidate sets.")def compile(self, student, *, teacher=None, trainset, valset=None, restrict=None, labeled_sample=True):self.trainset = trainset@@ -68,7 +70,6 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):for seed in range(-3, self.num_candidate_sets):if (restrict is not None) and (seed not in restrict):- print(seed, restrict)continuetrainset2 = list(self.trainset)@@ -131,16 +132,16 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):score = 0 if program2._assert_failures > 0 else score######################################################- print("Score:", score, "for set:", [len(predictor.demos) for predictor in program2.predictors()])+ dspy.logger.info("Score:", score, "for set:", [len(predictor.demos) for predictor in program2.predictors()])if len(scores) == 0 or score > max(scores):- print("New best score:", score, "for seed", seed)+ dspy.logger.info("New best sscore:", score, "for seed", seed)best_program = program2scores.append(score)- print(f"Scores so far: {scores}")+ dspy.logger.info(f"Scores so far: {scores}")- print("Best score:", max(scores))+ dspy.logger.info("Best score:", max(scores))score_data.append((score, subscores, seed, program2))@@ -152,17 +153,17 @@ class BootstrapFewShotWithRandomSearch(Teleprompter):transposed_subscores = zip(*[subscores for _, subscores, *_ in top_3_scores if subscores])avg_of_max_per_entry = sum(max(entry) for entry in transposed_subscores) / len(top_3_scores[0][1])- print(f"Average of max per entry across top {k} scores: {avg_of_max_per_entry}")+ dspy.logger.info(f"Average of max per entry across top {k} scores: {avg_of_max_per_entry}")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}")+ dspy.logger.info(f"Stopping early because score {score} is >= stop_at_score {self.stop_at_score}")break# To best program, attach all program candidates in decreasing average scorebest_program.candidate_programs = score_databest_program.candidate_programs = sorted(best_program.candidate_programs, key=lambda x: x[0], reverse=True)- print(len(best_program.candidate_programs), "candidate programs found.")+ dspy.logger.info(f"{len(best_program.candidate_programs)} candidate programs found.")return best_program