o
    Zho?                     @   s0  d dl Z d dlZd dlmZmZmZ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mZmZmZmZmZmZmZmZmZ d dlmZm Z m!Z! d dl"m#Z#m$Z$m%Z% G dd	 d	e%Z&G d
d de%Z'G dd de'Z(G dd de(Z)G dd dee#Z*dee+ef de
e defddZ,dS )    N)	AnyAsyncIteratorDictIteratorListMappingOptionalTypecast)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModel)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageChunkHumanMessageHumanMessageChunkSystemMessageSystemMessageChunkToolMessageChunk)ChatGenerationChatGenerationChunk
ChatResult)AzureMLBaseEndpointAzureMLEndpointApiTypeContentFormatterBasec                   @   s   e Zd ZdZdddZdS )LlamaContentFormatterzContent formatter for `LLaMA`.returnNc                 C   s   t d)Nzb`LlamaContentFormatter` is deprecated for chat models. Use `CustomOpenAIContentFormatter` instead.)	TypeErrorself r%   g/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_models/azureml_endpoint.py__init__.   s   zLlamaContentFormatter.__init__r!   N)__name__
__module____qualname____doc__r'   r%   r%   r%   r&   r    +   s    r    c                   @   s   e Zd ZU dZg dZee ed< ede	de
fddZedee fdd	Zd
ee	 de
dedefddZejfdededefddZdS ) CustomOpenAIChatContentFormatterz>Chat Content formatter for models with OpenAI like API scheme.)user	assistantsystemSUPPORTED_ROLESmessager!   c                 C   s   t t| j}t| trdt|dS t| tr dt|dS t| tr-dt|dS t| t	rA| j
tjv rA| j
t|dS ddd tjD }td| )	z0Converts a message to a dict according to a roler.   )rolecontentr/   r0   ,c                 S   s   g | ]}|qS r%   r%   ).0r3   r%   r%   r&   
<listcomp>W   s    zMCustomOpenAIChatContentFormatter._convert_message_to_dict.<locals>.<listcomp>z\Received unsupported role. 
                Supported roles for the LLaMa Foundation Model: )r
   strr4   
isinstancer   r   Zescape_special_charactersr   r   r   r3   r-   r1   join
ValueError)r2   r4   	supportedr%   r%   r&   _convert_message_to_dict:   s6   


z9CustomOpenAIChatContentFormatter._convert_message_to_dictc                 C   s   t jt jgS )N)r   	dedicated
serverlessr#   r%   r%   r&   supported_api_types^   s   z4CustomOpenAIChatContentFormatter.supported_api_typesmessagesmodel_kwargsapi_typec                 C   sl   dd |D }|t jt jfv rtd||di}n|t jkr)td|i|}ntd| dt|S )/Formats the request according to the chosen apic                 S      g | ]}t |qS r%   r-   r=   r6   r2   r%   r%   r&   r7   i       zTCustomOpenAIChatContentFormatter.format_messages_request_payload.<locals>.<listcomp>
input_dataZinput_string
parametersrA   `api_type` # is not supported by this formatter)	r   r>   realtimejsondumpsr?   r;   r8   encoder$   rA   rB   rC   Zchat_messagesrequest_payloadr%   r%   r&   format_messages_request_payloadb   s(   


z@CustomOpenAIChatContentFormatter.format_messages_request_payloadoutputc              
   C   sD  |t jt jfv r6z	t|d }W n tttfy* } z
t| j	j
|d|d}~ww tt| dddS |t jkrzt|d d }t|tsNtdW n tttfyh } z
t| j	j
|d|d}~ww t|d	 d
 dkr}t|d	 d  dnt|d	 d  |d	 d
 dt|d|dddS td| d)zFormats responserU   )rC   Nr4   r2   generation_infochoicesr   ziEndpoint response is not well formed for a chat model. Expected `dict` but `{type(choice)}` was received.r2   r3   r/   r4   )r4   typefinish_reasonlogprobs)r[   r\   rL   rM   )r   r>   rN   rO   loadsKeyError
IndexErrorr"   r;   Zformat_error_msgformatr   r   stripr?   r9   dictr   get)r$   rU   rC   choiceer%   r%   r&   format_response_payload   sR   


z8CustomOpenAIChatContentFormatter.format_response_payloadN)r)   r*   r+   r,   r1   r   r8   __annotations__staticmethodr   r   r=   propertyr   r@   bytesrT   r>   r   rf   r%   r%   r%   r&   r-   5   s0   
 #
"r-   c                       s"   e Zd ZdZd fddZ  ZS )LlamaChatContentFormatterzSDeprecated: Kept for backwards compatibility

    Chat Content formatter for Llama.r!   Nc                    s   t    td d S )Nz`LlamaChatContentFormatter` will be deprecated in the future. 
                Please use `CustomOpenAIChatContentFormatter` instead.  
            )superr'   warningswarnr#   	__class__r%   r&   r'      s   
z"LlamaChatContentFormatter.__init__r(   )r)   r*   r+   r,   r'   __classcell__r%   r%   ro   r&   rk      s    rk   c                   @   s.   e Zd ZdZdee dededefddZ	dS )	MistralChatContentFormatterz Content formatter for `Mistral`.rA   rB   rC   r!   c                    s    fdd|D }|r(|d d dkr(|d d d |d d  |d d< |d= |t jkr8td	||d
i}n|t jkrGtd|i|}ntd| dt|S )rD   c                    s   g | ]}  |qS r%   )r=   rG   r#   r%   r&   r7      s    zOMistralChatContentFormatter.format_messages_request_payload.<locals>.<listcomp>r   r3   r0   r4   z

   rI   rJ   rA   rL   rM   )r   rN   rO   rP   r?   r;   r8   rQ   rR   r%   r#   r&   rT      s&   




z;MistralChatContentFormatter.format_messages_request_payloadN)
r)   r*   r+   r,   r   r   r   r   rj   rT   r%   r%   r%   r&   rr      s    rr   c                   @   s   e Zd ZdZedeeef fddZedefddZ			dde
e d	ee
e  d
ee dedef
ddZ		dde
e d	ee
e  d
ee dedee f
ddZ		dde
e d	ee
e  d
ee dedee f
ddZdS )AzureMLChatOnlineEndpointa  Azure ML Online Endpoint chat models.

    Example:
        .. code-block:: python
            azure_llm = AzureMLOnlineEndpoint(
                endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/v1/chat/completions",
                endpoint_api_type=AzureMLApiType.serverless,
                endpoint_api_key="my-api-key",
                content_formatter=chat_content_formatter,
            )
    r!   c                 C   s   | j pi }i d|iS )zGet the identifying parameters.rB   )rB   )r$   _model_kwargsr%   r%   r&   _identifying_params   s   
z-AzureMLChatOnlineEndpoint._identifying_paramsc                 C   s   dS )zReturn type of llm.Zazureml_chat_endpointr%   r#   r%   r%   r&   	_llm_type   s   z#AzureMLChatOnlineEndpoint._llm_typeNrA   stoprun_managerkwargsc           	      K   s^   | j pi }|| |r||d< | j||| j}| jj||d}| j|| j}t|gdS )a  Call out to an AzureML Managed Online endpoint.
        Args:
            messages: The messages in the conversation with the chat model.
            stop: Optional list of stop words to use when generating.
        Returns:
            The string generated by the model.
        Example:
            .. code-block:: python
                response = azureml_model.invoke("Tell me a joke.")
        rx   )bodyry   )generations)	rB   updateZcontent_formatterrT   Zendpoint_api_typehttp_clientcallrf   r   )	r$   rA   rx   ry   rz   ru   rS   Zresponse_payloadr|   r%   r%   r&   	_generate   s   

z#AzureMLChatOnlineEndpoint._generatec                 k   s@   | j dd| _ d|vrd n|d }dd l}i }| j | j |d d d d}|jdi |}	dd |D }
d|d d	|}t}|	jjj	dd
|
i|D ]R}t
|tsV| }t|d dkr_qK|d d }t|d |}i }|d }ry||d< |d}|r||d< |j}t||pd d}|r|j|j||d |V  qKd S )N/chat/completions timeoutr   Zapi_keybase_urlr   default_headersZdefault_queryr~   c                 S   rE   r%   rF   r6   mr%   r%   r&   r7   4  rH   z5AzureMLChatOnlineEndpoint._stream.<locals>.<listcomp>Tstreamrx   modelrA   rY   deltar[   r\   rW   )chunkr\   r%   )endpoint_urlreplaceopenaiendpoint_api_keyget_secret_valueZOpenAIr   chatcompletionscreater9   rb   len_convert_delta_to_message_chunkrc   rp   r   on_llm_new_tokentext)r$   rA   rx   ry   rz   r   r   paramsclient_paramsclientmessage_dictsdefault_chunk_classr   rd   rX   r[   r\   r%   r%   r&   _stream  sV   	

z!AzureMLChatOnlineEndpoint._streamc                 K  sV  | j dd| _ d|vrd n|d }dd l}i }| j | j |d d d d}|jdi |}	dd |D }
d|d d	|}t}|	jjj	dd
|
i|I d H 2 zY3 d H W }t
|ts]| }t|d dkrfqN|d d }t|d |}i }|d }r||d< |d}|r||d< |j}t||pd d}|r|j|j||dI d H  |V  qN6 d S )Nr   r   r   r   r   c                 S   rE   r%   rF   r   r%   r%   r&   r7   k  rH   z6AzureMLChatOnlineEndpoint._astream.<locals>.<listcomp>Tr   rA   rY   r   r[   r\   rW   )tokenr   r\   r%   )r   r   r   r   r   ZAsyncOpenAIr   r   r   r   r9   rb   r   r   rc   rp   r   r   r   )r$   rA   rx   ry   rz   r   r   r   r   Zasync_clientr   r   r   rd   rX   r[   r\   r%   r%   r&   _astreamT  s^   	

z"AzureMLChatOnlineEndpoint._astream)NN)r)   r*   r+   r,   ri   r   r8   r   rv   rw   r   r   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r&   rt      sZ    

$

:
rt   _dictdefault_classr!   c                 C   s*  t t| d}t t| dpd}i }| dr1t| d }d|v r-|d d u r-d|d< ||d< | dr<| d |d< |dksD|tkrIt|dS |d	ksQ|tkrWt||d
S |dks_|tkrdt|dS |dksl|tkrtt|| d dS |dks||tkrt|| d dS |s|t	krt	||dS ||dS )Nr3   r4   r   function_callnameZ
tool_callsr.   rV   r/   )r4   additional_kwargsr0   function)r4   r   Ztooltool_call_id)r4   r   )r4   r3   )
r
   r8   rc   rb   r   r   r   r   r   r   )r   r   r3   r4   r   r   r%   r%   r&   r     s.   




r   )-rO   rm   typingr   r   r   r   r   r   r   r	   r
   Zlangchain_core.callbacksr   r   Z*langchain_core.language_models.chat_modelsr   Zlangchain_core.messagesr   r   r   r   r   r   r   r   r   r   r   r   Zlangchain_core.outputsr   r   r   Z)langchain_community.llms.azureml_endpointr   r   r   r    r-   rk   rr   rt   r8   r   r%   r%   r%   r&   <module>   s*    ,8
y& .
