
    3fi                         d dl Z d dlZd dlmZ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  e j0                  e      Z G d d	ed
      Z G d de      Zy)    N)AnyDictListMappingOptional)CallbackManagerForLLMRun)BaseChatModel)	AIMessageBaseMessageChatMessageFunctionMessageHumanMessageSystemMessage)ChatGeneration
ChatResult)	BaseModelc                   ^    e Zd ZU dZdZeed<   dZeed<   	 dZ	e
ee      ed<   dZe
e   ed<   y)	
ChatParamsz+Parameters for the `MLflow AI Gateway` LLM.g        temperature   candidate_countNstop
max_tokens)__name__
__module____qualname____doc__r   float__annotations__r   intr   r   r   strr        o/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_models/mlflow_ai_gateway.pyr   r      s=    5KOS- $D(49
$ $J$r#   r   allow)extrac                       e Zd ZU dZdef fdZeed<   dZe	e   ed<   dZ
e	e   ed<   edeeef   fd	       Z	 	 dd
ee   de	ee      de	e   dedef
dZedeeef   fd       Z	 dde	ee      dedeeef   f fdZedefd       Zedeeef   defd       Zedd       Zededefd       Zedeeef   defd       Z xZS )ChatMLflowAIGatewayaE  `MLflow AI Gateway` chat models API.

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

    Example:
        .. code-block:: python

            from langchain_community.chat_models import ChatMLflowAIGateway

            chat = ChatMLflowAIGateway(
                gateway_uri="<your-mlflow-ai-gateway-uri>",
                route="<your-mlflow-ai-gateway-chat-route>",
                params={
                    "temperature": 0.1
                }
            )
    kwargsc                     t        j                  dt               	 dd l}t        |   di | | j                  r&|j                  j                  | j                         y y # t        $ r}t	        d      |d }~ww xY w)NzR`ChatMLflowAIGateway` is deprecated. Use `ChatMlflow` or `ChatDatabricks` instead.r   _Could not import `mlflow.gateway` module. Please install it with `pip install mlflow[gateway]`.r"   )
warningswarnDeprecationWarningmlflow.gatewayImportErrorsuper__init__gateway_urigatewayset_gateway_uri)selfr)   mlflowe	__class__s       r$   r2   zChatMLflowAIGateway.__init__:   s    (	

	! 	"6"NN**4+;+;<   	H 	s   A# #	A=,A88A=routeNr3   paramsreturnc                     | j                   | j                  d| j                  r| j                  j                         }|S i }|S )N)r3   r:   )r3   r:   r;   dict)r6   r;   s     r$   _default_paramsz#ChatMLflowAIGateway._default_paramsP   sR      ++ZZ"
 &*[[t{{!"

  79"

 r#   messagesr   run_managerc                 d   	 dd l }|D cg c]  }t        j                  |       }}d|i| j                  r| j                  j                         ni }	|j                  j                  | j                  |	      }
t        j                  |
      S # t        $ r}t        d      |d }~ww xY wc c}w )Nr   r+   r@   )data)
r/   r0   r(   _convert_message_to_dictr;   r>   r4   queryr:   _create_chat_result)r6   r@   r   rA   r)   r7   r8   messagemessage_dictsrC   resps              r$   	_generatezChatMLflowAIGateway._generateY   s    	! $
  88A
 

  
%)[[t{{!b 

 ~~##DJJT#:"66t<<!  	H 	
s   B B-	B*B%%B*c                     | j                   S N)r?   r6   s    r$   _identifying_paramsz'ChatMLflowAIGateway._identifying_paramst   s    ###r#   c                 B    i | j                   t        |   dd|i|S )z>Get the parameters used to invoke the model FOR THE CALLBACKS.r   r"   )r?   r1   _get_invocation_params)r6   r   r)   r9   s      r$   rP   z*ChatMLflowAIGateway._get_invocation_paramsx   s5    
""
g,A$A&A
 	
r#   c                      y)zReturn type of chat model.zmlflow-ai-gateway-chatr"   rM   s    r$   	_llm_typezChatMLflowAIGateway._llm_type   s     (r#   _dictc                     | d   }| d   }|dk(  rt        |      S |dk(  rt        |      S |dk(  rt        |      S t        ||      S )Nrolecontentuser)rV   	assistantsystem)rV   rU   )r   r
   r   r   )rS   rU   rV   s      r$   _convert_dict_to_messagez,ChatMLflowAIGateway._convert_dict_to_message   sZ    V}	"6>00[ W--X 11wT::r#   c                      t        d      )NFunction messages are not supported by the MLflow AI Gateway. Please create a feature request at https://github.com/mlflow/mlflow/issues.)
ValueErrorr"   r#   r$   _raise_functions_not_supportedz2ChatMLflowAIGateway._raise_functions_not_supported   s    T
 	
r#   rG   c                    t        | t              r| j                  | j                  d}nt        | t              rd| j                  d}nit        | t
              rd| j                  d}nIt        | t              rd| j                  d}n)t        | t              rt        d      t        d|        d| j                  v rt        j                          | j                  r t        j                  d| j                         |S )	N)rU   rV   rW   rX   rY   r\   zGot unknown message type: function_callzZAdditional message arguments are unsupported by MLflow AI Gateway  and will be ignored: %s)
isinstancer   rU   rV   r   r
   r   r   r]   additional_kwargsr(   r^   loggerwarning)rG   message_dicts     r$   rD   z,ChatMLflowAIGateway._convert_message_to_dict   s    g{+$+LLW__ML.$*wGL+$/GOOLL/$,IL1X 
 9'CDDg777>>@$$NN+))
 r#   responsec                     g }| d   D ]S  }t         j                  |d         }|j                  di       }t        |t	        |            }|j                  |       U | j                  di       }t        ||      S )N
candidatesrG   metadata)rG   generation_info)generations
llm_output)r(   rZ   getr   r>   appendr   )rf   rk   	candidaterG   message_metadatagenresponse_metadatas          r$   rF   z'ChatMLflowAIGateway._create_chat_result   s    !,/ 	$I)BB9YCWXG(}}Z<  $%5 6C s#	$ %LLR8k>OPPr#   )NNrL   )r<   N)r   r   r   r   r   r2   r!   r   r3   r   r;   r   propertyr   r?   r   r   r   r   rJ   rN   rP   rR   staticmethodr   rZ   r^   r>   rD   rF   __classcell__)r9   s   @r$   r(   r(   &   s   &= =$ J!%K#%#'FHZ 'c3h   %):>	={#= tCy!= 67	=
 = 
=6 $T#s(^ $ $ +/
T#Y'
:=
	c3h
 (3 ( ( 
;S(9 
;k 
; 
; 
 
 + $  6 Qgc3h&7 QJ Q Qr#   r(   )loggingr,   typingr   r   r   r   r   langchain_core.callbacksr   *langchain_core.language_models.chat_modelsr	   langchain_core.messagesr
   r   r   r   r   r   langchain_core.outputsr   r   pydanticr   	getLoggerr   rc   r   r(   r"   r#   r$   <module>r~      s`      5 5 E  			8	$
%' %]Q- ]Qr#   