
    +#h                         d dl mZmZ d dlZd dlmZ 	 d dlZej                  j                  Z	d Zd Z G d de      Zy# e
$ r eZ	Y e$ r eZ	Y #w xY w)    )AnyOptionalN)LMc                 :    t         dj                  di |        y)z.Handler from https://pypi.org/project/backoff/zbBacking off {wait:0.1f} seconds after {tries} tries calling function {target} with kwargs {kwargs}N )printformatdetailss    O/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/cohere.pybackoff_hdlrr      s&    		 	6	%#	%    c                 "    d| j                   v ryy)z6wrapper function that decides when to give up on retryzrate limitsFT)messager
   s    r   giveup_hdlrr      s    'r   c                        e Zd ZdZddg fdedee   dee   f fdZdefd	Z e	j                  e	j                  ed
ee      defd       ZdefdZ xZS )CoherezWrapper around Cohere's API.

    Currently supported models include `command-r-plus`, `command-r`, `command`, `command-nightly`, `command-light`, `command-light-nightly`.
    z	command-rNmodelapi_keystop_sequencesc                     t         |   |       t        j                  |      | _        d| _        |ddddd|| _        || _        d| _        g | _	        y)a/  
        Parameters
        ----------
        model : str
            Which pre-trained model from Cohere to use?
            Choices are [`command-r-plus`, `command-r`, `command`, `command-nightly`, `command-light`, `command-light-nightly`]
        api_key : str
            The API key for Cohere.
            It can be obtained from https://dashboard.cohere.ai/register.
        stop_sequences : list of str
            Additional stop tokens to end generation.
        **kwargs: dict
            Additional arguments to pass to the API provider.
        cohereg              )r   temperature
max_tokenspnum_generations   N)
super__init__r   Clientcoproviderkwargsr   max_num_generationshistory)selfr   r   r   r%   	__class__s        r   r!   zCohere.__init__'   sd    * 	--(  
 
 -#$ -/r   promptc                 *   |}i | j                   | j                  g |d|}|j                  d       d|j                         v r|j                  d        | j                  j
                  di |}| j                  j                  ||||d       |S )N)r   chat_historyr   r   n)r*   responser%   
raw_kwargsr   )r%   r   popkeysr#   chatr'   append)r(   r*   r%   r/   r.   s        r   basic_requestzCohere.basic_requestL   s    

kk
"11	

 
 	

$%&++-JJsO477<<)&) $ (		
 r   i  )max_time
on_backoffgiveupc                 (     | j                   |fi |S )zGHandles retrieval of completions from Cohere whilst handling API errors)r4   )r(   r*   r%   s      r   requestzCohere.requeste   s     "t!!&3F33r   c                 B     | j                   |fi |}|j                  gS )N)r9   text)r(   r*   r%   r.   s       r   __call__zCohere.__call__p   s%    
  4<<1&1r   )__name__
__module____qualname____doc__strr   listr!   r4   backoffon_exceptionexpocohere_api_errorr   r   r9   r<   __classcell__)r)   s   @r   r   r   !   s     !!%$&	#0#0 ##0 S		#0JC 2 W	4c 44r   r   )typingr   r   rC   dsp.modules.lmr   r   errorsUnauthorizedErrorrF   ImportError	ExceptionAttributeErrorr   r   r   r   r   r   <module>rO      sc       !}}66UR U/  !  ! !s   A   A	AA