DSPy: The framework for programming—not prompting—language models
Python
finalize flow and signature
Herumb Shandilyac323c8d01da5
diff --git a/dspy/datasets/synthesizer.py b/dspy/datasets/synthesizer.pyindex 0ba9392a8..cc5b34e95 100644--- a/dspy/datasets/synthesizer.py+++ b/dspy/datasets/synthesizer.py@@ -1,5 +1,6 @@import dspyfrom typing import List+from tqdm import tqdm, trangefrom datasets import DatasetDictdef format_examples(examples: List[dspy.Example]):@@ -12,31 +13,31 @@ def format_examples(examples: List[dspy.Example]):input_keys = example.inputs().keys()label_keys = example.labels().keys()- formatted_example += f"Input:\n"+ formatted_example += f"Inputs:\n"for key in input_keys:formatted_example += f"{key}: {example[key]}\n"- formatted_example += f"Output:\n"+ formatted_example += f"Outputs:\n"for key in label_keys:formatted_example += f"{key}: {example[key]}\n"return formatted_exampleclass ExplainTask(dspy.Signature):- """Imagine you're a detective in a game where your mission is to unlock the mystery of a hidden treasure. The treasure map, in this case, is the task description. Your first step is to study the map carefully to understand where the treasure is hidden and how to get there. This means figuring out what the task is all about—like decoding clues. Once you've got a good grasp, your job is to explain your plan to your team in a way that's super easy to understand, as if you're telling a friend how to find the treasure without using the map. You won't be using the clues directly in your explanation but rather your understanding of them to guide your team clearly and simply to the treasure."""+ """Analyze the provided set of datapoints carefully, and prepare a concise, comprehensible summary that captures the essence and purpose of the task these datapoints aim to address. Your summary should illuminate the general objective and the type of problem being solved, offering a clear picture of what the task entails at a high level. Avoid getting into the nuances of individual datapoints, specifics about models, examples, algorithms, or any intricate technicalities. Your explanation should serve to clarify the task's overall goal and its basic premise, without touching on methodologies or solutions."""examples = dspy.InputField(- prefix="Few Shot Examples:-",- desc="List of examples to analyze and explain the task.",+ prefix="Examples Datapoints:-",+ desc="List of datapoints to analyze and explain the task.",format=format_examples,)explanation = dspy.OutputField(- prefix="Explanation:",+ prefix="Task Description:",desc="Explanation of the task.",)class GenerateFieldDescription(dspy.Signature):- """I'll be providing you with the name of the field and the task description. Your task is to generate a description for the field. The description should be such that it is easy to understand and gives a clear idea of what the field is about."""+ """Generate a concise and informative description for a given field based on the provided name and task description. This description should be no longer than 10 words and should be in simple english."""task_description = dspy.InputField(prefix="Task Description:",@@ -52,8 +53,6 @@ class GenerateFieldDescription(dspy.Signature):)class GenerateInputFieldsData(dspy.Signature):- """You are an expert data generator with 30 years of experience in generating synthetic data. We want you to put these skills at work, I'll be providing you with some input fields that are columns of the csv file and the explanation of the task thse fields would be an input to. Your task is to generate synthetic for these fields."""-task_description = dspy.InputField(prefix="Task Description:",desc="Description of the task the field is an input to.",@@ -64,77 +63,90 @@ class GenerateOutputFieldsData(dspy.Signature):class Synthesizer:def __init__(self):- self.explain_task = ExplainTask()- self.generate_field_description = GenerateFieldDescription()+ self.explain_task = dspy.Predict(ExplainTask)+ self.generate_field_description = dspy.Predict(GenerateFieldDescription)- self.generate_input_data = GenerateInputFieldsData()- self.generate_output_data = GenerateOutputFieldsData()+ self.generate_input_data = GenerateInputFieldsData+ self.generate_output_data = GenerateOutputFieldsData- def _prepare_synthetic_data_signatures(self, input_keys: List[str], output_keys: List[str], task_description: str):- for key in input_keys:+ def _prepare_synthetic_data_predictors(self, input_keys: List[str], output_keys: List[str], task_description: str):+ for key in tqdm(input_keys, desc="Preparing Input Fields"):field_details = self.generate_field_description(task_description=task_description,field_name=key,)- field_name = field_details.field_name+ field_name = keyfield_description = field_details.field_descriptionoutput_field = dspy.OutputField(prefix=f"{field_name}:",desc=field_description,)- setattr(self.generate_input_data, field_name, output_field)+ self.generate_input_data = self.generate_input_data.insert(+ -1,+ field_name,+ output_field+ )input_field = dspy.InputField(prefix=f"{field_name}:",desc=field_description,)- setattr(self.generate_output_data, field_name, input_field)+ self.generate_output_data = self.generate_output_data.insert(+ -1,+ field_name,+ input_field+ )- for key in output_keys:+ for key in tqdm(output_keys, desc="Preparing Output Fields"):field_details = self.generate_field_description(task_description=task_description,field_name=key,)- field_name = field_details.field_name+ field_name = keyfield_description = field_details.field_descriptionoutput_field = dspy.OutputField(prefix=f"{field_name}:",desc=field_description,)- setattr(self.generate_output_data, field_name, output_field)+ self.generate_output_data = self.generate_output_data.insert(+ -1,+ field_name,+ output_field+ )return dspy.Predict(self.generate_input_data), dspy.Predict(self.generate_output_data)def generate(self, examples: List[dspy.Example], num_data: int) -> List[dspy.Example]:- task_description = self.explain_task(examples=examples)- self.generate_output_data.__doc__ = task_description.explanation+ task_description = self.explain_task(examples=examples).explanation+ self.generate_output_data.__doc__ = task_descriptioninput_keys = [key for key in examples[0].inputs()]output_keys = [key for key in examples[0].labels()]- self.input_predictor, self.output_predictor = self._prepare_synthetic_data_signatures(+ self.input_predictor, self.output_predictor = self._prepare_synthetic_data_predictors(input_keys=input_keys,output_keys=output_keys,task_description=task_description,)data = []- for _ in range(num_data):- inputs = self.input_predictor(task_description=task_description)+ for idx in trange(num_data, desc="Generating Synthetic Data"):+ inputs = self.input_predictor(task_description=task_description, config=dict(temperature=0.7+0.01*idx))+input_kwargs = {- key: inputs[key]+ key: getattr(inputs, key)for key in input_keys}- outputs = self.output_predictor(**input_kwargs)+ outputs = self.output_predictor(**input_kwargs, config=dict(temperature=0.7+0.01*idx))output_kwargs = {- key: outputs[key]+ key: getattr(outputs, key)for key in output_keys}