
    AhL7                         S r SSKJr  SSKJrJrJr  SSKJr  SSK	J
r
Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  S\4S jr " S S\\5      r " S S\5      r " S S\\5      r " S S\\5      rg)z:String distance evaluators based on the RapidFuzz library.    )Enum)AnyCallableOptional)	Callbacks)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)pre_init)Field)override)Chain)PairwiseStringEvaluatorStringEvaluator)RUN_KEYreturnc                  d     SSK n U R                  $ ! [         a  nSn[        U5      UeSnAff = f)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   emsgs      a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/evaluation/string_distance/base.py_load_rapidfuzzr      sC    &   &> 	 #A%&s    
/*/c                   0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)StringDistance)   a  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indel N)__name__
__module____qualname____firstlineno____doc__DAMERAU_LEVENSHTEINLEVENSHTEINJAROJARO_WINKLERHAMMINGINDEL__static_attributes__r#       r   r   r   )   s(    	 0KD!LGEr0   r   c                   &   \ rS rSr% Sr\" \R                  S9r\\	S'   \" SS9r
\\	S'    \S\\\4   S\\\4   4S	 j5       r\S\\   4S
 j5       rS\\\4   S\\\4   4S jr\SS.S\S\S\4S jj5       r\S\4S j5       rS\S\S\4S jrSrg)_RapidFuzzChainMixin=   z<Shared methods for the rapidfuzz string distance evaluators.)defaultr   Tnormalize_scorevaluesr   c                     [        5         U$ )z
Validate that the rapidfuzz library is installed.

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

Returns:
    Dict[str, Any]: The validated values.
)r   )clsr6   s     r   validate_dependencies*_RapidFuzzChainMixin.validate_dependenciesE   s     	r0   c                     S/$ )z@
Get the output keys.

Returns:
    List[str]: The output keys.
scorer#   selfs    r   output_keys _RapidFuzzChainMixin.output_keysS   s     yr0   resultc                 d    SUS   0n[         U;   a  U[            R                  5       U[         '   U$ )z
Prepare the output dictionary.

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

Returns:
    Dict[str, Any]: The prepared output dictionary.
r<   )r   dict)r>   rA   s     r   _prepare_output$_RapidFuzzChainMixin._prepare_output]   s5     6'?+f$Wo224F7Or0   Fr5   c                   SSK Jn  [        R                  UR                  [        R
                  UR                  [        R                  UR                  [        R                  UR                  [        R                  UR                  [        R                  UR                  0nX;  a   SU  S[        [        5       3n[!        U5      eX0   nU(       a  UR"                  $ UR                  $ )z
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list
ValueErrornormalized_distance)r   r5   rf_distance
module_mapr   modules         r   _get_metric _RapidFuzzChainMixin._get_metricl   s     	6 ..0N0N&&(?(?!1!1'')@)@""K$7$7  +"3"3&

 %+H:$T.%9$:<  S/!%---r0   c                 R    [         R                  U R                  U R                  S9$ )zY
Get the distance metric function.

Returns:
    Callable: The distance metric function.
rF   )r2   rT   r   r5   r=   s    r   metric_RapidFuzzChainMixin.metric   s,     $//MM 00 0 
 	
r0   abc                 $    U R                  X5      $ )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.
)rW   )r>   rY   rZ   s      r   compute_metric#_RapidFuzzChainMixin.compute_metric   s     {{1  r0   r#   N)r$   r%   r&   r'   r(   r   r   r,   r   __annotations__r5   boolr
   rC   strr   r9   propertyrN   r?   rD   staticmethodr   rT   rW   floatr\   r/   r#   r0   r   r2   r2   =   s   F$^-H-HIHnI!$/OT/J 4S> d38n   T#Y  d38n c3h  >C  c  t      D 

 

 

! ! ! !r0   r2   c                      \ rS rSrSr\S\4S j5       r\S\4S j5       r\S\	\
   4S j5       r\S\
4S j5       r SS	\\
\4   S
\\   S\\
\4   4S jjr SS	\\
\4   S
\\   S\\
\4   4S jjr\SSSSSSS.S\
S\\
   S\\
   S\S\\	\
      S\\\
\4      S\S\S\4S jj5       r\SSSSSSS.S\
S\\
   S\\
   S\S\\	\
      S\\\
\4      S\S\S\4S jj5       rSrg)StringDistanceEvalChain   a+  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                     g)z(
This evaluator does not require input.
Fr#   r=   s    r   requires_input&StringDistanceEvalChain.requires_input   s    
 r0   c                     g)z.
This evaluator does not require a reference.
Tr#   r=   s    r   requires_reference*StringDistanceEvalChain.requires_reference   s    
 r0   c                 
    SS/$ )>
Get the input keys.

Returns:
    List[str]: The input keys.
	reference
predictionr#   r=   s    r   
input_keys"StringDistanceEvalChain.input_keys   s     \**r0   c                 4    U R                   R                   S3$ )B
Get the evaluation name.

Returns:
    str: The evaluation name.
	_distancer   valuer=   s    r   evaluation_name'StringDistanceEvalChain.evaluation_name   s     --%%&i00r0   Ninputsrun_managerc                 6    SU R                  US   US   5      0$ )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.
r<   ro   rp   r\   r>   rz   r{   s      r   _callStringDistanceEvalChain._call   s&      ,,VK-@&BVWXXr0   c                 >   #    SU R                  US   US   5      0$ 7f)a5  
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.
r<   ro   rp   r}   r~   s      r   _acallStringDistanceEvalChain._acall   s)     " ,,VK-@&BVWXX   F)ro   input	callbackstagsmetadatainclude_run_inforp   ro   r   r   r   r   r   kwargsc                <    U " XS.UUUUS9n	U R                  U	5      $ )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.
rp   ro   rz   r   r   r   r   rD   
r>   rp   ro   r   r   r   r   r   r   rA   s
             r   _evaluate_strings)StringDistanceEvalChain._evaluate_strings  s3    4 ",E-
 ##F++r0   c                j   #    U R                  XS.UUUUS9I Sh  vN n	U R                  U	5      $  N7f)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.
r   r   NacallrD   r   s
             r   _aevaluate_strings*StringDistanceEvalChain._aevaluate_strings,  sI     6 zz",E- " 
 
 ##F++
   313r#   N)r$   r%   r&   r'   r(   ra   r_   rh   rk   rN   r`   rq   rx   rC   r   r   r	   r   r   r   r   r   r   r   r/   r#   r0   r   re   re      s;   ,    D   +DI + + 1 1 1 =AYS#XY 89Y 
c3h	Y* BFYS#XY =>Y 
c3h	Y& 
 $(##$(-1!&!, !, C=	!,
 }!, !, tCy!!, 4S>*!, !, !, 
!, !,F 
 $(##$(-1!&!, !, C=	!,
 }!, !, tCy!!, 4S>*!, !, !, 
!, !,r0   re   c                   b   \ rS rSrSr\S\\   4S j5       r\S\4S j5       r	 SS\
\\4   S\\   S\
\\4   4S	 jjr SS\
\\4   S\\   S\
\\4   4S
 jjrSSSSS.S\S\S\S\\\      S\\
\\4      S\S\S\
4S jjrSSSSS.S\S\S\S\\\      S\\
\\4      S\S\S\
4S jjrSrg)PairwiseStringDistanceEvalChainiQ  z6Compute string edit distances between two predictions.r   c                 
    SS/$ )rn   rp   prediction_br#   r=   s    r   rq   *PairwiseStringDistanceEvalChain.input_keysT  s     n--r0   c                 6    SU R                   R                   S3$ )rt   	pairwise_ru   rv   r=   s    r   rx   /PairwiseStringDistanceEvalChain.evaluation_name^  s     4==../y99r0   Nrz   r{   c                 6    SU R                  US   US   5      0$ )a  
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.
r<   rp   r   r}   r~   s      r   r   %PairwiseStringDistanceEvalChain._callh  s*    " T(()=vn?UV
 	
r0   c                 >   #    SU R                  US   US   5      0$ 7f)a"  
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.
r<   rp   r   r}   r~   s      r   r   &PairwiseStringDistanceEvalChain._acall|  s-     " T(()=vn?UV
 	
r   F)r   r   r   r   rp   r   r   r   r   r   r   c                <    U " XS.UUUUS9nU R                  U5      $ )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.
rp   r   r   r   	r>   rp   r   r   r   r   r   r   rA   s	            r   _evaluate_string_pairs6PairwiseStringDistanceEvalChain._evaluate_string_pairs  s3    2 ",K-
 ##F++r0   c                j   #    U R                  XS.UUUUS9I Sh  vN nU R                  U5      $  N7f)a  
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   r   Nr   r   s	            r   _aevaluate_string_pairs7PairwiseStringDistanceEvalChain._aevaluate_string_pairs  sI     2 zz",K- " 
 
 ##F++
r   r#   r   )r$   r%   r&   r'   r(   ra   rN   r`   rq   rx   rC   r   r   r	   r   r   r   r   r_   r   r   r/   r#   r0   r   r   r   Q  s   @.DI . . : : : =A
S#X
 89
 
c3h	
. BF
S#X
 =>
 
c3h	
2  $$(-1!& ,  , 	 ,
  , tCy! , 4S>* ,  ,  , 
 ,N  $$(-1!& ,  , 	 ,
  , tCy! , 4S>* ,  ,  , 
 ,  ,r0   r   N)r(   enumr   typingr   r   r   langchain_core.callbacksr    langchain_core.callbacks.managerr   r	   langchain_core.utilsr
   pydanticr   typing_extensionsr   langchain.chains.baser   langchain.evaluation.schemar   r   langchain.schemar   r   r`   r   r2   re   r   r#   r0   r   <module>r      sz    @  * * . *  & ' P $ *S$ (j!5 j!Zd,o/C d,NA,&=?S A,r0   