HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/langchain/evaluation/__pycache__/schema.cpython-310.pyc
o

���gG�@sdZddlmZddlZddlmZmZddlmZddl	m
Z
mZmZm
Z
mZddlmZddlmZdd	lmZdd
lmZddlmZe�e�ZGdd
�d
ee�ZGdd�de�ZGdd�d�ZGdd�dee�Z Gdd�dee�Z!Gdd�dee�Z"dS)z3Interfaces to be implemented by general evaluators.�)�annotationsN)�ABC�abstractmethod)�Enum)�Any�Optional�Sequence�Tuple�Union)�warn)�AgentAction)�BaseLanguageModel)�run_in_executor)�Chainc@s�eZdZdZdZ	dZ	dZ	dZ	dZ	dZ		dZ
	d	Z	d
Z	dZ
	dZ	d
Z	dZ	dZ	dZ	dZ	dZ	dZ	dZ	dZdS)�
EvaluatorTypezThe types of the evaluators.�qa�cot_qa�
context_qa�pairwise_string�score_string�labeled_pairwise_string�labeled_score_string�
trajectory�criteria�labeled_criteria�string_distance�exact_match�regex_match�pairwise_string_distance�embedding_distance�pairwise_embedding_distance�
json_validity�
json_equality�json_edit_distance�json_schema_validationN)�__name__�
__module__�__qualname__�__doc__�QA�COT_QA�
CONTEXT_QA�PAIRWISE_STRING�SCORE_STRING�LABELED_PAIRWISE_STRING�LABELED_SCORE_STRING�AGENT_TRAJECTORY�CRITERIA�LABELED_CRITERIA�STRING_DISTANCE�EXACT_MATCH�REGEX_MATCH�PAIRWISE_STRING_DISTANCE�EMBEDDING_DISTANCE�PAIRWISE_EMBEDDING_DISTANCE�
JSON_VALIDITY�
JSON_EQUALITY�JSON_EDIT_DISTANCE�JSON_SCHEMA_VALIDATION�r=r=�F/usr/local/lib/python3.10/dist-packages/langchain/evaluation/schema.pyrsTrc@s"eZdZdZeed
dd���Zd	S)�LLMEvalChainz,A base class for evaluators that use an LLM.�llmr
�kwargsr�returncK�dS)z#Create a new evaluator from an LLM.Nr=)�clsr@rAr=r=r>�from_llmM�zLLMEvalChain.from_llmN)r@r
rArrBr?)r%r&r'r(�classmethodrrEr=r=r=r>r?Js
r?c@sXeZdZdZeddd��Zeddd��Zedd	d
��Zeddd��Z	
	
dddd�Z	d
S)�_EvalArgsMixinz(Mixin for checking evaluation arguments.rB�boolcCrC�z2Whether this evaluator requires a reference label.Fr=��selfr=r=r>�requires_referenceV�z!_EvalArgsMixin.requires_referencecCrC)�0Whether this evaluator requires an input string.Fr=rKr=r=r>�requires_input[rNz_EvalArgsMixin.requires_input�strcC�d|jj�d�S)z&Warning to show when input is ignored.zIgnoring input in �, as it is not expected.��	__class__r%rKr=r=r>�_skip_input_warning`sz"_EvalArgsMixin._skip_input_warningcCrR)z*Warning to show when reference is ignored.zIgnoring reference in rSrTrKr=r=r>�_skip_reference_warninges�z&_EvalArgsMixin._skip_reference_warningN�	reference�
Optional[str]�input�NonecCs||jr|durt|jj�d���|dur|jst|j�|jr,|dur,t|jj�d���|dur:|js<t|j�dSdSdS)a�Check if the evaluation arguments are valid.

        Args:
            reference (Optional[str], optional): The reference label.
            input (Optional[str], optional): The input string.
        Raises:
            ValueError: If the evaluator requires an input string but none is provided,
                or if the evaluator requires a reference label but none is provided.
        Nz requires an input string.z requires a reference string.)rP�
ValueErrorrUr%rrVrMrW)rLrXrZr=r=r>�_check_evaluation_argsls
�z%_EvalArgsMixin._check_evaluation_args�rBrI�rBrQ)NN)rXrYrZrYrBr[)
r%r&r'r(�propertyrMrPrVrWr]r=r=r=r>rHSs�rHc@sxeZdZdZeddd��Zeddd��Zed	d	d
�ddd��Zd	d	d
�ddd�Z	d	d	d
�ddd�Z
d	d	d
�ddd�Zd	S) �StringEvaluatorzcGrade, tag, or otherwise evaluate predictions relative to their inputs
    and/or reference labels.rBrQcCs|jjS)zThe name of the evaluation.rTrKr=r=r>�evaluation_name�szStringEvaluator.evaluation_namerIcCrCrJr=rKr=r=r>rM�rNz"StringEvaluator.requires_referenceN�rXrZ�
prediction�Union[str, Any]rX�Optional[Union[str, Any]]rZrAr�dictcKrC)a:Evaluate Chain or LLM output, based on optional input and label.

        Args:
            prediction (str): The LLM or chain prediction to evaluate.
            reference (Optional[str], optional): The reference label to evaluate against.
            input (Optional[str], optional): The input to consider during evaluation.
            kwargs: Additional keyword arguments, including callbacks, tags, etc.
        Returns:
            dict: The evaluation results containing the score or value.
                It is recommended that the dictionary contain the following keys:
                     - score: the score of the evaluation, if applicable.
                     - value: the string value of the evaluation, if applicable.
                     - reasoning: the reasoning for the evaluation, if applicable.
        Nr=�rLrdrXrZrAr=r=r>�_evaluate_strings�rFz!StringEvaluator._evaluate_stringsc�s$�td|jf|||d�|��IdHS)aIAsynchronously evaluate Chain or LLM output, based on optional input and label.

        Args:
            prediction (str): The LLM or chain prediction to evaluate.
            reference (Optional[str], optional): The reference label to evaluate against.
            input (Optional[str], optional): The input to consider during evaluation.
            kwargs: Additional keyword arguments, including callbacks, tags, etc.
        Returns:
            dict: The evaluation results containing the score or value.
                It is recommended that the dictionary contain the following keys:
                     - score: the score of the evaluation, if applicable.
                     - value: the string value of the evaluation, if applicable.
                     - reasoning: the reasoning for the evaluation, if applicable.
        N�rdrXrZ)rrirhr=r=r>�_aevaluate_strings�s����z"StringEvaluator._aevaluate_stringsrYcKs&|j||d�|jd|||d�|��S)a�Evaluate Chain or LLM output, based on optional input and label.

        Args:
            prediction (str): The LLM or chain prediction to evaluate.
            reference (Optional[str], optional): The reference label to evaluate against.
            input (Optional[str], optional): The input to consider during evaluation.
            kwargs: Additional keyword arguments, including callbacks, tags, etc.
        Returns:
            dict: The evaluation results containing the score or value.
        rcrjNr=)r]rirhr=r=r>�evaluate_strings�s��z StringEvaluator.evaluate_stringsc�s.�|j||d�|jd|||d�|��IdHS)a	Asynchronously evaluate Chain or LLM output, based on optional input and label.

        Args:
            prediction (str): The LLM or chain prediction to evaluate.
            reference (Optional[str], optional): The reference label to evaluate against.
            input (Optional[str], optional): The input to consider during evaluation.
            kwargs: Additional keyword arguments, including callbacks, tags, etc.
        Returns:
            dict: The evaluation results containing the score or value.
        rcrjNr=)r]rkrhr=r=r>�aevaluate_strings�s���z!StringEvaluator.aevaluate_stringsr_r^)
rdrerXrfrZrfrArrBrg)
rdrQrXrYrZrYrArrBrg)r%r&r'r(r`rbrMrrirkrlrmr=r=r=r>ra�s&��#��rac@s\eZdZdZeddd�ddd��Zddd�ddd�Zddd�ddd�Zddd�ddd�ZdS)�PairwiseStringEvaluatorzDCompare the output of two models (or two outputs of the same model).NrcrdrQ�prediction_brXrYrZrArrBrgcKrC)�1Evaluate the output string pairs.

        Args:
            prediction (str): The output string from the first model.
            prediction_b (str): The output string from the second model.
            reference (Optional[str], optional): The expected output / reference string.
            input (Optional[str], optional): The input string.
            kwargs: Additional keyword arguments, such as callbacks and optional reference strings.
        Returns:
            dict: A dictionary containing the preference, scores, and/or other information.
        Nr=�rLrdrorXrZrAr=r=r>�_evaluate_string_pairs�rFz.PairwiseStringEvaluator._evaluate_string_pairsc�s&�td|jf||||d�|��IdHS)�@Asynchronously evaluate the output string pairs.

        Args:
            prediction (str): The output string from the first model.
            prediction_b (str): The output string from the second model.
            reference (Optional[str], optional): The expected output / reference string.
            input (Optional[str], optional): The input string.
            kwargs: Additional keyword arguments, such as callbacks and optional reference strings.
        Returns:
            dict: A dictionary containing the preference, scores, and/or other information.
        N�rdrorXrZ)rrrrqr=r=r>�_aevaluate_string_pairss����z/PairwiseStringEvaluator._aevaluate_string_pairscKs(|j||d�|jd||||d�|��S)rprcrtNr=)r]rrrqr=r=r>�evaluate_string_pairs/s��z-PairwiseStringEvaluator.evaluate_string_pairsc�s0�|j||d�|jd||||d�|��IdHS)rsrcrtNr=)r]rurqr=r=r>�aevaluate_string_pairsLs���z.PairwiseStringEvaluator.aevaluate_string_pairs)rdrQrorQrXrYrZrYrArrBrg)	r%r&r'r(rrrrurvrwr=r=r=r>rn�s��#�"�rnc@sbeZdZdZeddd��Zedd�ddd��Zdd�ddd�Zdd�ddd�Z	dd�ddd�Z
dS)�AgentTrajectoryEvaluatorz,Interface for evaluating agent trajectories.rBrIcCrC)rOTr=rKr=r=r>rPmrNz'AgentTrajectoryEvaluator.requires_inputN)rXrdrQ�agent_trajectory�!Sequence[Tuple[AgentAction, str]]rZrXrYrArrgcKrC)�Evaluate a trajectory.

        Args:
            prediction (str): The final predicted response.
            agent_trajectory (List[Tuple[AgentAction, str]]):
                The intermediate steps forming the agent trajectory.
            input (str): The input to the agent.
            reference (Optional[str]): The reference answer.

        Returns:
            dict: The evaluation result.
        Nr=�rLrdryrZrXrAr=r=r>�_evaluate_agent_trajectoryrrFz3AgentTrajectoryEvaluator._evaluate_agent_trajectoryc�s&�td|jf||||d�|��IdHS)�Asynchronously evaluate a trajectory.

        Args:
            prediction (str): The final predicted response.
            agent_trajectory (List[Tuple[AgentAction, str]]):
                The intermediate steps forming the agent trajectory.
            input (str): The input to the agent.
            reference (Optional[str]): The reference answer.

        Returns:
            dict: The evaluation result.
        N)rdryrXrZ)rr}r|r=r=r>�_aevaluate_agent_trajectory�s����z4AgentTrajectoryEvaluator._aevaluate_agent_trajectorycKs(|j||d�|jd||||d�|��S)r{rc�rdrZryrXNr=)r]r}r|r=r=r>�evaluate_agent_trajectory�s��z2AgentTrajectoryEvaluator.evaluate_agent_trajectoryc�s0�|j||d�|jd||||d�|��IdHS)r~rcr�Nr=)r]rr|r=r=r>�aevaluate_agent_trajectory�s���z3AgentTrajectoryEvaluator.aevaluate_agent_trajectoryr^)rdrQryrzrZrQrXrYrArrBrg)r%r&r'r(r`rPrr}rr�r�r=r=r=r>rxjs��%�$�rx)#r(�
__future__r�logging�abcrr�enumr�typingrrrr	r
�warningsr�langchain_core.agentsr�langchain_core.language_modelsr
�langchain_core.runnables.configr�langchain.chains.baser�	getLoggerr%�loggerrQrr?rHrarnrxr=r=r=r>�<module>s$
6	1tr