o
    Zh                     @   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                	   @   s  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dedefddZdedefddZdedefddZdddee	 dee	 deee  fddZdddee	 dee	 deee  fd d!Zdee	 deee  fd"d#Zdee	 deee  fd$d%Zd&e	dee fd'd(Zd&e	dee f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   sl   t |dd}|d}zddl}|d }|j|||d|d< |j|||d|d	< W |S  ty5   td
w )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"   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/cohere.pyvalidate_environment8   s.   


z%CohereEmbeddings.validate_environmentkwargsc                    2   t  j}|dtdtf fdd}|di |S )%Use tenacity to retry the embed call.r%   r   c                     s    j jdi | S Nr"   )r   embedr%   selfr"   r#   _embed_with_retryZ   s   z<CohereEmbeddings.embed_with_retry.<locals>._embed_with_retryNr"   r   r   r   r,   r%   Zretry_decoratorr-   r"   r+   r#   embed_with_retryV      
z!CohereEmbeddings.embed_with_retryc                    r&   )r'   r%   r   c                     s    j jdi | I d H S r(   )r   r)   r*   r+   r"   r#   r-   d   s   z=CohereEmbeddings.aembed_with_retry.<locals>._embed_with_retryNr"   r.   r/   r"   r+   r#   aembed_with_retry`   r1   z"CohereEmbeddings.aembed_with_retry
input_typetextsr4   c                C   s&   | j | j||| jdj}dd |D S )Nr   r5   r4   r   c                 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,   r5   r4   rA   r"   r"   r#   r)   j   s   zCohereEmbeddings.embedc                   s.   | j | j||| jdI d H j}dd |D S )Nr6   c                 S   r7   r"   r8   r<   r"   r"   r#   r?      r@   z+CohereEmbeddings.aembed.<locals>.<listcomp>)r2   r   r   rA   rB   r"   r"   r#   aembedu   s   
zCohereEmbeddings.aembedc                 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_documentr3   r)   r,   r5   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.
        rD   r3   NrC   rF   r"   r"   r#   aembed_documents   s   	z!CohereEmbeddings.aembed_documentstextc                 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_queryr3   r   rE   r,   rJ   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.
        rK   r3   Nr   rH   rL   r"   r"   r#   aembed_query   s   	zCohereEmbeddings.aembed_query)!__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   r   r   r   intr   r;   r   r
   Zmodel_configr   classmethodr   r$   r0   r2   r   r)   rC   rG   rI   rM   rN   r"   r"   r"   r#   r      sZ   
 




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    