o
    ưi                     @   s   d Z ddlmZmZmZ ddlZddlmZmZ ddl	m
Z
 ddl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G dd deZdS )z
Support for OVHCloud AI Endpoints `/v1/chat/completions` endpoint.

Our unified API follows the OpenAI standard.
More information on our website: https://endpoints.ai.cloud.ovh.net
    )OptionalUnionListN)ModelResponseStreamget_model_info)OpenAIGPTConfig)verbose_loggerOVHCloudException)BaseModelResponseIterator)BaseLLMException)AllMessageValuesc                       s   e Zd Zedee fddZded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dee	ejf defddZde	d
e	dede
de	f
 fddZdedee d
e	de	de	de	f fddZ  ZS )OVHCloudChatConfigreturnc                 C   s   dS )Novhcloud )selfr   r   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/ovhcloud/chat/transformation.pycustom_llm_provider   s   z&OVHCloudChatConfig.custom_llm_providermodelc              
      s   d}zt |dd}|dd}W n ty* } ztd|  W Y d}~nd}~ww t |}|durNtd |d	 |d
 |d |d |S )z
        Details about function calling support can be found here:
        https://help.ovhcloud.com/csm/en-gb-public-cloud-ai-endpoints-function-calling?id=kb_article_view&sysparm_article=KB0071907
        Nr   )r   supports_function_callingFz'Error getting supported OpenAI params: TznYou can see our models supporting function_calling in our catalog: https://endpoints.ai.cloud.ovh.net/catalog ZtoolsZtool_choiceZfunction_callZresponse_format)r   get	Exceptionr   debugsuperget_supported_openai_paramsremove)r   r   r   Z
model_infoeoptional_params	__class__r   r   r      s*   



z.OVHCloudChatConfig.get_supported_openai_paramsNapi_baseapi_keyr   litellm_paramsstreamc                 C   s$   |d u rdn| d}| d}|S )Nz0https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/z/chat/completions)rstrip)r   r!   r"   r   r   r#   r$   Zcomplete_urlr   r   r   get_complete_url1   s   	
z#OVHCloudChatConfig.get_complete_urlerror_messagestatus_codeheadersc                 C   s   t |||dS )Nmessager)   r*   r	   )r   r(   r)   r*   r   r   r   get_error_class>   s
   z"OVHCloudChatConfig.get_error_classnon_default_paramsdrop_paramsc                    s   t  ||||}|S N)r   map_openai_params)r   r.   r   r   r/   Zmapped_openai_paramsr   r   r   r1   J   s   z$OVHCloudChatConfig.map_openai_paramsmessagesc                    s.   | di }t |||||}|| |S )N
extra_body)popr   transform_requestupdate)r   r   r2   r   r#   r*   r3   responser   r   r   r5   V   s   

z$OVHCloudChatConfig.transform_requestr0   )__name__
__module____qualname__propertyr   strr   listr   dictboolr'   intr   httpxZHeadersr   r-   r1   r   r   r5   __classcell__r   r   r   r   r      sj    !

r   c                   @   s"   e Zd ZdZdedefddZdS )&OVHCloudChatCompletionStreamingHandlerzO
    Handler for OVHCloud AI Endpoints streaming chat completion responses
    chunkr   c              
   C   s   zUd|v r|d }d |dd}t||ddddid	g }|d
 D ]}d|v r<d|d v r<|d d|d d< || q%t|d d|d |d|d |dW S  typ } ztd| d| dddid	d}~w ty} } z|d}~ww )zA
        Parse individual chunks from streaming response
        errorzOVHCloud Error: {}r,   zUnknown errorcodei  zContent-Typezapplication/jsonr+   choicesdeltaZ	reasoningZreasoning_contentidzchat.completion.chunkcreatedusager   )rI   objectrJ   rK   r   rG   z
KeyError: z), Got unexpected response from CometAPI: N)formatr   r
   appendr   KeyErrorr   )r   rD   Zerror_chunkr(   Znew_choiceschoicer   r   r   r   chunk_parserj   sF   

z3OVHCloudChatCompletionStreamingHandler.chunk_parserN)r8   r9   r:   __doc__r>   r   rQ   r   r   r   r   rC   e   s    rC   )rR   typingr   r   r   rA   Zlitellm.utilsr   r   Z+litellm.llms.openai.chat.gpt_transformationr   Zlitellm._loggingr   Zlitellm.llms.ovhcloud.utilsr
   Z)litellm.llms.base_llm.base_model_iteratorr   Z)litellm.llms.base_llm.chat.transformationr   Zlitellm.types.llms.openair   r   rC   r   r   r   r   <module>   s    S