
    3fiA5                         d 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
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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y)z:String distance evaluators based on the RapidFuzz library.    )Callable)Enum)Any)	Callbacks)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)pre_init)Field)override)Chain)PairwiseStringEvaluatorStringEvaluator)RUN_KEYreturnc                  b    	 ddl } | j                  S # t        $ r}d}t        |      |d}~ww xY w)zLoad the RapidFuzz library.

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

    Returns:
        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   emsgs      o/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/evaluation/string_distance/base.py_load_rapidfuzzr      sC    &   &> 	 #A%&s    	.).c                   (    e Zd ZdZdZdZdZdZdZdZ	y)	StringDistanceaC  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   )   s(    	 0KD!LGEr,   r   c                      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dede
defd       Zedefd       ZdededefdZy)_RapidFuzzChainMixinz<Shared methods for the rapidfuzz string distance evaluators.)defaultr   Tnormalize_scorevaluesr   c                     t                |S )zValidate that the rapidfuzz library is installed.

        Args:
            values: The input values.

        Returns:
            The validated values.
        )r   )clsr1   s     r   validate_dependenciesz*_RapidFuzzChainMixin.validate_dependenciesE   s     	r,   c                     dgS )zLGet the output keys.

        Returns:
            The output keys.
        scorer+   selfs    r   output_keysz _RapidFuzzChainMixin.output_keysR   s     yr,   resultc                 `    d|d   i}t         |v r|t            j                         |t         <   |S )zPrepare the output dictionary.

        Args:
            result: The evaluation results.

        Returns:
            The prepared output dictionary.
        r6   )r   dict)r8   r:   s     r   _prepare_outputz$_RapidFuzzChainMixin._prepare_output[   s5     6'?+f$Wo224F7Or,   Fr0   c                   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 d|  dt        t               }t!        |      ||    }|r|j"                  S |j                  S )aB  Get the distance metric function based on the distance type.

        Args:
            distance: The distance type.
            normalize_score: Whether to normalize the score.

        Returns:
            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list
ValueErrornormalized_distance)r   r0   rf_distance
module_mapr   modules         r   _get_metricz _RapidFuzzChainMixin._get_metrici   s     	6 ..0N0N&&(?(?!1!1'')@)@""K$7$7  +"3"3&

 :%+H:$T.%9$:<  S/!H%---r,   c                 X    t         j                  | j                  | j                        S )zfGet the distance metric function.

        Returns:
            The distance metric function.
        r>   )r.   rL   r   r0   r7   s    r   metricz_RapidFuzzChainMixin.metric   s,     $//MM 00 0 
 	
r,   abc                 &    | j                  ||      S )zCompute the distance between two strings.

        Args:
            a: The first string.
            b: The second string.

        Returns:
            The distance between the two strings.
        )rN   )r8   rO   rP   s      r   compute_metricz#_RapidFuzzChainMixin.compute_metric   s     {{1a  r,   N)r!   r"   r#   r$   r
   r   r(   r   __annotations__r0   boolr	   r<   strr   r4   propertyrF   r9   r=   staticmethodr   rL   rN   floatrR   r+   r,   r   r.   r.   =   s   F$^-H-HIHnI!$/OT/J 
4S> 
d38n 
 
 T#Y  d38n c3h  >C  c  t      D 	
 	
 	

! 
! 
! 
!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e	 ddee	ef   d	edz  dee	ef   fd
       Ze	 ddee	ef   d	edz  dee	ef   fd       Zedddddddde	de	dz  de	dz  dedee	   dz  dee	ef   dz  dededefd       Zedddddddde	de	dz  de	dz  dedee	   dz  dee	ef   dz  dededefd       Zy)StringDistanceEvalChaina{  Compute string distances between the prediction and the reference.

    Examples:
    ----------
    >>> from langchain_classic.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_classic.evaluation import load_evaluator
    >>> evaluator = load_evaluator("string_distance")
    >>> evaluator.evaluate_strings(
            prediction="The answer is three",
            reference="three",
        )
    r   c                      y)z&This evaluator does not require input.Fr+   r7   s    r   requires_inputz&StringDistanceEvalChain.requires_input   s     r,   c                      y)z,This evaluator does not require a reference.Tr+   r7   s    r   requires_referencez*StringDistanceEvalChain.requires_reference   s     r,   c                 
    ddgS )JGet the input keys.

        Returns:
            The input keys.
        	reference
predictionr+   r7   s    r   
input_keysz"StringDistanceEvalChain.input_keys   s     \**r,   c                 4    | j                   j                   dS )TGet the evaluation name.

        Returns:
            The evaluation name.
        	_distancer   valuer7   s    r   evaluation_namez'StringDistanceEvalChain.evaluation_name   s     --%%&i00r,   Ninputsrun_managerc                 6    d| j                  |d   |d         iS zCompute the string distance between the prediction and the reference.

        Args:
            inputs: The input values.
            run_manager: The callback manager.

        Returns:
            The evaluation results containing the score.
        r6   ra   rb   rR   r8   rj   rk   s      r   _callzStringDistanceEvalChain._call   s&     ,,VK-@&BVWXXr,   c                 >   K   d| j                  |d   |d         iS wrm   rn   ro   s      r   _acallzStringDistanceEvalChain._acall   s*      ,,VK-@&BVWXX   F)ra   input	callbackstagsmetadatainclude_run_inforb   ra   rt   ru   rv   rw   rx   kwargsc                D     | ||d||||      }	| j                  |	      S )a$  Evaluate the string distance between the prediction and the reference.

        Args:
            prediction: The prediction string.
            reference: The reference string.
            input: The input string.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to use.
            include_run_info: Whether to include run info in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            The evaluation results containing the score.
        rb   ra   rj   ru   rv   rw   rx   r=   
r8   rb   ra   rt   ru   rv   rw   rx   ry   r:   s
             r   _evaluate_stringsz)StringDistanceEvalChain._evaluate_strings   s5    8 ",9E-
 ##F++r,   c                r   K   | j                  ||d||||       d{   }	| j                  |	      S 7 w)a&  Evaluate the string distance between the prediction and the reference.

        Args:
            prediction: The prediction string.
            reference: The reference string.
            input: The input string.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to apply.
            include_run_info: Whether to include run info in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            The evaluation results containing the score.
        r{   r|   Nacallr=   r~   s
             r   _aevaluate_stringsz*StringDistanceEvalChain._aevaluate_strings  sL     8 zz",9E- " 
 
 ##F++
   757N)r!   r"   r#   r$   rV   rT   r\   r^   rF   rU   rc   ri   r   r<   r   r   rp   r   rr   r   r   r   r+   r,   r   rZ   rZ      sW   *    D   +DI + + 1 1 1  :>YS#XY 0$6Y 
c3h	Y Y   ?CYS#XY 5t;Y 
c3h	Y Y  
 !% #!%*.!&#, #, :	#,
 Tz#, #, 3i$#, sCx.4'#, #, #, 
#, #,J 
 !% #!%*.!&", ", :	",
 Tz", ", 3i$", sCx.4'", ", ", 
", ",r,   rZ   c                   n   e Zd ZdZedee   fd       Zedefd       Ze		 dde
eef   dedz  de
eef   fd       Ze		 dde
eef   dedz  de
eef   fd	       Ze	dddd
ddedededee   dz  de
eef   dz  dedede
fd       Ze	dddd
ddedededee   dz  de
eef   dz  dedede
fd       Zy)PairwiseStringDistanceEvalChainz6Compute string edit distances between two predictions.r   c                 
    ddgS )r`   rb   prediction_br+   r7   s    r   rc   z*PairwiseStringDistanceEvalChain.input_keysH  s     n--r,   c                 6    d| j                   j                   dS )re   	pairwise_rf   rg   r7   s    r   ri   z/PairwiseStringDistanceEvalChain.evaluation_nameQ  s     4==../y99r,   Nrj   rk   c                 6    d| j                  |d   |d         iS )zCompute the string distance between two predictions.

        Args:
            inputs: The input values.
            run_manager: The callback manager.

        Returns:
            The evaluation results containing the score.
        r6   rb   r   rn   ro   s      r   rp   z%PairwiseStringDistanceEvalChain._callZ  s*      T(()=vn?UV
 	
r,   c                 >   K   d| j                  |d   |d         iS w)zAsynchronously compute the string distance between two predictions.

        Args:
            inputs: The input values.
            run_manager: The callback manager.

        Returns:
            The evaluation results containing the score.
        r6   rb   r   rn   ro   s      r   rr   z&PairwiseStringDistanceEvalChain._acallm  s.       T(()=vn?UV
 	
rs   F)ru   rv   rw   rx   rb   r   ru   rv   rw   rx   ry   c                D     | ||d||||      }| j                  |      S )a  Evaluate the string distance between two predictions.

        Args:
            prediction: The first prediction string.
            prediction_b: The second prediction string.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to use.
            include_run_info: Whether to include run info in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            The evaluation results containing the score.
        rb   r   r|   r}   	r8   rb   r   ru   rv   rw   rx   ry   r:   s	            r   _evaluate_string_pairsz6PairwiseStringDistanceEvalChain._evaluate_string_pairs  s5    4 ",lK-
 ##F++r,   c                r   K   | j                  ||d||||       d{   }| j                  |      S 7 w)a  Asynchronously evaluate the string distance between two predictions.

        Args:
            prediction: The first prediction string.
            prediction_b: The second prediction string.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to use.
            include_run_info: Whether to include run info in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            The evaluation results containing the score.
        r   r|   Nr   r   s	            r   _aevaluate_string_pairsz7PairwiseStringDistanceEvalChain._aevaluate_string_pairs  sL     4 zz",lK- " 
 
 ##F++
r   r   )r!   r"   r#   r$   rV   rF   rU   rc   ri   r   r<   r   r   rp   r   rr   r   rT   r   r   r+   r,   r   r   r   E  s   @.DI . . : : :  :>
S#X
 0$6
 
c3h	
 
$  ?C
S#X
 5t;
 
c3h	
 
$   $!%*.!& ,  , 	 ,
  , 3i$ , sCx.4' ,  ,  , 
 ,  ,D   $!%*.!& ,  , 	 ,
  , 3i$ , sCx.4' ,  ,  , 
 ,  ,r,   r   N)r$   collections.abcr   enumr   typingr   langchain_core.callbacksr    langchain_core.callbacks.managerr   r   langchain_core.utilsr	   pydanticr
   typing_extensionsr   langchain_classic.chains.baser   #langchain_classic.evaluation.schemar   r   langchain_classic.schemar   r   rU   r   r.   rZ   r   r+   r,   r   <module>r      sz    @ $   . *  & / X , (S$ (e!5 e!P],o/C ],@,&=?S ,r,   