
    $h                        S SK Jr  S SKrS SKJr  S SKJrJrJrJ	r	J
r
Jr  S SKJr  \(       a  S SKr\R                   " \5      r\\\\      \\   \4   rSS jrSS jr " S S	\S
S9r " S S5      rg)    )annotationsN)Sequence)TYPE_CHECKINGAnyCallableLiteralOptionalUnion)	TypedDictc                   SSK n[        U 5      S:X  d  [        U5      S:X  a  UR                  " / 5      $ UR                  " U 5      n UR                  " U5      nU R                  S   UR                  S   :w  a&  [	        SU R                   SUR                   S35      e SSKnUR                  " XR                  S9n UR                  " XR                  S9nSUR                  XSS	9-
  n[        U[        5      (       a  UR                  " U/5      $ UR                  " U5      $ ! [         a    [        R                  S
5        UR                  R                  U SS9nUR                  R                  USS9nUR                  " SSS9   UR                   " XR"                  5      UR$                  " XV5      -  nSSS5        O! , (       d  f       O= fSWUR&                  " U5      UR(                  " U5      -  '   Us $ f = f)z<Row-wise cosine similarity between two equal-width matrices.r   N   z;Number of columns in X and Y must be the same. X has shape z and Y has shape .)dtypecosine)metriczUnable to import simsimd, defaulting to NumPy implementation. If you want to use simsimd please install with `pip install simsimd`.)axisignore)divideinvalidg        )numpylenarrayshape
ValueErrorsimsimdfloat32cdist
isinstancefloatImportErrorloggerdebuglinalgnormerrstatedotTouterisnanisinf)XYnpsimdZX_normY_norm
similaritys           _/var/www/html/shao/venv/lib/python3.13/site-packages/langsmith/_internal/_embedding_distance.pycosine_similarityr4      s   
1v{c!fkxx|
A
AwwqzQWWQZI!'' S wwiq*
 	
HHQjj)HHQjj)

1
11a88QC= xx{ H	
 **[[(;33"((6*BBJ <;;BE
288J'"((:*>>?s2    A2D% D% %A$H 	0G9	H 
G	1H ?H c                 X   ^  SSK Jm  SU4S jjn U $ ! [         a    [        S5      ef = f)zGet the OpenAI GPT-3 encoder.r   )ClientzTHe default encoder for the EmbeddingDistance class uses the OpenAI API. Please either install the openai library with `pip install openai` or provide a custom encoder function (Callable[[str], Sequence[float]]).c                   > T" 5       nUR                   R                  [        U 5      SS9nUR                   Vs/ sH  o3R                  PM     sn$ s  snf )Nztext-embedding-3-small)inputmodel)
embeddingscreatelistdata	embedding)textsclientresponsedOpenAIClients       r3   encode_text(_get_openai_encoder.<locals>.encode_textI   sO    $$++u+%= , 
 &.]]3]]333s   A)r?   zSequence[str]returnzSequence[Sequence[float]])openair6   r    )rD   rC   s    @r3   _get_openai_encoderrH   >   s<    
14   
T
 	

s    )c                  *    \ rS rSr% S\S'   S\S'   Srg)EmbeddingConfigS   z0Callable[[list[str]], Sequence[Sequence[float]]]encoderzCLiteral['cosine', 'euclidean', 'manhattan', 'chebyshev', 'hamming']r    N)__name__
__module____qualname____firstlineno____annotations____static_attributes__rM       r3   rJ   rJ   S   s    ==OOrT   rJ   F)totalc                      \ rS rSr S SS jjr      SS jrSS jr\SS j5       r\SS j5       r	\SS j5       r
\SS	 j5       r\SS
 j5       rSrg)EmbeddingDistanceX   Nc                    U=(       d    0 nUR                  S5      =(       d    SU l        UR                  S5      =(       d
    [        5       U l        g )Nr   r   rL   )getdistancerH   rL   )selfconfigs     r3   __init__EmbeddingDistance.__init__Y   s;     2

8,8zz),E0C0ErT   c                     SS K nU R                  X/5      nUR                  " U5      nU R	                  US   US   5      R                  5       $ ! [         a    [        S5      ef = f)Nr   zWThe EmbeddingDistance class requires NumPy. Please install it with `pip install numpy`.r   )r   r    rL   r   _compute_distanceitem)r\   
prediction	referencer-   r:   vectors         r3   evaluateEmbeddingDistance.evaluatea   sr    
	 \\:"9:
*%%%fQi;@@BB  	' 	s   A A&c                |   U R                   S:X  a  U R                  X5      $ U R                   S:X  a  U R                  X5      $ U R                   S:X  a  U R                  X5      $ U R                   S:X  a  U R	                  X5      $ U R                   S:X  a  U R                  X5      $ [        SU R                    35      e)Nr   	euclidean	manhattan	chebyshevhammingzInvalid distance metric: )r[   _cosine_distance_euclidean_distance_manhattan_distance_chebyshev_distance_hamming_distancer   )r\   abs      r3   ra   #EmbeddingDistance._compute_distanceq   s    ==H$((..]]k)++A11]]k)++A11]]k)++A11]]i'))!//8HIIrT   c                $    S[        U /U/5      -
  $ )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.
g      ?)r4   rr   rs   s     r3   rm   "EmbeddingDistance._cosine_distance   s     &sQC000rT   c                D    [         R                  R                  X-
  5      $ )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-   r#   r$   rv   s     r3   rn   %EmbeddingDistance._euclidean_distance   s     yy~~ae$$rT   c                Z    [         R                  " [         R                  " X-
  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.
)r-   sumabsrv   s     r3   ro   %EmbeddingDistance._manhattan_distance        vvbffQUm$$rT   c                Z    [         R                  " [         R                  " X-
  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.
)r-   maxr|   rv   s     r3   rp   %EmbeddingDistance._chebyshev_distance   r~   rT   c                2    [         R                  " X:g  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.
)r-   meanrv   s     r3   rq   #EmbeddingDistance._hamming_distance   s     wwqvrT   )r[   rL   )N)r]   zOptional[EmbeddingConfig])rc   strrd   r   rF   r   )rr   
np.ndarrayrs   r   rF   znp.floating)rr   r   rs   r   rF   r   )rN   rO   rP   rQ   r^   rf   ra   staticmethodrm   rn   ro   rp   rq   rS   rM   rT   r3   rW   rW   X   s     -1F)FCC C 
	C J 
1 
1 
% 
% 
% 
% 
% 
% 
 
rT   rW   )r+   Matrixr,   r   rF   r   )rF   z4Callable[[Sequence[str]], Sequence[Sequence[float]]])
__future__r   loggingcollections.abcr   typingr   r   r   r   r	   r
   typing_extensionsr   r   r-   	getLoggerrN   r!   r<   r   r   r4   rH   rJ   rW   rM   rT   r3   <module>r      sz    "  $  ( 
		8	$	tDK $s)S0	1"J*Piu P
f frT   