o
    i"                     @  s   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZmZ er2ddlmZ G dd deZG d	d
 d
e	eeeef B  ZdS )z`This module provides the EvaluatorSpec class for specifying evaluators in a serializable format.    )annotations)TYPE_CHECKINGAnycast)	BaseModel	RootModelValidationErrorfield_validatormodel_serializermodel_validator)SerializationInfoSerializerFunctionWrapHandler)ModelWrapValidatorHandlerc                   @  sn   e Zd ZU dZded< 	 ded< 	 eddd	ZedddZedde	dddZ
edddddZdS )EvaluatorSpeca  The specification of an evaluator to be run.

    This class is used to represent evaluators in a serializable format, supporting various
    short forms for convenience when defining evaluators in YAML or JSON dataset files.

    In particular, each of the following forms is supported for specifying an evaluator with name `MyEvaluator`:
    * `'MyEvaluator'` - Just the (string) name of the Evaluator subclass is used if its `__init__` takes no arguments
    * `{'MyEvaluator': first_arg}` - A single argument is passed as the first positional argument to `MyEvaluator.__init__`
    * `{'MyEvaluator': {k1: v1, k2: v2}}` - Multiple kwargs are passed to `MyEvaluator.__init__`
    strname"None | tuple[Any] | dict[str, Any]	argumentsreturntuple[Any, ...]c                 C  s   t | jtr	| jS dS )zGet the positional arguments for the evaluator.

        Returns:
            A tuple of positional arguments if arguments is a tuple, otherwise an empty tuple.
         )
isinstancer   tupleselfr   r   d/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_evals/evaluators/spec.pyargs,      zEvaluatorSpec.argsdict[str, Any]c                 C  s   t | jtr	| jS i S )zGet the keyword arguments for the evaluator.

        Returns:
            A dictionary of keyword arguments if arguments is a dict, otherwise an empty dict.
        )r   r   dictr   r   r   r   kwargs7   r   zEvaluatorSpec.kwargswrap)modevaluer   handler(ModelWrapValidatorHandler[EvaluatorSpec]c                 C  s^   z||}|W S  t y. } zzt|}W n	 t y   |w | W  Y d}~S d}~ww )a  Deserialize an EvaluatorSpec from various formats.

        This validator handles the various short forms of evaluator specifications,
        converting them to a consistent EvaluatorSpec instance.

        Args:
            value: The value to deserialize.
            handler: The validator handler.

        Returns:
            The deserialized EvaluatorSpec.

        Raises:
            ValidationError: If the value cannot be deserialized.
        N)r   _SerializedEvaluatorSpecmodel_validateto_evaluator_spec)clsr#   r$   resultexcdeserializedr   r   r   deserializeB   s   zEvaluatorSpec.deserializer   infor   c                 C  sX   t |jtr(|jdr(| jdu r| jS t | jtr"| j| jd iS | j| jiS || S )ao  Serialize using the appropriate short-form if possible.

        Returns:
            The serialized evaluator specification, using the shortest form possible:
            - Just the name if there are no arguments
            - {name: first_arg} if there's a single positional argument
            - {name: {kwargs}} if there are multiple (keyword) arguments
        use_short_formNr   )r   contextr   getr   r   r   )r   r$   r.   r   r   r   	serialize^   s   

zEvaluatorSpec.serializeN)r   r   )r   r   )r#   r   r$   r%   r   r   )r$   r   r.   r   r   r   )__name__
__module____qualname____doc____annotations__propertyr   r    r   classmethodr-   r
   r2   r   r   r   r   r      s   
 

r   c                   @  sL   e Zd ZdZededddZedd
dZedddZ	dddZ
dS )r&   zInternal class for handling the serialized form of an EvaluatorSpec.

    This is an auxiliary class used to serialize/deserialize instances of EvaluatorSpec
    from their serialized representation in YAML or JSON.
    rootr#   str | dict[str, Any]r   r   c                 C  s4   t |tr|S t|dkrtdt|  |S )a   Enforce that the root value has exactly one key (the evaluator name) when it is a dict.

        Args:
            value: The value to validate.

        Returns:
            The validated value.

        Raises:
            ValueError: If the value is a dict with multiple keys.
           zFExpected a single key containing the Evaluator class name, found keys )r   r   len
ValueErrorlistkeys)r)   r#   r   r   r   enforce_one_keyz   s   
z(_SerializedEvaluatorSpec.enforce_one_keyr   c                 C  s$   t | jtr	| jS tt| j S )zyGet the name of the evaluator from the serialized form.

        Returns:
            The name of the evaluator.
        )r   r:   r   nextiterr@   r   r   r   r   _name   s   z_SerializedEvaluatorSpec._namer   c                 C  sh   t | jtrdS tt| j }t |tr.t| }t	dd |D r.t
tttf |S t
t|fS )zGet the arguments for the evaluator from the serialized form.

        Returns:
            The arguments for the evaluator, which can be None (no arguments),
            a tuple (one positional arguments), or a dict (multiple keyword arguments).
        Nc                 s  s    | ]}t |tV  qd S )N)r   r   ).0kr   r   r   	<genexpr>   s    z1_SerializedEvaluatorSpec._args.<locals>.<genexpr>)r   r:   r   rB   rC   valuesr   r?   r@   allr   r   )r   r#   r@   r   r   r   _args   s   
z_SerializedEvaluatorSpec._argsr   c                 C  s   t | j| jdS )zsConvert this serialized form to an EvaluatorSpec.

        Returns:
            An EvaluatorSpec instance.
        )r   r   )r   rD   rJ   r   r   r   r   r(      s   z*_SerializedEvaluatorSpec.to_evaluator_specN)r#   r;   r   r   )r   r   )r   r   )r   r   )r3   r4   r5   r6   r	   r9   rA   r8   rD   rJ   r(   r   r   r   r   r&   s   s    
r&   N)r6   
__future__r   typingr   r   r   pydanticr   r   r   r	   r
   r   pydantic_core.core_schemar   r   r   r   r   r   r&   r   r   r   r   <module>   s     $\