
    AhJ                     ~   S r SSKrSSKrSSKJr  SSKJr  SSK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Jr  SSKJr  SSKJrJr  SSKJr  S\4S jr\R<                  " \5      r \RB                  " SS9S\"4S j5       r#S\4S jr$ " S S\%\5      r& " S S\5      r' " S S\'\5      r( " S S\'\5      r)g)z@A chain for comparing the output of two models using embeddings.    N)Enum)util)AnyOptional)	Callbacks)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)
Embeddings)pre_init)
ConfigDictField)Chain)PairwiseStringEvaluatorStringEvaluatorRUN_KEYreturnc                  P     SS K n U $ ! [         a  nSn[        U5      UeS nAff = f)Nr   z@Could not import numpy, please install with `pip install numpy`.)numpyImportError)npemsgs      d/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/evaluation/embedding_distance/base.py_import_numpyr      s5    & I  &P#A%&s    
% %   )maxsizec                  x    [        [        R                  " S5      5      (       a  g[        R	                  S5        g)Nr   Ta  NumPy not found in the current Python environment. langchain will use a pure Python implementation for embedding distance operations, which may significantly impact performance, especially for large datasets. For optimal speed and efficiency, consider installing NumPy: pip install numpyF)boolr   	find_specloggerwarning     r   _check_numpyr%   #   s/    DNN7#$$
NN	 r$   c                       SSK Jn   U " 5       $ ! [         a0     SSKJn    U " 5       $ ! [         a  nSn[        U5      UeSnAff = ff = f)zUCreate an Embeddings object.
Returns:
    Embeddings: The created Embeddings object.
r   OpenAIEmbeddingstCould not import OpenAIEmbeddings. Please install the OpenAIEmbeddings package using `pip install langchain-openai`.N)langchain_openair(   r   %langchain_community.embeddings.openai)r(   r   r   s      r   _embedding_factoryr,   1   sg    *5   
*		*   	*Q  c")	*
*s#    
A	(
AA  AA	c                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
EmbeddingDistanceI   zEmbedding Distance Metric.

Attributes:
    COSINE: Cosine distance metric.
    EUCLIDEAN: Euclidean distance metric.
    MANHATTAN: Manhattan distance metric.
    CHEBYSHEV: Chebyshev distance metric.
    HAMMING: Hamming distance metric.
cosine	euclidean	manhattan	chebyshevhammingr#   N)__name__
__module____qualname____firstlineno____doc__COSINE	EUCLIDEAN	MANHATTAN	CHEBYSHEVHAMMING__static_attributes__r#   r$   r   r.   r.   I   s"     FIIIGr$   r.   c                      \ rS rSr% Sr\" \S9r\\	S'   \" \
R                  S9r\
\	S'   \S\\\4   S\\\4   4S	 j5       r\" S
S9r\S\\   4S j5       rS\S\4S jrS\
S\4S jr\S\S\S\4S j5       r\S\S\S\4S j5       r\S\S\S\4S j5       r\S\S\S\4S j5       r\S\S\S\4S j5       rS\S\4S jr Sr!g)_EmbeddingDistanceChainMixin[   a  Shared functionality for embedding distance evaluators.

Attributes:
    embeddings (Embeddings): The embedding objects to vectorize the outputs.
    distance_metric (EmbeddingDistance): The distance metric to use
                                        for comparing the embeddings.
)default_factory
embeddings)defaultdistance_metricvaluesr   c                 v   UR                  S5      n/ n SSKJn  UR                  U5         SSKJn  UR                  U5        U(       d  Sn[	        U5      e[        U[        U5      5      (       a   SSKnU$ U$ ! [         a     N[f = f! [         a     NSf = f! [         a  nSn[	        U5      UeSnAff = f)zValidate that the TikTok library is installed.

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

Returns:
    Dict[str, Any]: The validated values.
rD   r   r'   r)   NzThe tiktoken library is required to use the default OpenAI embeddings with embedding distance evaluators. Please either manually select a different Embeddings object or install tiktoken using `pip install tiktoken`.)	getr*   r(   appendr   r+   
isinstancetupletiktoken)clsrG   rD   types_r(   r   rM   r   s           r   _validate_tiktoken_installed9_EmbeddingDistanceChainMixin._validate_tiktoken_installedg   s     ZZ-
	9MM*+	 MM*+ Q  c""j%-00	. v=  		  		  .I  "#&A-.s:   A; B 3B ;
BB
BB
B8%B33B8T)arbitrary_types_allowedc                     S/$ )zOReturn the output keys of the chain.

Returns:
    List[str]: The output keys.
scorer#   selfs    r   output_keys(_EmbeddingDistanceChainMixin.output_keys   s     yr$   resultc                 H    SUS   0n[         U;   a  U[            U[         '   U$ )NrT   r   )rV   rY   parseds      r   _prepare_output,_EmbeddingDistanceChainMixin._prepare_output   s*    6'?+f$WoF7Or$   metricc           
      <   [         R                  U R                  [         R                  U R                  [         R
                  U R                  [         R                  U R                  [         R                  U R                  0nX;   a  X!   $ SU 3n[        U5      e)zGet the metric function for the given metric name.

Args:
    metric (EmbeddingDistance): The metric name.

Returns:
    Any: The metric function.
zInvalid metric: )r.   r:   _cosine_distancer;   _euclidean_distancer<   _manhattan_distancer=   _chebyshev_distancer>   _hamming_distance
ValueError)rV   r^   metricsr   s       r   _get_metric(_EmbeddingDistanceChainMixin._get_metric   s     $$d&;&;'')A)A'')A)A'')A)A%%t'='=
 ?" )or$   abc                 f     SSK Jn  SU" X5      -
  $ ! [         a  nSn[        U5      UeSnAff = f)zCompute the cosine distance between two vectors.

Args:
    a (np.ndarray): The first vector.
    b (np.ndarray): The second vector.

Returns:
    np.ndarray: The cosine distance.
r   )cosine_similarityzThe cosine_similarity function is required to compute cosine distance. Please install the langchain-community package using `pip install langchain-community`.Ng      ?)langchain_community.utils.mathrl   r   )ri   rj   rl   r   r   s        r   r`   -_EmbeddingDistanceChainMixin._cosine_distance   sH    	*H &q,,,  	*6 
 c")	*s    
0+0c                     [        5       (       a!  SSKnUR                  R                  X-
  5      $ [	        S [        X5       5       5      S-  $ )zCompute the Euclidean distance between two vectors.

Args:
    a (np.ndarray): The first vector.
    b (np.ndarray): The second vector.

Returns:
    np.floating: The Euclidean distance.
r   Nc              3   6   #    U H  u  pX-
  X-
  -  v   M     g 7fNr#   .0xys      r   	<genexpr>C_EmbeddingDistanceChainMixin._euclidean_distance.<locals>.<genexpr>   s     ;AEae$s   g      ?)r%   r   linalgnormsumzipri   rj   r   s      r   ra   0_EmbeddingDistanceChainMixin._euclidean_distance   s=     >>99>>!%((;Q;;sBBr$   c                     [        5       (       a,  [        5       nUR                  UR                  X-
  5      5      $ [        S [	        X5       5       5      $ )zCompute the Manhattan distance between two vectors.

Args:
    a (np.ndarray): The first vector.
    b (np.ndarray): The second vector.

Returns:
    np.floating: The Manhattan distance.
c              3   >   #    U H  u  p[        X-
  5      v   M     g 7frq   absrr   s      r   rv   C_EmbeddingDistanceChainMixin._manhattan_distance.<locals>.<genexpr>        4)$!3qu::)   )r%   r   rz   r   r{   r|   s      r   rb   0_EmbeddingDistanceChainMixin._manhattan_distance   @     >>B66"&&-((4#a)444r$   c                     [        5       (       a,  [        5       nUR                  UR                  X-
  5      5      $ [        S [	        X5       5       5      $ )zCompute the Chebyshev distance between two vectors.

Args:
    a (np.ndarray): The first vector.
    b (np.ndarray): The second vector.

Returns:
    np.floating: The Chebyshev distance.
c              3   >   #    U H  u  p[        X-
  5      v   M     g 7frq   r   rr   s      r   rv   C_EmbeddingDistanceChainMixin._chebyshev_distance.<locals>.<genexpr>  r   r   )r%   r   maxr   r{   r|   s      r   rc   0_EmbeddingDistanceChainMixin._chebyshev_distance   r   r$   c                     [        5       (       a  [        5       nUR                  X:g  5      $ [        S [	        X5       5       5      [        U 5      -  $ )zCompute the Hamming distance between two vectors.

Args:
    a (np.ndarray): The first vector.
    b (np.ndarray): The second vector.

Returns:
    np.floating: The Hamming distance.
c              3   6   #    U H  u  pX:w  d  M  S v   M     g7f)r   Nr#   rr   s      r   rv   A_EmbeddingDistanceChainMixin._hamming_distance.<locals>.<genexpr>  s     5af11s   
	)r%   r   meanrz   r{   lenr|   s      r   rd   ._EmbeddingDistanceChainMixin._hamming_distance	  s@     >>B7716?"5Q55A>>r$   vectorsc                 L   U R                  U R                  5      n[        5       (       aa  [        U[	        5       R
                  5      (       a>  U" US   R                  SS5      US   R                  SS5      5      R                  5       nOU" US   US   5      n[        U5      $ )zCompute the score based on the distance metric.

Args:
    vectors (np.ndarray): The input vectors.

Returns:
    float: The computed score.
r   r   )	rg   rF   r%   rK   r   ndarrayreshapeitemfloat)rV   r   r^   rT   s       r   _compute_score+_EmbeddingDistanceChainMixin._compute_score  s     !!$"6"67>>j-/2I2IJJ71:--a4gaj6H6HB6OPUUWE71:wqz2EU|r$   r#   N)"r5   r6   r7   r8   r9   r   r,   rD   r
   __annotations__r.   r:   rF   r   dictstrr   rP   r   model_configpropertylistrW   r\   rg   staticmethodr`   ra   rb   rc   rd   r   r   r?   r#   r$   r   rA   rA   [   s    #3EFJ
F).7H7O7O)PO&P-$sCx. -T#s(^ - -^  $L T#Y  d t "3  * -C -C -C - -* Cs Cs Cs C C" 5s 5s 5s 5 5  5s 5s 5s 5 5  ?S ?S ?S ? ? c e r$   rA   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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\\\\4      S\S\S\4S jjrSSSSSS.S\S\\   S\S\\
\      S\\\\4      S\S\S\4S jjrSrg)EmbeddingDistanceEvalChaini+  a  Use embedding distances to score semantic difference between
a prediction and reference.

Examples:
    >>> chain = EmbeddingDistanceEvalChain()
    >>> result = chain.evaluate_strings(prediction="Hello", reference="Hi")
    >>> print(result)
    {'score': 0.5}
r   c                     g)zuReturn whether the chain requires a reference.

Returns:
    bool: True if a reference is required, False otherwise.
Tr#   rU   s    r   requires_reference-EmbeddingDistanceEvalChain.requires_reference6  s     r$   c                 6    SU R                   R                   S3$ )N
embedding_	_distancerF   valuerU   s    r   evaluation_name*EmbeddingDistanceEvalChain.evaluation_name?  s    D00667yAAr$   c                 
    SS/$ )MReturn the input keys of the chain.

Returns:
    List[str]: The input keys.

prediction	referencer#   rU   s    r   
input_keys%EmbeddingDistanceEvalChain.input_keysC  s     k**r$   Ninputsrun_managerc                     U R                   R                  US   US   /5      n[        5       (       a  [        5       nUR	                  U5      nU R                  U5      nSU0$ )zCompute the score for a prediction and reference.

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

Returns:
    Dict[str, Any]: The computed score.
r   r   rT   rD   embed_documentsr%   r   arrayr   rV   r   r   r   r   rT   s         r   _call EmbeddingDistanceEvalChain._callL  sc     //11L!6+#67
 >>Bhhw'G##G,r$   c                    #    U R                   R                  US   US   /5      I Sh  vN n[        5       (       a  [        5       nUR	                  U5      nU R                  U5      nSU0$  NC7f)a  Asynchronously compute the score for a prediction and reference.

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

Returns:
    Dict[str, Any]: The computed score.
r   r   NrT   rD   aembed_documentsr%   r   r   r   r   s         r   _acall!EmbeddingDistanceEvalChain._acalld  ss      88|${#
 
 >>Bhhw'G##G,
   'A/A-AA/F)r   	callbackstagsmetadatainclude_run_infor   r   r   r   r   r   kwargsc                <    U " XS.UUUUS9nU R                  U5      $ )a  Evaluate the embedding distance between a prediction and
reference.

Args:
    prediction (str): The output string from the first model.
    reference (str): The reference string (required)
    callbacks (Callbacks, optional): The callbacks to use.
    **kwargs (Any): Additional keyword arguments.

Returns:
    dict: A dictionary containing:
        - score: The embedding distance between the two
            predictions.
r   r   r   r   r   r   r   r\   	rV   r   r   r   r   r   r   r   rY   s	            r   _evaluate_strings,EmbeddingDistanceEvalChain._evaluate_strings  s3    2 ",E-
 ##F++r$   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 embedding distance between
a prediction and reference.

Args:
    prediction (str): The output string from the first model.
    reference (str): The output string from the second model.
    callbacks (Callbacks, optional): The callbacks to use.
    **kwargs (Any): Additional keyword arguments.

Returns:
    dict: A dictionary containing:
        - score: The embedding distance between the two
            predictions.
r   r   Nacallr\   r   s	            r   _aevaluate_strings-EmbeddingDistanceEvalChain._aevaluate_strings  sI     2 zz",E- " 
 
 ##F++
   313r#   rq   )r5   r6   r7   r8   r9   r   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r?   r#   r$   r   r   r   +  s    D   B B B +DI + + =A S#X  89  
c3h	 6 BF S#X  =>  
c3h	 > $(#$(-1!& ,  , C=	 ,
  , tCy! , 4S>* ,  ,  , 
 ,L $(#$(-1!& ,  , C=	 ,
  , tCy! , 4S>* ,  ,  , 
 ,  ,r$   r   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)"PairwiseEmbeddingDistanceEvalChaini  zUse embedding distances to score semantic difference between two predictions.

Examples:
>>> chain = PairwiseEmbeddingDistanceEvalChain()
>>> result = chain.evaluate_string_pairs(prediction="Hello", prediction_b="Hi")
>>> print(result)
{'score': 0.5}
r   c                 
    SS/$ )r   r   prediction_br#   rU   s    r   r   -PairwiseEmbeddingDistanceEvalChain.input_keys  s     n--r$   c                 6    SU R                   R                   S3$ )Npairwise_embedding_r   r   rU   s    r   r   2PairwiseEmbeddingDistanceEvalChain.evaluation_name  s    $T%9%9%?%?$@	JJr$   Nr   r   c                     U R                   R                  US   US   /5      n[        5       (       a  [        5       nUR	                  U5      nU R                  U5      nSU0$ )zCompute the score for two predictions.

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

Returns:
    Dict[str, Any]: The computed score.
r   r   rT   r   r   s         r   r   (PairwiseEmbeddingDistanceEvalChain._call  sf     //11|$~&
 >>Bhhw'G##G,r$   c                    #    U R                   R                  US   US   /5      I Sh  vN n[        5       (       a  [        5       nUR	                  U5      nU R                  U5      nSU0$  NC7f)zAsynchronously compute the score for two predictions.

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

Returns:
    Dict[str, Any]: The computed score.
r   r   NrT   r   r   s         r   r   )PairwiseEmbeddingDistanceEvalChain._acall  ss      88|$~&
 
 >>Bhhw'G##G,
r   F)r   r   r   r   r   r   r   r   r   r   r   c                <    U " XS.UUUUS9nU R                  U5      $ )a   Evaluate the embedding distance between two predictions.

Args:
    prediction (str): The output string from the first model.
    prediction_b (str): The output string from the second model.
    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
    **kwargs (Any): Additional keyword arguments.

Returns:
    dict: A dictionary containing:
        - score: The embedding distance between the two
            predictions.
r   r   r   r   	rV   r   r   r   r   r   r   r   rY   s	            r   _evaluate_string_pairs9PairwiseEmbeddingDistanceEvalChain._evaluate_string_pairs  s3    4 ",K-
 ##F++r$   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)a7  Asynchronously evaluate the embedding distance

between two predictions.

Args:
    prediction (str): The output string from the first model.
    prediction_b (str): The output string from the second model.
    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 (Any): Additional keyword arguments.

Returns:
    dict: A dictionary containing:
        - score: The embedding distance between the two
            predictions.
r   r   Nr   r   s	            r   _aevaluate_string_pairs:PairwiseEmbeddingDistanceEvalChain._aevaluate_string_pairs7  sI     8 zz",K- " 
 
 ##F++
r   r#   rq   )r5   r6   r7   r8   r9   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r?   r#   r$   r   r   r     s    .DI . . K K K =A S#X  89  
c3h	 < BF S#X  =>  
c3h	 @  $$(-1!&!, !, 	!,
 !, tCy!!, 4S>*!, !, !, 
!,P  $$(-1!&#, #, 	#,
 #, tCy!#, 4S>*#, #, #, 
#, #,r$   r   )*r9   	functoolsloggingenumr   	importlibr   typingr   r   langchain_core.callbacksr    langchain_core.callbacks.managerr   r	   langchain_core.embeddingsr
   langchain_core.utilsr   pydanticr   r   langchain.chains.baser   langchain.evaluation.schemar   r   langchain.schemar   r   	getLoggerr5   r!   	lru_cacher   r%   r,   r   r.   rA   r   r   r#   r$   r   <module>r      s    F       . 1 ) & ' P $s  
		8	$ Q
d 
  
J 0T $M5 M`V,!= V,rV, V,r$   