a
    `g|                     @  s   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rTd dlZeeZeeee  ee ef Zdddddd	Zd
dddZG dd deddZG dd dZdS )    )annotationsN)TYPE_CHECKINGAnyCallableListLiteralOptionalSequenceUnion)	TypedDictMatrix
np.ndarray)XYreturnc                 C  sv  ddl }t| dks t|dkr*|g S || } ||}| jd |jd krltd| j d|j dz`ddl}|j| |jd} |j||jd}d|j| |dd	 }t|t	r||gW S ||W S  t
yp   td
 |jj| dd}|jj|dd}|jddd* || |j||| }W d   n1 sF0    Y  d|||||B < | Y S 0 dS )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+   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langsmith/_internal/_embedding_distance.pycosine_similarity   s:    




:r-   z4Callable[[Sequence[str]], Sequence[Sequence[float]]])r   c                    sD   zddl m  W n ty*   tdY n0 dd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]]).zSequence[str]zSequence[Sequence[float]])textsr   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>O       z<_get_openai_encoder.<locals>.encode_text.<locals>.<listcomp>)
embeddingscreatelistdata)r/   clientresponseZOpenAIClientr+   r,   encode_textJ   s
    z(_get_openai_encoder.<locals>.encode_text)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?   T   s   
r?   F)totalc                   @  s   e Zd ZdddddZddddd	d
ZddddddZeddddddZeddddddZeddddddZ	eddddddZ
eddddddZdS )EmbeddingDistanceNzOptional[EmbeddingConfig])configc                 C  s.   |pi }| dpd| _| dp&t | _d S )Nr   r   r@   )getdistancer>   r@   )selfrG   r+   r+   r,   __init__Z   s    zEmbeddingDistance.__init__strr   )
prediction	referencer   c                 C  sX   zdd l }W n ty&   tdY n0 | ||g}||}| |d |d  S )Nr   zWThe EmbeddingDistance class requires NumPy. Please install it with `pip install numpy`.r   )r   r   r@   r   _compute_distanceitem)rJ   rM   rN   r)   r6   Zvectorr+   r+   r,   evaluateb   s    

zEmbeddingDistance.evaluater   znp.floating)abr   c                 C  s   | j dkr| ||S | j dkr,| ||S | j dkrB| ||S | j dkrX| ||S | j dkrn| ||S td| j  d S )Nr   Z	euclideanZ	manhattanZ	chebyshevZhammingzInvalid distance metric: )rI   _cosine_distance_euclidean_distance_manhattan_distance_chebyshev_distance_hamming_distancer   )rJ   rR   rS   r+   r+   r,   rO   r   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-   rR   rS   r+   r+   r,   rT      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#   rY   r+   r+   r,   rU      s    z%EmbeddingDistance._euclidean_distancec                 C  s   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absrY   r+   r+   r,   rV      s    z%EmbeddingDistance._manhattan_distancec                 C  s   t t | | 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.
        )r)   maxr[   rY   r+   r+   r,   rW      s    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)   meanrY   r+   r+   r,   rX      s    z#EmbeddingDistance._hamming_distance)N)rA   rB   rC   rK   rQ   rO   staticmethodrT   rU   rV   rW   rX   r+   r+   r+   r,   rF   Y   s    rF   )
__future__r   loggingtypingr   r   r   r   r   r   r	   r
   Ztyping_extensionsr   r   r)   	getLoggerrA   r    r   r   r-   r>   r?   rF   r+   r+   r+   r,   <module>   s   (
%