
    h                     v    d Z ddlmZmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ  G d d	e      Zy
)zY
Translates from OpenAI's `/v1/chat/completions` to Moonshot AI's `/v1/chat/completions`
    )Any	CoroutineListLiteralOptionalTupleUnionoverload)3handle_messages_with_content_list_to_str_conversionget_secret_str)AllMessageValues   )OpenAIGPTConfigc                       e Zd Zedee   deded   dee	e	ee   f   fd       Z
e	 ddee   deded   dee   fd       Z
	 ddee   dededeee   ee	e	ee   f   f   f fd	Z
d
ee   dee   deee   ee   f   fdZ	 dd
ee   dee   dedededee   defdZdedef fdZdededededef
dZdedee   dedededef fdZdee   dedee   fdZ xZS )MoonshotChatConfigmessagesmodelis_asyncTreturnc                      y N selfr   r   r   s       e/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/moonshot/chat/transformation.py_transform_messagesz&MoonshotChatConfig._transform_messages   s     	    Fc                      y r   r   r   s       r   r   z&MoonshotChatConfig._transform_messages   s     	r   c                 f    t        |      }|rt        | 	  ||d      S t        | 	  ||d      S )zF
        Moonshot AI does not support content in list format.
        T)r   r   r   F)r   superr   )r   r   r   r   	__class__s       r   r   z&MoonshotChatConfig._transform_messages    sQ     GxP7.! /   7.! /  r   api_baseapi_keyc                 N    |xs t        d      xs d}|xs t        d      }||fS )NMOONSHOT_API_BASEhttps://api.moonshot.ai/v1MOONSHOT_API_KEYr   )r   r#   r$   dynamic_api_keys       r   $_get_openai_compatible_provider_infoz7MoonshotChatConfig._get_openai_compatible_provider_info0   s?      ,12,+ 	
 "G^4F%G((r   optional_paramslitellm_paramsstreamc                 :    |sd}|j                  d      s| d}|S )zf
        If api_base is not provided, use the default Moonshot AI /chat/completions endpoint.
        r'   z/chat/completions)endswith)r   r#   r$   r   r+   r,   r-   s          r   get_complete_urlz#MoonshotChatConfig.get_complete_url;   s-     3H  !45"#45Hr   c                     dg}d|v r|j                  ddg       t        | 	  |      }g }|D ]  }||vs|j                  |        |S )a.  
        Get the supported OpenAI params for Moonshot AI models
        
        Moonshot AI limitations:
        - functions parameter is not supported (use tools instead)
        - tool_choice doesn't support "required" value
        - kimi-thinking-preview doesn't support tool calls at all
        	functionszkimi-thinking-previewtoolstool_choice)r   )extendr!   get_supported_openai_paramsappend)r   r   excluded_paramsbase_openai_paramsfinal_paramsparamr"   s         r   r6   z.MoonshotChatConfig.get_supported_openai_paramsO   si     '2] #e+""G]#;<"W@u@M"$'EO+##E* ( r   non_default_paramsdrop_paramsc                     | j                  |      }|j                         D ]  \  }}|dk(  r||d<   ||v s|||<    d|v r/|d   dkD  rd|d<   |d   dk  r|j                  dd      dkD  rd|d<   |S )z
        Map OpenAI parameters to Moonshot AI parameters
        
        Handles Moonshot AI specific limitations:
        - tool_choice doesn't support "required" value
        - Temperature <0.3 limitation for n>1
        max_completion_tokens
max_tokenstemperature   g333333?n)r6   itemsget)r   r<   r+   r   r=   supported_openai_paramsr;   values           r   map_openai_paramsz$MoonshotChatConfig.map_openai_paramsf   s     #'"B"B5"I.446LE5//05-11).&	 7 O+}-112.}-38K8KCQR8SVW8W14.r   headersc                 |    |j                  dd      dk(  r| j                  ||      }t        |   |||||      S )z
        Transform the overall request to be sent to the API.
        Returns:
            dict: The transformed request. Sent as the body of the API call.
        r4   Nrequired)r   r+   )r   r   r+   r,   rI   )rE   !_add_tool_choice_required_messager!   transform_request)r   r   r   r+   r,   rI   r"   s         r   rM   z$MoonshotChatConfig.transform_request   s]     }d3zA==! / > H w(+) ) 
 	
r   c                 P    |j                  ddd       |j                  d       |S )z
        Add a message to the messages list to indicate that the tool choice is required.

        https://platform.moonshot.ai/docs/guide/migrating-from-openai-to-kimi#about-tool_choice
        userz1Please select a tool to handle the current issue.)rolecontentr4   )r7   pop)r   r   r+   s      r   rL   z4MoonshotChatConfig._add_tool_choice_required_message   s0     	J
 	 	M*r   )Fr   )__name__
__module____qualname__r
   r   r   strr   r   r   r   boolr	   r   r   r*   dictr0   listr6   rH   rM   rL   __classcell__)r"   s   @r   r   r      sC   -.7:FMdm	3T"233	4 
 
 $)	'(  %.	
 
	  NS-.7:FJ	t$%yc4@P;Q1Q'RR	S 	) 	)08	)	x}hsm+	,	)$ "&3- # 	
    
(  .      	 
   
 F

 '(
 	

 
 
 

<$?O:P cg lp  rB  mC r   r   N)__doc__typingr   r   r   r   r   r   r	   r
   8litellm.litellm_core_utils.prompt_templates.common_utilsr   litellm.secret_managers.mainr   litellm.types.llms.openair   openai.chat.gpt_transformationr   r   r   r   r   <module>ra      s4    S R R 8 6 =b br   