
    hh                         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 j        e          Z G d d	ed
          Z G d de          ZdS )    N)AnyDictListMappingOptional)CallbackManagerForLLMRun)BaseChatModel)	AIMessageBaseMessageChatMessageFunctionMessageHumanMessageSystemMessage)ChatGeneration
ChatResult)	BaseModelc                   r    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<   dS )	
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        n/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/chat_models/mlflow_ai_gateway.pyr   r      sh         5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}n"# t          $ r}t	          d          |d }~ww xY w t                      j        di | | j        r!|j        	                    | j                   d S d S )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    (	
 	
 	

	!!!!! 	 	 	H  	 	""6""" 	=N**4+;<<<<<	= 	=s   " 
A<ArouteNr3   paramsreturnc                 h    | j         | j        d| j        r| j                                        ni }|S )N)r3   r:   )r3   r:   r;   dict)r6   r;   s     r$   _default_paramsz#ChatMLflowAIGateway._default_paramsP   sF      +Z"
 "
 &*[8t{!!!b"

 r#   messagesr   run_managerc                 ,   	 dd l }n"# t          $ r}t          d          |d }~ww xY wd |D             }d|i| j        r| j                                        ni }|j                            | j        |          }	t                              |	          S )Nr   r+   c                 B    g | ]}t                               |          S r"   )r(   _convert_message_to_dict).0messages     r$   
<listcomp>z1ChatMLflowAIGateway._generate.<locals>.<listcomp>h   s6     
 
 
  88AA
 
 
r#   r@   )data)	r/   r0   r;   r>   r4   queryr:   r(   _create_chat_result)
r6   r@   r   rA   r)   r7   r8   message_dictsrH   resps
             r$   	_generatezChatMLflowAIGateway._generateY   s    	!!!!! 	 	 	H  	
 
#
 
 

  
%)[8t{!!!b 

 ~##DJT#::"66t<<<s    
&!&c                     | j         S N)r?   r6   s    r$   _identifying_paramsz'ChatMLflowAIGateway._identifying_paramst   s    ##r#   c                 L    i | j          t                      j        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$   rS   z*ChatMLflowAIGateway._get_invocation_paramsx   s<    
"
,egg,AA$A&AA
 	
r#   c                     dS )zReturn type of chat model.zmlflow-ai-gateway-chatr"   rP   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)rY   	assistantsystem)rY   rX   )r   r
   r   r   )rV   rX   rY   s      r$   _convert_dict_to_messagez,ChatMLflowAIGateway._convert_dict_to_message   sz    V}	"6>>0000[  W----X 11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#   rF   c                    t          | t                    r| j        | j        d}nt          | t                    rd| j        d}nvt          | t
                    rd| j        d}nVt          | t                    rd| j        d}n6t          | t                    rt          d          t          d|            d| j	        v rt                                           | j	        r t                              d| j	                   |S )	N)rX   rY   rZ   r[   r\   r_   zGot unknown message type: function_callzZAdditional message arguments are unsupported by MLflow AI Gateway  and will be ignored: %s)
isinstancer   rX   rY   r   r
   r   r   r`   additional_kwargsr(   ra   loggerwarning)rF   message_dicts     r$   rD   z,ChatMLflowAIGateway._convert_message_to_dict   s,   g{++ 	E$+LW_MMLL.. 	E$*wGGLL++ 
	E$/GOLLLL// 	E$,IILL11 	EX  
 C'CCDDDg777>>@@@$ 	NN+)  
 r#   responsec                 <   g }| d         D ]k}t                               |d                   }|                    di           }t          |t	          |                    }|                    |           l|                     di           }t          ||          S )N
candidatesrF   metadata)rF   generation_info)generations
llm_output)r(   r]   getr   r>   appendr   )ri   rn   	candidaterF   message_metadatagenresponse_metadatas          r$   rJ   z'ChatMLflowAIGateway._create_chat_result   s    !,/ 	$ 	$I)BB9YCWXXG(}}Z<<  $%5 6 6  C s####$LLR88k>OPPPPr#   )NNrO   )r<   N)r   r   r   r   r   r2   r!   r   r3   r   r;   r   propertyr   r?   r   r   r   r   rM   rQ   rS   rU   staticmethodr   r]   ra   r>   rD   rJ   __classcell__)r9   s   @r$   r(   r(   &   s         &= = = = = = =$ JJJ!%K#%%%#'FHZ '''c3h    X %):>	= ={#= tCy!= 67	=
 = 
= = = =6 $T#s(^ $ $ $ X$ +/
 
T#Y'
:=
	c3h
 
 
 
 
 
 (3 ( ( ( X( 
;S(9 
;k 
; 
; 
; \
; 
 
 
 \
 + $    \6 Qgc3h&7 QJ Q Q Q \Q Q Q 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   rf   r   r(   r"   r#   r$   <module>r      s     5 5 5 5 5 5 5 5 5 5 5 5 5 5      E D D D D D                            		8	$	$
% % % % %' % % % %]Q ]Q ]Q ]Q ]Q- ]Q ]Q ]Q ]Q ]Qr#   