o
    i)                     @   s  U d dl Z d dlmZ d dlmZmZmZ d dl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 d dlmZ e jd	ed
d dedefddZG dd de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ZG dd deZG dd deZ e
eeB eB e B eddf Z!e
eeB eB e B eddf Z"ed e!d!Z#ed"ed!Z$ed#ed!Z%ed$e d!Z&ed%ed!Z'e$e%B e&B e'B Z(ee)d&< dS )'    N)Enum)AnyLiteralTypeVar)Field)	Annotated	TypeAlias)MistralBase)ContentChunk	TextChunk
ThinkChunkUserContentChunk_convert_openai_content_chunks)ToolCalloncezN.*has moved to 'mistral_common.protocol.instruct.chunk'\. It will be removed.*)actioncategorymessagenamereturnc                 C   s  |  dt  d}| dkrddlm} t|t |S | dkr,ddlm} t|t |S | dkr>dd	lm} t|t |S | d
krPddlm} t|t |S | dkrbddlm	} t|t |S | dkrtddlm
} t|t |S | dkrddlm} t|t |S | dkrddlm}	 t|t |	S | dkrddlm}
 t|t |
S | dkrddlm} t|t |S tdt  d|  )NzQ has moved to 'mistral_common.protocol.instruct.chunk'. It will be removed from 'z' in 1.10.0.AudioURLr   )r   AudioURLChunk)r   AudioURLType)r   
AudioChunk)r   BaseContentChunk)r   
ChunkTypes)r   ImageURL)r   ImageURLChunk)r   
ImageChunk)r   RawAudio)r   zmodule z has no attribute )__name__&mistral_common.protocol.instruct.chunkr   warningswarnFutureWarningr   r   r   r   r   r   r   r   r   AttributeError)r   msgr   r   r   r   r   r   r   r   r   r    r'   ]/home/ubuntu/.local/lib/python3.10/site-packages/mistral_common/protocol/instruct/messages.py__getattr__   sV   r)   c                   @   s    e Zd ZdZdZdZdZdZdS )RoleszEnum for the roles of the messages.

    Attributes:
       system: The system role.
       user: The user role.
       assistant: The assistant role.
       tool: The tool role.

    Examples:
        >>> role = Roles.user
    systemuser	assistanttoolN)r    
__module____qualname____doc__r+   r,   r-   r.   r'   r'   r'   r(   r*   T   s    r*   c                   @   s   e Zd ZU dZeejejejej	f e
d< deeeeeeeeeef B f  B f fddZedeeeeeeeeeef B f  B f dd fddZd	S )
BaseMessagezXBase class for all messages.

    Attributes:
       role: The role of the message.
    roler   c                 C   s   t dt| j )zaConverts the message to the OpenAI format.

        Should be implemented by subclasses.
        z%to_openai method not implemented for )NotImplementedErrortyper    selfr'   r'   r(   	to_openaip   s   zBaseMessage.to_openaiopenai_messagec                 C   s   t d| j d)ziConverts the OpenAI message to the Mistral format.

        Should be implemented by subclasses.
        z'from_openai method not implemented for .)r4   r    clsr9   r'   r'   r(   from_openaiw   s   zBaseMessage.from_openaiN)r    r/   r0   r1   r   r*   r+   r,   r-   r.   __annotations__dictstrlistr   r8   classmethodr=   r'   r'   r'   r(   r2   g   s   
 2<r2   c                   @   s   e Zd ZU dZejZeej ed< e	e
e B ed< dee	e	e
ee	e	ee	ef B f  B f fddZedee	e	e
ee	e	ee	ef B f  B f dd fdd	Zd
S )UserMessagezUser message.

    Attributes:
        content: The content of the message.

    Examples:
        >>> message = UserMessage(content="Can you help me to write a poem?")
    r3   contentr   c                 C   s2   t | jtr| j| jdS | jdd | jD dS )*Converts the message to the OpenAI format.r3   rD   c                 S      g | ]}|  qS r'   r8   .0chunkr'   r'   r(   
<listcomp>       z)UserMessage.to_openai.<locals>.<listcomp>)
isinstancerD   r@   r3   r6   r'   r'   r(   r8      s   zUserMessage.to_openair9   c                 C   s:   t |d tr| |S | |d dd |d D dS )2Converts the OpenAI message to the Mistral format.rD   r3   c                 S      g | ]}t |qS r'   r   rI   r'   r'   r(   rL      rM   z+UserMessage.from_openai.<locals>.<listcomp>rF   )rN   r@   model_validater;   r'   r'   r(   r=      s   
zUserMessage.from_openaiN)r    r/   r0   r1   r*   r,   r3   r   r>   r@   rA   r   r?   r   r8   rB   r=   r'   r'   r'   r(   rC      s   
 	2<rC   c                   @   s   e Zd ZU dZejZeej ed< e	e
eeB  B ed< dee	e	e
ee	e	ee	ef B f  B f fddZedee	e	e
ee	e	ee	ef B f  B f dd fdd	Zd
S )SystemMessagezSystem message.

    Attributes:
        content: The content of the message.

    Examples:
        >>> message = SystemMessage(content="You are a helpful assistant.")
    r3   rD   r   c                 C   s   |   S )rE   )
model_dumpr6   r'   r'   r(   r8      s   zSystemMessage.to_openair9   c                 C   s
   |  |S )rO   )rR   r;   r'   r'   r(   r=      s   
zSystemMessage.from_openaiN)r    r/   r0   r1   r*   r+   r3   r   r>   r@   rA   r   r   r?   r   r8   rB   r=   r'   r'   r'   r(   rS      s   
 	2<rS   c                   @   s   e Zd ZU dZejZeej ed< dZ	e
eeeB  B dB ed< dZee dB ed< dZeed< dee
e
eee
e
ee
ef B f  B f fd	d
Zedee
e
eee
e
ee
ef B f  B f dd fddZdS )AssistantMessagea<  Assistant message.

    Attributes:
        role: The role of the message.
        content: The content of the message.
        tool_calls: The tool calls of the message.
        prefix: Whether the message is a prefix.

    Examples:
        >>> message = AssistantMessage(content="Hello, how can I help you?")
    r3   NrD   
tool_callsFprefixr   c                 C   sd   d| j i}| jdu rnt| jtr| j|d< n
dd | jD |d< | jdur0dd | jD |d< |S )rE   r3   NrD   c                 S   rG   r'   rH   rI   r'   r'   r(   rL      rM   z.AssistantMessage.to_openai.<locals>.<listcomp>c                 S   rG   r'   rH   )rJ   	tool_callr'   r'   r(   rL      rM   rV   )r3   rD   rN   r@   rV   )r7   out_dictr'   r'   r(   r8      s   

zAssistantMessage.to_openair9   c                 C   s   | dd}|du rd}n t|tr$g }|pg D ]
}|t| qn	tdt| | dd}d}|du s>t|trA|}nt|trNdd |D }n	tdt| | 	|d ||d	S )
rO   rV   Nztool_calls must be a list, got rD   c                 S   rP   r'   rQ   rI   r'   r'   r(   rL      rM   z0AssistantMessage.from_openai.<locals>.<listcomp>zUnknown content type: r3   )r3   rD   rV   )
getrN   rA   appendr   r=   
ValueErrorr5   r@   rR   )r<   r9   openai_tool_callstools_callsopenai_tool_callopenai_contentrD   r'   r'   r(   r=      s,   

zAssistantMessage.from_openai)r    r/   r0   r1   r*   r-   r3   r   r>   rD   r@   rA   r   r   rV   r   rW   boolr?   r   r8   rB   r=   r'   r'   r'   r(   rU      s   
 2<rU   c                   @   s"   e Zd ZU dZdZedB ed< dS )FinetuningAssistantMessagezAssistant message for finetuning.

    Attributes:
        weight: The weight of the message to train on.

    Examples:
        >>> message = FinetuningAssistantMessage(content="Hello, how can I help you?", weight=0.5)
    Nweight)r    r/   r0   r1   rc   floatr>   r'   r'   r'   r(   rb      s   
 	rb   c                   @   s   e Zd ZU dZeee B ed< ej	Z
eej	 ed< dZedB ed< dZedB ed< deeeeeeeeeef B f  B f fdd	Zed
eeeeeeeeeef B f  B f dd fddZdS )ToolMessagea5  Tool message.

    Attributes:
        content: The content of the message.
        tool_call_id: The tool call id of the message.
        name: The name of the tool. (Deprecated in V3 tokenization)

    Examples:
       >>> message = ToolMessage(content="Hello, how can I help you?", tool_call_id="123")
    rD   r3   Ntool_call_idr   r   c                 C   s    | j dus	J d| jdhdS )rE   N0tool_call_id must be provided for tool messages.r   )exclude)rf   rT   r6   r'   r'   r(   r8     s   zToolMessage.to_openaimessagesc                 C   s    |  |}|jdusJ d|S )rO   Nrg   )rR   rf   )r<   ri   tool_messager'   r'   r(   r=     s   
zToolMessage.from_openai)r    r/   r0   r1   r@   rA   r   r>   r*   r.   r3   r   rf   r   r?   r   r8   rB   r=   r'   r'   r'   r(   re     s   
 2<re   r3   )discriminatorChatMessageType)boundUserMessageTypeAssistantMessageTypeToolMessageTypeSystemMessageTypeUATS)*r"   enumr   typingr   r   r   pydanticr   typing_extensionsr   r   mistral_common.baser	   r!   r
   r   r   r   r   +mistral_common.protocol.instruct.tool_callsr   filterwarningsr$   r@   r)   r*   r2   rC   rS   rU   rb   re   ChatMessageFinetuningMessagerl   rn   ro   rp   rq   rr   r>   r'   r'   r'   r(   <module>   sB   
 	9 @ 