Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created March 3, 2024 11:15
Show Gist options
  • Save fsndzomga/3f54fb76a2dcdf9da20866bc8393b157 to your computer and use it in GitHub Desktop.
Save fsndzomga/3f54fb76a2dcdf9da20866bc8393b157 to your computer and use it in GitHub Desktop.
import dspy
from dspy.experimental import SyntheticDataGenerator
from pydantic import BaseModel, Field
llm = dspy.OpenAI(model='gpt-3.5-turbo',api_key=openai_key)
dspy.settings.configure(lm=llm)
class SyntheticFacts(BaseModel):
fact: str = Field(..., description="a statement")
veracity: bool = Field(..., description="an assessment of the veracity of the statement")
# Generate synthetic data from pydantic model
generator = SyntheticDataGenerator(schema_class=SyntheticFacts)
examples = generator.generate(sample_size=10)
print(f"Examples from pydantic model: {examples}")
# If you already have some examples and potentially need more:
existing_examples = [dspy.Example({'fact': 'The Earth is round.', 'veracity': 'True'}),
dspy.Example({'fact': 'Water boils at 100°C.', 'veracity': 'True'})]
partial_generator = SyntheticDataGenerator(examples=existing_examples)
augmented_examples = partial_generator.generate(sample_size=8)
print(f"Examples from existing examples: {augmented_examples}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment