
    dh                         S r SSKJrJrJrJrJr  SSKJr  SSK	J
r
Jr  SSKJrJrJr  SSKJrJrJrJrJr  SSKJrJrJrJr  SSKJr  SS	KJr  SS
KJ r   SSK!J"r"  SSK#J$r$  Sr%\" SSSS9 " S S\5      5       r&g)zHugging Face Chat Wrapper.    )AnyAsyncIteratorIteratorListOptional)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModelagenerate_from_streamgenerate_from_stream)	AIMessageAIMessageChunkBaseMessageHumanMessageSystemMessage)ChatGenerationChatGenerationChunk
ChatResult	LLMResult)model_validator)Self)HuggingFaceEndpoint)HuggingFaceHub)HuggingFaceTextGenInferencez4You are a helpful, respectful, and honest assistant.z0.0.37z1.0z%langchain_huggingface.ChatHuggingFace)sinceremovalalternative_importc                     ^  \ rS rSr% Sr\\S'    \" \S9r	\\S'   Sr
\\S'   Sr\\   \S'   S	r\\S
'   S\4U 4S jjr\" SS9S\4S j5       r  S S\\   S\\\      S\\   S\S\\   4
S jjr  S S\\   S\\\      S\\   S\S\\   4
S jjr  S S\\   S\\\      S\\   S\S\4
S jjr  S S\\   S\\\      S\\   S\S\4
S jjrS\\   S\4S jr S\S\!4S jr"\#S\$S\4S j5       r%S!S jr&\'S\4S j5       r(Sr)U =r*$ )"ChatHuggingFace(   a~  
Wrapper for using Hugging Face LLM's as ChatModels.

Works with `HuggingFaceTextGenInference`, `HuggingFaceEndpoint`,
and `HuggingFaceHub` LLMs.

Upon instantiating this class, the model_id is resolved from the url
provided to the LLM, and the appropriate tokenizer is loaded from
the HuggingFace Hub.

Adapted from: https://python.langchain.com/docs/integrations/chat/llama2_chat
llmcontentsystem_messageN	tokenizermodel_idF	streamingkwargsc                    > [         TU ]  " S0 UD6  SSKJn  U R	                  5         U R
                  c!  UR                  U R                  5      U l        g U R
                  U l        g )Nr   )AutoTokenizer )super__init__transformersr+   _resolve_model_idr&   from_pretrainedr'   )selfr)   r+   	__class__s      c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/chat_models/huggingface.pyr.   ChatHuggingFace.__init__C   sY    "6".  ~~% ))$--8 	  	    after)modereturnc                     [        U R                  [        [        [        45      (       d!  [        S[        U R                  5       35      eU $ )NzeExpected llm to be one of HuggingFaceTextGenInference, HuggingFaceEndpoint, HuggingFaceHub, received )
isinstancer"   r   r   r   	TypeErrortyper2   s    r4   validate_llmChatHuggingFace.validate_llmP   sQ    HH(*=~N
 
 AAEdhh@PR  r6   messagesstoprun_managerc              +      #    U R                  U5      nU R                  R                  " U40 UD6 H/  nUn[        [	        US9S9nU(       a  UR                  XxS9  Uv   M1     g 7fNr#   )message)chunk)_to_chat_promptr"   streamr   r   on_llm_new_token	r2   rA   rB   rC   r)   requestdatadeltarG   s	            r4   _streamChatHuggingFace._stream\   s`      &&x0HHOOG6v6DE'u0MNE,,U,@K 7s   A#A%c                   #    U R                  U5      nU R                  R                  " U40 UD6  S h  vN nUn[        [	        US9S9nU(       a  UR                  XxS9I S h  vN   U7v   M@   N; N
 g 7frE   )rH   r"   astreamr   r   rJ   rK   s	            r4   _astreamChatHuggingFace._astreaml   sx      &&x0((**7=f= 	$E'u0MNE!2252FFFK	 G	 >s7   /A7A5A1A5,A7%A3&A71A53A75A7c                     U R                   (       a   U R                  " U4X#S.UD6n[        U5      $ U R                  U5      nU R                  R
                  " SU/X#S.UD6nU R                  U5      $ N)rB   rC   )promptsrB   rC   r,   )r(   rO   r   rH   r"   	_generate_to_chat_resultr2   rA   rB   rC   r)   stream_iter	llm_input
llm_results           r4   rX   ChatHuggingFace._generate{   s     >>,,#@FK (44((2	XX'' 
Kd
GM

 ##J//r6   c                   #    U R                   (       a(  U R                  " U4X#S.UD6n[        U5      I S h  vN $ U R                  U5      nU R                  R
                  " SU/X#S.UD6I S h  vN nU R                  U5      $  NN N7frV   )r(   rS   r   rH   r"   
_ageneraterY   rZ   s           r4   r`   ChatHuggingFace._agenerate   s      >>--#@FK /{;;;((2	88.. 
Kd
GM
 

 ##J// <
s!   5B
B8B
0B1B
B
c                     U(       d  [        S5      e[        US   [        5      (       d  [        S5      eU Vs/ sH  o R                  U5      PM     nnU R                  R                  USSS9$ s  snf )zHConvert a list of messages into a prompt format expected by wrapped LLM.z+At least one HumanMessage must be provided!z$Last message must be a HumanMessage!FT)tokenizeadd_generation_prompt)
ValueErrorr;   r   _to_chatml_formatr&   apply_chat_template)r2   rA   mmessages_dictss       r4   rH   ChatHuggingFace._to_chat_prompt   sv    
 JKK(2,55CDD=EFX003XF~~11U$ 2 
 	
 Gs   A0rF   c                     [        U[        5      (       a  SnOG[        U[        5      (       a  SnO/[        U[        5      (       a  SnO[	        S[        U5       35      eX!R                  S.$ )z+Convert LangChain message to ChatML format.system	assistantuserzUnknown message type: )roler$   )r;   r   r   r   rf   r=   r$   )r2   rF   rp   s      r4   rg   !ChatHuggingFace._to_chatml_format   s^     g}--D++D..D5d7m_EFF99r6   r]   c                     / nU R                   S    H9  n[        [        UR                  S9UR                  S9nUR                  U5        M;     [        XR                  S9$ )Nr   r#   )rF   generation_info)generations
llm_output)rt   r   r   textrs   appendr   ru   )r]   chat_generationsgchat_generations       r4   rY   ChatHuggingFace._to_chat_result   sc    ''*A,!!&&11CTCTO ##O4	 + (5J5J
 	
r6   c                 2   SSK Jn  U" S5      n[        U R                  [        5      (       d6  [        U R                  S5      (       a7  U R                  R                  (       a  U R                  R                  U l        g[        U R                  [        5      (       a  U R                  R                  nOU R                  R                  nU H&  nUR                  U:X  d  M  UR                  U l        M(     U R                  (       d  [        SU S35      eg)z8Resolve the model_id from the LLM's inference_server_urlr   )list_inference_endpoints*repo_idNzIFailed to resolve model_id:Could not find model id for inference server: zBMake sure that your Hugging Face token has access to the endpoint.)huggingface_hubr}   r;   r"   r   hasattrr   r'   r   inference_server_urlendpoint_urlurl
repositoryrf   )r2   r}   available_endpointsr   endpoints        r4   r0   !ChatHuggingFace._resolve_model_id   s     	=6s;dhh//DHHi((TXX-=-= HH,,DM"=>>*.((*G*GL8800L+H|||+ ( 3 3 , }}AAMTU  r6   c                     g)Nzhuggingface-chat-wrapperr,   r>   s    r4   	_llm_typeChatHuggingFace._llm_type   s    )r6   )r'   r&   )NN)r9   N)+__name__
__module____qualname____firstlineno____doc__r   __annotations__r   DEFAULT_SYSTEM_PROMPTr%   r&   r'   r   strr(   boolr.   r   r   r?   r   r   r
   r   r   rO   r	   r   rS   r   rX   r`   rH   dictrg   staticmethodr   rY   r0   propertyr   __static_attributes____classcell__)r3   s   @r4   r    r    (   s3    
H$1:O$PNMPIs"Hhsm"It
 
 '"	d 	 #	 %):>	{# tCy! 67	
  
%	&& %)?C	{# tCy! ;<	
  
*	+$ %):>	0{#0 tCy!0 67	0
 0 
0, %)?C	0{#0 tCy!0 ;<	0
 0 
0&
{#
 

": : : 
I 
* 
 
6 *3 * *r6   r    N)'r   typingr   r   r   r   r   langchain_core._api.deprecationr    langchain_core.callbacks.managerr	   r
   *langchain_core.language_models.chat_modelsr   r   r   langchain_core.messagesr   r   r   r   r   langchain_core.outputsr   r   r   r   pydanticr   typing_extensionsr   -langchain_community.llms.huggingface_endpointr   (langchain_community.llms.huggingface_hubr   7langchain_community.llms.huggingface_text_gen_inferencer   r   r    r,   r6   r4   <module>r      s|      ? ? 6 
   % " M C S  
>
~*m ~*
~*r6   