
    dh!                    T   S SK Jr  S SKrS SKJrJrJrJrJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJr  S SKJrJrJr  S S	KJrJrJr  S S
KJrJrJrJrJr  S SK J!r!  \RD                  " \#5      r$SS jr%SS jr&SS jr'\
" SSSS9 " S S\5      5       r(\
" SSSS9 " S S\\(5      5       r)g)    )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           
        SS K n[        US5      (       a  [        UR                  R                  5      O[        [
        5      nSnSn[        S[        U 5      [        SX4S9U[        [        [        R                  5      S9$ )	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        W/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/cohere.py_create_retry_decoratorr2      sv    
 67## 	  8 89$Y/  KK ,M%fgoo>     Coherec                X   ^  [        T R                  5      nUSU 4S jj5       nU" S0 UD6$ )*Use tenacity to retry the completion call.c                 <   > TR                   R                  " S0 U D6$ N )clientgeneratekwargsllms    r1   _completion_with_retry5completion_with_retry.<locals>._completion_with_retry7   s    zz"",V,,r3   r=   r   returnr   r9   r2   r-   r>   r=   retry_decoratorr?   s   `   r1   completion_with_retryrF   3   s3    -coo>O- - "+F++r3   c                X   ^  [        T R                  5      nUSU 4S jj5       nU" S0 UD6$ )r6   c                 X   >#    TR                   R                  " S0 U D6I S h  vN $  N7fr8   )async_clientr;   r<   s    r1   r?   6acompletion_with_retry.<locals>._completion_with_retryB   s&     %%..88888s    *(*rA   r9   rC   rD   s   `   r1   acompletion_with_retryrK   >   s3    -coo>O9 9 "+F++r3   z0.0.30z1.0zlangchain_cohere.BaseCohere)sinceremovalalternative_importc                      \ rS rSr% SrSrS\S'   SrS\S'   \" SS9r	S\S	'    S
r
S\S'    SrS\S'    SrS\S'   \" SS9rS\S'    SrS\S'    \SS j5       rSrg)
BaseCohereI   zBase class for Cohere models.Nr   r:   rI   )defaultOptional[str]modelg      ?floattemperaturezOptional[SecretStr]cohere_api_keyOptional[List[str]]r#   Fbool	streaming	langchainstr
user_agentc                    SSK n[        [        USS5      5      US'   US   nUR                  US   R	                  5       US9US'   UR                  US   R	                  5       US9US'   U$ ! [         a    [        S	5      ef = f)
z?Validate that api key and python package exists in environment.r   NrW   COHERE_API_KEYr]   )api_keyclient_namer:   rI   zTCould not import cohere python package. Please install it with `pip install cohere`.)r&   r   r   Clientget_secret_valueAsyncClientImportError)clsvaluesr&   ra   s       r1   validate_environmentBaseCohere.validate_environmentb   s    	 (=$V-=?OP(F#$ !.K%}}/0AAC'  -  F8 &,%7%7/0AAC' &8 &F>" %  	? 	s   A. .Br9   )rg   r   rB   r   )__name__
__module____qualname____firstlineno____doc__r:   __annotations__rI   r   rT   rV   rW   r#   rZ   r]   r   rh   __static_attributes__r9   r3   r1   rP   rP   I   s     (FCL# .E=.KQ*.N'.V $D
$E*It*(!J!< r3   rP   z0.1.14zlangchain_cohere.Coherec                  B   \ rS rSr% SrSrS\S'    SrS\S'    SrS\S	'    S
r	S\S'    S
r
S\S'    SrS\S'    SrS\S'    \" SS9r\SS j5       r\SS j5       r\SS j5       r\S S j5       rS!S jrS"S jr  S#         S$S jjr  S#         S%S jjrSrg)&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        rU   frequency_penaltypresence_penaltyNrS   truncater   r-   forbid)extrac                    U R                   U R                  U R                  U R                  U R                  U R
                  U R                  S.$ )z2Get the default parameters for calling Cohere API.ru   rV   rv   rw   rx   ry   rz   r~   selfs    r1   _default_paramsCohere._default_params   sE     //++!%!7!7 $ 5 5
 	
r3   c                
    SS0$ )NrW   r_   r9   r   s    r1   
lc_secretsCohere.lc_secrets   s     "233r3   c                :    0 SU R                   0EU R                  E$ )zGet the identifying parameters.rT   )rT   r   r   s    r1   _identifying_paramsCohere._identifying_params   s$     A7DJJ'@4+?+?@@r3   c                    g)zReturn type of llm.r&   r9   r   s    r1   	_llm_typeCohere._llm_type   s     r3   c                    U R                   nU R                  b  Ub  [        S5      eU R                  b  U R                  US'   OXS'   0 UEUE$ )Nz2`stop` found in both the input and default params.stop_sequences)r   r#   
ValueError)r   r#   r=   paramss       r1   _invocation_paramsCohere._invocation_params   sZ    %%99 T%5QRRYY"'+yyF#$'+#$#&#F##r3   c                \    UR                   S   R                  nU(       a  [        X25      nU$ )Nr   )generationstextr   )r   responser#   r   s       r1   _process_responseCohere._process_response   s,    ##A&++ &t2Dr3   c                    U R                   " U40 UD6n[        U 4U R                  US.UD6nUR                  S5      nU R	                  Xg5      $ )a   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.")
rT   promptr   )r   rF   rT   getr   r   r   r#   run_managerr=   r   r   _stops           r1   _callCohere._call   s^    * ((88(


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

+,%%h66r3   c                   #    U R                   " U40 UD6n[        U 4U R                  US.UD6I Sh  vN nUR                  S5      nU R	                  Xg5      $  N&7f)a,  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   rK   rT   r   r   r   s           r1   _acallCohere._acall   sk     * ((88/


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

+,%%h66	
s   0AA'Ar9   )rB   zDict[str, Any])rB   zDict[str, str])rB   r\   )r#   rX   r=   r   rB   dict)r   r   r#   rX   rB   r\   )NN)
r   r\   r#   rX   r   z"Optional[CallbackManagerForLLMRun]r=   r   rB   r\   )
r   r\   r#   rX   r   z'Optional[AsyncCallbackManagerForLLMRun]r=   r   rB   r\   )rj   rk   rl   rm   rn   ru   ro   rv   rw   rx   ry   rz   r-   r   model_configpropertyr   r   r   r   r   r   r   r   rp   r9   r3   r1   r4   r4   |   sP    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 
7 7r3   )r-   rt   rB   zCallable[[Any], Any])r>   r4   r=   r   rB   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   	getLoggerrj   r*   r2   rF   rK   rP   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   