
    Ng|                        d dl mZ d dlZd dlmZmZ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)TYPE_CHECKINGAnyCallableListLiteralOptionalSequence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           c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langsmith/_internal/_embedding_distance.pycosine_similarityr8      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     r7   
<listcomp>z<_get_openai_encoder.<locals>.encode_text.<locals>.<listcomp>O   s    333333    )
embeddingscreatelistdata)r<   clientresponseOpenAIClients      r7   encode_textz(_get_openai_encoder.<locals>.encode_textJ   sO    $++u++%= , 
 
 43X]3333rH   )r<   r=   r   r>   )openair;   r&   )rP   rO   s    @r7   _get_openai_encoderrR   ?   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__rC   rH   r7   rT   rT   T   s*         ====OOOOOOrH   rT   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   rU   )getdistancerR   rU   )selfr]   s     r7   __init__zEmbeddingDistance.__init__Z   sH     2

8,,8zz),,E0C0E0ErH   
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&   rU   r   _compute_distanceitem)rb   rd   rf   r1   rI   vectors         r7   evaluatezEmbeddingDistance.evaluateb   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: )ra   _cosine_distance_euclidean_distance_manhattan_distance_chebyshev_distance_hamming_distancer    )rb   rl   rm   s      r7   rh   z#EmbeddingDistance._compute_distancer   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IrH   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      ?)r8   rl   rm   s     r7   rt   z"EmbeddingDistance._cosine_distance   s     &sQC0000rH   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.
        )r1   r)   r*   rz   s     r7   ru   z%EmbeddingDistance._euclidean_distance   s     y~~a!e$$$rH   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.
        )r1   sumabsrz   s     r7   rv   z%EmbeddingDistance._manhattan_distance         vbfQUmm$$$rH   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.
        )r1   maxr~   rz   s     r7   rw   z%EmbeddingDistance._chebyshev_distance   r   rH   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.
        )r1   meanrz   s     r7   rx   z#EmbeddingDistance._hamming_distance   s     wqAvrH   )N)r]   r^   )rd   re   rf   re   r   r%   )rl   r   rm   r   r   rn   )rl   r   rm   r   r   r   )rV   rW   rX   rc   rk   rh   staticmethodrt   ru   rv   rw   rx   rC   rH   r7   r\   r\   Y   s        -1F F F F FC C C C J J J J 
1 
1 
1 \
1 
% 
% 
% \
% 
% 
% 
% \
% 
% 
% 
% \
% 
 
 
 \
 
 
rH   r\   )r   r   r   r   r   r   )r   r9   )
__future__r   loggingtypingr   r   r   r   r   r	   r
   r   typing_extensionsr   r   r1   	getLoggerrV   r'   r%   r   r8   rR   rT   r\   rC   rH   r7   <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rH   