a
    •˜ëjÈR  ã                   @   s>  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ edœdd„Ze  e!¡Z"ej#dd�e$dœdd„ƒZ%edœdd„Z&G dd„ de'eƒZ(G dd„ deƒZ)G dd„ de)eƒZ*G dd„ de)eƒZ+dS ) z@A chain for comparing the output of two models using embeddings.é    N)ÚEnum)Úutil)ÚAnyÚOptional)Ú	Callbacks)ÚAsyncCallbackManagerForChainRunÚCallbackManagerForChainRun)Ú
Embeddings)Úpre_init)Ú
ConfigDictÚField)Úoverride)ÚChain)ÚPairwiseStringEvaluatorÚStringEvaluator©ÚRUN_KEY©Úreturnc               
   C   sD   zdd l } W n2 ty> } zd}t|ƒ|‚W Y d }~n
d }~0 0 | S )Nr   z@Could not import numpy, please install with `pip install numpy`.)ÚnumpyÚImportError)ÚnpÚeÚmsg© r   úk/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/evaluation/embedding_distance/base.pyÚ_import_numpy   s     r   é   )Úmaxsizec                   C   s    t t d¡ƒrdS t d¡ dS )Nr   Ta  NumPy not found in the current Python environment. langchain will use a pure Python implementation for embedding distance operations, which may significantly impact performance, especially for large datasets. For optimal speed and efficiency, consider installing NumPy: pip install numpyF)Úboolr   Ú	find_specÚloggerÚwarningr   r   r   r   Ú_check_numpy$   s    ÿr#   c                  C   sn   zddl m}  W nV tyf   zddlm}  W n2 ty` } zd}t|ƒ|‚W Y d}~n
d}~0 0 Y n0 | ƒ S )zbCreate an Embeddings object.

    Returns:
        Embeddings: The created Embeddings object.
    r   ©ÚOpenAIEmbeddingsútCould not import OpenAIEmbeddings. Please install the OpenAIEmbeddings package using `pip install langchain-openai`.N)Úlangchain_openair%   r   Ú%langchain_community.embeddings.openai)r%   r   r   r   r   r   Ú_embedding_factory2   s    	ÿ&r)   c                   @   s$   e Zd ZdZdZdZdZdZdZdS )ÚEmbeddingDistancea  Embedding Distance Metric.

    Attributes:
        COSINE: Cosine distance metric.
        EUCLIDEAN: Euclidean distance metric.
        MANHATTAN: Manhattan distance metric.
        CHEBYSHEV: Chebyshev distance metric.
        HAMMING: Hamming distance metric.
    ÚcosineÚ	euclideanZ	manhattanÚ	chebyshevÚhammingN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚCOSINEÚ	EUCLIDEANÚ	MANHATTANÚ	CHEBYSHEVÚHAMMINGr   r   r   r   r*   K   s   
r*   c                   @   s  e Zd ZU dZeed�Zeed< ee	j
d�Ze	ed< eeeef eeef dœdd„ƒZed	d
�Zeee dœdd„ƒZeedœdd„Ze	edœdd„Zeeeedœdd„ƒZeeeedœdd„ƒZeeeedœdd„ƒZeeeedœdd„ƒZeeeedœdd„ƒZeedœd d!„Zd"S )#Ú_EmbeddingDistanceChainMixina0  Shared functionality for embedding distance evaluators.

    Attributes:
        embeddings (Embeddings): The embedding objects to vectorize the outputs.
        distance_metric (EmbeddingDistance): The distance metric to use
                                            for comparing the embeddings.
    )Údefault_factoryÚ
embeddings)ÚdefaultÚdistance_metric)Úvaluesr   c              
   C   sÌ   |  d¡}g }zddlm} | |¡ W n ty:   Y n0 zddlm} | |¡ W n tyh   Y n0 |szd}t|ƒ‚t|t|ƒƒrÈzddl}W n2 tyÆ } zd}t|ƒ|‚W Y d}~n
d}~0 0 |S )zÇValidate that the TikTok library is installed.

        Args:
            values (Dict[str, Any]): The values to validate.

        Returns:
            Dict[str, Any]: The validated values.
        r:   r   r$   r&   Nz×The tiktoken library is required to use the default OpenAI embeddings with embedding distance evaluators. Please either manually select a different Embeddings object or install tiktoken using `pip install tiktoken`.)	Úgetr'   r%   Úappendr   r(   Ú
isinstanceÚtupleÚtiktoken)Úclsr=   r:   Ztypes_r%   r   rB   r   r   r   r   Ú_validate_tiktoken_installedi   s0    

ÿÿ z9_EmbeddingDistanceChainMixin._validate_tiktoken_installedT)Zarbitrary_types_allowedr   c                 C   s   dgS )zgReturn the output keys of the chain.

        Returns:
            List[str]: The output keys.
        Úscorer   ©Úselfr   r   r   Úoutput_keys�   s    z(_EmbeddingDistanceChainMixin.output_keys)Úresultr   c                 C   s$   d|d i}t |v r |t  |t < |S )NrE   r   )rG   rI   Úparsedr   r   r   Ú_prepare_output¦   s    z,_EmbeddingDistanceChainMixin._prepare_output)Úmetricr   c              
   C   sR   t j| jt j| jt j| jt j| jt j	| j
i}||v r<|| S d|› �}t|ƒ‚dS )z»Get the metric function for the given metric name.

        Args:
            metric (EmbeddingDistance): The metric name.

        Returns:
            Any: The metric function.
        zInvalid metric: N)r*   r3   Ú_cosine_distancer4   Ú_euclidean_distancer5   Ú_manhattan_distancer6   Ú_chebyshev_distancer7   Ú_hamming_distanceÚ
ValueError)rG   rL   Zmetricsr   r   r   r   Ú_get_metric¬   s    
û
z(_EmbeddingDistanceChainMixin._get_metric)ÚaÚbr   c           
      C   s¤  zddl m} d|| |ƒ W S  t�yž   z$ddlm} ||  ¡ | ¡ ƒW  Y S  t�y˜   tƒ rÐtƒ }|  ¡ }| ¡ }| ||¡}|j	 
|¡}|j	 
|¡}	|dks°|	dkr¸Y Y dS d|||	    Y  Y S t| dƒrÞ| n| g}t|dƒrò|n|g}t| dƒ�r|  ¡ }t|dƒ�r | ¡ }tdd	„ t||ƒD ƒƒ}td
d	„ |D ƒƒd }tdd	„ |D ƒƒd }	|dk�sx|	dk�r€Y Y dS d|||	    Y  Y S 0 Y n0 dS )zäCompute 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.
        r   )Ú_cosine_similarityg      ð?)r+   g        Ú__len__Úflattenc                 s   s   | ]\}}|| V  qd S ©Nr   ©Ú.0ÚxÚyr   r   r   Ú	<genexpr>ê   ó    z@_EmbeddingDistanceChainMixin._cosine_distance.<locals>.<genexpr>c                 s   s   | ]}|| V  qd S rY   r   ©r[   r\   r   r   r   r^   ë   r_   ç      à?c                 s   s   | ]}|| V  qd S rY   r   r`   r   r   r   r^   ì   r_   N)Z!langchain_core.vectorstores.utilsrV   r   Úscipy.spatial.distancer+   rX   r#   r   ÚdotÚlinalgÚnormÚhasattrÚsumÚzip)
rT   rU   rV   r+   r   Za_flatZb_flatZdot_productZnorm_aZnorm_br   r   r   rM   Á   s<    z-_EmbeddingDistanceChainMixin._cosine_distancec                 C   sv   z ddl m} ||  ¡ | ¡ ƒW S  typ   tƒ rPddl}|j | | ¡ Y S tdd„ t	| |ƒD ƒƒd  Y S 0 dS )zëCompute 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,   Nc                 s   s"   | ]\}}|| ||  V  qd S rY   r   rZ   r   r   r   r^     r_   zC_EmbeddingDistanceChainMixin._euclidean_distance.<locals>.<genexpr>ra   )
rb   r,   rX   r   r#   r   rd   re   rg   rh   )rT   rU   r,   r   r   r   r   rN   ñ   s    z0_EmbeddingDistanceChainMixin._euclidean_distancec              	   C   st   z ddl m} ||  ¡ | ¡ ƒW S  tyn   tƒ rRtƒ }| | | | ¡¡ Y S tdd„ t| |ƒD ƒƒ Y S 0 dS )zëCompute 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   )Ú	cityblockc                 s   s   | ]\}}t || ƒV  qd S rY   ©ÚabsrZ   r   r   r   r^     r_   zC_EmbeddingDistanceChainMixin._manhattan_distance.<locals>.<genexpr>N)	rb   ri   rX   r   r#   r   rg   rk   rh   )rT   rU   ri   r   r   r   r   rO     s    z0_EmbeddingDistanceChainMixin._manhattan_distancec              	   C   st   z ddl m} ||  ¡ | ¡ ƒW S  tyn   tƒ rRtƒ }| | | | ¡¡ Y S tdd„ t| |ƒD ƒƒ Y S 0 dS )zëCompute 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   )r-   c                 s   s   | ]\}}t || ƒV  qd S rY   rj   rZ   r   r   r   r^   2  r_   zC_EmbeddingDistanceChainMixin._chebyshev_distance.<locals>.<genexpr>N)	rb   r-   rX   r   r#   r   Úmaxrk   rh   )rT   rU   r-   r   r   r   r   rP     s    z0_EmbeddingDistanceChainMixin._chebyshev_distancec                 C   sv   z ddl m} ||  ¡ | ¡ ƒW S  typ   tƒ rLtƒ }| | |k¡ Y S tdd„ t| |ƒD ƒƒt	| ƒ  Y S 0 dS )zçCompute 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   )r.   c                 s   s   | ]\}}||krd V  qdS )r   Nr   rZ   r   r   r   r^   H  r_   zA_EmbeddingDistanceChainMixin._hamming_distance.<locals>.<genexpr>N)
rb   r.   rX   r   r#   r   Úmeanrg   rh   Úlen)rT   rU   r.   r   r   r   r   rQ   4  s    z._EmbeddingDistanceChainMixin._hamming_distance)Úvectorsr   c                 C   sb   |   | j¡}tƒ rHt|tƒ jƒrH||d  dd¡|d  dd¡ƒ ¡ }n||d |d ƒ}t|ƒS )zµCompute the score based on the distance metric.

        Args:
            vectors (np.ndarray): The input vectors.

        Returns:
            float: The computed score.
        r   r   éÿÿÿÿ)	rS   r<   r#   r@   r   ZndarrayZreshapeÚitemÚfloat)rG   ro   rL   rE   r   r   r   Ú_compute_scoreJ  s
    	(z+_EmbeddingDistanceChainMixin._compute_scoreN) r/   r0   r1   r2   r   r)   r:   r	   Ú__annotations__r*   r3   r<   r
   ÚdictÚstrr   rD   r   Zmodel_configÚpropertyÚlistrH   rK   rS   ÚstaticmethodrM   rN   rO   rP   rQ   rr   rs   r   r   r   r   r8   ]   s.   
"/ÿ/r8   c                   @   s2  e Zd ZdZeedœdd„ƒZeeedœdd„ƒƒZ	ee
e dœdd„ƒZedeeef ee eeef d
œdd„ƒZedeeef ee eeef d
œdd„ƒZed	d	d	d	ddœeee eee
e  eeeef  eeedœdd„ƒZed	d	d	d	ddœeee eee
e  eeeef  eeedœdd„ƒZd	S )ÚEmbeddingDistanceEvalChainaL  Embedding distance evaluation chain.

    Use embedding distances to score semantic difference between
    a prediction and reference.

    Examples:
        >>> chain = EmbeddingDistanceEvalChain()
        >>> result = chain.evaluate_strings(prediction="Hello", reference="Hi")
        >>> print(result)
        {'score': 0.5}
    r   c                 C   s   dS )z�Return whether the chain requires a reference.

        Returns:
            bool: True if a reference is required, False otherwise.
        Tr   rF   r   r   r   Úrequires_referenceh  s    z-EmbeddingDistanceEvalChain.requires_referencec                 C   s   d| j j› d�S )NZ
embedding_Ú	_distance©r<   ÚvaluerF   r   r   r   Úevaluation_nameq  s    z*EmbeddingDistanceEvalChain.evaluation_namec                 C   s   ddgS )úeReturn the input keys of the chain.

        Returns:
            List[str]: The input keys.
        Ú
predictionÚ	referencer   rF   r   r   r   Ú
input_keysv  s    z%EmbeddingDistanceEvalChain.input_keysN©ÚinputsÚrun_managerr   c                 C   s@   | j  |d |d g¡}tƒ r.tƒ }| |¡}|  |¡}d|iS )a0  Compute the score for a prediction and reference.

        Args:
            inputs (Dict[str, Any]): The input data.
            run_manager (Optional[CallbackManagerForChainRun], optional):
                The callback manager.

        Returns:
            Dict[str, Any]: The computed score.
        r�   r‚   rE   ©r:   Zembed_documentsr#   r   Úarrayrs   ©rG   r…   r†   ro   r   rE   r   r   r   Ú_call  s    ÿ

z EmbeddingDistanceEvalChain._callc                 Ã   sF   | j  |d |d g¡I dH }tƒ r4tƒ }| |¡}|  |¡}d|iS )a:  Asynchronously compute the score for a prediction and reference.

        Args:
            inputs (Dict[str, Any]): The input data.
            run_manager (AsyncCallbackManagerForChainRun, optional):
                The callback manager.

        Returns:
            Dict[str, Any]: The computed score.
        r�   r‚   NrE   ©r:   Zaembed_documentsr#   r   rˆ   rs   r‰   r   r   r   Ú_acall˜  s    þÿ


z!EmbeddingDistanceEvalChain._acallF)r‚   Ú	callbacksÚtagsÚmetadataÚinclude_run_info)r�   r‚   r�   rŽ   r�   r�   Úkwargsr   c          	      K   s"   | ||dœ||||d�}|   |¡S )á`  Evaluate the embedding distance between a prediction and reference.

        Args:
            prediction: The output string from the first model.
            reference: The output string from the second model.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to use.
            include_run_info: Whether to include run information in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            A dictionary containing:
                - score: The embedding distance between the two predictions.
        ©r�   r‚   ©r…   r�   rŽ   r�   r�   ©rK   ©	rG   r�   r‚   r�   rŽ   r�   r�   r‘   rI   r   r   r   Ú_evaluate_strings´  s    ûz,EmbeddingDistanceEvalChain._evaluate_stringsc          	      Ë   s*   | j ||dœ||||d�I dH }|  |¡S )r’   r“   r”   N©ZacallrK   r–   r   r   r   Ú_aevaluate_stringsØ  s    ûz-EmbeddingDistanceEvalChain._aevaluate_strings)N)N)r/   r0   r1   r2   rw   r   r{   r   rv   r   rx   rƒ   ru   r   r   r   rŠ   r   rŒ   r   r—   r™   r   r   r   r   rz   [  sl    ý

ü ý

üø
ö#ø
örz   c                   @   s  e Zd ZdZeee dœdd„ƒZeedœdd„ƒZe	de
eef ee e
eef dœd	d
„ƒZe	de
eef ee e
eef dœdd„ƒZe	dddddœeeeeee  ee
eef  eee
dœdd„ƒZe	dddddœeeeeee  ee
eef  eee
dœdd„ƒZdS )Ú"PairwiseEmbeddingDistanceEvalChaina  Use embedding distances to score semantic difference between two predictions.

    Examples:
    >>> chain = PairwiseEmbeddingDistanceEvalChain()
    >>> result = chain.evaluate_string_pairs(prediction="Hello", prediction_b="Hi")
    >>> print(result)
    {'score': 0.5}
    r   c                 C   s   ddgS )r€   r�   Úprediction_br   rF   r   r   r   rƒ   
  s    z-PairwiseEmbeddingDistanceEvalChain.input_keysc                 C   s   d| j j› d�S )zReturn the evaluation name.Zpairwise_embedding_r|   r}   rF   r   r   r   r     s    z2PairwiseEmbeddingDistanceEvalChain.evaluation_nameNr„   c                 C   s@   | j  |d |d g¡}tƒ r.tƒ }| |¡}|  |¡}d|iS )a  Compute the score for two predictions.

        Args:
            inputs (Dict[str, Any]): The input data.
            run_manager (CallbackManagerForChainRun, optional):
                The callback manager.

        Returns:
            Dict[str, Any]: The computed score.
        r�   r›   rE   r‡   r‰   r   r   r   rŠ     s    þÿ

z(PairwiseEmbeddingDistanceEvalChain._callc                 Ã   sF   | j  |d |d g¡I dH }tƒ r4tƒ }| |¡}|  |¡}d|iS )a/  Asynchronously compute the score for two predictions.

        Args:
            inputs (Dict[str, Any]): The input data.
            run_manager (AsyncCallbackManagerForChainRun, optional):
                The callback manager.

        Returns:
            Dict[str, Any]: The computed score.
        r�   r›   NrE   r‹   r‰   r   r   r   rŒ   4  s    þÿ


z)PairwiseEmbeddingDistanceEvalChain._acallF)r�   rŽ   r�   r�   )r�   r›   r�   rŽ   r�   r�   r‘   r   c          	      K   s"   | ||dœ||||d�}|   |¡S )aX  Evaluate the embedding distance between two predictions.

        Args:
            prediction: The output string from the first model.
            prediction_b: The output string from the second model.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to use.
            include_run_info: Whether to include run information in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            A dictionary containing:
                - score: The embedding distance between the two predictions.
        ©r�   r›   r”   r•   ©	rG   r�   r›   r�   rŽ   r�   r�   r‘   rI   r   r   r   Ú_evaluate_string_pairsP  s    ûz9PairwiseEmbeddingDistanceEvalChain._evaluate_string_pairsc          	      Ë   s*   | j ||dœ||||d�I dH }|  |¡S )ac  Asynchronously evaluate the embedding distance between two predictions.

        Args:
            prediction: The output string from the first model.
            prediction_b: The output string from the second model.
            callbacks: The callbacks to use.
            tags: The tags to apply.
            metadata: The metadata to use.
            include_run_info: Whether to include run information in the output.
            **kwargs: Additional keyword arguments.

        Returns:
            A dictionary containing:
            - score: The embedding distance between the two predictions.
        rœ   r”   Nr˜   r�   r   r   r   Ú_aevaluate_string_pairst  s    ûz:PairwiseEmbeddingDistanceEvalChain._aevaluate_string_pairs)N)N)r/   r0   r1   r2   rw   rx   rv   rƒ   r   r   ru   r   r   r   rŠ   r   rŒ   r   r   rž   rŸ   r   r   r   r   rš   ý  sb   	 ý

ü ý

üø
ö#ø
örš   ),r2   Ú	functoolsÚloggingÚenumr   Ú	importlibr   Útypingr   r   Zlangchain_core.callbacksr   Z langchain_core.callbacks.managerr   r   Zlangchain_core.embeddingsr	   Zlangchain_core.utilsr
   Zpydanticr   r   Ztyping_extensionsr   Zlangchain.chains.baser   Zlangchain.evaluation.schemar   r   Zlangchain.schemar   r   Ú	getLoggerr/   r!   Ú	lru_cacher   r#   r)   rv   r*   r8   rz   rš   r   r   r   r   Ú<module>   s:   	

    #
þ