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 Ndzomgaee6c409341bf
diff --git a/dsp/modules/mistral.py b/dsp/modules/mistral.py
index 6c304c306..abe5d9a23 100644
--- a/dsp/modules/mistral.py
+++ b/dsp/modules/mistral.py
@@ -12,7 +12,6 @@ try:
mistralai_api_error = MistralAPIException
except ImportError:
mistralai_api_error = Exception
- print("Not loading Mistral AI because it is not installed. Install it with `pip install mistralai`.")
def backoff_hdlr(details):
@@ -55,7 +54,12 @@ class Mistral(LM):
Additional arguments to pass to the API provider.
"""
super().__init__(model)
+
+ if mistralai_api_error == Exception:
+ raise ImportError("Not loading Mistral AI because it is not installed. Install it with `pip install mistralai`.")
+
self.client = MistralClient(api_key=api_key)
+
self.provider = "mistral"
self.kwargs = {
"model": model,
diff --git a/dspy/experimental/synthetic_data.py b/dspy/experimental/synthetic_data.py
index 37c140352..72dcfdbfe 100644
--- a/dspy/experimental/synthetic_data.py
+++ b/dspy/experimental/synthetic_data.py
@@ -1,15 +1,19 @@
import random
from typing import List, Optional
+import logging
from pydantic import BaseModel
import 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,21 @@ 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:
- 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 +44,19 @@ 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:
@@ -42,6 +64,15 @@ 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)
@@ -54,6 +85,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"),
diff --git a/tests/experimental/test_synthetic_data.py b/tests/experimental/test_synthetic_data.py
new file mode 100644
index 000000000..7d183c576
--- /dev/null
+++ b/tests/experimental/test_synthetic_data.py
@@ -0,0 +1 @@
+# TO DO TO INCREASE COVERAGE