
    +#h                     v    d dl Z d dlZd dlmZmZ d dlmZ d dlZ G d dej                        Z	 G d d      Z
y)    N)ListOptional)	BaseModelc                   ~    e Zd Z ej                  d      Z ej                  d      Z ej                  d      Zy)DescriptionSignaturezname of a fielddesczan example value for the fieldz8a short text only description of what the field containsN)	__name__
__module____qualname__dspy
InputField
field_nameexampleOutputFielddescription     ]/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/experimental/synthetic_data.pyr   r   
   s7     &78Jdoo#CDG"$""(bcKr   r   c                       e Zd Zddee   deeej                        fdZde	deej                     fdZ
d Zd	e	deej                     fd
ZdefdZy)SyntheticDataGeneratorNschema_classexamplesc                      || _         || _        y )N)r   r   )selfr   r   s      r   __init__zSyntheticDataGenerator.__init__   s    ( r   sample_sizereturnc                    | j                   s| j                  st        d      | j                  r<t        | j                        |k\  r$t	        j
                  d       | j                  d| S || j                  rt        | j                        ndz
  }| j                  |      }| j                  r| j                  |z   S |S )a%  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
        z3Either a schema_class or examples must be provided.z%No additional data generation needed.Nr   )r   r   
ValueErrorlenlogginginfo_generate_additional_examples)r   r   additional_samples_neededgenerated_exampless       r   generatezSyntheticDataGenerator.generate   s       RSS==S/;>LL@A==+..$/3t}}3E\]$^!!??@YZ59]]t}}11ZHZZr   c                    | j                   r!| j                   j                         }|d   }|S | j                  r~| j                  d   j                  d   }t	        j
                  t              }|j                         D ci c].  }|dt         ||t        ||               j                        i0 }}|S i }|S c c}w )zDefine fields to generate if a schema class is provided.
        Infer fields to generate if an inital sample of examples is provided.

        Returns:
            dict: dictionary of fields to generate
        
propertiesr   _storer   )r   r   )
r   model_json_schemar   __dict__r   Predictr   keysstrr   )r   data_schemar)   inferred_schema
descriptorfields         r   _define_or_infer_fieldsz.SyntheticDataGenerator._define_or_infer_fields*   s     ++==?K$\2J  ]]"mmA.77AO&:;J'6';';'=?#  -jE[^_not_u[v6w  6E  6E  2F  "G  G ?J ?  J	?s   3B<r%   c                    | j                         }| j                  r| j                  j                  nd d}| j                  |      }t	        |t
        j                  f|      }t        j                  ||      } |t        t        j                  d|                  }|j                  D 	cg c]>  }t        j                  |j                         D 	ci c]  }	|	t        ||	       c}	      @ c}	}S c c}	w c c}	}w )a  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
        Inferred	Signature)n   )sindex)r4   r   r
   _prepare_fieldstyper   r7   r-   r/   randomrandintcompletionsExampler.   getattr)
r   r%   r)   
class_namefieldssignature_class	generatorresponse
completionr   s
             r   r$   z4SyntheticDataGenerator._generate_additional_examples=   s     113
6:6G6G))22ZXXab
%%j1zDNN+<fELL4MN	Cq:S(T$UV #+"6"68 \f\k\k\mnjj'*j*IIno 8 	8n 8s   1&C=C8+	C=8C=c                    ddj                  |j                                dt        j                  d      d|j                         D ci c],  }|t        j                  ||   j                  dd            . c}S c c}w )	z4Prepare fields to generate in an appropriate format.z"Generates the following outputs: {z, z}.za random stringr   )__doc__r:   r   zNo description)joinr.   r   r   r   get)r   r)   r   s      r   r;   z&SyntheticDataGenerator._prepare_fieldsQ   s     =TYYzGX=Y<ZZ]^oo+<=
 ",!24 4++J1G1K1KM[k1lmm 4
 	
4s   1B)NN)r
   r   r   r   r   r   r   r@   r   intr'   r4   r$   dictr;   r   r   r   r   r      s{    !Xi%8 !8TXY]YeYeTfKg ![C [D,> [*&8s 8tTXT`T`Oa 8(
T 
r   r   )r"   r=   typingr   r   pydanticr   r   r7   r   r   r   r   r   <module>rP      s2      !  d4>> dH
 H
r   