o
    Zh!                     @  s  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Z%d$ddZ&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max_retriesintreturnCallable[[Any], Any]c                 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.   V/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/cohere.py_create_retry_decorator   s   
r0   llmCoherekwargsr   c                   *   t  j}|d fdd}|di |S )	*Use tenacity to retry the completion call.r3   r   r   c                    s    j jdi | S Nr.   )clientgenerater3   r1   r.   r/   _completion_with_retry7   s   z5completion_with_retry.<locals>._completion_with_retryNr3   r   r   r   r.   r0   r   r1   r3   Zretry_decoratorr;   r.   r:   r/   completion_with_retry3      
r?   c                   r4   )	r5   r3   r   r   c                    s    j jdi | I d H S r6   )async_clientr8   r9   r:   r.   r/   r;   B   s   z6acompletion_with_retry.<locals>._completion_with_retryNr<   r.   r=   r>   r.   r:   r/   acompletion_with_retry>   r@   rB   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ZdS )
BaseCoherezBase class for Cohere models.Nr   r7   rA   )defaultOptional[str]modelg      ?floattemperaturezOptional[SecretStr]cohere_api_keyOptional[List[str]]r%   Fbool	streamingZ	langchainstr
user_agentvaluesr   r   c                 C  sx   zddl }W n ty   tdw 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`.rI   COHERE_API_KEYrN   )Zapi_keyclient_namer7   rA   )r(   ImportErrorr   r   ZClientZget_secret_valueZAsyncClient)clsrO   r(   rQ   r.   r.   r/   validate_environmentb   s(   




zBaseCohere.validate_environment)rO   r   r   r   )__name__
__module____qualname____doc__r7   __annotations__rA   r   rF   rH   rI   r%   rL   rN   r   rT   r.   r.   r.   r/   rC   I   s"   
 rC   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d2ddZed3ddZed2ddZed4ddZd5d%d&Zd6d(d)Z		d7d8d-d.Z		d7d9d0d1ZdS ):r2   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        rG   frequency_penaltypresence_penaltyNrE   truncater   r   Zforbid)extrar   Dict[str, Any]c                 C  s"   | j | j| j| j| j| j| jdS )z2Get the default parameters for calling Cohere API.r[   rH   r\   r]   r^   r_   r`   rc   selfr.   r.   r/   _default_params   s   zCohere._default_paramsDict[str, str]c                 C  s   ddiS )NrI   rP   r.   rd   r.   r.   r/   
lc_secrets   s   zCohere.lc_secretsc                 C  s   i d| j i| jS )zGet the identifying parameters.rF   )rF   rf   rd   r.   r.   r/   _identifying_params   s   zCohere._identifying_paramsrM   c                 C  s   dS )zReturn type of llm.r(   r.   rd   r.   r.   r/   	_llm_type   s   zCohere._llm_typer%   rJ   r3   r   dictc                 K  sJ   | j }| jd ur|d urtd| jd ur| j|d< n||d< i ||S )Nz2`stop` found in both the input and default params.stop_sequences)rf   r%   
ValueError)re   r%   r3   paramsr.   r.   r/   _invocation_params   s   
zCohere._invocation_paramsresponsec                 C  s   |j d j}|rt||}|S )Nr   )Zgenerationstextr   )re   rp   r%   rq   r.   r.   r/   _process_response   s   
zCohere._process_responsepromptrun_manager"Optional[CallbackManagerForLLMRun]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.")
        rF   rs   rl   )ro   r?   rF   getrr   re   rs   r%   rt   r3   rn   rp   _stopr.   r.   r/   _call   s   
zCohere._call'Optional[AsyncCallbackManagerForLLMRun]c                   sH   | 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.")
        rv   Nrl   )ro   rB   rF   rw   rr   rx   r.   r.   r/   _acall   s   
zCohere._acall)r   rb   )r   rg   )r   rM   )r%   rJ   r3   r   r   rk   )rp   r   r%   rJ   r   rM   )NN)
rs   rM   r%   rJ   rt   ru   r3   r   r   rM   )
rs   rM   r%   rJ   rt   r{   r3   r   r   rM   )rU   rV   rW   rX   r[   rY   r\   r]   r^   r_   r`   r   r   Zmodel_configpropertyrf   rh   ri   rj   ro   rr   rz   r|   r.   r.   r.   r/   r2   |   sF   
 


)r   r   r   r   )r1   r2   r3   r   r   r   )*
__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   	getLoggerrU   r+   r0   r?   rB   rC   r2   r.   r.   r.   r/   <module>   s*    



0