a
    bg                     @   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
 d dlmZmZ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)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)_create_retry_decoratorz0.0.30z1.0z!langchain_cohere.CohereEmbeddings)ZsinceZremovalZalternative_importc                   @   sv  e Zd ZU dZdZeed< dZ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< eddZedde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"
            )
    Nclientasync_clientzembed-english-v2.0modeltruncatecohere_api_key   max_retriesrequest_timeoutZ	langchain
user_agentZforbid)extrabefore)mode)valuesreturnc                 C   sr   t |dd}|d}z<ddl}|d }|j|||d|d< |j|||d|d	< W n tyl   td
Y n0 |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"   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/cohere.pyvalidate_environment8   s,    


z%CohereEmbeddings.validate_environmentkwargsr   c                    s0   t  j}|ttd fdd}|f i |S )%Use tenacity to retry the embed call.r%   c                     s    j jf i | S N)r   embedr&   selfr"   r#   _embed_with_retryZ   s    z<CohereEmbeddings.embed_with_retry.<locals>._embed_with_retryr   r   r   r,   r&   Zretry_decoratorr-   r"   r+   r#   embed_with_retryV   s    
z!CohereEmbeddings.embed_with_retryc                    s0   t  j}|ttd fdd}|f i |S )r'   r%   c                     s    j jf i | I d H S r(   )r   r)   r*   r+   r"   r#   r-   d   s    z=CohereEmbeddings.aembed_with_retry.<locals>._embed_with_retryr.   r/   r"   r+   r#   aembed_with_retry`   s    
z"CohereEmbeddings.aembed_with_retry
input_type)textsr3   r   c                C   s&   | j | j||| jdj}dd |D S )Nr   r4   r3   r   c                 S   s   g | ]}t tt|qS r"   listmapfloat.0er"   r"   r#   
<listcomp>s       z*CohereEmbeddings.embed.<locals>.<listcomp>)r0   r   r   
embeddingsr,   r4   r3   r?   r"   r"   r#   r)   j   s    zCohereEmbeddings.embedc                   s,   | j | j||| jdI d H j}dd |D S )Nr5   c                 S   s   g | ]}t tt|qS r"   r6   r:   r"   r"   r#   r=      r>   z+CohereEmbeddings.aembed.<locals>.<listcomp>)r1   r   r   r?   r@   r"   r"   r#   aembedu   s    
zCohereEmbeddings.aembed)r4   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_documentr2   r)   r,   r4   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.
        rB   r2   NrA   rD   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_queryr2   r   rC   r,   rH   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.
        rI   r2   Nr   rF   rJ   r"   r"   r#   aembed_query   s    	zCohereEmbeddings.aembed_query)!__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   r   r   r   intr   r9   r   r
   Zmodel_configr   classmethodr   r$   r0   r1   r   r)   rA   rE   rG   rK   rL   r"   r"   r"   r#   r      s>   



r   N)typingr   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   Zpydanticr	   r
   r   Zlangchain_community.llms.coherer   r   r"   r"   r"   r#   <module>   s   