
    dhD                        S SK r S SK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  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JrJrJrJrJrJrJrJ r J!r!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K0J1r1J2r2  S SK3J4r4  S SK5J6r6  S SK7J8r8J9r9J:r:  \Rv                  " \<5      r= " S S\5      r>S\$S\?4S jr@S\!S\?4S jrAg)    N)AnyCallableDictIteratorListLiteralMappingOptionalSequenceTypeUnioncast)urlparse)CallbackManagerForLLMRun)BaseChatModel)LanguageModelInput)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageHumanMessageHumanMessageChunkInvalidToolCallSystemMessageSystemMessageChunkToolCallToolMessageToolMessageChunk)tool_call_chunk)make_invalid_tool_callparse_tool_call)ChatGenerationChatGenerationChunk
ChatResult)RunnableRunnableConfig)BaseTool)convert_to_openai_tool)	BaseModelFieldPrivateAttrc                     ^  \ rS rSr% Sr\\S'    \\S'    Sr\\S'    Sr	\
\S'    S	r\\\      \S
'    S	r\\
   \S'    \" \S9r\\S'    \" 5       r\\S'   S\4U 4S jjr\S\4S j5       rS0S jr\S\\\4   4S j5       r S1S\\   S
\\\      S\S\\\4   4S jjr  S2S\\   S
\\\      S\\   S\S\4
S jjr S1S	S.S\ S\\!   S
\\\      S\S\"\#   4
U 4S jjjjr$  S2S\\   S
\\\      S\\   S\S\"\%   4
S jjr&\S\\\4   4S j5       r' S1S
\\\      S\S\\\4   4U 4S jjjr(\S\4S  j5       r)\*S!\+\\4   S\4S" j5       r,\*S!\+\\4   S#\S\#4S$ j5       r-\*S0S% j5       r.\*S&\S\4S' j5       r/\*S(\+\\4   S\4S) j5       r0S	S*.S+\1\2\\\4   \3\4   \5\64      S,\\2\\\7S-   \84      S\S\9\ \4   4U 4S. jjjr:S/r;U =r<$ )3
ChatMlflow9   a  `MLflow` chat models API.

To use, you should have the `mlflow[genai]` python package installed.
For more information, see https://mlflow.org/docs/latest/llms/deployments.

Example:
    .. code-block:: python

        from langchain_community.chat_models import ChatMlflow

        chat = ChatMlflow(
            target_uri="http://localhost:5000",
            endpoint="chat",
            temperature=0.1,
        )
endpoint
target_urig        temperature   nNstop
max_tokens)default_factoryextra_params_clientkwargsc                    > [         TU ]  " S0 UD6  U R                  5          SSKJn  U" U R
                  5      U l        g ! [         a  n[        SU R                   S35      UeS nAff = f)Nr   )get_deploy_clientz;Failed to create the client. Please run `pip install mlflowz#` to install required dependencies. )	super__init___validate_urimlflow.deploymentsr>   r3   r;   ImportError_mlflow_extras)selfr<   r>   e	__class__s       ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/chat_models/mlflow.pyrA   ChatMlflow.__init__[   su    "6"		<,T__=DL 	1151D1D0E F)) 		s   A 
A*A%%A*returnc                     g)Nz[genai]r?   rF   s    rI   rE   ChatMlflow._mlflow_extrasi   s        c                     U R                   S:X  a  g / SQn[        U R                   5      R                  U;  a  [        SU R                    SU S35      eg )N
databricks)httphttpsrQ   zInvalid target URI: z. The scheme must be one of .)r3   r   scheme
ValueError)rF   alloweds     rI   rB   ChatMlflow._validate_urim   s\    ??l*1DOO$++7:&t&7 8--4IQ8  ;rO   c                     U R                   U R                  U R                  U R                  U R                  U R
                  U R                  S.nU$ )Nr3   r2   r4   r6   r7   r8   r:   rZ   )rF   paramss     rI   _default_paramsChatMlflow._default_paramsw   sG     //++II// --"
 rO   messagesc                     U Vs/ sH  n[         R                  U5      PM     nnUU R                  U R                  S.U R                  EUEnU R
                  =(       d    U=n(       a  X&S'   U R                  b  U R                  US'   U$ s  snf )N)r^   r4   r6   r7   r8   )r0   _convert_message_to_dictr4   r6   r:   r7   r8   )rF   r^   r7   r<   messagemessage_dictsdatas          rI   _prepare_inputsChatMlflow._prepare_inputs   s     IQ
HPWJ//8 	 
 &++ 
 	 

  
 99$$4$L??&!%D
s   Brun_managerc                     U R                   " UU40 UD6nU R                  R                  U R                  US9n[        R                  U5      $ )Nr2   inputs)rd   r;   predictr2   r0   _create_chat_result)rF   r^   r7   rf   r<   rc   resps          rI   	_generateChatMlflow._generate   sT     ##
 

 ||##T]]4#H--d33rO   )r7   inputconfigc          	   +      >#    [        U R                  S5      (       d&  [        [        U R                  " U4X#S.UD65      v   g [
        TU ]  " X4SU0UD6 S h  vN   g  N7f)Npredict_stream)rp   r7   r7   )hasattrr;   r   r   invoker@   stream)rF   ro   rp   r7   r<   rH   s        rI   ru   ChatMlflow.stream   s`      t||%566  $++e"XF"XQW"X  w~eI$I&IIIs   AA#A!A#c              +     #    U R                   " UU40 UD6nU R                  R                  U R                  US9nS nU H  nUS   (       a  US   S   n	U	S   n
Uc  U
R	                  S5      n[
        R                  X5      n0 nU	R	                  S5      =n(       a  XS'   U	R	                  S5      =n(       a  XS'   [        X=(       d    S S9nU(       a  UR                  UR                  XS	9  Uv   M  M     g 7f)
Nrh   choicesr   deltarolefinish_reasonlogprobsra   generation_info)chunkr|   )
rd   r;   rr   r2   getr0   _convert_delta_to_message_chunkr&   on_llm_new_tokentext)rF   r^   r7   rf   r<   rc   
chunk_iterfirst_chunk_roler   choicechunk_deltachunk_messager~   r{   r|   s                  rI   _streamChatMlflow._stream   s     ##
 
 \\00$--PT0U
EYy)!,$Wo#+'2v'>$ * J J! #%$*JJ$??=?7DO4%zz*55852:J/+);Rd 00

% 1   =  s   C8C:c                     U R                   $ N)r\   rM   s    rI   _identifying_paramsChatMlflow._identifying_params   s    ###rO   c                 D   > 0 U R                   E[        TU ]  " SSU0UD6E$ )z>Get the parameters used to invoke the model FOR THE CALLBACKS.r7   r?   )r\   r@   _get_invocation_params)rF   r7   r<   rH   s      rI   r   !ChatMlflow._get_invocation_params   s5    
""
g,A$A&A
 	
rO   c                     g)zReturn type of chat model.zmlflow-chatr?   rM   s    rI   	_llm_typeChatMlflow._llm_type   s     rO   _dictc           
         U S   n[        [        U R                  S5      5      nUS:X  a	  [        US9$ US:X  al  U=(       d    Sn0 n/ n/ nU R                  S5      =n(       a'  XcS'   U H  n UR	                  [        USS	95        M     [        UUU R                  S
5      UUS9$ US:X  a	  [        US9$ [        X!S9$ ! [         a/  nUR	                  [        U[        U5      5      5         S nAM  S nAff = f)Nrz   contentuserr   	assistant 
tool_callsT)	return_idid)r   additional_kwargsr   r   invalid_tool_callssystemr   rz   )r   strr   r   appendr$   	Exceptionr#   r   r   r   )	r   rz   r   r   r   r   raw_tool_callsraw_tool_callrG   s	            rI   _convert_dict_to_message#ChatMlflow._convert_dict_to_message   s   V}sEIIi016>00[ mG&(J!#!&<!88~82@,/%3M"))+MTJ &4 "399T?%#5  X 11w:: % *112=#a&I s   -B>>
C7$C22C7default_rolec           
      <   U R                  SU5      nU R                  S5      =(       d    SnUS:X  a	  [        US9$ US:X  a  0 n/ nU R                  S5      =n(       aV  XdS'    U Vs/ sHE  n[        US   R                  S	5      US   R                  S
5      UR                  S5      US   S9PMG     nn[	        UUU R                  S5      US9$ US:X  a	  [        US9$ US:X  a  [        X0S   U R                  S5      S9$ [        X2S9$ s  snf ! [         a     Nff = f)Nrz   r   r   r   r   r   r   functionname	argumentsr   index)r   argsr   r   )r   r   r   tool_call_chunksr   tooltool_call_id)r   r   r   r   )r   r   r"   KeyErrorr   r   r!   r   )r   r   rz   r   r   r   r   rtcs           rI   r   *ChatMlflow._convert_delta_to_message_chunk   sT    yy.))I&,"6>$W55[ &(!!&<!88~82@,/ $2( $2C (!$Z!4!4V!<!$Z!4!4[!A"wwt}"%g,	 $2 % ( ""399T?!1	  X%g66V^#N.C		RV  $G??1(   s%   #D 'AD	2D 	D 
DDc                      [        S5      e)N~Function messages are not supported by Databricks. Please create a feature request at https://github.com/mlflow/mlflow/issues.)rV   r?   rO   rI   _raise_functions_not_supported)ChatMlflow._raise_functions_not_supportedH  s    T
 	
rO   ra   c                    SU R                   0nU R                  =(       d    U R                  R                  S5      =nb  X!S'   [	        U [
        5      (       a  U R                  US'   GO[	        U [        5      (       a  SUS'   GO[	        U [        5      (       a  SUS'   U R                  (       d  U R                  (       aO  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'   OmSU R                  ;   a\  U R                  S   US'   1 SknUS    VVVs/ sH.  nUR                  5        VVs0 sH  u  pgXd;   d  M  Xg_M     snnPM0     snnnUS'   O SU;   a  US   =(       d    S US'   O[	        U [        5      (       a  SUS'   O[	        U [        5      (       aC  S	US'   U R                   US
'   1 SknUR                  5        VVs0 sH  u  pgXh;   d  M  Xg_M     nnnO.[	        U ["        5      (       a  [%        S5      e[%        SU  35      eSU R                  ;   a  [&        R)                  5         U$ s  snf s  snf s  snnf s  snnnf s  snnf )Nr   r   rz   r   r   r   >   r   typer   r   r   r   >   rz   r   r   r   zGot unknown message type: function_call)r   r   r   r   
isinstancer   rz   r   r   r   r   !_lc_tool_call_to_openai_tool_call)_lc_invalid_tool_call_to_openai_tool_callitemsr   r    r   r   rV   r0   r   )	ra   message_dictr   tctool_call_supported_props	tool_callkvsupported_propss	            rI   r`   #ChatMlflow._convert_message_to_dictO  s}   !7??3LLIG$=$=$A$A&$IIDV#' g{++#*<<L ..#)L ++#.L !!W%?%?DKDVDV.DVb5b9DV. &888 >bA8.\* !:!::-4-F-F|-T\*,F) &2,%?. &@	 %.OO$5$5DA9 $5
 &@.\* |+*6y*A*ITY'//#+L --#)L +2+?+?L(AO!-!3!3!5!59M!5  L 11X 
 9'CDDg777557S..&s6   I4I" I-
I'&I',I-1
I4?I4'I-responsec                     / nU S    HH  n[         R                  US   5      nUR                  S0 5      n[        UUS9nUR	                  U5        MJ     U R                  S0 5      n[        XS9$ )Nrx   ra   usager}   )generations
llm_output)r0   r   r   r%   r   r'   )r   r   r   ra   r   gens         rI   rk   ChatMlflow._create_chat_result  sx    y)F 99&:KLGJJw+E  %C s# * Wb)kDDrO   )tool_choicetoolsr   )autononerequiredanyc                  >^ U Vs/ sH  n[        U5      PM     nnT(       a  [        T[        5      (       a  TS;  a  SST0S.mOg[        T[        5      (       aD  U Vs/ sH  nUS   S   PM     nn[	        U4S jU 5       5      (       d  [        ST SU S35      eO[        S	T 35      eTUS
'   [        TU ]  " SSU0UD6$ s  snf 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.
        Can be  a dictionary, pydantic model, callable, or BaseTool. Pydantic
        models, callables, and BaseTools will be automatically converted to
        their schema dictionary representation.
    tool_choice: Which tool to require the model to call.
        Options are:
        name of the tool (str): calls corresponding tool;
        "auto": automatically selects a tool (including no tool);
        "none": model does not generate any tool calls and instead must
            generate a standard assistant message;
        "required": the model picks the most relevant tool in tools and
            must generate a tool call;

        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.
)r   r   r   r   r   )r   r   c              3   8   >#    U H  nUTS    S   :H  v   M     g7f)r   r   Nr?   ).0	tool_namer   s     rI   	<genexpr>(ChatMlflow.bind_tools.<locals>.<genexpr>  s'      %/	 Z!8!@@%/s   zTool choice z1 was specified, but the only provided tools were rT   zEUnrecognized tool_choice type. Expected str, bool or dict. Received: r   r   r?   )r+   r   r   dictr   rV   r@   bind)	rF   r   r   r<   r   formatted_toolsformatted_tool
tool_namesrH   s	     `     rI   
bind_toolsChatMlflow.bind_tools  s   @ EJJED1$7EJ+s++&BB *%+[$9#K K.. +:*9 #:.v6*9    %/   %&{m 4//9l!= 	 !!!,/  %0F=!w|</<V<<9 Ks   C C
)r;   )rK   Nr   )NN)=__name__
__module____qualname____firstlineno____doc__r   __annotations__r4   floatr6   intr7   r
   r   r8   r-   r   r:   r.   r;   r   rA   propertyrE   rB   r   r\   r   rd   r   r'   rm   r   r)   r   r   ru   r&   r   r   r   r   staticmethodr	   r   r   r   r`   rk   r   r   r   r,   r   r*   r   boolr(   r   __static_attributes____classcell__)rH   s   @rI   r0   r0   9   s   " MO K#AsJ7 $D(49
$ $J$3t4L$47=GS      
c3h 
 
 %){# tCy! 	
 
c3h4 %):>	4{#4 tCy!4 67	4
 4 
4$ ,0J
 %)J!J (J
 tCy!J J 
"	#J J, %):>	-{#- tCy!- 67	-
 - 
%	&-^ $T#s(^ $ $ +/
T#Y'
:=
	c3h
 
 3   ;S(9 ;k ; ;B %@sCx %@03%@	%@ %@N 
 
 4+ 4$ 4 4l Egc3h&7 EJ E E( <=d38nd9oxQRS<= $W%FGMN
	<= <= 
$k1	2<= <=rO   r0   r   rK   c                 N    SU S   U S   [         R                  " U S   5      S.S.$ Nr   r   r   r   )r   r   )r   r   r   )jsondumps)r   s    rI   r   r     s4    of%If$56
 rO   invalid_tool_callc                 &    SU S   U S   U S   S.S.$ r   r?   )r   s    rI   r   r     s.     %%f-*62
 rO   )Br   loggingtypingr   r   r   r   r   r   r	   r
   r   r   r   r   urllib.parser   langchain_core.callbacksr   langchain_core.language_modelsr   #langchain_core.language_models.baser   langchain_core.messagesr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   langchain_core.messages.toolr"   *langchain_core.output_parsers.openai_toolsr#   r$   langchain_core.outputsr%   r&   r'   langchain_core.runnablesr(   r)   langchain_core.toolsr*   %langchain_core.utils.function_callingr+   pydanticr,   r-   r.   	getLoggerr   loggerr0   r   r   r   r?   rO   rI   <module>r     s         " = 8 B    " 9 S R = ) H  
		8	$X= X=v d 
&
	
rO   