
    ih                         d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZmZmZmZmZ ddlmZmZ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Z% eddd       G d de             Z&y)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                       e Zd ZU dZeed<   	  ee      Zeed<   dZ	eed<   dZ
ee   ed<   dZeed	<   d
ef fdZ ed      defd       Z	 	 ddee   deee      dee   d
edee   f
dZ	 	 ddee   deee      dee   d
edee   f
dZ	 	 ddee   deee      dee   d
edef
dZ	 	 ddee   deee      dee   d
edef
dZdee   defdZdede fdZ!e"de#defd       Z$ddZ%e&defd       Z' xZ(S ) ChatHuggingFacea  
    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        |   di | ddlm} | j	                          | j
                  !|j                  | j                        | _        y | j
                  | _        y )Nr   )AutoTokenizer )super__init__transformersr*   _resolve_model_idr%   from_pretrainedr&   )selfr(   r*   	__class__s      i/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain_community/chat_models/huggingface.pyr-   zChatHuggingFace.__init__C   sY    "6".  ~~% ))$--8 	  	    after)modereturnc                     t        | j                  t        t        t        f      s!t        dt        | j                               | S )NzeExpected llm to be one of HuggingFaceTextGenInference, HuggingFaceEndpoint, HuggingFaceHub, received )
isinstancer!   r   r   r   	TypeErrortyper1   s    r3   validate_llmzChatHuggingFace.validate_llmP   sL    HH(*=~N
 AAEdhh@PR  r4   messagesstoprun_managerc              +      K   | j                  |      } | j                  j                  |fi |D ]3  }|}t        t	        |            }|r|j                  ||       | 5 y wNr"   )message)chunk)_to_chat_promptr!   streamr   r   on_llm_new_token	r1   r>   r?   r@   r(   requestdatadeltarD   s	            r3   _streamzChatHuggingFace._stream\   sj      &&x0#DHHOOG6v6 	DE'u0MNE,,U%,@K	s   A&A(c                   K   | j                  |      } | j                  j                  |fi |2 3 d {   }|}t        t	        |            }|r|j                  ||       d {    | D7 ?7 6 y wrB   )rE   r!   astreamr   r   rG   rH   s	            r3   _astreamzChatHuggingFace._astreaml   s      &&x0*$((**7=f= 	 	$E'u0MNE!2252FFFK	 G	 >s7   /A;A9A5A91A;*A7+
A;5A97A;9A;c                     | j                   r! | j                  |f||d|}t        |      S | j                  |      } | j                  j
                  d|g||d|}| j                  |      S N)r?   r@   )promptsr?   r@   r+   )r'   rL   r   rE   r!   	_generate_to_chat_resultr1   r>   r?   r@   r(   stream_iter	llm_input
llm_results           r3   rS   zChatHuggingFace._generate{   s     >>&$,,#@FK (44((2	'TXX'' 
Kd
GM

 ##J//r4   c                   K   | j                   r) | j                  |f||d|}t        |       d {   S | j                  |      } | j                  j
                  d|g||d| d {   }| j                  |      S 7 O7 wrQ   )r'   rO   r   rE   r!   
_ageneraterT   rU   s           r3   rZ   zChatHuggingFace._agenerate   s      >>'$--#@FK /{;;;((2	.488.. 
Kd
GM
 

 ##J// <
s!   1BB9B-B.BBc                     |st        d      t        |d   t              st        d      |D cg c]  }| j                  |       }}| j                  j                  |dd      S c c}w )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)
ValueErrorr9   r   _to_chatml_formatr%   apply_chat_template)r1   r>   mmessages_dictss       r3   rE   zChatHuggingFace._to_chat_prompt   st    
 JKK(2,5CDD=EF$003FF~~11U$ 2 
 	
 Gs   A(rC   c                     t        |t              rd}n=t        |t              rd}n*t        |t              rd}nt	        dt        |             ||j                  dS )z+Convert LangChain message to ChatML format.system	assistantuserzUnknown message type: )roler#   )r9   r   r   r   r_   r;   r#   )r1   rC   rh   s      r3   r`   z!ChatHuggingFace._to_chatml_format   sW     g}-D+D.D5d7m_EFF99r4   rX   c                     g }| j                   d   D ]>  }t        t        |j                        |j                        }|j                  |       @ t        || j                        S )Nr   r"   )rC   generation_info)generations
llm_output)rk   r   r   textrj   appendr   rl   )rX   chat_generationsgchat_generations       r3   rT   zChatHuggingFace._to_chat_result   sj    ''* 	5A,!!&&11CTCTO ##O4		5 (Z5J5J
 	
r4   c                    ddl m}  |d      }t        | j                  t              s,t        | j                  d      r2| j                  j                  r| j                  j                  | _        yt        | j                  t              r| j                  j                  }n| j                  j                  }|D ]#  }|j                  |k(  s|j                  | _        % | j                  st        d| d      y)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_hubrs   r9   r!   r   hasattrru   r&   r   inference_server_urlendpoint_urlurl
repositoryr_   )r1   rs   available_endpointsry   endpoints        r3   r/   z!ChatHuggingFace._resolve_model_id   s     	=6s;dhh/DHHi(TXX-=-= HH,,DM"=>*.((*G*GL8800L+ 	4H|||+ ( 3 3	4 }}AAMTU  r4   c                      y)Nzhuggingface-chat-wrapperr+   r<   s    r3   	_llm_typezChatHuggingFace._llm_type   s    )r4   )NN)r7   N))__name__
__module____qualname____doc__r   __annotations__r   DEFAULT_SYSTEM_PROMPTr$   r%   r&   r   strr'   boolr-   r   r   r=   r   r   r
   r   r   rL   r	   r   rO   r   rS   rZ   rE   dictr`   staticmethodr   rT   r/   propertyr   __classcell__)r2   s   @r3   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 * *r4   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+   r4   r3   <module>r      s|      ? ? 6 
   % " M C S  
>
~*m ~*
~*r4   