o
    ưi                     @   s   d dl Z d dlmZ d dlmZmZmZ d dlZd dlm	Z	 dZ
G dd de	Zed Zg dZd	efd
dZedddedefddZdS )    N)	lru_cache)LiteralOptionalUnion)BaseLLMExceptionzhttps://huggingface.coc                
       sL   e Zd Z			ddeej deej deeeje	f  f fddZ
  ZS )HuggingFaceErrorNrequestresponseheadersc                    s   t  j|||||d d S )N)status_codemessager   r	   r
   )super__init__)selfr   r   r   r	   r
   	__class__ \/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/huggingface/common_utils.pyr      s   
zHuggingFaceError.__init__)NNN)__name__
__module____qualname__r   httpxRequestResponser   ZHeadersdictr   __classcell__r   r   r   r   r      s    r   )ztext-generation-inferenceZconversationalztext-classificationztext-generationgenerated_textc                 C   sh   g d}|D ]+}|   |r| |dd} | |r1| ddd |ddd ddddd } q| S )z
    Parse the output text to remove any special characters. In our current approach we just check for ChatML tokens.

    Initial issue that prompted this - https://github.com/BerriAI/litellm/issues/763
    )z<|assistant|>z
<|system|>z<|user|>z<s>z</s>    N)strip
startswithreplaceendswith)r   Zchat_template_tokenstokenr   r   r   output_parser-   s   
,r%      )maxsizemodelreturnc              
   C   s   ddi}t drdt d |d< t d|  }ddgi}z!tj|||d	}|  | d}|d
u r>td|  |W S  tjyq } z$t	|dr]t
|jdd}t
|jdi }nd}i }tdt| ||dd
}~ww )aT  
    Fetch provider mappings for a model from the Hugging Face Hub.

    Args:
        model: The model identifier (e.g., 'meta-llama/Llama-2-7b')

    Returns:
        dict: The inference provider mapping for the model

    Raises:
        ValueError: If no provider mapping is found
        HuggingFaceError: If the API request fails
    Acceptzapplication/jsonZHUGGINGFACE_API_KEYzBearer Authorizationz/api/models/expandZinferenceProviderMapping)r
   paramsNz$No provider mapping found for model r	   r   i  r
   z"Failed to fetch provider mapping: )r   r   r
   )osgetenv
HF_HUB_URLr   getraise_for_statusjson
ValueError	HTTPErrorhasattrgetattrr	   r   str)r(   r
   pathr-   r	   Zprovider_mappinger   r   r   r   !_fetch_inference_provider_mapping<   s2   


r;   )r.   	functoolsr   typingr   r   r   r   Z)litellm.llms.base_llm.chat.transformationr   r0   r   Zhf_tasksZhf_task_listr8   r%   r   r;   r   r   r   r   <module>   s    