o
    +i                     @  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 .)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
similarity r4   d/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/langsmith/_internal/_embedding_distance.pycosine_similarity   s>   




r6   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 r4   )	embedding).0dr4   r4   r5   
<listcomp>N   s    z<_get_openai_encoder.<locals>.encode_text.<locals>.<listcomp>)
embeddingscreatelistdata)r9   clientresponseOpenAIClientr4   r5   encode_textI   s
   z(_get_openai_encoder.<locals>.encode_textN)r9   r:   r   r;   )openair8   r#   )rJ   r4   rH   r5   _get_openai_encoder>   s   rL   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__r4   r4   r4   r5   rM   S   s   
 rM   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   rN   )getdistancerL   rN   )selfrU   r4   r4   r5   __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#   rN   r   _compute_distanceitem)rY   r[   r]   r.   rB   vectorr4   r4   r5   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   	euclidean	manhattan	chebyshevhammingzInvalid distance metric: )rX   _cosine_distance_euclidean_distance_manhattan_distance_chebyshev_distance_hamming_distancer   )rY   rb   rc   r4   r4   r5   r^   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      ?)r6   rb   rc   r4   r4   r5   ri      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'   rn   r4   r4   r5   rj      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absrn   r4   r4   r5   rk         z%EmbeddingDistance._manhattan_distancec                 C  ro   )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.   maxrq   rn   r4   r4   r5   rl      rr   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.   meanrn   r4   r4   r5   rm      s   z#EmbeddingDistance._hamming_distance)N)rU   rV   )r[   r\   r]   r\   r   r"   )rb   r   rc   r   r   rd   )rb   r   rc   r   r   r   )rO   rP   rQ   rZ   ra   r^   staticmethodri   rj   rk   rl   rm   r4   r4   r4   r5   rT   X   s    

rT   )r   r   r   r   r   r   )r   r7   )
__future__r   loggingcollections.abcr   typingr   r   r   r   r   r	   typing_extensionsr
   r   r.   	getLoggerrO   r$   rD   r"   r   r6   rL   rM   rT   r4   r4   r4   r5   <module>   s     	


%