
    gv                     n    d Z ddlmZmZmZmZmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZ  G d de      Zy	)
z
Transformation logic from OpenAI /v1/chat/completion format to Mistral's /chat/completion format.

Why separate file? Make it easy to see how transformation works

Docs - https://docs.mistral.ai/api/
    )ListLiteralOptionalTupleUnion)3handle_messages_with_content_list_to_str_conversionstrip_none_values_from_message)OpenAIGPTConfig)get_secret_str)AllMessageValuesc                   .    e Zd ZU dZdZee   ed<   dZee   ed<   dZ	ee   ed<   dZ
ee   ed<   dZeed      ed<   dZee   ed	<   dZee   ed
<   dZee   ed<   dZeeeef      ed<   	 	 	 	 	 	 	 	 	 ddee   dee   dee   dee   deed      d	ee   d
ee   dee   deeeef      ddfdZe fd       Zdedee   fdZdedefdZdededededef
dZdee   dee   deee   ee   f   fdZdee   dedee   fdZ ededefd       Z! xZ"S )MistralConfiga  
    Reference: https://docs.mistral.ai/api/

    The class `MistralConfig` provides configuration for the Mistral's Chat API interface. Below are the parameters:

    - `temperature` (number or null): Defines the sampling temperature to use, varying between 0 and 2. API Default - 0.7.

    - `top_p` (number or null): An alternative to sampling with temperature, used for nucleus sampling. API Default - 1.

    - `max_tokens` (integer or null): This optional parameter helps to set the maximum number of tokens to generate in the chat completion. API Default - null.

    - `tools` (list or null): A list of available tools for the model. Use this to specify functions for which the model can generate JSON inputs.

    - `tool_choice` (string - 'auto'/'any'/'none' or null): Specifies if/how functions are called. If set to none the model won't call a function and will generate a message instead. If set to auto the model can choose to either generate a message or call a function. If set to any the model is forced to call a function. Default - 'auto'.

    - `stop` (string or array of strings): Stop generation if this token is detected. Or if one of these tokens is detected when providing an array

    - `random_seed` (integer or null): The seed to use for random sampling. If set, different calls will generate deterministic results.

    - `safe_prompt` (boolean): Whether to inject a safety prompt before all conversations. API Default - 'false'.

    - `response_format` (object or null): An object specifying the format that the model must output. Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is in JSON. When using JSON mode you MUST also instruct the model to produce JSON yourself with a system or a user message.
    Ntemperaturetop_p
max_tokenstools)autoanynonetool_choicerandom_seedsafe_promptresponse_formatstopreturnc
                     t               j                         }
|
j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y )Nself)localscopyitemssetattr	__class__)r   r   r   r   r   r   r   r   r   r   locals_keyvalues                e/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/mistral/mistral_chat_transformation.py__init__zMistralConfig.__init__7   sA     (--/!--/JCf}!2U3 *    c                      t         |          S )N)super
get_config)clsr"   s    r&   r+   zMistralConfig.get_configH   s    w!##r(   modelc                 
    g dS )N)	streamr   r   r   r   r   seedr   r    )r   r-   s     r&   get_supported_openai_paramsz)MistralConfig.get_supported_openai_paramsL   s    

 
	
r(   c                 (    |dk(  s|dk(  r|S |dk(  ryy)Nr   r   requiredr   r1   )r   r   s     r&   _map_tool_choicezMistralConfig._map_tool_choiceY   s%    & K6$9J&r(   non_default_paramsoptional_paramsdrop_paramsc                 8   |j                         D ]  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r	|du r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r%t        |t              r| j                  |	      |d<   |d
k(  rd|i|d<   |dk(  s||d<    |S )Nr   r   r/   Tr   r   r   r   )r   r0   r   
extra_bodyr   )r    
isinstancestrr5   )r   r6   r7   r-   r8   paramr%   s          r&   map_openai_paramszMistralConfig.map_openai_paramsa   s     /446LE5$05-+0( Ud],1)%16.+0(*/'%*UC*@151F1F % 2G 2. 1>0F-))5: 12) 7* r(   api_baseapi_keyc                     |xs t        d      xs d}||j                  d      s|dz   }|xs t        d      xs t        d      }||fS )NMISTRAL_AZURE_API_BASEzhttps://api.mistral.ai/v1z/v1MISTRAL_AZURE_API_KEYMISTRAL_API_KEY)r   endswith)r   r?   r@   dynamic_api_keys       r&   $_get_openai_compatible_provider_infoz2MistralConfig._get_openai_compatible_provider_info   sw    
  +67+* 	 (9(9)
  %'H 1561/0 	
 ((r(   messagesc                 *   |D ]H  }|j                  d      }|st        |t              s(|D ]  }|j                  d      dk(  s|c c S  J t        |      }g }|D ]3  }t        j                  |      }t        |      }|j                  |       5 |S )ap  
        - handles scenario where content is list and not string
        - content list is just text, and no images
        - if image passed in, then just return as is (user-intended)
        - if `name` is passed, then drop it for mistral API: https://github.com/BerriAI/litellm/issues/6696

        Motivation: mistral api doesn't support content as a list
        contenttype	image_url)getr;   listr   r   _handle_name_in_messager	   append)r   rH   r-   m_content_blockcnew_messagess          r&   _transform_messagesz!MistralConfig._transform_messages   s     AUU9-N*^T"B'AuuV}3' (  GxP 02A55a8A.q1A" 
 r(   messagec                 `    |j                  d      }||d   dk7  r|j                  dd       |S )z
        Mistral API only supports `name` in tool messages

        If role == tool, then we keep `name`
        Otherwise, we drop `name`
        nameNroletool)rM   pop)r,   rV   _names      r&   rO   z%MistralConfig._handle_name_in_message   s6     F#F!:KK%r(   )	NNNNNNNNN)#__name__
__module____qualname____doc__r   r   int__annotations__r   r   r   rN   r   r   r   r   boolr   dictr   r   r<   r'   classmethodr+   r   r2   r5   r>   r   rG   r   rU   rO   __classcell__)r"   s   @r&   r   r      sI   0 "&K#%E8C= $J$ E8D> <@K'"789@!%K#%"&K$&&*OXd^*'+D(5d#
$+ &*#$( $@D%)&**.+/4c]4 }4 SM	4
 ~4 g&;<=4 c]4 d^4 "$4 uS$Y'(4 
4" $ $
 
c 
C C    	
  
<) )08)	x}hsm+	,),-.7:		> .> CS  r(   r   N)r`   typingr   r   r   r   r   8litellm.litellm_core_utils.prompt_templates.common_utilsr   r	   +litellm.llms.openai.chat.gpt_transformationr
   litellm.secret_managers.mainr   litellm.types.llms.openair   r   r1   r(   r&   <module>rl      s1    9 8 H 7 6lO lr(   