
    h                         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e	      Zed   Zg dZd	efd
Z ed      dedefd       Zy)    N)	lru_cache)LiteralOptionalUnion)BaseLLMExceptionzhttps://huggingface.coc            
            e Zd Z	 	 	 ddeej
                     deej                     deeej                  e	f      f fdZ
 xZS )HuggingFaceErrorrequestresponseheadersc                 .    t         |   |||||       y )N)status_codemessager
   r   r   )super__init__)selfr   r   r
   r   r   	__class__s         a/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/huggingface/common_utils.pyr   zHuggingFaceError.__init__   s&     	# 	 	
    )NNN)__name__
__module____qualname__r   httpxRequestResponser   Headersdictr   __classcell__)r   s   @r   r	   r	      s[    
 ,0-18<
 %--(	

 5>>*
 %t 345
 
r   r	   )ztext-generation-inferenceconversationalztext-classificationztext-generationgenerated_textc                     g d}|D ]k  }| j                         j                  |      r| j                  |dd      } | j                  |      sG| ddd   j                  |ddd   dd      ddd   } m | 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    chat_template_tokenstokens      r   output_parserr+   -   s     V%!,,U3+33E2qAN""5)+DbD199%"+r1MdPRdSN	 &
 r      )maxsizemodelreturnc                 &   ddi}t        j                  d      rdt        j                  d       |d<   t         d|  }ddgi}	 t        j                  |||	      }|j                          |j                         j	                  d      }|t        d|        |S # t        j                  $ r^}t        |d      r/t        |j                  dd      }t        |j                  di       }nd}i }t        dt        |       ||      d
}~ww xY 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/jsonHUGGINGFACE_API_KEYzBearer Authorizationz/api/models/expand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   pathr6   r   provider_mappinger   s           r   !_fetch_inference_provider_mappingrE   <   s    +,G	yy&'%,RYY7L-M,N#O \eW-D345F
99T76B!!##==?../IJ#CE7KLL?? 
1j!!!**mSAKajj)R8GKG8QA#
 	

s   AB D2ADD)r7   	functoolsr   typingr   r   r   r   )litellm.llms.base_llm.chat.transformationr   r9   r	   hf_taskshf_task_listrA   r+   r   rE    r   r   <module>rL      st    	  + +  F%

' 
$ #  3)
S )
T )
 )
r   