a
    bg!                     @  s4  d dl mZ d dlZd dlmZmZmZ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mZ d d	lmZmZmZ d d
lmZmZmZmZmZ d dl m!Z! e"e#Z$dddddZ%ddddddZ&ddddddZ'e
ddddG dd deZ(e
ddddG d d dee(Z)dS )!    )annotationsN)AnyCallableDictListOptional)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)Serializable)convert_to_secret_strget_from_dict_or_envpre_init)
ConfigDictField	SecretStr)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponential)enforce_stop_tokensintzCallable[[Any], Any])max_retriesreturnc                 C  sV   dd l }t|drt|jjntt}d}d}tdt| td||d|t	t
tjdS )	Nr   error   
   T   )
multiplierminmax)reraisestopwaitr   before_sleep)coherehasattrr   r   ZCohereError	Exceptionr   r   r   r   loggerloggingWARNING)r   r'   Zretry_conditionsZmin_secondsZmax_seconds r-   m/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/cohere.py_create_retry_decorator   s    
r/   Coherer   )llmkwargsr   c                   s0   t  j}|ddd fdd}|f i |S )*Use tenacity to retry the completion call.r   r2   r   c                    s    j jf i | S N)clientgenerater2   r1   r-   r.   _completion_with_retry7   s    z5completion_with_retry.<locals>._completion_with_retryr/   r   r1   r2   Zretry_decoratorr:   r-   r9   r.   completion_with_retry3   s    
r=   c                   s0   t  j}|ddd fdd}|f i |S )r3   r   r4   c                    s    j jf i | I d H S r5   )async_clientr7   r8   r9   r-   r.   r:   B   s    z6acompletion_with_retry.<locals>._completion_with_retryr;   r<   r-   r9   r.   acompletion_with_retry>   s    
r?   z0.0.30z1.0zlangchain_cohere.BaseCohere)ZsinceZremovalZalternative_importc                   @  s   e Zd ZU dZdZded< dZded< eddZded< d	Z	d
ed< dZ
ded< dZded< eddZded< dZded< edddddZdS )
BaseCoherezBase class for Cohere models.Nr   r6   r>   )defaultOptional[str]modelg      ?floattemperaturezOptional[SecretStr]cohere_api_keyOptional[List[str]]r$   Fbool	streamingZ	langchainstr
user_agentr   )valuesr   c                 C  s|   zddl }W n ty&   tdY nR0 tt|dd|d< |d }|j|d  |d|d< |j|d  |d|d	< |S )
z?Validate that api key and python package exists in environment.r   NzTCould not import cohere python package. Please install it with `pip install cohere`.rF   COHERE_API_KEYrK   )Zapi_keyclient_namer6   r>   )r'   ImportErrorr   r   ZClientZget_secret_valueZAsyncClient)clsrL   r'   rN   r-   r-   r.   validate_environmentb   s&    





zBaseCohere.validate_environment)__name__
__module____qualname____doc__r6   __annotations__r>   r   rC   rE   rF   r$   rI   rK   r   rQ   r-   r-   r-   r.   r@   I   s   
r@   z0.1.14zlangchain_cohere.Coherec                   @  s  e Zd ZU dZdZded< dZded< dZded< d	Zd
ed< d	Z	d
ed< dZ
ded< dZded< eddZeddddZeddddZeddddZeddddZd d!d"d#d$d%Zd!d dd&d'd(Zd0dd d)d!dd*d+d,Zd1dd d-d!dd*d.d/ZdS )2r0   a  Cohere large language 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.llms import Cohere

            cohere = Cohere(model="gptd-instruct-tft", cohere_api_key="my-api-key")
       r   
max_tokensr   kr   pg        rD   frequency_penaltypresence_penaltyNrB   truncater   r   Zforbid)extrazDict[str, Any])r   c                 C  s"   | j | j| j| j| j| j| jdS )z2Get the default parameters for calling Cohere API.rX   rE   rY   rZ   r[   r\   r]   r_   selfr-   r-   r.   _default_params   s    zCohere._default_paramszDict[str, str]c                 C  s   ddiS )NrF   rM   r-   r`   r-   r-   r.   
lc_secrets   s    zCohere.lc_secretsc                 C  s   i d| j i| jS )zGet the identifying parameters.rC   )rC   rb   r`   r-   r-   r.   _identifying_params   s    zCohere._identifying_paramsrJ   c                 C  s   dS )zReturn type of llm.r'   r-   r`   r-   r-   r.   	_llm_type   s    zCohere._llm_typerG   r   dict)r$   r2   r   c                 K  sL   | j }| jd ur"|d ur"tdn| jd ur8| j|d< n||d< i ||S )Nz2`stop` found in both the input and default params.stop_sequences)rb   r$   
ValueError)ra   r$   r2   paramsr-   r-   r.   _invocation_params   s    

zCohere._invocation_params)responser$   r   c                 C  s   |j d j}|rt||}|S )Nr   )Zgenerationstextr   )ra   rk   r$   rl   r-   r-   r.   _process_response   s    
zCohere._process_responsez"Optional[CallbackManagerForLLMRun])promptr$   run_managerr2   r   c                 K  s@   | j |fi |}t| f| j|d|}|d}| ||S )ah  Call out to Cohere's generate endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = cohere("Tell me a joke.")
        rC   rn   rg   )rj   r=   rC   getrm   ra   rn   r$   ro   r2   ri   rk   _stopr-   r-   r.   _call   s    
zCohere._callz'Optional[AsyncCallbackManagerForLLMRun]c                   sF   | j |fi |}t| f| j|d|I dH }|d}| ||S )at  Async call out to Cohere's generate endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = await cohere("Tell me a joke.")
        rp   Nrg   )rj   r?   rC   rq   rm   rr   r-   r-   r.   _acall   s    
zCohere._acall)NN)NN)rR   rS   rT   rU   rX   rV   rY   rZ   r[   r\   r]   r   r   Zmodel_configpropertyrb   rc   rd   re   rj   rm   rt   ru   r-   r-   r-   r.   r0   |   s6   

    )*
__future__r   r+   typingr   r   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr	   r
   Z#langchain_core.language_models.llmsr   Z langchain_core.load.serializabler   Zlangchain_core.utilsr   r   r   Zpydanticr   r   r   tenacityr   r   r   r   r   Zlangchain_community.llms.utilsr   	getLoggerrR   r*   r/   r=   r?   r@   r0   r-   r-   r-   r.   <module>   s(   
0