Utilities for generating example datasets for pydantic_evals.
This module provides functions for generating sample datasets for testing and examples,
using LLMs to create realistic test data with proper structure.
Use an LLM to generate a dataset of test cases, each consisting of input, expected output, and metadata.
This function creates a properly structured dataset with the specified input, output, and metadata types.
It uses an LLM to attempt to generate realistic test cases that conform to the types' schemas.
asyncdefgenerate_dataset(*,dataset_type:type[Dataset[InputsT,OutputT,MetadataT]],path:Path|str|None=None,custom_evaluator_types:Sequence[type[Evaluator[InputsT,OutputT,MetadataT]]]=(),model:models.Model|models.KnownModelName='openai:gpt-4o',n_examples:int=3,extra_instructions:str|None=None,)->Dataset[InputsT,OutputT,MetadataT]:# pragma: no cover"""Use an LLM to generate a dataset of test cases, each consisting of input, expected output, and metadata. This function creates a properly structured dataset with the specified input, output, and metadata types. It uses an LLM to attempt to generate realistic test cases that conform to the types' schemas. Args: path: Optional path to save the generated dataset. If provided, the dataset will be saved to this location. dataset_type: The type of dataset to generate, with the desired input, output, and metadata types. custom_evaluator_types: Optional sequence of custom evaluator classes to include in the schema. model: The PydanticAI model to use for generation. Defaults to 'gpt-4o'. n_examples: Number of examples to generate. Defaults to 3. extra_instructions: Optional additional instructions to provide to the LLM. Returns: A properly structured Dataset object with generated test cases. Raises: ValidationError: If the LLM's response cannot be parsed as a valid dataset. """output_schema=dataset_type.model_json_schema_with_evaluators(custom_evaluator_types)# TODO(DavidM): Update this once we add better response_format and/or ResultTool support to PydanticAIagent=Agent(model,system_prompt=(f'Generate an object that is in compliance with this JSON schema:\n{output_schema}\n\n'f'Include {n_examples} example cases.'' You must not include any characters in your response before the opening { of the JSON object, or after the closing }.'),output_type=str,retries=1,)result=awaitagent.run(extra_instructionsor'Please generate the object.')try:result=dataset_type.from_text(result.output,fmt='json',custom_evaluator_types=custom_evaluator_types)exceptValidationErrorase:print(f'Raw response from model:\n{result.output}')raiseeifpathisnotNone:result.to_file(path,custom_evaluator_types=custom_evaluator_types)returnresult