
    Ng!                    |   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j"        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)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           
     (   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)r   r*   retry_conditionsmin_secondsmax_secondss        [/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/cohere.py_create_retry_decoratorr5      s    MMM
 67##	0 8999$Y//  KK ,,MMM%fgo>>       llmCoherekwargsr   c                X     t           j                  }|d fd            } |di |S )*Use tenacity to retry the completion call.r9   r   r   c                 (     j         j        di | S N )clientgenerater9   r7   s    r4   _completion_with_retryz5completion_with_retry.<locals>._completion_with_retry7   s    "sz",,V,,,r6   r9   r   r   r   r>   r5   r   r7   r9   retry_decoratorrB   s   `   r4   completion_with_retryrG   3   sS    -co>>O- - - - - _- "!++F+++r6   c                X     t           j                  }|d fd            } |di |S )r;   r9   r   r   c                 8   K    j         j        di |  d {V S r=   )async_clientr@   rA   s    r4   rB   z6acompletion_with_retry.<locals>._completion_with_retryB   s5      .S%.88888888888r6   rC   r>   rD   rE   s   `   r4   acompletion_with_retryrK   >   sS    -co>>O9 9 9 9 9 _9 "!++F+++r6   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dS )
BaseCoherezBase class for Cohere models.Nr   r?   rJ   )defaultOptional[str]modelg      ?floattemperaturezOptional[SecretStr]cohere_api_keyOptional[List[str]]r'   Fbool	streaming	langchainstr
user_agentvaluesr   r   c                f   	 ddl }t          t          |dd                    |d<   |d         }|                    |d                                         |          |d<   |                    |d                                         |          |d<   n# t          $ r t          d	          w xY w|S )
z?Validate that api key and python package exists in environment.r   NrV   COHERE_API_KEYr\   )api_keyclient_namer?   rJ   zTCould not import cohere python package. Please install it with `pip install cohere`.)r*   r   r   Clientget_secret_valueAsyncClientImportError)clsr]   r*   ra   s       r4   validate_environmentzBaseCohere.validate_environmentb   s    	MMM (=$V-=?OPP( (F#$ !.K%}}/0AACC'  -    F8 &,%7%7/0AACC' &8 & &F>""  	 	 	?  	$ s   B B.)r]   r   r   r   )__name__
__module____qualname____doc__r?   __annotations__rJ   r   rS   rU   rV   r'   rY   r\   r   rg   r>   r6   r4   rP   rP   I   s          ('FL 5...E....KQ*.N....V $D$$$$eE***I****(!J!!!!<   X  r6   rP   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/d0d'Z	 	 d/d1d)ZdS )2r8   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        rT   frequency_penaltypresence_penaltyNrR   truncater!   r   forbid)extrar   Dict[str, Any]c                \    | j         | j        | j        | j        | j        | j        | j        dS )z2Get the default parameters for calling Cohere API.ro   rU   rp   rq   rr   rs   rt   ry   selfs    r4   _default_paramszCohere._default_params   s9     /+!%!7 $ 5
 
 	
r6   Dict[str, str]c                
    ddiS )NrV   r_   r>   rz   s    r4   
lc_secretszCohere.lc_secrets   s     "233r6   c                &    i d| j         i| j        S )zGet the identifying parameters.rS   )rS   r|   rz   s    r4   _identifying_paramszCohere._identifying_params   s     A7DJ'@4+?@@r6   r[   c                    dS )zReturn type of llm.r*   r>   rz   s    r4   	_llm_typezCohere._llm_type   s	     xr6   r'   rW   r9   r   dictc                z    | j         }| j        |t          d          | j        | j        |d<   n||d<   i ||S )Nz2`stop` found in both the input and default params.stop_sequences)r|   r'   
ValueError)r{   r'   r9   paramss       r4   _invocation_paramszCohere._invocation_params   sY    %9 T%5QRRRY"'+yF#$$'+F#$#&#F##r6   responsec                N    |j         d         j        }|rt          ||          }|S )Nr   )generationstextr   )r{   r   r'   r   s       r4   _process_responsezCohere._process_response   s0    #A&+  	3&tT22Dr6   promptrun_manager"Optional[CallbackManagerForLLMRun]c                     | 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.")
        rS   r   r   )r   rG   rS   getr   r{   r   r'   r   r9   r   r   _stops           r4   _callzCohere._call   ss    * )(8888(

6
 
5;
 
 

+,,%%h666r6   'Optional[AsyncCallbackManagerForLLMRun]c                   K    | j         |fi |}t          | f| j        |d| d{V }|                    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.")
        r   Nr   )r   rK   rS   r   r   r   s           r4   _acallzCohere._acall   s      * )(8888/

6
 
5;
 
 
 
 
 
 
 
 

+,,%%h666r6   )r   rw   )r   r}   )r   r[   )r'   rW   r9   r   r   r   )r   r   r'   rW   r   r[   )NN)
r   r[   r'   rW   r   r   r9   r   r   r[   )
r   r[   r'   rW   r   r   r9   r   r   r[   )rh   ri   rj   rk   ro   rl   rp   rq   rr   rs   rt   r   r   model_configpropertyr|   r   r   r   r   r   r   r   r>   r6   r4   r8   r8   |   s          JAAJJJJ@AJJJJD"""""L!!!!!5"H""""0 K<:  L 

 

 

 X

 4 4 4 X4 A A A XA    X$ $ $ $    %):>	7 7 7 7 7> %)?C	7 7 7 7 7 7 7r6   )r   r   r   r   )r7   r8   r9   r   r   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   	getLoggerrh   r.   r5   rG   rK   rP   r8   r>   r6   r4   <module>r      sA   " " " " " "  6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6        4 3 3 3 3 3 9 9 9 9 9 9 V V V V V V V V V V 1 1 1 1 1 1 1 1 1 1              ? > > > > >		8	$	$   ., , , ,, , , , 
E6S  - - - - - - - -` (E>WXXXL7 L7 L7 L7 L7S* L7 L7 YXL7 L7 L7r6   