
    hYH                    B   d Z ddlmZ ddl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mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ  ej2                  e      Z G d dee
      Z G d de      Z G d d      Z G d dee      Z  G d dee      Z! G d dee      Z"y)z3Interfaces to be implemented by general evaluators.    )annotationsN)ABCabstractmethod)Sequence)Enum)AnyOptionalUnion)warn)AgentAction)BaseLanguageModel)run_in_executor)Chainc                      e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZ	 dZ		 dZ
	 d	Z	 d
Z	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZy)EvaluatorTypezThe 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     Y/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain/evaluation/schema.pyr   r      s    &	BF% JS'O!L7H1@#VH<)7'OPKIKN9=-M"?;#M.#M=-T5Ir?   r   c                  ,    e Zd ZdZeedd              Zy)LLMEvalChainz,A base class for evaluators that use an LLM.c                     y)z#Create a new evaluator from an LLM.Nr>   )clsllmkwargss      r@   from_llmzLLMEvalChain.from_llmN       r?   N)rE   r   rF   r   returnrB   )r&   r'   r(   r)   classmethodr   rG   r>   r?   r@   rB   rB   K   s    62  2r?   rB   c                  p    e Zd ZdZedd       Zedd       Zed	d       Zed	d       Z	 	 d
	 	 	 	 	 ddZ	y)_EvalArgsMixinz(Mixin for checking evaluation arguments.c                     yz2Whether this evaluator requires a reference label.Fr>   selfs    r@   requires_referencez!_EvalArgsMixin.requires_referenceW        r?   c                     y)0Whether this evaluator requires an input string.Fr>   rO   s    r@   requires_inputz_EvalArgsMixin.requires_input\   rR   r?   c                6    d| j                   j                   dS )z&Warning to show when input is ignored.zIgnoring input in , as it is not expected.	__class__r&   rO   s    r@   _skip_input_warningz"_EvalArgsMixin._skip_input_warninga   s     $DNN$;$;#<<TUUr?   c                6    d| j                   j                   dS )z*Warning to show when reference is ignored.zIgnoring reference in rW   rX   rO   s    r@   _skip_reference_warningz&_EvalArgsMixin._skip_reference_warningf   s!     %T^^%<%<$==UV	
r?   Nc                d   | j                   r&|$| j                  j                   d}t        |      |#| j                   st	        | j
                  d       | j                  r&|$| j                  j                   d}t        |      |%| j                  st	        | j                  d       yyy)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.   )
stacklevelz requires a reference string.)rU   rY   r&   
ValueErrorr   rZ   rQ   r\   )rP   	referenceinput_msgs       r@   _check_evaluation_argsz%_EvalArgsMixin._check_evaluation_argsm   s     6>^^,,--GHCS/!d&9&9))a8""y'8^^,,--JKCS/! )@)@--!< *A r?   rI   boolrI   str)NN)ra   Optional[str]rb   ri   rI   None)
r&   r'   r(   r)   propertyrQ   rU   rZ   r\   rd   r>   r?   r@   rL   rL   T   s~    2    V V 
 
 $( $= = = 
	=r?   rL   c                      e Zd ZdZed
d       Zedd       Zeddd	 	 	 	 	 	 	 	 	 dd       Zddd	 	 	 	 	 	 	 	 	 ddZ	ddd	 	 	 	 	 	 	 	 	 ddZ
ddd	 	 	 	 	 	 	 	 	 dd	Zy)StringEvaluatorzcGrade, tag, or otherwise evaluate predictions relative to their inputs
    and/or reference labels.c                .    | j                   j                  S )zThe name of the evaluation.rX   rO   s    r@   evaluation_namezStringEvaluator.evaluation_name   s     ~~&&&r?   c                     yrN   r>   rO   s    r@   rQ   z"StringEvaluator.requires_reference   rR   r?   Nra   inputc                    y)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>   rP   
predictionra   rr   rF   s        r@   _evaluate_stringsz!StringEvaluator._evaluate_strings   rH   r?   c               T   K   t        d| j                  f|||d| d{   S 7 w)aI  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.
                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ru   ra   rr   )r   rv   rt   s        r@   _aevaluate_stringsz"StringEvaluator._aevaluate_strings   sE     , %""
 "
 
 
 	
 
s   (&(c               T    | j                  ||        | j                  d|||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.
        ra   rb   rx   r>   )rd   rv   rt   s        r@   evaluate_stringsz StringEvaluator.evaluate_strings   sD    $ 	##i#F%t%% 
!
 	
 	
r?   c               p   K   | j                  ||        | j                  d|||d| d{   S 7 w)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.
        r{   rx   Nr>   )rd   ry   rt   s        r@   aevaluate_stringsz!StringEvaluator.aevaluate_strings   sR     $ 	##i#F,T,, 
!
 	
 
 	
 
s   -646rg   re   )
ru   zUnion[str, Any]ra   Optional[Union[str, Any]]rr   r   rF   r   rI   dict)
ru   rh   ra   ri   rr   ri   rF   r   rI   r   )r&   r'   r(   r)   rk   ro   rQ   r   rv   ry   r|   r~   r>   r?   r@   rm   rm      s;     ' '   
 04+/ $ -	
 )  
 6 04+/
 $
 -	

 )
 
 

F $(#
 
 !	

 
 
 

< $(#
 
 !	

 
 
 

r?   rm   c                      e Zd ZdZeddd	 	 	 	 	 	 	 	 	 	 	 dd       Zddd	 	 	 	 	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 	 	 ddZy)	PairwiseStringEvaluatorzDCompare the output of two models (or two outputs of the same model).Nrq   c                    y)1  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.
        Nr>   rP   ru   prediction_bra   rr   rF   s         r@   _evaluate_string_pairsz.PairwiseStringEvaluator._evaluate_string_pairs  rH   r?   c               V   K   t        d| j                  f||||d| d{   S 7 w)@  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ru   r   ra   rr   )r   r   r   s         r@   _aevaluate_string_pairsz/PairwiseStringEvaluator._aevaluate_string_pairs  sH     ( %''
 "%
 
 
 	
 
    )')c               V    | j                  ||        | j                  d||||d|S )r   r{   r   r>   )rd   r   r   s         r@   evaluate_string_pairsz-PairwiseStringEvaluator.evaluate_string_pairs8  sG    ( 	##i#F*t** 
!%	

 
 	
r?   c               r   K   | j                  ||        | j                  d||||d| d{   S 7 w)r   r{   r   Nr>   )rd   r   r   s         r@   aevaluate_string_pairsz.PairwiseStringEvaluator.aevaluate_string_pairsU  sU     ( 	##i#F1T11 
!%	

 
 
 	
 
   .757)ru   rh   r   rh   ra   ri   rr   ri   rF   r   rI   r   )	r&   r'   r(   r)   r   r   r   r   r   r>   r?   r@   r   r     s8   N $(#  	
 !   
 4 $(#
 
 	

 !
 
 
 

F $(#
 
 	

 !
 
 
 

D $(#
 
 	

 !
 
 
 

r?   r   c                      e Zd ZdZed	d       Zedd	 	 	 	 	 	 	 	 	 	 	 d
d       Zdd	 	 	 	 	 	 	 	 	 	 	 d
dZdd	 	 	 	 	 	 	 	 	 	 	 d
dZ	dd	 	 	 	 	 	 	 	 	 	 	 d
dZ
y)AgentTrajectoryEvaluatorz,Interface for evaluating agent trajectories.c                     y)rT   Tr>   rO   s    r@   rU   z'AgentTrajectoryEvaluator.requires_inputv  s     r?   N)ra   c                    y)  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>   rP   ru   agent_trajectoryrr   ra   rF   s         r@   _evaluate_agent_trajectoryz3AgentTrajectoryEvaluator._evaluate_agent_trajectory{  rH   r?   c               V   K   t        d| j                  f||||d| d{   S 7 w)  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)ru   r   ra   rr   )r   r   r   s         r@   _aevaluate_agent_trajectoryz4AgentTrajectoryEvaluator._aevaluate_agent_trajectory  sH     * %++
 "-
 
 
 	
 
r   c               V    | j                  ||        | j                  d||||d|S )r   r{   ru   rr   r   ra   r>   )rd   r   r   s         r@   evaluate_agent_trajectoryz2AgentTrajectoryEvaluator.evaluate_agent_trajectory  sG    * 	##i#F.t.. 
!-	

 
 	
r?   c               r   K   | j                  ||        | j                  d||||d| d{   S 7 w)r   r{   r   Nr>   )rd   r   r   s         r@   aevaluate_agent_trajectoryz3AgentTrajectoryEvaluator.aevaluate_agent_trajectory  sU     * 	##i#F5T55 
!-	

 
 
 	
 
r   re   )ru   rh   r   z!Sequence[tuple[AgentAction, str]]rr   rh   ra   ri   rF   r   rI   r   )r&   r'   r(   r)   rk   rU   r   r   r   r   r   r>   r?   r@   r   r   s  s@   6   $(  <	
  !  
 8 $(
 
 <	

 
 !
 
 

J $(
 
 <	

 
 !
 
 

H $(
 
 <	

 
 !
 
 

r?   r   )#r)   
__future__r   loggingabcr   r   collections.abcr   enumr   typingr   r	   r
   warningsr   langchain_core.agentsr   langchain_core.language_modelsr   langchain_core.runnables.configr   langchain.chains.baser   	getLoggerr&   loggerrh   r   rB   rL   rm   r   r   r>   r?   r@   <module>r      s    9 "  # $  ' '  - < ; '			8	$3JC 3Jl25 20= 0=fw
nc w
to
nc o
dx
~s x
r?   