
    KKi                       S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
  SSKJr  SSKJr  SSKJrJrJrJr  \(       a  SSK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"  SSK#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3  SSK4J5r5  SSK4J6r7  SSK8J9r9  SSK:J;r;J<r<J=r=  SSK>J?r?J@r@JArAJBrB  SSKCJDrDJErEJFrF  SSKGJHrH  SSKIJJrJJKrK  SSKLJMrM  SSKNJOrOJPrPJQrQ  SSKRJSrS  SSKTJUrU  SSKJr  SS	KJr  \" S\U5      rVS+S jrW\ " S S5      5       rX\ " S S5      5       rYS,S  jrZ    S-S! jr[S.S" jr\S/S# jr]S0S$ jr^      S1S% jr_S0S& jr`S0S' jraS0S( jrb " S) S*\ 5      rcg)2zHugging Face Chat Wrapper.    )annotationsN)AsyncIteratorCallableIteratorMappingSequence)	dataclass)
itemgetter)TYPE_CHECKINGAnyLiteralcastHuggingFaceEndpointHuggingFacePipeline)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LanguageModelInputModelProfileModelProfileRegistry)BaseChatModelagenerate_from_streamgenerate_from_stream)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageFunctionMessageChunkHumanMessageHumanMessageChunkInvalidToolCallSystemMessageSystemMessageChunkToolCallToolMessageToolMessageChunk)ToolCallChunk)tool_call_chunk)JsonOutputParser)JsonOutputKeyToolsParsermake_invalid_tool_callparse_tool_call)ChatGenerationChatGenerationChunk
ChatResult	LLMResult)RunnableRunnableMapRunnablePassthrough)BaseTool)convert_to_json_schemaconvert_to_openai_tool)is_basemodel_subclass)	BaseModelFieldmodel_validator)Self)	_PROFILESr   c                ^    [         R                  U 5      =(       d    0 nUR                  5       $ N)_MODEL_PROFILESgetcopy)
model_namedefaults     o/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain_huggingface/chat_models/huggingface.py_get_default_model_profilerI   O   s#    !!*-3G<<>    c                  .    \ rS rSr% SrS\S'   S\S'   Srg)	TGI_RESPONSET   z'Response from the TextGenInference API.z	list[Any]choicesdictusage N__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__rQ   rJ   rH   rL   rL   T   s    1KrJ   rL   c                  8    \ rS rSr% SrS\S'   S\S'   S\S'   Srg	)
TGI_MESSAGE\   z,Message to send to the TextGenInference API.strrolecontentz
list[dict]
tool_callsrQ   NrR   rQ   rJ   rH   r[   r[   \   s    6
ILrJ   r[   c                L    SU S   U S   [         R                  " U S   SS9S.S.$ )	NfunctionidnameargsF)ensure_asciird   	argumentstyperc   rb   )jsondumps)	tool_calls    rH   _lc_tool_call_to_hf_tool_callrn   e   s6    of%If$5EJ
 rJ   c                &    SU S   U S   U S   S.S.$ )Nrb   rc   rd   re   rg   ri   rQ   )invalid_tool_calls    rH   %_lc_invalid_tool_call_to_hf_tool_callrq   p   s.     %%f-*62
 rJ   c                2   [        U [        5      (       a  U R                  U R                  S.nGO[        U [        5      (       a  SU R                  S.nGO[        U [
        5      (       a  SU R                  S.nSU R                  ;   a   U R                  S   US'   US   S:X  a  SUS'   U R                  (       d  U R                  (       aQ  U R                   Vs/ s H  n[        U5      PM     snU R                   Vs/ s H  n[        U5      PM     sn-   US'   O"SU R                  ;   a  U R                  S   US'   SU;   a  US   S:X  a  SUS'   OO[        U [        5      (       a  S	U R                  S.nOp[        U [        5      (       a  S
U R                  U R                  S.nO@[        U [        5      (       a  SU R                  U R                  S.nOSU  3n[!        U5      eSU R                  ;   a  U R                  S   US'   U$ s  snf s  snf )zwConvert a LangChain message to a dictionary.

Args:
    message: The LangChain message.

Returns:
    The dictionary.

r^   r_   user	assistantfunction_callr_    Nr`   systemrb   )r^   r_   rd   tool)r^   r_   tool_call_idzGot unknown type rd   )
isinstancer   r^   r_   r#   r   additional_kwargsr`   invalid_tool_callsrn   rq   r&   r!   rd   r)   rz   	TypeError)messagemessage_dicttcmsgs       rH   _convert_message_to_dictr   }   s    ';'' 'I	G\	*	* &7??C	GY	'	' +Hg777,3,E,Eo,VL)I&",*.Y'!;!;<C<N<N*<Nb-b1<N* "444B 6b94*L& W666)0)B)B<)PL&<'L,Cr,I&*L#	G]	+	+ (W__E	G_	-	-LL

 
G[	)	)#00
 "'+n***&88@VA*s   H Hc                   U R                  S5      nUS:X  a  [        U R                  SS5      S9$ US:X  a  U R                  SS5      =(       d    Sn0 nU R                  S5      =n(       a  [        U5      US'   / n/ nU R                  S5      =n(       a'  XsS'   U H  n UR                  [	        US	S
95        M     [        UUUUS9$ US:X  a  [        U R                  SS5      S9$ US:X  a*  [        U R                  SS5      U R                  SS5      S9$ US:X  a;  0 nSU ;   a  U S   US'   [        U R                  SS5      U R                  SS5      US9$ [        U R                  SS5      U=(       d    SS9$ ! [
         a9  n	UR                  [        [        U[        U	5      5      5      5         Sn	A	GM  Sn	A	ff = f)zuConvert a dictionary to a LangChain message.

Args:
    _dict: The dictionary.

Returns:
    The LangChain message.

r^   rt   r_   rw   r_   ru   rv   r`   T)	return_idN)r_   r|   r`   r}   rx   rb   rd   r_   rd   ry   rz   )r_   rz   r|   r_   r^   )rD   r#   rO   appendr0   	Exceptionr/   r]   r   r&   r!   r)   r   )
_dictr^   r_   r|   rv   r`   r}   raw_tool_callsraw_tool_calles
             rH   _convert_dict_to_messager      s    99VDv~EIIi$<=={))Ir*0b"$!IIo66=615m1Do.
"YY|44>4.<l+!/%%omt&TU "0 /!1	
 	
 xUYYy"%=>>zIIi,599VR3H
 	
 v~U?(-ff%IIi,>26/
 	

 uyyB7djbII3 ! &--3M3q6JK s    E<<
F?-F::F?c                F     SSK Jn  [        X5      $ ! [         a     gf = f)Nr   )HuggingFaceHubF)(langchain_community.llms.huggingface_hubr   r{   ImportError)llmr   s     rH   _is_huggingface_hubr      s+    	
 #..     
  c                   U S   S   nUS   n[        [        UR                  S5      5      n[        [        UR                  S5      =(       d    S5      n0 n/ nUR                  S5      (       a#  [        US   5      nSU;   a  US   c  SUS'   XS'   UR                  S	5      =n	(       a  XS	'   U	 H  n
[        R
                  " [        5         UR                  [        U
S
   R                  S5      U
S
   R                  S5      U
R                  S5      U
R                  S5      S95        S S S 5        M     US:X  d
  U[        :X  a	  [        US9$ US:X  d
  U[        :X  ac  U R                  S5      =n(       a=  UR                  SS5      nUR                  SS5      nUUUR                  SX-   5      S.nOS n[        UUUUS9$ US:X  d
  U[        :X  a	  [        US9$ US
:X  d
  U[        :X  a  [        XSS   S9$ US:X  d
  U[        :X  a  [        XSS   S9$ U(       d
  U[        :X  a	  [        XTS9$ U" US9$ ! , (       d  f       GM  = f)NrN   r   deltar^   r_   rw   rv   rd   r`   rb   rh   rc   index)rd   re   rc   r   rt   r   ru   rP   prompt_tokenscompletion_tokenstotal_tokensinput_tokensoutput_tokensr   )r_   r|   tool_call_chunksusage_metadatarx   r   ry   rz   )r_   rz   r   )r   r]   rD   rO   
contextlibsuppressKeyErrorr   create_tool_call_chunkr$   r   r'   r"   r*   r    )chunkdefault_classchoicer   r^   r_   r|   r   rv   r   rtcrP   r   r   r   s                  rH   _convert_chunk_to_message_chunkr      sJ    9a F7OEUYYv&'D3		),23G ,.yy!!U?34]"}V'<'D$&M&!-:/*<00~0*8,'!C$$X. ''* _008 _00=774=!ggg.	 /. " v~*;; 11{m~=IIg&&5& 99_a8L!II&91=M ,!. %		.,:V WN "N/-)	
 	
 x=,>>!'22z].BB#G-HHv~*::N>STT} 00;;))I /.s   AH44
I	c                F     SSK Jn  [        X5      $ ! [         a     gf = f)Nr   HuggingFaceTextGenInferenceF)7langchain_community.llms.huggingface_text_gen_inferencer   r{   r   )r   r   s     rH   !_is_huggingface_textgen_inferencer   0  s+    	
 #;; r   c                "    [        U [        5      $ rB   )r{   r   r   s    rH   _is_huggingface_endpointr   <      c.//rJ   c                "    [        U [        5      $ rB   )r{   r   r   s    rH   _is_huggingface_pipeliner   @  r   rJ   c                  d  ^  \ rS rSr% SrS\S'    SrS\S'    SrS\S'    SrS	\S
'    \	" SSS9r
S\S'    SrS	\S'    SrS	\S'    SrS\S'    SrS\S'    SrS\S'    SrS\S'    SrS\S'    SrS\S'    SrS\S'    SrS	\S'    SrS\S'    \	" \S9rS \S!'    S=U 4S" jjrS>S# jr\" S$S%9S?S& j5       r\" S$S%9S?S' j5       r\  S@         SAS( jj5       rSBS) jr   SC           SDS* jjr    SC           SES+ jjr!SS,.     SFS- jjr"  SGSS,.           SHS. jjjr#  SGSS,.           SIS/ jjjr$    SJS0 jr%SKS1 jr&\'SLS2 j5       r(S>S3 jr)SS4.       SMU 4S5 jjjr* SNS6SS7.         SOS8 jjjr+      SPS9 jr,\-SQS: j5       r.\-SRS; j5       r/S<r0U =r1$ )SChatHuggingFaceiD  u*  Hugging Face LLM's as ChatModels.

Works with `HuggingFaceTextGenInference`, `HuggingFaceEndpoint`,
`HuggingFaceHub`, and `HuggingFacePipeline` 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.

Setup:
    Install `langchain-huggingface` and ensure your Hugging Face token
    is saved.

    ```bash
    pip install langchain-huggingface
    ```

    ```python
    from huggingface_hub import login

    login()  # You will be prompted for your HF key, which will then be saved locally
    ```

Key init args — completion params:
    llm:
        LLM to be used.

Key init args — client params:
    custom_get_token_ids:
        Optional encoder to use for counting tokens.
    metadata:
        Metadata to add to the run trace.
    tags:
        Tags to add to the run trace.
    verbose:
        Whether to print out response text.

See full list of supported init args and their descriptions in the params
section.

Instantiate:
    ```python
    from langchain_huggingface import HuggingFaceEndpoint,
    ChatHuggingFace

    model = HuggingFaceEndpoint(
        repo_id="microsoft/Phi-3-mini-4k-instruct",
        task="text-generation",
        max_new_tokens=512,
        do_sample=False,
        repetition_penalty=1.03,
    )

    chat = ChatHuggingFace(llm=model, verbose=True)
    ```

Invoke:
    ```python
    messages = [
        ("system", "You are a helpful translator. Translate the user
        sentence to French."),
        ("human", "I love programming."),
    ]

    chat(...).invoke(messages)
    ```

    ```python
    AIMessage(content='Je ai une passion pour le programme.\n\nIn
    French, we use "ai" for masculine subjects and "a" for feminine
    subjects. Since "programming" is gender-neutral in English, we
    will go with the masculine "programme".\n\nConfirmation: "J\'aime
    le programme." is more commonly used. The sentence above is
    technically accurate, but less commonly used in spoken French as
    "ai" is used less frequently in everyday speech.',
    response_metadata={'token_usage': ChatCompletionOutputUsage
    (completion_tokens=100, prompt_tokens=55, total_tokens=155),
    'model': '', 'finish_reason': 'length'},
    id='run-874c24b7-0272-4c99-b259-5d6d7facbc56-0')
    ```

Stream:
    ```python
    for chunk in chat.stream(messages):
        print(chunk)
    ```

    ```python
    content='Je ai une passion pour le programme.\n\nIn French, we use
    "ai" for masculine subjects and "a" for feminine subjects.
    Since "programming" is gender-neutral in English,
    we will go with the masculine "programme".\n\nConfirmation:
    "J\'aime le programme." is more commonly used. The sentence
    above is technically accurate, but less commonly used in spoken
    French as "ai" is used less frequently in everyday speech.'
    response_metadata={'token_usage': ChatCompletionOutputUsage
    (completion_tokens=100, prompt_tokens=55, total_tokens=155),
    'model': '', 'finish_reason': 'length'}
    id='run-7d7b1967-9612-4f9a-911a-b2b5ca85046a-0'
    ```

Async:
    ```python
    await chat.ainvoke(messages)
    ```

    ```python
    AIMessage(content='Je déaime le programming.\n\nLittérale : Je
    (j\'aime) déaime (le) programming.\n\nNote: "Programming" in
    French is "programmation". But here, I used "programming" instead
    of "programmation" because the user said "I love programming"
    instead of "I love programming (in French)", which would be
    "J\'aime la programmation". By translating the sentence
    literally, I preserved the original meaning of the user\'s
    sentence.', id='run-fd850318-e299-4735-b4c6-3496dc930b1d-0')
    ```

Tool calling:
    ```python
    from pydantic import BaseModel, Field

    class GetWeather(BaseModel):
        '''Get the current weather in a given location'''

        location: str = Field(..., description="The city and state,
        e.g. San Francisco, CA")

    class GetPopulation(BaseModel):
        '''Get the current population in a given location'''

        location: str = Field(..., description="The city and state,
        e.g. San Francisco, CA")

    chat_with_tools = chat.bind_tools([GetWeather, GetPopulation])
    ai_msg = chat_with_tools.invoke("Which city is hotter today and
    which is bigger: LA or NY?")
    ai_msg.tool_calls
    ```

    ```python
    [
        {
            "name": "GetPopulation",
            "args": {"location": "Los Angeles, CA"},
            "id": "0",
        }
    ]
    ```

Response metadata
    ```python
    ai_msg = chat.invoke(messages)
    ai_msg.response_metadata
    ```

    ```python
    {
        "token_usage": ChatCompletionOutputUsage(
            completion_tokens=100, prompt_tokens=8, total_tokens=108
        ),
        "model": "",
        "finish_reason": "length",
    }
    ```
r   r   N	tokenizer
str | Nonemodel_idzfloat | Nonetemperaturestop_sequences)rG   aliaszstr | list[str] | Nonestoppresence_penaltyfrequency_penaltyz
int | Noneseedbool | Nonelogprobstop_logprobszdict[int, int] | None
logit_biasFbool	streamingstream_usagentop_p
max_tokens)default_factorydict[str, Any]model_kwargsc                f   > [         TU ]  " S0 UD6  U R                  5         U R                  5         g )NrQ   )super__init___inherit_llm_properties_resolve_model_id)selfkwargs	__class__s     rH   r   ChatHuggingFace.__init__  s-    "6" 	$$& rJ   c                Z   [        U S5      (       a  U R                  c  gSSSSSSS	.nUR                  5        Hb  u  p#[        U R                  U5      (       d  M"  [        U R                  U5      n[        XS5      nU(       a  MM  U(       d  MV  [	        XU5        Md     [        U R                  5      (       ar  S
S0nUR                  5        HZ  u  p#[        U R                  U5      (       d  M"  [        U R                  U5      n[        XS5      nUb  MI  Uc  MN  [	        XU5        M\     U R                  (       dp  [        U R                  S5      (       aT  [        U R                  R                  [        5      (       a*  U R                  R                  R                  5       U l        gggg)zGInherit properties from the wrapped LLM instance if not explicitly set.r   Nr   max_new_tokensr   r   r   r   )r   r   r   r   r   r   r   repetition_penaltyr   )
hasattrr   itemsgetattrsetattrr   r   r{   rO   rE   )r   property_mappings	chat_propllm_prop	llm_value
chat_valueendpoint_mappingss          rH   r   'ChatHuggingFace._inherit_llm_properties  sh   tU##txx'7 )*$$
 $5#:#:#<Itxx**#DHHh7	$Td;
!ziiDY7 $= $DHH-- $%9! (9'>'>'@#	488X.. '( ;I!($!?J!)i.C; (A !!.1148800$77 $ 5 5 : : <D 8 2 "rJ   after)modec                   [        U R                  5      (       dq  [        U R                  5      (       dW  [        U R                  5      (       d=  [	        U R                  5      (       d#  S[        U R                  5       3n[        U5      eU $ )NzyExpected llm to be one of HuggingFaceTextGenInference, HuggingFaceEndpoint, HuggingFaceHub, HuggingFacePipeline received )r   r   r   r   r   rj   r~   )r   r   s     rH   validate_llmChatHuggingFace.validate_llmG  sn     $DHH--5dhh??,TXX66,TXX66 N+- 
 C. rJ   c                v    U R                   c+  U R                  (       a  [        U R                  5      U l         U $ )z$Set model profile if not overridden.)profiler   rI   r   s    rH   _set_model_profile"ChatHuggingFace._set_model_profileW  s)     <<DMM5dmmDDLrJ   c                .   US:X  a  SSK Jn  Ub  UOSn0 n/ SQnU H  nX;   d  M
  UR                  U5      Xh'   M     SU;  a  0 US'   0 n	[        UR	                  5       5       H?  u  pU
S:X  a  XS'   UR                  U
5        M"  U
S	;   d  M*  XU
'   UR                  U
5        MA     US   R                  U	5        UR                  " SXS
.UD6nO>US:X  a  SSKJn  U" SXS.UD6nO'US:X  a  SSK	J
n  U" SSU0UD6nOSU 3n[        U5      eU " SSU0UD6$ )aC  Construct a ChatHuggingFace model from a model_id.

Args:
    model_id: The model ID of the Hugging Face model.
    task: The task to perform (e.g., "text-generation").
    backend: The backend to use. One of "pipeline", "endpoint", "text-gen".
    **kwargs: Additional arguments to pass to the backend or ChatHuggingFace.
pipeliner   r   ztext-generation)backenddevice
device_mapr   pipeline_kwargs
batch_sizer   r   r   )r   r   r   top_kr   	do_sample)r   taskendpointr   )repo_idr   ztext-genr   inference_server_urlzUnknown backend: r   rQ   )/langchain_huggingface.llms.huggingface_pipeliner   poplistr   updatefrom_model_id/langchain_huggingface.llms.huggingface_endpointr   r   r   
ValueError)clsr   r   r   r   r   pipeline_specific_kwargspipeline_keyskeygeneration_paramskvr   r   r   r   s                   rH   r  ChatHuggingFace.from_model_id^  sk   & j   +41BD (*$M %=4:JJsO,1 % !(@@>@():; !#V\\^,$:;&67JJqM   ,-a(JJqM - %%67>>?PQ &33 !0HC 
" &LhLVLC
" .V8VvVC%gY/CS/!%s%f%%rJ   c                   / nUR                  S0 5      nUS    H  n[        US   5      nU(       aP  [        U[        5      (       a;  UR                  SS5      UR                  SS5      UR                  SS5      S.Ul        S	UR                  S	5      0nS
U;   a  US
   US
'   [        UUS9nUR                  U5        M     UU R                  UR                  SS5      S.n[        X(S9$ )NrP   rN   r   r   r   r   r   r   finish_reasonr   r   generation_infosystem_fingerprintrw   )token_usagerF   r  generations
llm_output)	rD   r   r{   r   r   r1   r   r   r3   )	r   responser  r  resr   r  genr  s	            rH   _create_chat_result#ChatHuggingFace._create_chat_result  s    ll7B/I&C.s9~>Gz'9==$/OOOQ$G%0__5H!%L$/OONA$F*&
  /0HIOS .1*o
+  /C s# '" '--"*,,/CR"H


 kIIrJ   c                   Ub  UOU R                   n[        U R                  5      (       aL  U R                  X5      u  pxU R                  R                  R
                  " SSU0UD6n	U R                  U	5      $ [        U R                  5      (       a  U(       a   U R                  " U4X#S.UD6n
[        U
5      $ U R                  X5      u  pxSU0UEUb  SU0O0 EUEnU R                  R                  R                  " SSU0UD6n	U R                  U	5      $ U R                  U5      nU(       a*  U R                  R                  " U4X#S.UD6n
[        U
5      $ U R                  R                  " SU/X#S.UD6nU R                  U5      $ )Nmessagesr   run_managerr   streampromptsr   r  rQ   )r   r   r   _create_message_dictsclientchatr  r   _streamr   chat_completion_to_chat_prompt	_generate_to_chat_result)r   r  r   r  r  r   should_streammessage_dictsparamsanswerstream_iter	llm_input
llm_results                rH   r'  ChatHuggingFace._generate  s    #)"4$..,TXX66$($>$>x$N!MXX__))K=KFKF++F33#DHH--"ll#'DJ ,K88$($>$>x$N!M *0);Hf% 	F XX__44VmVvVF++F33((2	((** $AGK (44XX'' 
Kd
GM

 ##J//rJ   c                T  #    [        U R                  5      (       aT  U R                  X5      u  pgU R                  R                  R                  " SSU0UD6I S h  vN nU R                  U5      $ [        U R                  5      (       a  Ub  UOU R                  n	U	(       a(  U R                  " U4X#S.UD6n
[        U
5      I S h  vN $ U R                  X5      u  pg0 UEUb  SU0O0 EUEnU R                  R                  R                  " SSU0UD6I S h  vN nU R                  U5      $ [        U R                  5      (       a  Sn[        U5      e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      $  GND N N N7f)Nr  r  r  z:async generation is not supported with HuggingFacePipeliner  rQ   )r   r   r!  async_clientr#  r  r   r   _astreamr   r%  r   NotImplementedErrorr&  
_agenerater(  )r   r  r   r  r  r   r*  r+  r,  r)  r-  r   r.  r/  s                 rH   r5  ChatHuggingFace._agenerate  s     -TXX66$($>$>x$N!M880055W}WPVWWF++F33#DHH--&,&8FdnnM"mm#'DJ 3;???$($>$>x$N!M)/);Hf% F  8800@@ &*0 F ++F33#DHH--NC%c**((2	88.. 
Kd
GM
 

 ##J//7 X @
sK   AF(FA*F(F"AF(F$A0F(	F&
F("F($F(&F()r   c                  UUR                  S0 5      R                  S5      U R                  R                  S0 5      R                  S5      U R                  /nU H  n[        U[        5      (       d  M  Us  $    U R                  $ )zDetermine whether to include usage metadata in streaming output.

For backwards compatibility, we check for `stream_options` passed
explicitly to kwargs or in the model_kwargs and override self.stream_usage.
stream_optionsinclude_usage)rD   r   r   r{   r   )r   r   r   stream_usage_sourcessources        rH   _should_stream_usage$ChatHuggingFace._should_stream_usage#  s     JJ',00A!!"2B7;;OL	 
 +F&$'' +    rJ   c             +  $  #    [        U R                  5      (       Ga  U R                  " SSU0UD6nU(       a  SU0US'   U R                  X5      u  pg0 UEUESS0En[        nU R                  R
                  R                  " SSU0UD6 GH  n	[        U	S   5      S:X  ae  U	R                  S	5      =n
(       aK  [	        S
0 0 U
R                  SS5      U
R                  SS5      U
R                  SS5      S.S9n[        US9v   M{  U	S   S   n[        X5      n0 nUR                  S5      =n(       a  XS'   U R                  US'   UR                  S5      nU(       a  UUS'   UR                  n[        X=(       d    S S9nU(       a  UR                  UR                  UUS9  Uv   GM     g U R                  U5      nU R                  R                   " U4X#S.UD6nU H,  n	[        [	        U	R                  S9U	R"                  S9nUv   M.     g 7f)Nr   r9  r8  r  Tr  rN   r   rP   rw   r   r   r   r   r_   r|   response_metadatar   r   r  rF   r   r  )r   r   r  r   rQ   )r   r   r<  r!  r   r"  r%  lenrD   r2   r   r   r   on_llm_new_tokentextr&  r$  r  )r   r  r   r  r   r   r*  r+  default_chunk_classr   rP   	usage_msgr   message_chunkr  r  r   generation_chunkr.  r-  
chat_chunks                        rH   r$  ChatHuggingFace._stream6  sG     $DHH--44 )-3L ,;\+J'($($>$>x$N!M99&9(D9F:H88 &*0 uY'(A- %		' 22u2$2$&.0.005		/10M16;NPQ1R05		.!0L,		%	 2)DDy)!, ?! #%$*JJ$??=?7DO448MMOL1!::j12:OJ/&3&=&=##6);Rd$  00(--5EPX 1  '&KN ,,X6I((** $AGK %0*5::>$)$9$9
 !  %s   HHc              d  #    U R                   " SSU0UD6nU(       a  SU0US'   U R                  X5      u  pg0 UEUESS0En[        nU R                  R                  R
                  " SSU0UD6I S h  vN   S h  vN n	[        U	S   5      S:X  af  U	R                  S	5      =n
(       aL  [        S
0 0 U
R                  SS5      U
R                  SS5      U
R                  SS5      S.S9n[        US97v   M  U	S   S   n[        X5      n0 nUR                  S5      =n(       a  XS'   U R                  US'   UR                  S5      nU(       a  UUS'   UR                  n[        X=(       d    S S9nU(       a#  UR                  UR                  UUS9I S h  vN   U7v   GM.   GN3 GN- N
 g 7f)Nr   r9  r8  r  Tr  rN   r   rP   rw   r   r   r   r   r?  rA  r  rF   r   r  )tokenr   r   rQ   )r<  r!  r   r   r2  r%  rB  rD   r2   r   r   r   rC  rD  )r   r  r   r  r   r   r*  r+  rE  r   rP   rF  r   rG  r  r  r   rH  s                     rH   r3  ChatHuggingFace._astream{  s     00UlUfU(7'FF#$ $ : :8 J5F5f5h56D!%!6!6!F!F "
""
&,"
 
 
 %	#% 5#$)!IIg..5. . "*,*,,1IIoq,I-2YY7JA-N,1IIna,H(		!I .i@@9%a(F;EWM O &

? ;;};3@004-zz*-H.6
+"/"9"92%7N$  !22*//*% 3   
 #"K
 %	#@A
sI   A1F03F&4F08F.<F)=F. DF0F,F0)F.,F0.F0c                    U(       d  Sn[        U5      e[        US   [        5      (       d  Sn[        U5      eU Vs/ s H  o0R                  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)r  r{   r#   _to_chatml_formatr   apply_chat_template)r   r  r   mmessages_dictss        rH   r&  ChatHuggingFace._to_chat_prompt  s|    
 ?CS/!(2,558CS/!=EFX003XF~~11U$ 2 
 	
 Gs   A5c                    [        U[        5      (       a  SnOI[        U[        5      (       a  SnO1[        U[        5      (       a  SnOS[	        U5       3n[        U5      eX!R                  S.$ )z+Convert LangChain message to ChatML format.rx   ru   rt   zUnknown message type: rs   )r{   r&   r   r#   rj   r  r_   )r   r   r^   r   s       rH   rR  !ChatHuggingFace._to_chatml_format  s_    g}--D++D..D*4=/:CS/!99rJ   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   r  r  )r  r1   r   rD  r  r   r3   r  )r/  chat_generationsgchat_generations       rH   r(  ChatHuggingFace._to_chat_result  sc    ''*A,!!&&11CTCTO ##O4	 + (5J5J
 	
rJ   c                   SSK J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[        U R                  5      (       an  SSK
Jn  U R                  =(       d    U R                  R                  U l        U R                  c  UR                  U R                  5      OU R                  U l        g[        U R                  5      (       a9  U R                  R
                  =(       d    U R                  R                  U l        gU R                  R                   nU" S5      nU H&  nUR"                  U:X  d  M  UR$                  U l        M(     U R                  (       d  SU S3n['        U5      eg)	z9Resolve the model_id from the LLM's inference_server_url.r   )list_inference_endpointsr   N)AutoTokenizer*zIFailed 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   r   r   r   r   r   transformersr`  r   from_pretrainedr   modelendpoint_urlurl
repositoryr  )r   r_  rf  r`  available_endpointsr   r   s          rH   r   !ChatHuggingFace._resolve_model_id  sb   <txx((DHHi((TXX-=-= HH,,DM,TXX66'+xx'D'DL#DHH--2 MM>TXX->->DM >>) --dmm<^^ N
 #DHH-- HH,,>DMxx,,6s;+H|||+ ( 3 3 , }}AAMTU 
 S/! rJ   )tool_choicec                 > U Vs/ s H  n[        U5      PM     nnUb  U(       a  [        U5      S:w  a  S[        U5       S3n[        U5      e[        U[        5      (       a  US;  a  SSU0S.nOs[        U[
        5      (       a  US   nOX[        U[        5      (       a3  US   S   S   US   S   :w  a  S	U S
US   S   S    S3n[        U5      eOSU 3n[        U5      eX#S'   [        TU ]   " SSU0UD6$ s  snf )a  Bind tool-like objects to this chat model.

Assumes model is compatible with OpenAI tool-calling API.

Args:
    tools: A list of tool definitions to bind to this chat model.

        Supports any tool definition handled by [`convert_to_openai_tool`][langchain_core.utils.function_calling.convert_to_openai_tool].
    tool_choice: Which tool to require the model to call.
        Must be the name of the single provided function or
        `'auto'` to automatically determine which function to call
        (if any), or a dict of the form:
        {"type": "function", "function": {"name": <<tool_name>>}}.
    **kwargs: Any additional parameters to pass to the
        `langchain.runnable.Runnable` constructor.
   zKWhen specifying `tool_choice`, you must provide exactly one tool. Received z tools.)autononerequiredrb   rd   )rj   rb   r   zTool choice z/ was specified, but the only provided tool was .zEUnrecognized tool_choice type. Expected str, bool or dict. Received: rk  toolsrQ   )	r:   rB  r  r{   r]   r   rO   r   bind)r   rr  rk  r   ry   formatted_toolsr   r   s          rH   
bind_toolsChatHuggingFace.bind_tools  sZ   . EJJED1$7EJ"{?#q(&&)/&:%;7D  !o%+s++&BB *%+[$9#K K..-a0K..#A&z26:":.v67 '{m 4--<Q-?
-KF-S,TTUW  %S/)7!!,/  !o%$/=!w|</<V<<C Ks   C<function_calling)methodinclude_rawc                  UR                  SS5      nU(       a  SU 3n[        U5      e[        U[        5      =(       a    [	        U5      nUS:X  aY  Uc  Sn[        U5      e[        U5      nUS   S   n	U R                  U/U	SS0US	.S
9n
U(       a  Sn[        U5      e[        U	SS9nO}US:X  a>  Uc  Sn[        U5      e[        U5      nU R                  SUS.SS0US	.S9n
[        5       nO9US:X  a"  U R                  SS0SS0US	.S9n
[        5       nOSU S3n[        U5      eU(       aT  [        R                  " [        S5      U-  S S9n[        R                  " S S9nUR                  U/SS9n[!        U
S9U-  $ X-  $ )aM  Model wrapper that returns outputs formatted to match the given schema.

Args:
    schema: The output schema. Can be passed in as:

        - An OpenAI function/tool schema,
        - A JSON Schema,
        - A `TypedDict` class

        Pydantic class is currently supported.

    method: The method for steering model generation, one of:

        - `'function_calling'`: uses tool-calling features.
        - `'json_schema'`: uses dedicated structured output features.
        - `'json_mode'`: uses JSON mode.

    include_raw:
        If `False` then only the parsed structured output is returned.

        If an error occurs during model output parsing it will be raised.

        If `True` then both the raw model response (a `BaseMessage`) and the
        parsed model response will be returned.

        If an error occurs during output parsing it will be caught and returned
        as well.

        The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
        `'parsing_error'`.

    kwargs:
        Additional parameters to pass to the underlying LLM's
        `langchain_core.language_models.chat.BaseChatModel.bind`
        method, such as `response_format` or `ls_structured_output_format`.

Returns:
    A `Runnable` that takes same inputs as a
        `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is
        `False` and `schema` is a Pydantic class, `Runnable` outputs an instance
        of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is
        `False` then `Runnable` outputs a `dict`.

        If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:

        - `'raw'`: `BaseMessage`
        - `'parsed'`: `None` if there was a parsing error, otherwise the type
            depends on the `schema` as described above.
        - `'parsing_error'`: `BaseException | None`
strictNzReceived unsupported arguments rw  zJschema must be specified when method is 'function_calling'. Received None.rb   rd   rx  )r   schema)rk  ls_structured_output_formatz5Pydantic schema is not supported for function callingT)key_namefirst_tool_onlyjson_schemazEschema must be specified when method is 'json_schema'. Received None.json_object)rj   r|  )response_formatr}  	json_moderj   z\Unrecognized method argument. Expected one of 'function_calling' or 'json_mode'. Received: ''rawc                    g rB   rQ   _s    rH   <lambda>8ChatHuggingFace.with_structured_output.<locals>.<lambda>  s    RVrJ   )parsedparsing_errorc                    g rB   rQ   r  s    rH   r  r    s    drJ   )r  r  )exception_key)r  )r   r  r{   rj   r;   r:   ru  r4  r.   r9   rs  r-   r7   assignr
   with_fallbacksr6   )r   r|  rx  ry  r   r  r   is_pydantic_schemaformatted_tool	tool_namer   output_parserformatted_schemaparser_assignparser_noneparser_with_fallbacks                   rH   with_structured_output&ChatHuggingFace.with_structured_outputB  s   x JJx&3F8<CS/!'5W:OPV:W''~%  !o%3F;N&z26:I//%');<,- " C "M)#..()TR  }$~%  !o%5f=)))6BR S'7$-  C -.M{"))!' 7'5$-  C -.M++1(!5  S/!/66!%(=8M .44NKK#0#?#?_ $@ $  3'*>>>""rJ   c                n    U R                   nUb  X#S'   U Vs/ s H  n[        U5      PM     nnXS4$ s  snf )Nr   )_default_paramsr   )r   r  r   r+  rT  r*  s         rH   r!  %ChatHuggingFace._create_message_dicts  sE     %%!6N>FGh1!4hG$$ Hs   2c                    U R                   U R                  U R                  U R                  U R                  S.U R
                  (       a  U R
                  O0 EnU R                  b  U R                  US'   U$ )zHGet default parameters for calling Hugging Face Inference Providers API.)re  r  r   r   r   r   )r   r   r   r   r   r   r   )r   r+  s     rH   r  ChatHuggingFace._default_params  si     ]]nn++II
 %)$5$5t  2
 ??&#'??F< rJ   c                    g)Nzhuggingface-chat-wrapperrQ   r   s    rH   	_llm_typeChatHuggingFace._llm_type  s    )rJ   )r   r   r   r   )r   r   )returnNone)r  r?   )Nr   )
r   r]   r   r   r   z+Literal['pipeline', 'endpoint', 'text-gen']r   r   r  r   )r  rO   r  r3   )NNN)r  list[BaseMessage]r   list[str] | Noner  CallbackManagerForLLMRun | Noner  r   r   r   r  r3   )r  r  r   r  r  $AsyncCallbackManagerForLLMRun | Noner  r   r   r   r  r3   )r   r   r   r   r  r   )NN)r  r  r   r  r  r  r   r   r   r   r  zIterator[ChatGenerationChunk])r  r  r   r  r  r  r   r   r   r   r  z"AsyncIterator[ChatGenerationChunk])r  r  r  r]   r   r   r  rO   )r/  r4   r  r3   )rr  z5Sequence[dict[str, Any] | type | Callable | BaseTool]rk  zdict | str | bool | Noner   r   r  z'Runnable[LanguageModelInput, AIMessage]rB   )
r|  zdict | type[BaseModel] | Nonerx  z7Literal['function_calling', 'json_mode', 'json_schema']ry  r   r   r   r  z.Runnable[LanguageModelInput, dict | BaseModel])r  r  r   r  r  z+tuple[list[dict[str, Any]], dict[str, Any]])r  r   )r  r]   )2rS   rT   rU   rV   rW   rX   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r   r   rO   r   r   r   r>   r   r   classmethodr  r  r'  r5  r<  r$  r3  r&  rR  staticmethodr(  r   ru  r  r!  propertyr  r  rY   __classcell__)r   s   @rH   r   r   D  s   dL 
H3IsEHjD $K$+#(=M#ND
 N!%)l)$&*|*;D* Hk %#L*## )-J%,PIt/ $L+$SAzAE<D!J
!/#(#>L.>V!+=Z '" # '" #   ?I	X&X& X& =	X&
 X& 
X& X&tJ: "&7;"'0#'0 '0 5	'0
 '0 '0 
'0X "&<@"%0#%0 %0 :	%0
 %0 %0 
%0P .2!*!=@!	!, "&7;	C! %)C!#C! C! 5	C! "C! C! 
'C!P "&<@	6# %)6##6# 6# :	6# "6# 6# 
,6#p
#
 

&: 
 
$"T 15	8=D8= .	8=
 8= 
18= 8=x 15A# !A#-A#
	A# A# A# 
8A#F%)%1A%	4%   * *rJ   r   )rF   r]   r  r   )rm   r(   r  rO   )rp   r%   r  rO   r  )r   Mapping[str, Any]r  r   )r   r   r  r   )r   r  r   ztype[BaseMessageChunk]r  r   )drW   
__future__r   r   rk   collections.abcr   r   r   r   r   dataclassesr	   operatorr
   typingr   r   r   r   r  r   r   r    langchain_core.callbacks.managerr   r   langchain_core.language_modelsr   r   r   *langchain_core.language_models.chat_modelsr   r   r   langchain_core.messagesr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   langchain_core.messages.toolr+   r,   r   langchain_core.output_parsersr-   *langchain_core.output_parsers.openai_toolsr.   r/   r0   langchain_core.outputsr1   r2   r3   r4   langchain_core.runnablesr5   r6   r7   langchain_core.toolsr8   %langchain_core.utils.function_callingr9   r:   langchain_core.utils.pydanticr;   pydanticr<   r=   r>   typing_extensionsr?   $langchain_huggingface.data._profilesr@   rC   rI   rL   r[   rn   rq   r   r   r   r   r   r   r   r   rQ   rJ   rH   <module>r     sQ     "   P P !  4 4SS 
 
    $ 7 R : 
  P O ) @ 6 6 " : O O-y9
      
&
	
7t2Jj	5*5*-C5*5*p	00[*m [*rJ   