o
    ưiO                     @   s   d dl Z d dlZd dlmZmZmZmZmZmZ d dl	Z	d dl
mZmZ er/d dlmZ eZneZd dlmZ ddlmZ dd	lmZmZ e eZd
ZdedefddZG dd deZdS )    N)TYPE_CHECKINGAnyDictListOptionalUnion)AllMessageValuesChatCompletionRequest)Logging)BaseLLMException   )OpenAIGPTConfig   )HuggingFaceError!_fetch_inference_provider_mappingzhttps://router.huggingface.co	model_urlreturnc                 C   s2   |  d} | dr| d7 } | ds| d7 } | S )N/z/v1z/chat/completions/v1/chat/completions)rstripendswith)r    r   c/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/huggingface/chat/transformation.py_build_chat_completion_url   s   


r   c                   @   s   e Zd ZdZ		ddededee dedede	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	e d
e	e 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e dededed
efddZdS )HuggingFaceChatConfigzQ
    Reference: https://huggingface.co/docs/huggingface_hub/guides/inference
    Nheadersmodelmessagesoptional_paramslitellm_paramsapi_keyapi_baser   c           	      C   s.   ddi}|d urd| |d< i ||}|S )Nzcontent-typezapplication/jsonzBearer Authorizationr   )	selfr   r   r   r   r   r    r!   default_headersr   r   r   validate_environment.   s   z*HuggingFaceChatConfig.validate_environmenterror_messagestatus_codec                 C   s   t |||dS )N)r'   messager   )r   )r#   r&   r'   r   r   r   r   get_error_classB   s   z%HuggingFaceChatConfig.get_error_classbase_urlc                 C   s4   | dr	|}|S |du rtdptdd}|S )z
        Get the API base for the Huggingface API.

        Do not add the chat/embedding/rerank extension here. Let the handler do this.
        zhttp://zhttps://NHF_API_BASEHUGGINGFACE_API_BASE )
startswithosgetenv)r#   r   r*   r   r   r   get_base_urlI   s   
z"HuggingFaceChatConfig.get_base_urlstreamc                 C   s   |dur|}t |}nbtdstdr$ttdp"ttd}nI|dr0|}t |}n=d}|dd\}}	d|	v rm|}
|
dkrM|
 d	| d
}n|
dkrW|
 d}n|
dkra|
 d}n|
 d
}t d| }|d}|S )zr
        Get the complete URL for the API call.
        For provider-specific routing through huggingface
        Nr,   r-   r+   z1https://router.huggingface.co/v1/chat/completionsr      zhf-inferencez/models/r   Znovitaz/v3/openai/chat/completionszfireworks-aiz/inference/v1/chat/completions)r   r0   r1   strr/   splitBASE_URLr   )r#   r!   r    r   r   r   r3   Zcomplete_url
first_part	remainingproviderZrouter   r   r   get_complete_urlU   s0   




z&HuggingFaceChatConfig.get_complete_urlc                 C   s   | drttd||d|S d|v r td |dd  |dd\}}|}d|v ra|}	|}
t|
}|	|vrGtd|
 d|	 d	i d
||	 }|d dkr]td|
 d|	 d |d }| j	||d}ttd||d|S )Nr!   )r   r   max_retriesz3`max_retries` is not supported. It will be ignored.r   r4   zModel z is not supported for provider i  )r(   r'   r   statusZstagingz! is in staging mode for provider z. Meant for test purposes only.Z
providerId)r   r   r   )
getdictr	   loggerwarningpopr6   r   r   Z_transform_messages)r#   r   r   r   r   r   r8   r9   Zmapped_modelr:   Zmodel_idZprovider_mappingr   r   r   transform_request   sB   

z'HuggingFaceChatConfig.transform_request)NN)N)__name__
__module____qualname____doc__r?   r5   r   r   r   r   r%   intr   httpxZHeadersr   r)   r2   boolr;   rC   r   r   r   r   r   )   sx    	


,r   )loggingr0   typingr   r   r   r   r   r   rI   Zlitellm.types.llms.openair   r	   Z*litellm.litellm_core_utils.litellm_loggingr
   ZLiteLLMLoggingObjZLoggingClassZ)litellm.llms.base_llm.chat.transformationr   Zopenai.chat.gpt_transformationr   Zcommon_utilsr   r   	getLoggerrD   r@   r7   r5   r   r   r   r   r   r   <module>   s      
