
    hK                    ^   d Z ddlmZ ddlZddl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mZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl/m1Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZA ddlBmCZC ddlDmEZEmFZF ddlGmHZH ddlImJZJmKZKmLZL ddlMmNZN ddlOmPZP ddlQmRZR e G d d             ZSe G d d             ZTd(dZU	 	 	 	 d)dZVd*dZWd+d ZXd,d!ZY	 	 	 	 	 	 d-d"ZZd,d#Z[d,d$Z\d,d%Z] G d& d'e      Z^y).zHugging Face Chat Wrapper.    )annotationsN)AsyncIteratorIteratorMappingSequence)	dataclass)
itemgetter)AnyCallableLiteralOptionalUnioncast)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LanguageModelInput)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)HuggingFaceEndpoint)HuggingFacePipelinec                  &    e Zd ZU dZded<   ded<   y)TGI_RESPONSEz'Response from the TextGenInference API.z	list[Any]choicesdictusageN__name__
__module____qualname____doc____annotations__     l/var/www/html/aiagenthome/venv/lib/python3.12/site-packages/langchain_huggingface/chat_models/huggingface.pyr>   r>   D   s    1KrI   r>   c                  0    e Zd ZU dZded<   ded<   ded<   y)TGI_MESSAGEz,Message to send to the TextGenInference API.strrolecontentz
list[dict]
tool_callsNrB   rH   rI   rJ   rL   rL   L   s    6
ILrI   rL   c                L    d| d   | d   t        j                  | d         ddS Nfunctionidnameargs)rU   	arguments)typerT   rS   )jsondumps)	tool_calls    rJ   _lc_tool_call_to_hf_tool_callr\   U   s4    of%If$56
 rI   c                &    d| d   | d   | d   ddS rR   rH   )invalid_tool_calls    rJ   %_lc_invalid_tool_call_to_hf_tool_callr_   `   s.     %%f-*62
 rI   c                   t        | t              r| j                  | j                  d}nt        | t              rd| j                  d}nht        | t
              rd| j                  d}d| j                  v r| j                  d   |d<   |d   dk(  rd|d<   | j                  s| j                  rK| j                  D cg c]  }t        |       c}| j                  D cg c]  }t        |       c}z   |d<   n d| j                  v r| j                  d   |d<   d|v r|d   dk(  rd|d<   nnt        | t              rd	| j                  d}nft        | t              rd
| j                  | j                  d}n;t        | t              rd| j                  | j                  d}nd|  }t!        |      d| j                  v r| j                  d   |d<   |S c c}w c c}w )zConvert a LangChain message to a dictionary.

    Args:
        message: The LangChain message.

    Returns:
        The dictionary.

    rN   rO   user	assistantfunction_callrO    NrP   systemrS   )rN   rO   rU   tool)rN   rO   tool_call_idzGot unknown type rU   )
isinstancer   rN   rO   r   r   additional_kwargsrP   invalid_tool_callsr\   r_   r!   r   rU   r$   rh   	TypeError)messagemessage_dicttcmsgs       rJ   _convert_message_to_dictrq   m   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   ?G!Gc                r   | j                  d      }|dk(  rt        | j                  dd            S |dk(  r| j                  dd      xs d}i }| j                  d      x}rt        |      |d<   g }g }| j                  d      x}r)||d<   |D ]  }	 |j                  t	        |d	
             ! t        ||||      S |dk(  rt        | j                  dd            S |dk(  r-t        | j                  dd      | j                  dd            S |dk(  r<i }d| v r| d   |d<   t        | j                  dd      | j                  dd      |      S t        | j                  dd      |xs d      S # t
        $ r8}	|j                  t        t        |t        |	                         Y d}	~	"d}	~	ww xY w)zConvert a dictionary to a LangChain message.

    Args:
        _dict: The dictionary.

    Returns:
        The LangChain message.

    rN   rb   rO   re   rO   rc   rd   rP   T)	return_idN)rO   rj   rP   rk   rf   rS   rU   rO   rU   rg   rh   )rO   rh   rj   rO   rN   )getr   r@   appendr+   	Exceptionr*   rM   r   r!   r   r$   r   )
_dictrN   rO   rj   rd   rP   rk   raw_tool_callsraw_tool_calles
             rJ   _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   E55	F6>-F11F6c                F    	 ddl m} t        | |      S # t        $ r Y yw xY w)Nr   )HuggingFaceHubF)(langchain_community.llms.huggingface_hubr   ri   ImportError)llmr   s     rJ   _is_huggingface_hubr      s-    	
 #~..     	  c                j   | d   d   }|d   }t        t        |j                  d            }t        t        |j                  d      xs d      }i }g }|j                  d      r!t        |d         }d|v r
|d   d|d<   ||d<   |j                  d	      x}	r|	|d	<   |	D ]  }
t	        j
                  t              5  |j                  t        |
d
   j                  d      |
d
   j                  d      |
j                  d      |
j                  d                   d d d         |dk(  s	|t        k(  rt        |      S |dk(  s	|t        k(  rb| j                  d      x}r>|j                  dd      }|j                  dd      }|||j                  d||z         d}nd }t        ||||      S |dk(  s	|t        k(  rt        |      S |d
k(  s	|t        k(  rt        ||d         S |dk(  s	|t        k(  rt        ||d         S |s	|t        k(  rt        ||      S  ||      S # 1 sw Y   xY w)Nr?   r   deltarN   rO   re   rd   rU   rP   rS   rW   rT   index)rU   rV   rT   r   rb   rs   rc   rA   prompt_tokenscompletion_tokenstotal_tokensinput_tokensoutput_tokensr   )rO   rj   tool_call_chunksusage_metadatarf   ru   rg   rh   )rO   rh   rv   )r   rM   rw   r@   
contextlibsuppressKeyErrorrx   create_tool_call_chunkr   r   r"   r   r%   r   )chunkdefault_classchoicerz   rN   rO   rj   r   rd   r{   rtcrA   r   r   r   s                  rJ   _convert_chunk_to_message_chunkr      sU    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~*::eN>STT} 00d;;))I /.s   <A!H((H2	c                F    	 ddl m} t        | |      S # t        $ r Y yw xY w)Nr   )HuggingFaceTextGenInferenceF)7langchain_community.llms.huggingface_text_gen_inferencer   ri   r   )r   r   s     rJ   !_is_huggingface_textgen_inferencer      s.    	
 #:;; r   c                "    t        | t              S N)ri   r;   r   s    rJ   _is_huggingface_endpointr   ,      c.//rI   c                "    t        | t              S r   )ri   r<   r   s    rJ   _is_huggingface_pipeliner   0  r   rI   c                      e Zd ZU dZded<   	 dZded<   	 dZded<   	 dZded	<   	  edd
      Z	ded<   	 dZ
ded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	  ee      Zded<   	 d5 fd Z ed!"      d6d#       Zd7d$Z	 	 	 d8	 	 	 	 	 	 	 	 	 	 	 d9d%Z	 	 	 d8	 	 	 	 	 	 	 	 	 	 	 d:d&Z	 	 d;	 	 	 	 	 	 	 	 	 d<d'Z	 	 d;	 	 	 	 	 	 	 	 	 d=d(Z	 	 	 	 d>d)Zd?d*Ze d@d+       Z!dAd,Z"dd-	 	 	 	 	 	 	 dB fd.Z#	 dCd/dd0	 	 	 	 	 	 	 	 	 dDd1Z$	 	 	 	 	 	 dEd2Z%e&dFd3       Z'e&dGd4       Z( xZ)S )HChatHuggingFaceu  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.

        .. code-block:: bash

            pip install langchain-huggingface

        .. code-block:: 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: `HuggingFaceTextGenInference`, `HuggingFaceEndpoint`, `HuggingFaceHub`, or
            'HuggingFacePipeline' LLM to be used.

    Key init args — client params:
        custom_get_token_ids: Optional[Callable[[str], list[int]]]
            Optional encoder to use for counting tokens.
        metadata: Optional[dict[str, Any]]
            Metadata to add to the run trace.
        tags: Optional[list[str]]
            Tags to add to the run trace.
        tokenizer: Any
        verbose: bool
            Whether to print out response text.

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

    Instantiate:
        .. code-block:: python

            from langchain_huggingface import HuggingFaceEndpoint,
            ChatHuggingFace

            llm = 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=llm, verbose=True)

    Invoke:
        .. code-block:: python

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

            chat(...).invoke(messages)

        .. code-block:: 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:
        .. code-block:: python

            for chunk in chat.stream(messages):
                print(chunk)

        .. code-block:: 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:
        .. code-block:: python

            await chat.ainvoke(messages)

        .. code-block:: 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:
        .. code-block:: 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

        .. code-block:: python

            [{'name': 'GetPopulation',
              'args': {'location': 'Los Angeles, CA'},
              'id': '0'}]

    Response metadata
        .. code-block:: python

            ai_msg = chat.invoke(messages)
            ai_msg.response_metadata

        .. code-block:: python
            {'token_usage': ChatCompletionOutputUsage(completion_tokens=100,
            prompt_tokens=8, total_tokens=108),
             'model': '',
             'finish_reason': 'length'}

    r
   r   N	tokenizerzOptional[str]model_idzOptional[float]temperaturestop_sequences)defaultaliaszOptional[Union[str, list[str]]]stoppresence_penaltyfrequency_penaltyzOptional[int]seedOptional[bool]logprobstop_logprobszOptional[dict[int, int]]
logit_biasFbool	streamingntop_p
max_tokens)default_factorydict[str, Any]model_kwargsc                D    t        |   di | | j                          y )NrH   )super__init___resolve_model_id)selfkwargs	__class__s     rJ   r   zChatHuggingFace.__init__  s    "6" rI   after)modec                    t        | j                        sbt        | j                        sMt        | j                        s8t	        | j                        s#dt        | j                         }t        |      | S )NzyExpected llm to be one of HuggingFaceTextGenInference, HuggingFaceEndpoint, HuggingFaceHub, HuggingFacePipeline received )r   r   r   r   r   rX   rl   )r   rp   s     rJ   validate_llmzChatHuggingFace.validate_llm  sb     $DHH-5dhh?,TXX6,TXX6 N+- 
 C. rI   c                   g }|j                  di       }|d   D ]  }t        |d         }|rKt        |t              r;|j                  dd      |j                  dd      |j                  dd      d|_        d	|j                  d	      i}d
|v r|d
   |d
<   t        ||      }|j                  |        || j                  |j                  dd      d}t        ||      S )NrA   r?   rm   r   r   r   r   r   finish_reasonr   rm   generation_infosystem_fingerprintre   )token_usage
model_namer   generations
llm_output)	rw   r~   ri   r   r   r,   rx   r   r.   )	r   responser   r   resrm   r   genr   s	            rJ   _create_chat_resultz#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


 kjIIrI   c                   ||n| j                   }t        | j                        rN| j                  ||      \  }} | j                  j                  j
                  dd|i|}	| j                  |	      S t        | j                        r|r! | j                  |f||d|}
t        |
      S | j                  ||      \  }}d|i||d|ini |} | j                  j                  j                  dd|i|}	| j                  |	      S | j                  |      }|r+ | j                  j                  |f||d|}
t        |
      S  | j                  j                  d|g||d|}| j                  |      S )Nmessagesr   run_managerr   streampromptsr   r   rH   )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                rJ   r   zChatHuggingFace._generate&  s    #)"4$..,TXX6$($>$>x$N!M6)TXX__))K=KFKF++F33#DHH-*dll#'[DJ ,K88$($>$>x$N!M6 *0);Hf% 	F 5TXX__44VmVvVF++F33((2	*$((** $+AGK (44'TXX'' 
Kd
GM

 ##J//rI   c                4  K   t        | j                        rV| j                  ||      \  }} | j                  j                  j                  dd|i| d {   }| j                  |      S t        | j                        r||n| j                  }	|	r) | j                  |f||d|}
t        |
       d {   S | j                  ||      \  }}i ||d|ini |} | j                  j                  j                  dd|i| d {   }| j                  |      S t        | j                        rd}t        |      | j                  |      } | j                  j                  d|g||d| d {   }| j                  |      S 7 77 7 7 w)Nr   r   r   z:async generation is not supported with HuggingFacePipeliner   rH   )r   r   r   async_clientr   r   r   r   _astreamr   r   r   NotImplementedErrorr   
_agenerater   )r   r   r   r   r   r   r   r   r   r   r   rp   r   r   s                 rJ   r   zChatHuggingFace._agenerateO  s     -TXX6$($>$>x$N!M654880055W}WPVWWF++F33#DHH-&,&8FdnnM+dmm#'[DJ 3;???$($>$>x$N!M6)/);Hf% F A48800@@ &*0 F ++F33#DHH-NC%c**((2	.488.. 
Kd
GM
 

 ##J//7 X @
sK   AFFA F9F:AFFA,F9F:FFFFc              +    K   t        | j                        r| j                  ||      \  }}i ||ddi}t        } | j                  j                  j
                  dd|i|D ]  }t        |d         dk(  r|d   d   }	t        ||      }
i }|	j                  d      x}r||d<   | j                  |d<   |	j                  d      }|r||d<   |
j                  }t        |
|xs d 	      }|r|j                  |j                  ||
       |  y | j                  |      } | j                  j                  |f||d|}|D ]1  }t        t        |j                        |j                   	      }| 3 y w)Nr   Tr   r?   r   r   r   r   r   )r   r   r   rs   rH   )r   r   r   r   r   r   lenr   rw   r   r   r-   on_llm_new_tokentextr   r   r   )r   r   r   r   r   r   r   default_chunk_classr   r   message_chunkr   r   r   generation_chunkr   r   
chat_chunks                     rJ   r   zChatHuggingFace._streamv  s     $DHH-$($>$>x$N!M699&9(D9F:H888 &*0 uY'(A-y)!, ?.! #%$*JJ$??=?7DOO448MMOL1!::j12:OJ/&3&=&=##6)?;Rd$  00(--5EPX 1  '&14 ,,X6I*$((** $+AGK %0*5::>$)$9$9
 !  %s   E9E;c               :  K   | j                  ||      \  }}i ||ddi}t        } | j                  j                  j                  dd|i| d {   2 3 d {   }t        |d         dk(  r|d   d   }	t        ||      }
i }|	j                  d      x}r||d<   | j                  |d<   |	j                  d      }|r||d<   |
j                  }t        |
|xs d 	      }|r&|j                  |j                  ||
       d {    | 7 7 7 6 y w)Nr   Tr   r?   r   r   r   r   r   )tokenr   r   rH   )r   r   r   r   r   r   r   rw   r   r   r-   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                  rJ   r   zChatHuggingFace._astream  sf     !% : :8T Jv5F5f5h56D!F!6!6!F!F "
""
&,"
 
 
 	#% 5#$)9%a(F;ECVWM O &

? ;;};3@004-zz*-H.6
+"/"9"92%7N$  !22*//*% 3   
 #"1
 	#&'
sI   ADDDDDD B(DD	DDDDc                    |sd}t        |      t        |d   t              sd}t        |      |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)
ValueErrorri   r   _to_chatml_formatr   apply_chat_template)r   r   rp   mmessages_dictss        rJ   r   zChatHuggingFace._to_chat_prompt  s{    
 ?CS/!(2,58CS/!=EFX$003XF~~11U$ 2 
 	
 Gs   A,c                    t        |t              rd}n?t        |t              rd}n,t        |t              rd}ndt	        |       }t        |      ||j                  dS )z+Convert LangChain message to ChatML format.rf   rc   rb   zUnknown message type: ra   )ri   r!   r   r   rX   r   rO   )r   rm   rN   rp   s       rJ   r   z!ChatHuggingFace._to_chatml_format  sX    g}-D+D.D*4=/:CS/!99rI   c                    g }| j                   d   D ]>  }t        t        |j                        |j                        }|j                  |       @ t        || j                        S )Nr   rs   r   r   )r   r,   r   r   r   rx   r.   r   )r   chat_generationsgchat_generations       rJ   r   zChatHuggingFace._to_chat_result  se    ''*A,!!&&11CTCTO ##O4	 + (Z5J5J
 	
rI   c                ~   ddl m} t        | j                        s,t	        | j                  d      r2| j                  j
                  r| j                  j
                  | _        yt        | j                        r| j                  j                  }t        | j                        rnddl
m} | j                  xs | j                  j                  | _        | j                  !|j                  | j                        | _        y| j                  | _        yt        | j                        r4| j                  j
                  xs | j                  j                  | _        y| j                  j                   } |d      }|D ]#  }|j"                  |k(  s|j$                  | _        % | j                  sd| d}t'        |      y)	z9Resolve the model_id from the LLM's inference_server_url.r   )list_inference_endpointsrepo_idN)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   hasattrr  r   r   inference_server_urlr   transformersr  r   from_pretrainedr   modelendpoint_urlurl
repositoryr   )r   r
  r  r  available_endpointsendpointrp   s          rJ   r   z!ChatHuggingFace._resolve_model_id  sa   <txx(DHHi(TXX-=-= HH,,DM,TXX6*.((*G*GL#DHH-2 MM>TXX->->DM >>) --dmm< N
  ^^ N
 #DHH- HH,,>DMxx,,6s;+H|||+ ( 3 3 , }}AAMTU 
 S/! rI   )tool_choicec                  |D cg c]  }t        |       }}||rt        |      dk7  rdt        |       d}t        |      t        |t              r|dvrodd|id}ngt        |t
              r|d   }nQt        |t              r1|d   d   d   |d   d   k7  r-d	| d
|d   d   d    d}t        |      d| }t        |      ||d<   t        |    dd|i|S c c}w )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
                :meth:`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
                :class:`~langchain.runnable.Runnable` constructor.

           zKWhen specifying `tool_choice`, you must provide exactly one tool. Received z tools.)autononerequiredrS   rU   )rX   rS   r   zTool choice z/ was specified, but the only provided tool was .zEUnrecognized tool_choice type. Expected str, bool or dict. Received: r  toolsrH   )	r5   r   r   ri   rM   r   r@   r   bind)r   r   r  r   rg   formatted_toolsrp   r   s          rJ   
bind_toolszChatHuggingFace.bind_tools"  sM   4 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/)!!,/  !o%$/F=!w|</<V<<C Ks   Cfunction_calling)methodinclude_rawc               
   |j                  dd      }|rd| }t        |      t        |t              xr t	        |      }|dk(  rY|d}t        |      t        |      }|d   d   }	| j                  |g|	ddi|d	
      }
|rd}t        |      t        |	d      }n|dk(  r@|d}t        |      t        |      }| j                  d|dddi|d	      }
t               }n;|dk(  r%| j                  ddiddi|d	      }
t               }nd| d}t        |      |r^t        j                  t        d      |z  d       }t        j                  d       }|j                  |gd      }t!        |
      |z  S |
|z  S )a8  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 (support added in 0.1.7),

                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
                :meth:`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 :class:`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"``: Optional[BaseException]

        strictNzReceived unsupported arguments r$  zJschema must be specified when method is 'function_calling'. Received None.rS   rU   r%  )r   schema)r  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)rX   r)  )response_formatr*  	json_moderX   z\Unrecognized method argument. Expected one of 'function_calling' or 'json_mode'. Received: ''rawc                     y r   rH   _s    rJ   <lambda>z8ChatHuggingFace.with_structured_output.<locals>.<lambda>  s    RVrI   )parsedparsing_errorc                     y r   rH   r4  s    rJ   r6  z8ChatHuggingFace.with_structured_output.<locals>.<lambda>  s    drI   )r7  r8  )exception_key)r2  )popr   ri   rX   r6   r5   r#  r   r)   r4   r!  r(   r2   assignr	   with_fallbacksr1   )r   r)  r%  r&  r   r5  rp   is_pydantic_schemaformatted_tool	tool_namer   output_parserformatted_schemaparser_assignparser_noneparser_with_fallbacks                   rJ   with_structured_outputz&ChatHuggingFace.with_structured_output_  s   l JJx&3F8<CS/!'5W:OPV:W''~%  !o%3F;N&z26:I//%');<,- " C "M)#..()TR  }$~%  !o%5f=)))6BR S'7$-  C !"  {"))!' 7'5$-  C !" 
++1(!5  S/!/66!%(=8M .44NKK#0#?#?_ $@ $  3'*>>>]""rI   c                j    | j                   }|||d<   |D cg c]  }t        |       }}||fS c c}w )Nr   )_default_paramsrq   )r   r   r   r   r  r   s         rJ   r   z%ChatHuggingFace._create_message_dicts  sI     %%!F6N>FGh1!4hGf$$ Hs   0c                    | j                   | j                  | j                  | j                  | j                  d| j
                  r| j
                  ni }| j                  | j                  |d<   |S )zHGet default parameters for calling Hugging Face Inference Providers API.)r  r   r   r   r   r   )r   r   r   r   r   r   r   )r   r   s     rJ   rH  zChatHuggingFace._default_params  si     ]]nn++II
 %)$5$5t  2
 ??&#'??F< rI   c                     y)Nzhuggingface-chat-wrapperrH   )r   s    rJ   	_llm_typezChatHuggingFace._llm_type  s    )rI   )r   r
   )returnr:   )r   r@   rL  r.   )NNN)r   list[BaseMessage]r   Optional[list[str]]r   "Optional[CallbackManagerForLLMRun]r   r   r   r
   rL  r.   )r   rM  r   rN  r   'Optional[AsyncCallbackManagerForLLMRun]r   r   r   r
   rL  r.   )NN)
r   rM  r   rN  r   rO  r   r
   rL  zIterator[ChatGenerationChunk])
r   rM  r   rN  r   rP  r   r
   rL  z"AsyncIterator[ChatGenerationChunk])r   rM  rL  rM   rm   r   rL  r@   )r   r/   rL  r.   )rL  None)r   z9Sequence[Union[dict[str, Any], type, Callable, BaseTool]]r  zEOptional[Union[dict, str, Literal['auto', 'none', 'required'], bool]]r   r
   rL  z)Runnable[LanguageModelInput, BaseMessage]r   )
r)  z&Optional[Union[dict, type[BaseModel]]]r%  z7Literal['function_calling', 'json_mode', 'json_schema']r&  r   r   r
   rL  z4Runnable[LanguageModelInput, Union[dict, BaseModel]])r   rM  r   rN  rL  z+tuple[list[dict[str, Any]], dict[str, Any]])rL  r   )rL  rM   )*rC   rD   rE   rF   rG   r   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   r@   r   r   r9   r   r   r   r   r   r   r   r   staticmethodr   r   r#  rF  r   propertyrH  rK  __classcell__)r   s   @rJ   r   r   4  s   ]~ 
H3IsE"Hm"D#'K'+,1$FV,WD
)W!(,o,$)--;D-#Hn#%"&L-&# ,0J(/PIt/A}A!E?!D $J$/#(#>L.>V! '" #J: %):>!%'0#'0 "'0 8	'0
 '0 '0 
'0X %)?C!%%0#%0 "%0 =	%0
 %0 %0 
%0T %):>	/!#/! "/! 8	/!
 /! 
'/!h %)?C	$##$# "$# =	$#
 $# 
,$#L
#
 

&: 
 
$"X ;=H;=
	;= ;= 
3;=~ :># !#6#
	# # # 
>#B%)%1D%	4%   * *rI   r   )r[   r#   rL  r@   )r^   r    rL  r@   rQ  )rz   Mapping[str, Any]rL  r   )r   r
   rL  r   )r   rV  r   ztype[BaseMessageChunk]rL  r   )_rF   
__future__r   r   rY   collections.abcr   r   r   r   dataclassesr   operatorr	   typingr
   r   r   r   r   r    langchain_core.callbacks.managerr   r   langchain_core.language_modelsr   *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*   r+   langchain_core.outputsr,   r-   r.   r/   langchain_core.runnablesr0   r1   r2   langchain_core.toolsr3   %langchain_core.utils.function_callingr4   r5   langchain_core.utils.pydanticr6   pydanticr7   r8   r9   typing_extensionsr:   /langchain_huggingface.llms.huggingface_endpointr;   /langchain_huggingface.llms.huggingface_pipeliner<   r>   rL   r\   r_   rq   r~   r   r   r   r   r   r   rH   rI   rJ   <module>rl     s.     "   F F !  @ @ > 
    $ 7 R : 
  P O ) @ 6 6 " O O      
&
	
7t2Jj	5*5*-C5*5*p	00F*m F*rI   