§
    è˜ºh¾6  ã                   ó   — d Z ddlmZ ddlmZmZmZ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 ddlmZmZ ddlmZ d	efd
„Z G d„ dee¦  «        Z G d„ de¦  «        Z G d„ dee¦  «        Z G d„ dee¦  «        ZdS )z:String distance evaluators based on the RapidFuzz library.é    )ÚEnum)ÚAnyÚCallableÚDictÚListÚOptional)ÚFieldÚroot_validator)ÚAsyncCallbackManagerForChainRunÚCallbackManagerForChainRunÚ	Callbacks)ÚChain)ÚPairwiseStringEvaluatorÚStringEvaluator)ÚRUN_KEYÚreturnc                  óV   — 	 ddl } n# t          $ r t          d¦  «        ‚w xY w| j        S )z¬
    Load the RapidFuzz library.

    Raises:
        ImportError: If the rapidfuzz library is not installed.

    Returns:
        Any: The rapidfuzz.distance module.
    r   NzyPlease install the rapidfuzz library to use the FuzzyMatchStringEvaluator.Please install it with `pip install rapidfuzz`.)Ú	rapidfuzzÚImportErrorÚdistance)r   s    úq/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/langchain/evaluation/string_distance/base.pyÚ_load_rapidfuzzr      sS   € ð
ØÐÐÐÐøÝð 
ð 
ð 
Ýð>ñ
ô 
ð 	
ð
øøøð
 ÔÐs   ‚ ‡!c                   ó*   — e Zd ZdZdZdZdZdZdZdZ	dS )	ÚStringDistancea7  Distance metric to use.

    Attributes:
        DAMERAU_LEVENSHTEIN: The Damerau-Levenshtein distance.
        LEVENSHTEIN: The Levenshtein distance.
        JARO: The Jaro distance.
        JARO_WINKLER: The Jaro-Winkler distance.
        HAMMING: The Hamming distance.
        INDEL: The Indel distance.
    Údamerau_levenshteinÚlevenshteinÚjaroÚjaro_winklerÚhammingÚindelN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDAMERAU_LEVENSHTEINÚLEVENSHTEINÚJAROÚJARO_WINKLERÚHAMMINGÚINDEL© ó    r   r   r   &   s9   € € € € € ð	ð 	ð 0ÐØ€KØ€DØ!€LØ€GØ€E€E€Er,   r   c                   óR  — e Zd ZU dZ eej        ¬¦  «        Zeed<    ed¬¦  «        Z	e
ed<   	 edeeef         deeef         fd„¦   «         Zedee         fd	„¦   «         Zd
eeef         deeef         fd„Zeddede
defd„¦   «         Zedefd„¦   «         Zdededefd„ZdS )Ú_RapidFuzzChainMixinz<Shared methods for the rapidfuzz string distance evaluators.)Údefaultr   TÚnormalize_scoreÚvaluesr   c                 ó"   — t          ¦   «          |S )zÍ
        Validate that the rapidfuzz library is installed.

        Args:
            values (Dict[str, Any]): The input values.

        Returns:
            Dict[str, Any]: The validated values.
        )r   )Úclsr1   s     r   Úvalidate_dependenciesz*_RapidFuzzChainMixin.validate_dependenciesB   s   € õ 	ÑÔÐØˆr,   c                 ó   — dgS )z`
        Get the output keys.

        Returns:
            List[str]: The output keys.
        Úscorer+   ©Úselfs    r   Úoutput_keysz _RapidFuzzChainMixin.output_keysP   s   € ð ˆyÐr,   Úresultc                 óz   — d|d         i}t           |v r'|t                                         ¦   «         |t           <   |S )zÊ
        Prepare the output dictionary.

        Args:
            result (Dict[str, Any]): The evaluation results.

        Returns:
            Dict[str, Any]: The prepared output dictionary.
        r6   )r   Údict)r8   r:   s     r   Ú_prepare_outputz$_RapidFuzzChainMixin._prepare_outputZ   s;   € ð ˜6 'œ?Ð+ˆÝ�fÐÐØ$¥Wœo×2Ò2Ñ4Ô4ˆF•7‰OØˆr,   Fc                 ód  — ddl m} t          j        |j        t          j        |j        t          j        |j        t          j	        |j
        t          j        |j        t          j        |j        i}| |vr't          d| › dt!          t          ¦  «        › �¦  «        ‚||          }|r|j        S |j        S )a  
        Get the distance metric function based on the distance type.

        Args:
            distance (str): The distance type.

        Returns:
            Callable: The distance metric function.

        Raises:
            ValueError: If the distance metric is invalid.
        r   )r   zInvalid distance metric: z
Must be one of: )r   r   r   r%   ÚDamerauLevenshteinr&   ÚLevenshteinr'   ÚJaror(   ÚJaroWinklerr)   ÚHammingr*   ÚIndelÚ
ValueErrorÚlistÚnormalized_distance)r   r0   Úrf_distanceÚ
module_mapÚmodules        r   Ú_get_metricz _RapidFuzzChainMixin._get_metrici   sË   € ð 	6Ð5Ð5Ð5Ð5Ð5õ Ô.°Ô0NÝÔ&¨Ô(?ÝÔ Ô!1ÝÔ'¨Ô)@ÝÔ" KÔ$7ÝÔ  +Ô"3ð&
ˆ
ð ˜:Ð%Ð%Ýð<¨Hð <ð <Ý%)­.Ñ%9Ô%9ð<ð <ñô ð ð ˜HÔ%ˆØð 	#ØÔ-Ð-à”?Ð"r,   c                 óN   — t                                | j        | j        ¬¦  «        S )zy
        Get the distance metric function.

        Returns:
            Callable: The distance metric function.
        )r0   )r.   rK   r   r0   r7   s    r   Úmetricz_RapidFuzzChainMixin.metricŒ   s+   € õ $×/Ò/ØŒM¨4Ô+?ð 0ñ 
ô 
ð 	
r,   ÚaÚbc                 ó.   — |                       ||¦  «        S )zä
        Compute the distance between two strings.

        Args:
            a (str): The first string.
            b (str): The second string.

        Returns:
            float: The distance between the two strings.
        )rM   )r8   rN   rO   s      r   Úcompute_metricz#_RapidFuzzChainMixin.compute_metric˜   s   € ð �{Š{˜1˜aÑ Ô Ð r,   N)F)r!   r"   r#   r$   r	   r   r(   r   Ú__annotations__r0   Úboolr
   r   Ústrr   r4   Úpropertyr   r9   r=   Ústaticmethodr   rK   rM   ÚfloatrQ   r+   r,   r   r.   r.   :   s…  € € € € € € ØFÐFà$˜u¨^Ô-HÐIÑIÔI€HˆnÐIÐIÑIØ!˜E¨$Ð/Ñ/Ô/€O�TÐ/Ð/Ñ/ðJð ð¨4°°S°¬>ð ¸dÀ3ÈÀ8¼nð ð ð ñ „^ðð ð˜T #œYð ð ð ñ „Xðð d¨3°¨8¤nð ¸¸cÀ3¸h¼ð ð ð ð ð ð #ð  #˜cð  #°Dð  #ÀXð  #ð  #ð  #ñ „\ð #ðD ð	
˜ð 	
ð 	
ð 	
ñ „Xð	
ð! ð !¨ð !°ð !ð !ð !ð !ð !ð !r,   r.   c                   ó&  — e Zd ZdZedefd„¦   «         Zedefd„¦   «         Zedee	         fd„¦   «         Z
ede	fd„¦   «         Z	 ddee	ef         d	ee         dee	ef         fd
„Z	 ddee	ef         d	ee         dee	ef         fd„Zdddddddœde	dee	         dee	         dedeee	                  deee	ef                  dededefd„Zdddddddœde	dee	         dee	         dedeee	                  deee	ef                  dededefd„ZdS )ÚStringDistanceEvalChainak  Compute string distances between the prediction and the reference.

    Examples
    ----------

    >>> from langchain.evaluation import StringDistanceEvalChain
    >>> evaluator = StringDistanceEvalChain()
    >>> evaluator.evaluate_strings(
            prediction="Mindy is the CTO",
            reference="Mindy is the CEO",
        )

    Using the `load_evaluator` function:

    >>> from langchain.evaluation import load_evaluator
    >>> evaluator = load_evaluator("string_distance")
    >>> evaluator.evaluate_strings(
            prediction="The answer is three",
            reference="three",
        )
    r   c                 ó   — dS )z8
        This evaluator does not require input.
        Fr+   r7   s    r   Úrequires_inputz&StringDistanceEvalChain.requires_input½   s	   € ð
 ˆur,   c                 ó   — dS )z>
        This evaluator does not require a reference.
        Tr+   r7   s    r   Úrequires_referencez*StringDistanceEvalChain.requires_referenceÄ   s	   € ð
 ˆtr,   c                 ó
   — ddgS )ú^
        Get the input keys.

        Returns:
            List[str]: The input keys.
        Ú	referenceÚ
predictionr+   r7   s    r   Ú
input_keysz"StringDistanceEvalChain.input_keysË   s   € ð ˜\Ð*Ð*r,   c                 ó    — | j         j        › d�S )úb
        Get the evaluation name.

        Returns:
            str: The evaluation name.
        Ú	_distance©r   Úvaluer7   s    r   Úevaluation_namez'StringDistanceEvalChain.evaluation_nameÕ   s   € ð ”-Ô%Ð0Ð0Ð0Ð0r,   NÚinputsÚrun_managerc                 óJ   — d|                       |d         |d         ¦  «        iS )a^  
        Compute the string distance between the prediction and the reference.

        Args:
            inputs (Dict[str, Any]): The input values.
            run_manager (Optional[CallbackManagerForChainRun]):
                The callback manager.

        Returns:
            Dict[str, Any]: The evaluation results containing the score.
        r6   r`   ra   ©rQ   ©r8   ri   rj   s      r   Ú_callzStringDistanceEvalChain._callß   s)   € ð  ˜×,Ò,¨V°KÔ-@À&ÈÔBVÑWÔWÐXÐXr,   c              ƒ   óN   K  — d|                       |d         |d         ¦  «        iS )a}  
        Asynchronously compute the string distance between the prediction
            and the reference.

        Args:
            inputs (Dict[str, Any]): The input values.
            run_manager (Optional[AsyncCallbackManagerForChainRun]:
                The callback manager.

        Returns:
            Dict[str, Any]: The evaluation results containing the score.
        r6   r`   ra   rl   rm   s      r   Ú_acallzStringDistanceEvalChain._acallñ   s-   è è € ð" ˜×,Ò,¨V°KÔ-@À&ÈÔBVÑWÔWÐXÐXr,   F)r`   ÚinputÚ	callbacksÚtagsÚmetadataÚinclude_run_infora   r`   rq   rr   rs   rt   ru   Úkwargsc                óR   —  | ||dœ||||¬¦  «        }	|                       |	¦  «        S )aê  
        Evaluate the string distance between the prediction and the reference.

        Args:
            prediction (str): The prediction string.
            reference (Optional[str], optional): The reference string.
            input (Optional[str], optional): The input string.
            callbacks (Callbacks, optional): The callbacks to use.
            **kwargs: Additional keyword arguments.

        Returns:
            dict: The evaluation results containing the score.
        ©ra   r`   ©ri   rr   rs   rt   ru   ©r=   ©
r8   ra   r`   rq   rr   rs   rt   ru   rv   r:   s
             r   Ú_evaluate_stringsz)StringDistanceEvalChain._evaluate_strings  sG   € ð2 �Ø",¸9ÐEÐEØØØØ-ð
ñ 
ô 
ˆð ×#Ò# FÑ+Ô+Ð+r,   c             ‹   óv   K  — |                       ||dœ||||¬¦  «        ƒ d{V —†}	|                      |	¦  «        S )a  
        Asynchronously evaluate the string distance between the
            prediction and the reference.

        Args:
            prediction (str): The prediction string.
            reference (Optional[str], optional): The reference string.
            input (Optional[str], optional): The input string.
            callbacks (Callbacks, optional): The callbacks to use.
            **kwargs: Additional keyword arguments.

        Returns:
            dict: The evaluation results containing the score.
        rx   ry   N©Úacallr=   r{   s
             r   Ú_aevaluate_stringsz*StringDistanceEvalChain._aevaluate_strings'  sk   è è € ð4 —z’zØ",¸9ÐEÐEØØØØ-ð "ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
ˆð ×#Ò# FÑ+Ô+Ð+r,   ©N)r!   r"   r#   r$   rU   rS   r[   r]   r   rT   rb   rh   r   r   r   r   rn   r   rp   r   r<   r|   r€   r+   r,   r   rY   rY   ¦   sÆ  € € € € € ðð ð, ð ð ð ð ñ „Xðð ð Dð ð ð ñ „Xðð ð+˜D œIð +ð +ð +ñ „Xð+ð ð1 ð 1ð 1ð 1ñ „Xð1ð =AðYð Yà�S˜#�X”ðYð Ð8Ô9ðYð 
ˆc�3ˆhŒð	Yð Yð Yð Yð* BFðYð Yà�S˜#�X”ðYð Ð=Ô>ðYð 
ˆc�3ˆhŒð	Yð Yð Yð Yð. $(Ø#Ø#Ø$(Ø-1Ø!&ð!,ð !,ð !,ð ð!,ð ˜C”=ð	!,ð
 ˜Œ}ð!,ð ð!,ð �t˜C”yÔ!ð!,ð ˜4  S œ>Ô*ð!,ð ð!,ð ð!,ð 
ð!,ð !,ð !,ð !,ðN $(Ø#Ø#Ø$(Ø-1Ø!&ð!,ð !,ð !,ð ð!,ð ˜C”=ð	!,ð
 ˜Œ}ð!,ð ð!,ð �t˜C”yÔ!ð!,ð ˜4  S œ>Ô*ð!,ð ð!,ð ð!,ð 
ð!,ð !,ð !,ð !,ð !,ð !,r,   rY   c                   ó®  — e Zd ZdZedee         fd„¦   «         Zedefd„¦   «         Z	 dde	ee
f         dee         de	ee
f         fd„Z	 dde	ee
f         dee         de	ee
f         fd	„Zdddd
dœdedededeee                  dee	ee
f                  dede
defd„Zdddd
dœdedededeee                  dee	ee
f                  dede
defd„ZdS )ÚPairwiseStringDistanceEvalChainz6Compute string edit distances between two predictions.r   c                 ó
   — ddgS )r_   ra   Úprediction_br+   r7   s    r   rb   z*PairwiseStringDistanceEvalChain.input_keysN  s   € ð ˜nÐ-Ð-r,   c                 ó"   — d| j         j        › d�S )rd   Ú	pairwise_re   rf   r7   s    r   rh   z/PairwiseStringDistanceEvalChain.evaluation_nameX  s   € ð :˜4œ=Ô.Ð9Ð9Ð9Ð9r,   Nri   rj   c                 óJ   — d|                       |d         |d         ¦  «        iS )aN  
        Compute the string distance between two predictions.

        Args:
            inputs (Dict[str, Any]): The input values.
            run_manager (CallbackManagerForChainRun , optional):
                The callback manager.

        Returns:
            Dict[str, Any]: The evaluation results containing the score.
        r6   ra   r…   rl   rm   s      r   rn   z%PairwiseStringDistanceEvalChain._callb  s-   € ð" �T×(Ò(¨°Ô)=¸vÀnÔ?UÑVÔVð
ð 	
r,   c              ƒ   óN   K  — d|                       |d         |d         ¦  «        iS )ab  
        Asynchronously compute the string distance between two predictions.

        Args:
            inputs (Dict[str, Any]): The input values.
            run_manager (AsyncCallbackManagerForChainRun , optional):
                The callback manager.

        Returns:
            Dict[str, Any]: The evaluation results containing the score.
        r6   ra   r…   rl   rm   s      r   rp   z&PairwiseStringDistanceEvalChain._acallv  s1   è è € ð" �T×(Ò(¨°Ô)=¸vÀnÔ?UÑVÔVð
ð 	
r,   F)rr   rs   rt   ru   ra   r…   rr   rs   rt   ru   rv   c                óR   —  | ||dœ||||¬¦  «        }|                       |¦  «        S )a&  
        Evaluate the string distance between two predictions.

        Args:
            prediction (str): The first prediction string.
            prediction_b (str): The second prediction string.
            callbacks (Callbacks, optional): The callbacks to use.
            tags (List[str], optional): Tags to apply to traces.
            metadata (Dict[str, Any], optional): Metadata to apply to traces.
            **kwargs: Additional keyword arguments.

        Returns:
            dict: The evaluation results containing the score.
        ©ra   r…   ry   rz   ©	r8   ra   r…   rr   rs   rt   ru   rv   r:   s	            r   Ú_evaluate_string_pairsz6PairwiseStringDistanceEvalChain._evaluate_string_pairsŠ  sG   € ð2 �Ø",¸lÐKÐKØØØØ-ð
ñ 
ô 
ˆð ×#Ò# FÑ+Ô+Ð+r,   c             ‹   óv   K  — |                       ||dœ||||¬¦  «        ƒ d{V —†}|                      |¦  «        S )a5  
        Asynchronously evaluate the string distance between two predictions.

        Args:
            prediction (str): The first prediction string.
            prediction_b (str): The second prediction string.
            callbacks (Callbacks, optional): The callbacks to use.
            tags (List[str], optional): Tags to apply to traces.
            metadata (Dict[str, Any], optional): Metadata to apply to traces.
            **kwargs: Additional keyword arguments.

        Returns:
            dict: The evaluation results containing the score.
        r‹   ry   Nr~   rŒ   s	            r   Ú_aevaluate_string_pairsz7PairwiseStringDistanceEvalChain._aevaluate_string_pairs¬  sk   è è € ð2 —z’zØ",¸lÐKÐKØØØØ-ð "ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
ˆð ×#Ò# FÑ+Ô+Ð+r,   r�   )r!   r"   r#   r$   rU   r   rT   rb   rh   r   r   r   r   rn   r   rp   r   rS   r<   r�   r�   r+   r,   r   rƒ   rƒ   K  s2  € € € € € Ø@Ð@àð.˜D œIð .ð .ð .ñ „Xð.ð ð: ð :ð :ð :ñ „Xð:ð =Að
ð 
à�S˜#�X”ð
ð Ð8Ô9ð
ð 
ˆc�3ˆhŒð	
ð 
ð 
ð 
ð. BFð
ð 
à�S˜#�X”ð
ð Ð=Ô>ð
ð 
ˆc�3ˆhŒð	
ð 
ð 
ð 
ð2  $Ø$(Ø-1Ø!&ð ,ð  ,ð  ,ð ð ,ð ð	 ,ð
 ð ,ð �t˜C”yÔ!ð ,ð ˜4  S œ>Ô*ð ,ð ð ,ð ð ,ð 
ð ,ð  ,ð  ,ð  ,ðN  $Ø$(Ø-1Ø!&ð ,ð  ,ð  ,ð ð ,ð ð	 ,ð
 ð ,ð �t˜C”yÔ!ð ,ð ˜4  S œ>Ô*ð ,ð ð ,ð ð ,ð 
ð ,ð  ,ð  ,ð  ,ð  ,ð  ,r,   rƒ   N)r$   Úenumr   Útypingr   r   r   r   r   Úlangchain_core.pydantic_v1r	   r
   Úlangchain.callbacks.managerr   r   r   Úlangchain.chains.baser   Úlangchain.evaluation.schemar   r   Úlangchain.schemar   r   rT   r   r.   rY   rƒ   r+   r,   r   ú<module>r—      sÁ  ðØ @Ð @à Ð Ð Ð Ð Ð Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6à <Ð <Ð <Ð <Ð <Ð <Ð <Ð <ðð ð ð ð ð ð ð ð ð ð
 (Ð 'Ð 'Ð 'Ð 'Ð 'Ø PÐ PÐ PÐ PÐ PÐ PÐ PÐ PØ $Ð $Ð $Ð $Ð $Ð $ð˜ð ð ð ð ð(ð ð ð ð �S˜$ñ ô ð ð(i!ð i!ð i!ð i!ð i!˜5ñ i!ô i!ð i!ðXb,ð b,ð b,ð b,ð b,˜oÐ/Cñ b,ô b,ð b,ðJA,ð A,ð A,ð A,ð A,Ð&=Ð?Sñ A,ô A,ð A,ð A,ð A,r,   