
    +#hG                         d dl mZmZ d dlZd dlmZ 	 d dlZd dlmZ d dl	m
Z
 d dlmZmZ e
Zd Zd Z G d	 d
e      Zy# e$ r eZY w xY w)    )AnyOptionalN)LM)MistralClient)MistralAPIException)ChatCompletionResponseChatMessagec                 :    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    P/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/mistral.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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ded
edefdZ xZS )MistralzWrapper around Mistral AI's API.

    Currently supported models include `mistral-small-latest`, `mistral-medium-latest`, `mistral-large-latest`.
    modelapi_keyc                     t         |   |       t        t        k(  rt	        d      t        |      | _        d| _        |ddd|| _        g | _	        y)at  
        Parameters
        ----------
        model : str
            Which pre-trained model from Mistral AI to use?
            Choices are [`mistral-small-latest`, `mistral-medium-latest`, `mistral-large-latest`]
        api_key : str
            The API key for Mistral AI.
        **kwargs: dict
            Additional arguments to pass to the API provider.
        z\Not loading Mistral AI because it is not installed. Install it with `pip install mistralai`.)r   mistralg(\?   )r   temperature
max_tokensN)
super__init__mistralai_api_error	ExceptionImportErrorr   clientproviderkwargshistory)selfr   r   r&   	__class__s       r   r    zMistral.__init__'   s^    " 	)+|}}#G4!
 	
 .0r   promptc                    |}i | j                   dt        d|      gi|}|j                  dd      }||dkD  r|d   dk(  rd	|d<    | j                  j                  di |}||||d
}| j
                  j                  |       |S )z"Basic request to Mistral AI's API.messagesuser)rolecontentnN   r   g        gffffff?)r*   responser&   
raw_kwargsr   )r&   r	   popr$   chatr'   append)r(   r*   r&   r3   r0   r2   r'   s          r   basic_requestzMistral.basic_requestI   s    

kk
&&AB
 
 JJsD!=QUvm'<'C$'F=!#4;;##-f-  $	
 	G$r   i  )max_time
on_backoffgiveupc                 2    |dz   } | j                   |fi |S )zLHandles retrieval of completions from Mistral AI whilst handling API errors.zFollow the format only once !)r7   )r(   r*   r&   s      r   requestzMistral.requestc   s'     99!t!!&3F33r   only_completedreturn_sortedc                     |sJ d       |du sJ d       |j                  dd      }g }t        |      D ]G  } | j                  |fi |}|j                  |j                  d   j
                  j                         I |S )Nzfor nowFr0   r1   r   )r4   ranger<   r6   choicesr   r/   )	r(   r*   r=   r>   r&   r0   completions_r2   s	            r   __call__zMistral.__call__o   s     (y(~%0y0%JJsAq 	DA#t||F5f5Hx//2::BBC	D r   )zmistral-medium-latestN)TF)__name__
__module____qualname____doc__strr   r    r7   backoffon_exceptionexpor!   r   r   r<   boolrD   __classcell__)r)   s   @r   r   r   !   s     -!% 0 0 # 0DC 4 W	4c 44  $#	  	r   r   )typingr   r   rJ   dsp.modules.lmr   	mistralaimistralai.clientr   mistralai.exceptionsr    mistralai.models.chat_completionr   r	   r!   r#   r"   r   r   r   r   r   r   <module>rU      sT       $.8T-
`b `)  $#$s   A   A
	A
