
    	h                       d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ erd dlZ ej        e          Zeeee                  ee         ef         ZddZddZ G d ded          Z G d d          ZdS )    )annotationsN)Sequence)TYPE_CHECKINGAnyCallableLiteralOptionalUnion)	TypedDictXMatrixYreturn
np.ndarrayc                   ddl }t          |           dk    st          |          dk    r |j        g           S  |j        |           }  |j        |          }| j        d         |j        d         k    r t	          d| j         d|j         d          	 ddl} |j        | |j                  }  |j        ||j                  }d|                    | |d	          z
  }t          |t                    r |j        |g          S  |j        |          S # t          $ r t                              d
           |j                            | d          }|j                            |d          } |j        dd          5   |j        | |j                   |j        ||          z  }ddd           n# 1 swxY w Y   d| |j        |           |j        |          z  <   |cY S w xY w)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)r   r   npsimdZX_normY_norm
similaritys           t/var/www/html/web-builder-api.evdpl.com/venv/lib/python3.11/site-packages/langsmith/_internal/_embedding_distance.pycosine_similarityr7      sw   
1vv{{c!ffkkrx||AAwqzQWQZ*!' * * w* * *
 
 	
BHQbj)))BHQbj)))

1a
111a 	!28QC== rx{{   H	
 	
 	
 ****R[(;;; 	C 	C13("(66*B*BBJ	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	CBE
828J''("(:*>*>>?s>   A2D D A.G2	)F>2G2>G	G2G	)G21G24Callable[[Sequence[str]], Sequence[Sequence[float]]]c                 ^    	 ddl m n# t          $ r t          d          w xY wd	fd} | S )
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]]).textsSequence[str]r   Sequence[Sequence[float]]c                                 }|j                             t          |           d          }d |j        D             S )Nztext-embedding-3-small)inputmodelc                    g | ]	}|j         
S  )	embedding).0ds     r6   
<listcomp>z<_get_openai_encoder.<locals>.encode_text.<locals>.<listcomp>N   s    333333    )
embeddingscreatelistdata)r;   clientresponseOpenAIClients      r6   encode_textz(_get_openai_encoder.<locals>.encode_textI   sO    $++u++%= , 
 
 43X]3333rG   )r;   r<   r   r=   )openair:   r%   )rO   rN   s    @r6   _get_openai_encoderrQ   >   sv    
1111111 
 
 
T
 
 	

4 4 4 4 4 4 s   
 $c                  $    e Zd ZU ded<   ded<   dS )EmbeddingConfigz0Callable[[list[str]], Sequence[Sequence[float]]]encoderzCLiteral['cosine', 'euclidean', 'manhattan', 'chebyshev', 'hamming']r   N)__name__
__module____qualname____annotations__rB   rG   r6   rS   rS   S   s*         ====OOOOOOrG   rS   F)totalc                      e Zd Z	 dddZdd
ZddZedd            Zedd            Zedd            Z	edd            Z
edd            ZdS )EmbeddingDistanceNconfigOptional[EmbeddingConfig]c                    |pi }|                     d          pd| _        |                     d          pt                      | _        d S )Nr   r   rT   )getdistancerQ   rT   )selfr\   s     r6   __init__zEmbeddingDistance.__init__Y   sH     2

8,,8zz),,E0C0E0ErG   
predictionstr	referencer   r$   c                    	 dd l }n# t          $ r t          d          w xY w|                     ||g          } |j        |          }|                     |d         |d                                                   S )Nr   zWThe EmbeddingDistance class requires NumPy. Please install it with `pip install numpy`.r   )r   r%   rT   r   _compute_distanceitem)ra   rc   re   r0   rH   vectors         r6   evaluatezEmbeddingDistance.evaluatea   s    
	 	 	 	'  	
 \\:y"9::
*%%%%fQi;;@@BBBs    !ar   bnp.floatingc                z   | j         dk    r|                     ||          S | j         dk    r|                     ||          S | j         dk    r|                     ||          S | j         dk    r|                     ||          S | j         dk    r|                     ||          S t          d| j                    )Nr   	euclidean	manhattan	chebyshevhammingzInvalid distance metric: )r`   _cosine_distance_euclidean_distance_manhattan_distance_chebyshev_distance_hamming_distancer   )ra   rk   rl   s      r6   rg   z#EmbeddingDistance._compute_distanceq   s    =H$$((A...]k))++Aq111]k))++Aq111]k))++Aq111]i''))!Q///HHHIIIrG   c                ,    dt          | g|g          z
  S )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      ?)r7   rk   rl   s     r6   rs   z"EmbeddingDistance._cosine_distance   s     &sQC0000rG   c                F    t           j                            | |z
            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.
        )r0   r(   r)   ry   s     r6   rt   z%EmbeddingDistance._euclidean_distance   s     y~~a!e$$$rG   c                T    t          j        t          j        | |z
                      S )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.
        )r0   sumabsry   s     r6   ru   z%EmbeddingDistance._manhattan_distance         vbfQUmm$$$rG   c                T    t          j        t          j        | |z
                      S )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.
        )r0   maxr}   ry   s     r6   rv   z%EmbeddingDistance._chebyshev_distance   r~   rG   c                2    t          j        | |k              S )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.
        )r0   meanry   s     r6   rw   z#EmbeddingDistance._hamming_distance   s     wqAvrG   )N)r\   r]   )rc   rd   re   rd   r   r$   )rk   r   rl   r   r   rm   )rk   r   rl   r   r   r   )rU   rV   rW   rb   rj   rg   staticmethodrs   rt   ru   rv   rw   rB   rG   r6   r[   r[   X   s        -1F F F F FC C C C J J J J 
1 
1 
1 \
1 
% 
% 
% \
% 
% 
% 
% \
% 
% 
% 
% \
% 
 
 
 \
 
 
rG   r[   )r   r   r   r   r   r   )r   r8   )
__future__r   loggingcollections.abcr   typingr   r   r   r   r	   r
   typing_extensionsr   r   r0   	getLoggerrU   r&   rJ   r$   r   r7   rQ   rS   r[   rB   rG   r6   <module>r      s{   " " " " " "  $ $ $ $ $ $                ( ' ' ' ' '  
	8	$	$	tDK $s)S0	1" " " "J   *P P P P Piu P P P P
f f f f f f f f f frG   