DSPy: The framework for programming—not prompting—language models
Python
(style) adding docstrings to the synthetic data generator class
Franck Stephane Ndzomga2c00ad21839f
diff --git a/dspy/experimental/synthetic_data.py b/dspy/experimental/synthetic_data.pyindex 37c140352..0b5d546d8 100644--- a/dspy/experimental/synthetic_data.py+++ b/dspy/experimental/synthetic_data.py@@ -1,3 +1,4 @@+import loggingimport randomfrom typing import List, Optional@@ -6,10 +7,13 @@ from pydantic import BaseModelimport dspy-class descriptionSignature(dspy.Signature):- field_name = dspy.InputField(desc="name of a field")- example = dspy.InputField(desc="an example value for the field")- description = dspy.OutputField(desc="a short text only description of what the field contains")+class DescriptionSignature(dspy.Signature):+ """Custom signature to generate descriptions."""++ field_name = dspy.InputField(desc="name of a field")+ example = dspy.InputField(desc="an example value for the field")+ description = dspy.OutputField(desc="a short text only description of what the field contains")+class SyntheticDataGenerator:def __init__(self, schema_class: Optional[BaseModel] = None, examples: Optional[List[dspy.Example]] = None):@@ -17,10 +21,19 @@ class SyntheticDataGenerator:self.examples = examplesdef generate(self, sample_size: int) -> List[dspy.Example]:+ """Generate synthetic examples.++ Args:+ sample_size (int): number of examples to generate+ Raises:+ ValueError: either a schema_class or examples should be provided+ Returns:+ List[dspy.Example]: list of synthetic examples generated+ """if not self.schema_class and not self.examples:raise ValueError("Either a schema_class or examples must be provided.")if self.examples and len(self.examples) >= sample_size:- print("No additional data generation needed.")+ logging.info("No additional data generation needed.")return self.examples[:sample_size]additional_samples_needed = sample_size - (len(self.examples) if self.examples else 0)@@ -29,12 +42,18 @@ class SyntheticDataGenerator:return self.examples + generated_examples if self.examples else generated_examplesdef _define_or_infer_fields(self):+ """Define fields to generate if a schema class is provided.+ Infer fields to generate if an inital sample of examples is provided.++ Returns:+ dict: dictionnary of fields to generate+ """ # noqa: D205if self.schema_class:data_schema = self.schema_class.model_json_schema()properties = data_schema['properties']elif self.examples:inferred_schema = self.examples[0].__dict__['_store']- descriptor = dspy.Predict(descriptionSignature)+ descriptor = dspy.Predict(DescriptionSignature)properties = {field: {'description': str((descriptor(field_name=field, example=str(inferred_schema[field]))).description)}for field in inferred_schema.keys()}else:@@ -42,6 +61,14 @@ class SyntheticDataGenerator:return propertiesdef _generate_additional_examples(self, additional_samples_needed: int) -> List[dspy.Example]:+ """Generate additional examples if needed.++ Args:+ additional_samples_needed (int): the difference between the desired+ number of examples and the current number of examples+ Returns:+ List[dspy.Example]: list of synthetic examples+ """properties = self._define_or_infer_fields()class_name = f"{self.schema_class.__name__ if self.schema_class else 'Inferred'}Signature"fields = self._prepare_fields(properties)@@ -54,6 +81,7 @@ class SyntheticDataGenerator:for completion in response.completions]def _prepare_fields(self, properties) -> dict:+ """Prepare fields to generate in an appropriate format."""return {'__doc__': f"Generates the following outputs: {{{', '.join(properties.keys())}}}.",'sindex': dspy.InputField(desc="a random string"),