DSPy: The framework for programming—not prompting—language models
Python
fix E701 rule (#7870)
TomuHirata293c802880fa
diff --git a/dspy/propose/dataset_summary_generator.py b/dspy/propose/dataset_summary_generator.pyindex 7a4c03a96..77839a87d 100644--- a/dspy/propose/dataset_summary_generator.py+++ b/dspy/propose/dataset_summary_generator.py@@ -45,7 +45,8 @@ def order_input_keys_in_string(unordered_repr):return ordered_reprdef create_dataset_summary(trainset, view_data_batch_size, prompt_model, log_file=None, verbose=False):- if verbose: print("\nBootstrapping dataset summary (this will be used to generate instructions)...")+ if verbose:+ print("\nBootstrapping dataset summary (this will be used to generate instructions)...")upper_lim = min(len(trainset), view_data_batch_size)prompt_model = prompt_model if prompt_model else dspy.settings.lmwith dspy.settings.context(lm=prompt_model):@@ -63,7 +64,8 @@ def create_dataset_summary(trainset, view_data_batch_size, prompt_model, log_filcalls+=1if calls >= max_calls:break- if verbose: print(f"b: {b}")+ if verbose:+ print(f"b: {b}")upper_lim = min(len(trainset), b+view_data_batch_size)with dspy.settings.context(lm=prompt_model):output = dspy.Predict(DatasetDescriptorWithPriorObservations, n=1, temperature=1.0)(prior_observations=observations, examples=order_input_keys_in_string(trainset[b:upper_lim].__repr__()))@@ -77,17 +79,20 @@ def create_dataset_summary(trainset, view_data_batch_size, prompt_model, log_filif log_file:log_file.write(f"observations {observations}\n")except Exception as e:- if verbose: print(f"e {e}. using observations from past round for a summary.")+ if verbose:+ print(f"e {e}. using observations from past round for a summary.")if prompt_model:with dspy.settings.context(lm=prompt_model):summary = dspy.Predict(ObservationSummarizer, n=1, temperature=1.0)(observations=observations)else:summary = dspy.Predict(ObservationSummarizer, n=1, temperature=1.0)(observations=observations)- if verbose: print(f"summary: {summary}")+ if verbose:+ print(f"summary: {summary}")if log_file:log_file.write(f"summary: {summary}\n")- if verbose: print(f"\nGenerated summary: {strip_prefix(summary.summary)}\n")+ if verbose:+ print(f"\nGenerated summary: {strip_prefix(summary.summary)}\n")return strip_prefix(summary.summary)diff --git a/dspy/propose/grounded_proposer.py b/dspy/propose/grounded_proposer.pyindex f618552b1..3f2914304 100644--- a/dspy/propose/grounded_proposer.py+++ b/dspy/propose/grounded_proposer.py@@ -213,7 +213,8 @@ class GenerateModuleInstruction(dspy.Module):program_code=self.program_code_string, program_example=task_demos,).program_description,)- if self.verbose: print(f"PROGRAM DESCRIPTION: {program_description}")+ if self.verbose:+ print(f"PROGRAM DESCRIPTION: {program_description}")inputs = []outputs = []@@ -237,11 +238,13 @@ class GenerateModuleInstruction(dspy.Module):max_depth=10,).module_descriptionexcept:- if self.verbose: print("Error getting program description. Running without program aware proposer.")+ if self.verbose:+ print("Error getting program description. Running without program aware proposer.")self.program_aware = False# Generate an instruction for our chosen module- if self.verbose: print(f"task_demos {task_demos}")+ if self.verbose:+ print(f"task_demos {task_demos}")instruct = self.generate_module_instruction(dataset_description=data_summary,@@ -297,7 +300,8 @@ class GroundedProposer(Proposer):if self.program_aware:try:self.program_code_string = get_dspy_source_code(program)- if self.verbose: print("SOURCE CODE:",self.program_code_string)+ if self.verbose:+ print("SOURCE CODE:",self.program_code_string)except Exception as e:print(f"Error getting source code: {e}.\n\nRunning without program aware proposer.")self.program_aware = False@@ -308,7 +312,8 @@ class GroundedProposer(Proposer):self.data_summary = create_dataset_summary(trainset=trainset, view_data_batch_size=view_data_batch_size, prompt_model=prompt_model,)- if self.verbose: print(f"DATA SUMMARY: {self.data_summary}")+ if self.verbose:+ print(f"DATA SUMMARY: {self.data_summary}")except Exception as e:print(f"Error getting data summary: {e}.\n\nRunning without data aware proposer.")self.use_dataset_summary = False@@ -332,10 +337,12 @@ class GroundedProposer(Proposer):# Randomly select whether or not we're using instruction historyuse_history = self.rng.random() < 0.5self.use_instruct_history = use_history- if self.verbose: print(f"Use history T/F: {self.use_instruct_history}")+ if self.verbose:+ print(f"Use history T/F: {self.use_instruct_history}")if not demo_candidates:- if self.verbose: print("No demo candidates provided. Running without task demos.")+ if self.verbose:+ print("No demo candidates provided. Running without task demos.")self.use_task_demos = False# Create an instruction for each predictor@@ -345,14 +352,16 @@ class GroundedProposer(Proposer):proposed_instructions[pred_i] = []selected_tip = Noneif self.set_tip_randomly:- if self.verbose: print("Using a randomly generated configuration for our grounded proposer.")+ if self.verbose:+ print("Using a randomly generated configuration for our grounded proposer.")# Randomly select the tipselected_tip_key = self.rng.choice(list(TIPS.keys()))selected_tip = TIPS[selected_tip_key]self.use_tip = bool(selected_tip,)- if self.verbose: print(f"Selected tip: {selected_tip_key}")+ if self.verbose:+ print(f"Selected tip: {selected_tip_key}")proposed_instructions[pred_i].append(self.propose_instruction_for_predictor(@@ -420,7 +429,8 @@ class GroundedProposer(Proposer):self.prompt_model.kwargs["temperature"] = original_temp# Log the trace used to generate the new instruction, along with the new instruction itself- if self.verbose: self.prompt_model.inspect_history(n=1)- if self.verbose: print(f"PROPOSED INSTRUCTION: {proposed_instruction}")+ if self.verbose:+ self.prompt_model.inspect_history(n=1)+ print(f"PROPOSED INSTRUCTION: {proposed_instruction}")return strip_prefix(proposed_instruction)\ No newline at end of filediff --git a/examples/outdated_v2.4_examples/tweets/tweet_metric.py b/examples/outdated_v2.4_examples/tweets/tweet_metric.pyindex a2c522a75..d6b98d8b7 100644--- a/examples/outdated_v2.4_examples/tweets/tweet_metric.py+++ b/examples/outdated_v2.4_examples/tweets/tweet_metric.py@@ -42,9 +42,13 @@ def metric(gold, pred, trace=None):score = (correct + engaging + faithful) if correct and (len(tweet) <= 280) else 0if METRIC is not None:- if METRIC == 'correct': return correct- if METRIC == 'engaging': return engaging- if METRIC == 'faithful': return faithful-- if trace is not None: return score >= 3+ if METRIC == 'correct':+ return correct+ if METRIC == 'engaging':+ return engaging+ if METRIC == 'faithful':+ return faithful++ if trace is not None:+ return score >= 3return score / 3.0