o
    i,                     @  s  d dl mZ d dlZd dlmZmZ d dlmZmZ d dl	m
Z
mZmZ d dlmZmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ ddlmZ ddlmZ ddl m!Z! dZ"e#e$B e%B e&B Z'	 eG dd dZ(e'e(B ee&e'e(B f B Z)	 ede'ddZ*	 edZ+eG dd dee* Z,eG dd dZ-ededdZ.	 ededdZ/	 ededdZ0	 G d d! d!eZ1ed"d#G d$d% d%ee.e/e0f e1d&Z2dS )'    )annotationsN)ABCMetaabstractmethod)	AwaitableMapping)MISSING	dataclassfields)AnyGenericcast)
ConfigDictmodel_serializer)to_jsonable_python)SerializationInfo)TypeVar
deprecated)_utils   )get_event_loop   )EvaluatorContext)EvaluatorSpec)EvaluationReasonEvaluationResultEvaluationScalar	EvaluatorEvaluatorFailureEvaluatorOutputr   c                   @  s&   e Zd ZU dZded< dZded< dS )r   a7  The result of running an evaluator with an optional explanation.

    Contains a scalar value and an optional "reason" explaining the value.

    Args:
        value: The scalar result of the evaluation (boolean, integer, float, or string).
        reason: An optional explanation of the evaluation result.
    r   valueN
str | Nonereason)__name__
__module____qualname____doc____annotations__r!    r'   r'   i/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_evals/evaluators/evaluator.pyr   (   s   
 	r   EvaluationScalarTT)default	covariantTc                   @  s<   e Zd ZU dZded< ded< ded< ded	< dddZdS )r   ay  The details of an individual evaluation result.

    Contains the name, value, reason, and source evaluator for a single evaluation.

    Args:
        name: The name of the evaluation.
        value: The scalar result of the evaluation.
        reason: An optional explanation of the evaluation result.
        source: The spec of the evaluator that produced this result.
    strnamer)   r   r    r!   r   sourcevalue_typestype[T]returnEvaluationResult[T] | Nonec                 G  sB   |D ]}t | j|rt | jtr|turqttt |   S qdS )a#  Attempt to downcast this result to a more specific type.

        Args:
            *value_types: The types to check the value against.

        Returns:
            A downcast version of this result if the value is an instance of one of the given types,
            otherwise None.
        N)
isinstancer   boolr   r   r,   )selfr0   
value_typer'   r'   r(   downcastT   s   zEvaluationResult.downcastN)r0   r1   r2   r3   )r"   r#   r$   r%   r&   r8   r'   r'   r'   r(   r   B   s   
 r   c                   @  s2   e Zd ZU dZded< ded< ded< ded< dS )	r   zARepresents a failure raised during the execution of an evaluator.r-   r.   error_messageerror_stacktracer   r/   N)r"   r#   r$   r%   r&   r'   r'   r'   r(   r   h   s   
 r   InputsT)r*   contravariantOutputT	MetadataTc                      s"   e Zd ZdZd fd
dZ  ZS )_StrictABCMetazXAn ABC-like metaclass that goes further and disallows even defining abstract subclasses.r.   r-   basestuple[type, ...]	namespacedict[str, Any]kwargsr
   c                  sl   t  j| |||fi |}tdd |jdd  D }|r4|jr4ddd |jD }t| d| |S )Nc                 s  s    | ]}t |tV  qd S )N)r4   r?   ).0cr'   r'   r(   	<genexpr>   s    z)_StrictABCMeta.__new__.<locals>.<genexpr>r   z, c                 S  s   g | ]}|qS r'   r'   )rE   mr'   r'   r(   
<listcomp>   s    z*_StrictABCMeta.__new__.<locals>.<listcomp>z& must implement all abstract methods: )super__new__any__mro____abstractmethods__join	TypeError)mclsr.   r@   rB   rD   resultis_proper_subclassabstractmethods	__class__r'   r(   rK      s   
z_StrictABCMeta.__new__)r.   r-   r@   rA   rB   rC   rD   r
   )r"   r#   r$   r%   rK   __classcell__r'   r'   rU   r(   r?   }   s    r?   F)reprc                   @  s   e Zd ZdZeddZed%ddZeedd%d	d
Z	d%ddZ
ed&ddZd'ddZd'ddZeddd(ddZd)dd Zd*d"d#ZejZd$S )+r   a7  Base class for all evaluators.

    Evaluators can assess the performance of a task in a variety of ways, as a function of the EvaluatorContext.

    Subclasses must implement the `evaluate` method. Note it can be defined with either `def` or `async def`.

    Example:
    ```python
    from dataclasses import dataclass

    from pydantic_evals.evaluators import Evaluator, EvaluatorContext


    @dataclass
    class ExactMatch(Evaluator):
        def evaluate(self, ctx: EvaluatorContext) -> bool:
            return ctx.output == ctx.expected_output
    ```
    T)arbitrary_types_allowedr2   r-   c                 C  s   | j S )zReturn the 'name' of this Evaluator to use during serialization.

        Returns:
            The name of the Evaluator, which is typically the class name.
        )r"   clsr'   r'   r(   get_serialization_name   s   z Evaluator.get_serialization_name>`name` has been renamed, use `get_serialization_name` instead.c                 C  s   |   S )r]   )r\   rZ   r'   r'   r(   r.      s   zEvaluator.namec                 C  s"   t | dd}t|tr|S |  S )aL  Return the default name to use in reports for the output of this evaluator.

        By default, if the evaluator has an attribute called `evaluation_name` of type string, that will be used.
        Otherwise, the serialization name of the evaluator (which is usually the class name) will be used.

        This can be overridden to get a more descriptive name in evaluation reports, e.g. using instance information.

        Note that evaluators that return a mapping of results will always use the keys of that mapping as the names
        of the associated evaluation results.
        evaluation_nameN)getattrr4   r-   r\   )r6   r^   r'   r'   r(   get_default_evaluation_name   s   
z%Evaluator.get_default_evaluation_namectx-EvaluatorContext[InputsT, OutputT, MetadataT],EvaluatorOutput | Awaitable[EvaluatorOutput]c                 C  s   t d)az  Evaluate the task output in the given context.

        This is the main evaluation method that subclasses must implement. It can be either synchronous
        or asynchronous, returning either an EvaluatorOutput directly or an Awaitable[EvaluatorOutput].

        Args:
            ctx: The context containing the inputs, outputs, and metadata for evaluation.

        Returns:
            The evaluation result, which can be a scalar value, an EvaluationReason, or a mapping
            of evaluation names to either of those. Can be returned either synchronously or as an
            awaitable for asynchronous evaluation.
        zYou must implement `evaluate`.)NotImplementedError)r6   ra   r'   r'   r(   evaluate   s   zEvaluator.evaluater   c                 C  s*   |  |}t|rt |S tt|S )a  Run the evaluator synchronously, handling both sync and async implementations.

        This method ensures synchronous execution by running any async evaluate implementation
        to completion using run_until_complete.

        Args:
            ctx: The context containing the inputs, outputs, and metadata for evaluation.

        Returns:
            The evaluation result, which can be a scalar value, an EvaluationReason, or a mapping
            of evaluation names to either of those.
        )re   inspectiscoroutiner   run_until_completer   r   r6   ra   outputr'   r'   r(   evaluate_sync   s   


zEvaluator.evaluate_syncc                   s*   |  |}t|r|I dH S tt|S )a  Run the evaluator asynchronously, handling both sync and async implementations.

        This method ensures asynchronous execution by properly awaiting any async evaluate
        implementation. For synchronous implementations, it returns the result directly.

        Args:
            ctx: The context containing the inputs, outputs, and metadata for evaluation.

        Returns:
            The evaluation result, which can be a scalar value, an EvaluationReason, or a mapping
            of evaluation names to either of those.
        N)re   rf   rg   r   r   ri   r'   r'   r(   evaluate_async   s
   



zEvaluator.evaluate_asyncplain)modeinfor   r
   c                 C  s   t |  |jddS )zSerialize this Evaluator to a JSON-serializable form.

        Returns:
            A JSON-serializable representation of this evaluator as an EvaluatorSpec.
        T)contextserialize_unknown)r   as_specrp   )r6   ro   r'   r'   r(   	serialize   s
   zEvaluator.serializer   c                 C  sN   |   }t|dkrd }nt|dkrtt| f}n|}t|  |dS )Nr   r   )r.   	arguments)build_serialization_argumentslennextitervaluesr   r\   )r6   raw_argumentsrt   r'   r'   r(   rr     s   zEvaluator.as_specrC   c                 C  sZ   i }t | D ]$}t| |j}|jtur||jkrq|jtur%|| kr%q|||j< q|S )a[  Build the arguments for serialization.

        Evaluators are serialized for inclusion as the "source" in an `EvaluationResult`.
        If you want to modify how the evaluator is serialized for that or other purposes, you can override this method.

        Returns:
            A dictionary of arguments to be used during serialization.
        )r	   r_   r.   r*   r   default_factory)r6   rz   fieldr   r'   r'   r(   ru     s   	


z'Evaluator.build_serialization_argumentsN)r2   r-   )ra   rb   r2   rc   )ra   rb   r2   r   )ro   r   r2   r
   )r2   r   )r2   rC   )r"   r#   r$   r%   r   __pydantic_config__classmethodr\   r   r.   r`   r   re   rk   rl   r   rs   rr   ru   r   dataclasses_no_defaults_repr__repr__r'   r'   r'   r(   r      s$    






r   )	metaclass)3
__future__r   rf   abcr   r   collections.abcr   r   dataclassesr   r   r	   typingr
   r   r   pydanticr   r   pydantic_corer   pydantic_core.core_schemar   typing_extensionsr   r   pydantic_air   r   rp   r   specr   __all__r5   intfloatr-   r   r   r   r)   r,   r   r   r;   r=   r>   r?   r   r'   r'   r'   r(   <module>   sJ    
%
$