
    ^hv                     n    S r SSKJrJrJrJrJr  SSKJrJ	r	  SSK
Jr  SSKJr  SSKJr   " S S\5      rg	)
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                   H  ^  \ rS rSr% SrSr\\   \S'   Sr	\\   \S'   Sr
\\   \S'   Sr\\   \S'   Sr\\S      \S	'   Sr\\   \S
'   Sr\\   \S'   Sr\\   \S'   Sr\\\\4      \S'            S S\\   S\\   S\\   S\\   S	\\S      S
\\   S\\   S\\   S\\\\4      SS4S jjr\U 4S j5       rS\S\\   4S jrS	\S\4S jrS\S\S\S\S\4
S jrS\\   S\\   S\\\   \\   4   4S jrS\\    S\S\\    4S jr!\S\ S\ 4S j5       r"Sr#U =r$$ )!MistralConfig   a  
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
                     [        5       R                  5       n
U
R                  5        H(  u  pUS:w  d  M  Uc  M  [        U R                  X5        M*     g )Nself)localscopyitemssetattr	__class__)r   r   r   r   r   r   r   r   r   r   locals_keyvalues                j/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/mistral/mistral_chat_transformation.py__init__MistralConfig.__init__7   s=     (--/!--/JCf}!23 *    c                     > [         TU ]  5       $ )N)super
get_config)clsr#   s    r'   r-   MistralConfig.get_configH   s    w!##r*   modelc                 
    / SQ$ )N)	streamr   r   r   r   r   seedr   r    )r   r0   s     r'   get_supported_openai_params)MistralConfig.get_supported_openai_paramsL   s    

 
	
r*   c                 .    US:X  d  US:X  a  U$ US:X  a  gg)Nr   r   requiredr   r4   )r   r   s     r'   _map_tool_choiceMistralConfig._map_tool_choiceY   s%    & K6$9J&r*   non_default_paramsoptional_paramsdrop_paramsc                 F   UR                  5        H  u  pVUS:X  a  XbS'   US:X  a  XbS'   US:X  a	  USL a  XbS'   US:X  a  XbS'   US:X  a  XbS'   US:X  a  XbS'   US:X  a'  [        U[        5      (       a  U R                  US	9US'   US
:X  a  SU0US'   US:X  d  M  XbS'   M     U$ )Nr   r   r2   Tr   r   r   r   )r   r3   r   
extra_bodyr   )r!   
isinstancestrr9   )r   r;   r<   r0   r=   paramr&   s          r'   map_openai_paramsMistralConfig.map_openai_paramsa   s     /446LE$05-+0( Ud],1)%16.+0(*/'%*UC*@*@151F1F % 2G 2. 1>0F-))5: 12) 7* r*   api_baseapi_keyc                     U=(       d    [        S5      =(       d    SnUb  UR                  S5      (       d  US-   nU=(       d    [        S5      =(       d    [        S5      nX4$ )NMISTRAL_AZURE_API_BASEzhttps://api.mistral.ai/v1z/v1MISTRAL_AZURE_API_KEYMISTRAL_API_KEY)r   endswith)r   rE   rF   dynamic_api_keys       r'   $_get_openai_compatible_provider_info2MistralConfig._get_openai_compatible_provider_info   sz    
  +67+* 	 (9(9)
 )
  %'H 1561/0 	
 ((r*   messagesc                 P   U HX  nUR                  S5      nU(       d  M  [        U[        5      (       d  M4  U H  nUR                  S5      S:X  d  M  Us  s  $    MZ     [        U5      n/ nU H4  n[        R                  U5      n[        U5      nUR                  U5        M6     U$ )a@  
- 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   rO   r0   m_content_blockcnew_messagess          r'   _transform_messages!MistralConfig._transform_messages   s     AUU9-N~*^T"B"B'AuuV}3' (  GxP 02A55a8A.q1A" 
 r*   messagec                 d    UR                  S5      nUb  US   S:w  a  UR                  SS5        U$ )zs
Mistral API only supports `name` in tool messages

If role == tool, then we keep `name`
Otherwise, we drop `name`
nameNroletool)rT   pop)r.   r^   _names      r'   rV   %MistralConfig._handle_name_in_message   s6     F#F!:KK%r*   r4   )	NNNNNNNNN)%__name__
__module____qualname____firstlineno____doc__r   r   int__annotations__r   r   r   rU   r   r   r   r   boolr   dictr   r   rA   r(   classmethodr-   r   r5   r9   rC   r   rM   r   r\   rV   __static_attributes____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)rj   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   r4   r*   r'   <module>rw      s1    9 8 H 7 6lO lr*   