
    7|h!                    Z   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jD                  e#      Z$ddZ%ddZ&ddZ' e
ddd       G d de             Z( e
ddd       G d dee(             Z)y)    )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_tokensc           
        dd l }t        |d      rt        |j                  j                        nt        t
              }d}d}t        dt        |       t        d||      |t        t        t        j                              S )	Nr   error   
   T   )
multiplierminmax)reraisestopwaitr   before_sleep)coherehasattrr   r   CohereError	Exceptionr   r   r   r   loggerloggingWARNING)max_retriesr&   retry_conditionsmin_secondsmax_secondss        ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/cohere.py_create_retry_decoratorr2      su    
 67# 	  8 89$Y/  KK ,M%fgoo>     Coherec                T     t         j                        }|d fd       } |di |S )*Use tenacity to retry the completion call.c                 <     j                   j                  di | S N )clientgeneratekwargsllms    r1   _completion_with_retryz5completion_with_retry.<locals>._completion_with_retry7   s    "szz"",V,,r3   r=   r   returnr   r9   r2   r-   r>   r=   retry_decoratorr?   s   `   r1   completion_with_retryrE   3   s3    -coo>O- - "+F++r3   c                T     t         j                        }|d fd       } |di |S )r6   c                 X   K    j                   j                  di |  d {   S 7 wr8   )async_clientr;   r<   s    r1   r?   z6acompletion_with_retry.<locals>._completion_with_retryB   s)     .S%%..88888s    *(*r@   r9   rB   rC   s   `   r1   acompletion_with_retryrI   >   s3    -coo>O9 9 "+F++r3   z0.0.30z1.0zlangchain_cohere.BaseCohere)sinceremovalalternative_importc                      e Zd ZU dZdZded<   dZded<    ed      Zded<   	 d	Z	d
ed<   	 dZ
ded<   	 dZded<    ed      Zded<   	 dZded<   	 edd       Zy)
BaseCoherezBase class for Cohere models.Nr   r:   rH   )defaultOptional[str]modelg      ?floattemperaturezOptional[SecretStr]cohere_api_keyOptional[List[str]]r#   Fbool	streaming	langchainstr
user_agentc                   	 ddl }t        t        |dd            |d<   |d   }|j                  |d   j	                         |      |d<   |j                  |d   j	                         |      |d<   |S # t        $ r t        d	      w xY w)
z?Validate that api key and python package exists in environment.r   NrT   COHERE_API_KEYrZ   )api_keyclient_namer:   rH   zTCould not import cohere python package. Please install it with `pip install cohere`.)r&   r   r   Clientget_secret_valueAsyncClientImportError)clsvaluesr&   r^   s       r1   validate_environmentzBaseCohere.validate_environmentb   s    	 (=$V-=?OP(F#$ !.K%}}/0AAC'  -  F8 &,%7%7/0AAC' &8 &F>" %  	? 	s   A4 4B	)rd   r   rA   r   )__name__
__module____qualname____doc__r:   __annotations__rH   r   rQ   rS   rT   r#   rW   rZ   r   re   r9   r3   r1   rN   rN   I   s     (FCL# .E=.KQ*.N'.V $D
$E*It*(!J!< r3   rN   z0.1.14zlangchain_cohere.Coherec                  ,   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      Zedd       Zedd       Zedd       Zedd       ZddZd dZ	 	 d!	 	 	 	 	 	 	 	 	 d"dZ	 	 d!	 	 	 	 	 	 	 	 	 d#dZy)$r4   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")
       int
max_tokensr   kr   pg        rR   frequency_penaltypresence_penaltyNrP   truncater   r-   forbid)extrac                    | j                   | j                  | j                  | j                  | j                  | j
                  | j                  dS )z2Get the default parameters for calling Cohere API.rn   rS   ro   rp   rq   rr   rs   rw   selfs    r1   _default_paramszCohere._default_params   sE     //++!%!7!7 $ 5 5
 	
r3   c                
    ddiS )NrT   r\   r9   rx   s    r1   
lc_secretszCohere.lc_secrets   s     "233r3   c                :    i d| j                   i| j                  S )zGet the identifying parameters.rQ   )rQ   rz   rx   s    r1   _identifying_paramszCohere._identifying_params   s$     A7DJJ'@4+?+?@@r3   c                     y)zReturn type of llm.r&   r9   rx   s    r1   	_llm_typezCohere._llm_type   s     r3   c                    | j                   }| j                  |t        d      | j                  | j                  |d<   n||d<   i ||S )Nz2`stop` found in both the input and default params.stop_sequences)rz   r#   
ValueError)ry   r#   r=   paramss       r1   _invocation_paramszCohere._invocation_params   s\    %%99 T%5QRRYY"'+yyF#$'+F#$#&#F##r3   c                T    |j                   d   j                  }|rt        ||      }|S )Nr   )generationstextr   )ry   responser#   r   s       r1   _process_responsezCohere._process_response   s.    ##A&++ &tT2Dr3   c                     | j                   |fi |}t        | f| j                  |d|}|j                  d      }| j	                  ||      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.")
        rQ   promptr   )r   rE   rQ   getr   ry   r   r#   run_managerr=   r   r   _stops           r1   _callzCohere._call   sb    * )((88(


6
5;
 

+,%%h66r3   c                   K    | j                   |fi |}t        | f| j                  |d| d{   }|j                  d      }| j	                  ||      S 7 'w)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.")
        r   Nr   )r   rI   rQ   r   r   r   s           r1   _acallzCohere._acall   sp     * )((88/


6
5;
 
 

+,%%h66	
s   0AA(A)rA   zDict[str, Any])rA   zDict[str, str])rA   rY   )r#   rU   r=   r   rA   dict)r   r   r#   rU   rA   rY   )NN)
r   rY   r#   rU   r   z"Optional[CallbackManagerForLLMRun]r=   r   rA   rY   )
r   rY   r#   rU   r   z'Optional[AsyncCallbackManagerForLLMRun]r=   r   rA   rY   )rf   rg   rh   ri   rn   rj   ro   rp   rq   rr   rs   r-   r   model_configpropertyrz   r|   r~   r   r   r   r   r   r9   r3   r1   r4   r4   |   sK    JAAsJ@AsJD"u"L!e!5"Hm"0 K<L 

 

 4 4 A A  $ %):>	77 "7 8	7
 7 
7> %)?C	77 "7 =	7
 7 
7r3   )r-   rm   rA   zCallable[[Any], Any])r>   r4   r=   r   rA   r   )*
__future__r   r+   typingr   r   r   r   r   langchain_core._api.deprecationr	   langchain_core.callbacksr
   r   #langchain_core.language_models.llmsr    langchain_core.load.serializabler   langchain_core.utilsr   r   r   pydanticr   r   r   tenacityr   r   r   r   r   langchain_community.llms.utilsr   	getLoggerrf   r*   r2   rE   rI   rN   r4   r9   r3   r1   <module>r      s    "  6 6 6 4 9 V V 1 1  ?			8	$.,, 
E6S- --` (E>WXL7S* L7 YL7r3   