a
    bgo?                     @   s,  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*ee+ef e
e ed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dZdS )LlamaContentFormatterzContent formatter for `LLaMA`.Nreturnc                 C   s   t dd S )Nzb`LlamaContentFormatter` is deprecated for chat models. Use `CustomOpenAIContentFormatter` instead.)	TypeErrorself r&   ~/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_models/azureml_endpoint.py__init__.   s    zLlamaContentFormatter.__init__)__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e	e
dddZeee ddd	Zee	 e
eed
ddZejfeeed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rZdt|dS t| t	r| j
tjv r| j
t|dS ddd tjD }td| d	S )
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       zMCustomOpenAIChatContentFormatter._convert_message_to_dict.<locals>.<listcomp>z\Received unsupported role. 
                Supported roles for the LLaMa Foundation Model: N)r
   strr4   
isinstancer   r   Zescape_special_charactersr   r   r   r3   r-   r1   join
ValueError)r2   r4   	supportedr&   r&   r'   _convert_message_to_dict:   s8    



z9CustomOpenAIChatContentFormatter._convert_message_to_dictr!   c                 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_typer"   c                 C   sl   dd |D }|t jt jfv r4td||di}n.|t jkrRtd|i|}ntd| dt|S )/Formats the request according to the chosen apic                 S   s   g | ]}t |qS r&   r-   r>   r6   r2   r&   r&   r'   r7   i   s   zTCustomOpenAIChatContentFormatter.format_messages_request_payload.<locals>.<listcomp>
input_dataZinput_string
parametersrC   `api_type` # is not supported by this formatter)	r   r?   realtimejsondumpsr@   r<   r9   encoder%   rC   rD   rE   Zchat_messagesrequest_payloadr&   r&   r'   format_messages_request_payloadb   s(    

z@CustomOpenAIChatContentFormatter.format_messages_request_payload)outputrE   r"   c              
   C   sd  |t jt jfv rxzt|d }W n> tttfy` } z t| j	j
|d|W Y d}~n
d}~0 0 tt| dddS |t jkrPz(t|d d }t|tstdW n> tttfy } z t| j	j
|d|W Y d}~n
d}~0 0 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dS )zFormats responserU   )rE   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@   r:   dictr   get)r%   rU   rE   choiceer&   r&   r'   format_response_payload   sH    *
*
z8CustomOpenAIChatContentFormatter.format_response_payloadN)r)   r*   r+   r,   r1   r   r9   __annotations__staticmethodr   r   r>   propertyr   rA   bytesrT   r?   r   rf   r&   r&   r&   r'   r-   5   s"   
#"r-   c                       s&   e Zd ZdZdd fddZ  ZS )LlamaChatContentFormatterzSDeprecated: Kept for backwards compatibility

    Chat Content formatter for Llama.Nr!   c                    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(   __classcell__r&   r&   ro   r'   rk      s   rk   c                   @   s(   e Zd ZdZee eeedddZ	dS )MistralChatContentFormatterz Content formatter for `Mistral`.rB   c                    s    fdd|D }|rP|d d dkrP|d d d |d d  |d d< |d= |t jkrptd	||d
i}n.|t jkrtd|i|}ntd| dt|S )rF   c                    s   g | ]}  |qS r&   )r>   rH   r$   r&   r'   r7      r8   zOMistralChatContentFormatter.format_messages_request_payload.<locals>.<listcomp>r   r3   r0   r4   z

   rI   rJ   rC   rL   rM   )r   rN   rO   rP   r@   r<   r9   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eeef dddZeedddZ	de
e ee
e  ee eedd	d
Zde
e ee
e  ee eee dddZde
e ee
e  ee eee d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.rD   )rD   )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_typeN)rC   stoprun_managerkwargsr"   c           	      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)	rD   updateZcontent_formatterrT   Zendpoint_api_typehttp_clientcallrf   r   )	r%   rC   rx   ry   rz   ru   rS   Zresponse_payloadr|   r&   r&   r'   	_generate   s    

z#AzureMLChatOnlineEndpoint._generatec                 k   sD  | j dd| _ d|vrd n|d }dd l}i }| j | j |d d d d}|jf i |}	dd |D }
d|d d	|}t}|	jjj	f d
|
i|D ]}t
|ts| }t|d dkrq|d d }t|d |}i }|d }r||d< |d}|r||d< |j}t||pd d}|r8|j|j||d |V  qd S )N/chat/completions timeoutr   Zapi_keybase_urlr   default_headersZdefault_queryr~   c                 S   s   g | ]}t |qS r&   rG   r6   mr&   r&   r'   r7   4  s   z5AzureMLChatOnlineEndpoint._stream.<locals>.<listcomp>Tstreamrx   modelrC   rY   deltar[   r\   rW   )chunkr\   )endpoint_urlreplaceopenaiendpoint_api_keyget_secret_valueZOpenAIr   chatcompletionscreater:   rb   len_convert_delta_to_message_chunkrc   rp   r   on_llm_new_tokentext)r%   rC   rx   ry   rz   r   r   paramsclient_paramsclientmessage_dictsdefault_chunk_classr   rd   rX   r[   r\   r&   r&   r'   _stream  sR    	

z!AzureMLChatOnlineEndpoint._streamc                 K  sZ  | j dd| _ d|vrd n|d }dd l}i }| j | j |d d d d}|jf i |}	dd |D }
d|d d	|}t}|	jjj	f d
|
i|I d H 2 z3 d H W }t
|ts| }t|d dkrq|d d }t|d |}i }|d }r||d< |d}|r||d< |j}t||p(d d}|rL|j|j||dI d H  |V  q6 d S )Nr   r   r   r   r   c                 S   s   g | ]}t |qS r&   rG   r   r&   r&   r'   r7   k  s   z6AzureMLChatOnlineEndpoint._astream.<locals>.<listcomp>Tr   rC   rY   r   r[   r\   rW   )tokenr   r\   )r   r   r   r   r   ZAsyncOpenAIr   r   r   r   r:   rb   r   r   rc   rp   r   r   r   )r%   rC   rx   ry   rz   r   r   r   r   Zasync_clientr   r   r   rd   rX   r[   r\   r&   r&   r'   _astreamT  sZ    	


z"AzureMLChatOnlineEndpoint._astream)NN)NN)NN)r)   r*   r+   r,   ri   r   r9   r   rv   rw   r   r   r   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   rt      s@     
$  
:  
rt   )_dictdefault_classr"   c                 C   s6  t t| d}t t| dp d}i }| drbt| d }d|v rZ|d d u rZd|d< ||d< | drx| d |d< |dks|tkrt|dS |d	ks|tkrt||d
S |dks|tkrt|dS |dks|tkrt|| d dS |dk s|tkrt|| d dS |s|t	kr(t	||dS ||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
   r9   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   r9   r   r&   r&   r&   r'   <module>   s    ,8
y& /