o
    Zh                     @  s   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eZeeee  ee ef ZdddZdddZG dd deddZG dd dZdS )    )annotationsN)Sequence)TYPE_CHECKINGAnyCallableLiteralOptionalUnion)	TypedDictXMatrixYreturn
np.ndarrayc                 C  sn  ddl }t| dkst|dkr|g S || } ||}| jd |jd kr6td| j d|j dz0ddl}|j| |jd} |j||jd}d|j| |dd	 }t|t	ra||gW S ||W S  t
y   td
 |jj| dd}|jj|dd}|jddd || |j||| }W d   n1 sw   Y  d|||||B < | Y S 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 .)Zdtypecosine)metriczUnable to import simsimd, defaulting to NumPy implementation. If you want to use simsimd please install with `pip install simsimd`.)Zaxisignore)divideinvalidg        )numpylenarrayshape
ValueErrorZsimsimdZfloat32Zcdist
isinstancefloatImportErrorloggerdebuglinalgnormZerrstatedotTouterisnanisinf)r   r   npZsimdZZX_normZY_normZ
similarity r*   ^/var/www/html/lang_env/lib/python3.10/site-packages/langsmith/_internal/_embedding_distance.pycosine_similarity   s>   




r,   4Callable[[Sequence[str]], Sequence[Sequence[float]]]c                    s:   zddl m  W n ty   tdw d fd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                   s*     }|j jt| dd}dd |jD S )Nztext-embedding-3-small)inputmodelc                 S  s   g | ]}|j qS r*   )Z	embedding).0dr*   r*   r+   
<listcomp>N   s    z<_get_openai_encoder.<locals>.encode_text.<locals>.<listcomp>)
embeddingscreatelistdata)r/   clientresponseZOpenAIClientr*   r+   encode_textI   s
   z(_get_openai_encoder.<locals>.encode_textN)r/   r0   r   r1   )Zopenair.   r   )r>   r*   r=   r+   _get_openai_encoder>   s   r?   c                   @  s   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__r*   r*   r*   r+   r@   S   s   
 r@   F)totalc                   @  st   e Zd Z	ddddZdddZd ddZed!ddZed ddZed ddZ	ed ddZ
ed ddZdS )"EmbeddingDistanceNconfigOptional[EmbeddingConfig]c                 C  s.   |pi }| dp
d| _| dpt | _d S )Nr   r   rA   )getdistancer?   rA   )selfrH   r*   r*   r+   __init__Y   s   zEmbeddingDistance.__init__
predictionstr	referencer   r   c                 C  sT   zdd l }W n ty   tdw | ||g}||}| |d |d  S )Nr   zWThe EmbeddingDistance class requires NumPy. Please install it with `pip install numpy`.r   )r   r   rA   r   _compute_distanceitem)rL   rN   rP   r(   r7   Zvectorr*   r*   r+   evaluatea   s   
zEmbeddingDistance.evaluatear   bnp.floatingc                 C  s~   | j dkr| ||S | j dkr| ||S | j dkr!| ||S | j dkr,| ||S | j dkr7| ||S td| j  )Nr   Z	euclideanZ	manhattanZ	chebyshevZhammingzInvalid distance metric: )rK   _cosine_distance_euclidean_distance_manhattan_distance_chebyshev_distance_hamming_distancer   )rL   rT   rU   r*   r*   r+   rQ   q   s   




z#EmbeddingDistance._compute_distancec                 C  s   dt | g|g 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      ?)r,   rT   rU   r*   r*   r+   rW      s   z"EmbeddingDistance._cosine_distancec                 C  s   t j| | 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(   r!   r"   r\   r*   r*   r+   rX      s   z%EmbeddingDistance._euclidean_distancec                 C     t t | | 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.
        )r(   sumabsr\   r*   r*   r+   rY         z%EmbeddingDistance._manhattan_distancec                 C  r]   )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_   r\   r*   r*   r+   rZ      r`   z%EmbeddingDistance._chebyshev_distancec                 C  s   t | |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.
        )r(   meanr\   r*   r*   r+   r[      s   z#EmbeddingDistance._hamming_distance)N)rH   rI   )rN   rO   rP   rO   r   r   )rT   r   rU   r   r   rV   )rT   r   rU   r   r   r   )rB   rC   rD   rM   rS   rQ   staticmethodrW   rX   rY   rZ   r[   r*   r*   r*   r+   rG   X   s    

rG   )r   r   r   r   r   r   )r   r-   )
__future__r   loggingcollections.abcr   typingr   r   r   r   r   r	   typing_extensionsr
   r   r(   	getLoggerrB   r   r9   r   r   r,   r?   r@   rG   r*   r*   r*   r+   <module>   s     	


%