U
    hN                     @   s|   d dl mZmZ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 eddd	d
G dd de
eZdS )    )AnyDictListOptional)
deprecated)
Embeddings)	BaseModelroot_validator)get_from_dict_or_env)_create_retry_decoratorz0.0.30z1.0z!langchain_cohere.CohereEmbeddings)ZsinceZremovalZalternative_importc                   @   sn  e Zd ZU dZeed< eed< dZeed< dZe	e ed< dZ
e	e ed< d	Zeed
< dZe	e ed< dZeed< G dd dZeddeedddZeedddZeedddZddee e	e eee  dddZddee e	e eee  dddZee eee  d d!d"Zee eee  d d#d$Zeee d%d&d'Zeee d%d(d)ZdS )*CohereEmbeddingsa  Cohere embedding models.

    To use, you should have the ``cohere`` python package installed, and the
    environment variable ``COHERE_API_KEY`` set with your API key or pass it
    as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import CohereEmbeddings
            cohere = CohereEmbeddings(
                model="embed-english-light-v3.0",
                cohere_api_key="my-api-key"
            )
    clientasync_clientzembed-english-v2.0modelNtruncatecohere_api_key   max_retriesrequest_timeoutZ	langchain
user_agentc                   @   s   e Zd ZdZdS )zCohereEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   I/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/cohere.pyConfig4   s   r   T)pre)valuesreturnc                 C   st   t |dd}|d}z<ddl}|d }|j|||d|d< |j|||d|d	< W n tk
rn   td
Y nX |S )z?Validate that api key and python package exists in environment.r   ZCOHERE_API_KEYr   r   Nr   )timeoutclient_namer   r   zTCould not import cohere python package. Please install it with `pip install cohere`.)r
   getcohereZClientZAsyncClientImportError)clsr   r   r   r#   r!   r   r   r   validate_environment7   s0      


z%CohereEmbeddings.validate_environmentkwargsr   c                    s,   t  j}|ttd fdd}|f |S )%Use tenacity to retry the embed call.r'   c                     s    j jf | S N)r   embedr(   selfr   r   _embed_with_retryX   s    z<CohereEmbeddings.embed_with_retry.<locals>._embed_with_retryr   r   r   r.   r(   Zretry_decoratorr/   r   r-   r   embed_with_retryT   s    
z!CohereEmbeddings.embed_with_retryc                    s,   t  j}|ttd fdd}|f |S )r)   r'   c                     s    j jf | I d H S r*   )r   r+   r,   r-   r   r   r/   b   s    z=CohereEmbeddings.aembed_with_retry.<locals>._embed_with_retryr0   r1   r   r-   r   aembed_with_retry^   s    
z"CohereEmbeddings.aembed_with_retry
input_type)textsr5   r   c                C   s&   | j | j||| jdj}dd |D S )Nr   r6   r5   r   c                 S   s   g | ]}t tt|qS r   listmapfloat.0er   r   r   
<listcomp>q   s     z*CohereEmbeddings.embed.<locals>.<listcomp>)r2   r   r   
embeddingsr.   r6   r5   r@   r   r   r   r+   h   s    zCohereEmbeddings.embedc                   s,   | j | j||| jdI d H j}dd |D S )Nr7   c                 S   s   g | ]}t tt|qS r   r8   r<   r   r   r   r?   ~   s     z+CohereEmbeddings.aembed.<locals>.<listcomp>)r3   r   r   r@   rA   r   r   r   aembeds   s    
zCohereEmbeddings.aembed)r6   r   c                 C   s   | j |ddS )zEmbed a list of document texts.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        search_documentr4   r+   r.   r6   r   r   r   embed_documents   s    	z CohereEmbeddings.embed_documentsc                    s   | j |ddI dH S )zAsync call out to Cohere's embedding endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        rC   r4   NrB   rE   r   r   r   aembed_documents   s    	z!CohereEmbeddings.aembed_documents)textr   c                 C   s   | j |gddd S )zCall out to Cohere's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        search_queryr4   r   rD   r.   rI   r   r   r   embed_query   s    	zCohereEmbeddings.embed_queryc                    s   | j |gddI dH d S )zAsync call out to Cohere's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        rJ   r4   Nr   rG   rK   r   r   r   aembed_query   s    	zCohereEmbeddings.aembed_query)r   r   r   __doc__r   __annotations__r   strr   r   r   r   intr   r;   r   r   r	   r   r&   r2   r3   r   r+   rB   rF   rH   rL   rM   r   r   r   r   r      s<   

 
 
r   N)typingr   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r	   Zlangchain_core.utilsr
   Zlangchain_community.llms.coherer   r   r   r   r   r   <module>   s   