o
    ưi                     @   sp   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	 d	eZd
S )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                       s  e Zd Zedee deded dee	e	ee f fddZ
e	d dee deded dee fd	dZ
	d dee dededeee ee	e	ee f f f fd
dZ
dee dee deee ee f fddZ	d!dee dee dedededee defddZdedef fddZdededededef
ddZdedee dedededef fddZdee dedee fddZ  ZS )"MoonshotChatConfigmessagesmodelis_asyncTreturnc                 C      d S N selfr   r   r   r   r   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/moonshot/chat/transformation.py_transform_messages   s   z&MoonshotChatConfig._transform_messagesFc                 C   r   r   r   r   r   r   r   r      s   c                    st   d}|D ]}| d}|rt|trtdd |D rd} nq|s&t|}|r1t j||ddS t j||ddS )a  
        Moonshot text-only models don't support content in list format.
        Multimodal models (kimi-k2.5, kimi-latest, etc.) accept the
        standard OpenAI content array with non-text blocks (image_url,
        input_audio, video_url, file, etc.).

        If any message contains a non-text content part, skip flattening
        so the multimodal payload is preserved.
        Fcontentc                 s   s    | ]
}| d dkV  qdS )typetextN)get).0cr   r   r   	<genexpr>0   s    z9MoonshotChatConfig._transform_messages.<locals>.<genexpr>T)r   r   r   )r   
isinstancelistanyr
   superr   )r   r   r   r   Zhas_non_textm_content	__class__r   r   r       s"   
api_baseapi_keyc                 C   s$   |pt dpd}|pt d}||fS )NZMOONSHOT_API_BASEhttps://api.moonshot.ai/v1ZMOONSHOT_API_KEYr   )r   r+   r,   Zdynamic_api_keyr   r   r   $_get_openai_compatible_provider_info@   s   z7MoonshotChatConfig._get_openai_compatible_provider_infoNoptional_paramslitellm_paramsstreamc                 C   s    |sd}| 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/   r0   r1   r   r   r   get_complete_urlK   s
   

z#MoonshotChatConfig.get_complete_urlc                    sN   dg}d|v r| ddg t j|d}g }|D ]}||vr$|| q|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
        Z	functionszkimi-thinking-previewZtoolstool_choice)r   )extendr&   get_supported_openai_paramsappend)r   r   Zexcluded_paramsZbase_openai_paramsZfinal_paramsparamr)   r   r   r6   _   s   	
z.MoonshotChatConfig.get_supported_openai_paramsnon_default_paramsdrop_paramsc                 C   s   |  |}| D ]\}}|dkr||d< q	||v r|||< q	d|v r?|d dkr-d|d< |d dk r?|dddk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
        Zmax_completion_tokensZ
max_tokensZtemperature   g333333?n)r6   itemsr   )r   r9   r/   r   r:   Zsupported_openai_paramsr8   valuer   r   r   map_openai_paramsv   s   

z$MoonshotChatConfig.map_openai_paramsheadersc                    s4   | dddkr| j||d}t j|||||d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/   r0   r@   )r   !_add_tool_choice_required_messager&   transform_request)r   r   r   r/   r0   r@   r)   r   r   rC      s   z$MoonshotChatConfig.transform_requestc                 C   s   | ddd |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.)Zroler   r4   )r7   pop)r   r   r/   r   r   r   rB      s   
z4MoonshotChatConfig._add_tool_choice_required_message)Fr   )__name__
__module____qualname__r	   r   r   strr   r   r   r   boolr   r   r   r.   dictr3   r$   r6   r?   rC   rB   __classcell__r   r   r)   r   r      s    	 


#&r   N)__doc__typingr   r   r   r   r   r   r   r	   Z8litellm.litellm_core_utils.prompt_templates.common_utilsr
   Zlitellm.secret_managers.mainr   Zlitellm.types.llms.openair   Zopenai.chat.gpt_transformationr   r   r   r   r   r   <module>   s    (