Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

(feature) Adding support for Mistral AI's API

Franck Stephane Ndzomga445c45adf274
diff --git a/dspy/experimental/synthetic_data.py b/dspy/experimental/synthetic_data.py
index 223a70bc1..37c140352 100644
--- a/dspy/experimental/synthetic_data.py
+++ b/dspy/experimental/synthetic_data.py
@@ -1,4 +1,3 @@
-import logging
import random
from typing import List, Optional
@@ -7,13 +6,10 @@ from pydantic import BaseModel
import dspy
-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 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 SyntheticDataGenerator:
def __init__(self, schema_class: Optional[BaseModel] = None, examples: Optional[List[dspy.Example]] = None):
@@ -21,21 +17,10 @@ class SyntheticDataGenerator:
self.examples = examples
def 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:
- logging.info("No additional data generation needed.")
+ print("No additional data generation needed.")
return self.examples[:sample_size]
additional_samples_needed = sample_size - (len(self.examples) if self.examples else 0)
@@ -44,19 +29,12 @@ class SyntheticDataGenerator:
return self.examples + generated_examples if self.examples else generated_examples
def _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
- """
if 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:
@@ -64,15 +42,6 @@ class SyntheticDataGenerator:
return properties
def _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)
@@ -85,7 +54,6 @@ 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"),
diff --git a/tests/experimental/test_synthetic_data.py b/tests/experimental/test_synthetic_data.py
deleted file mode 100644
index 7d183c576..000000000
--- a/tests/experimental/test_synthetic_data.py
+++ /dev/null
@@ -1 +0,0 @@
-# TO DO TO INCREASE COVERAGE