o
    c۷i                      @   sx  U 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 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 )     )Enum)AnyLiteralTypeVar)Field)	Annotated	TypeAlias)MistralBase)ContentChunk	TextChunk
ThinkChunkUserContentChunk_convert_openai_content_chunks)ToolCallc                   @   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)__name__
__module____qualname____doc__r   r   r   r    r   r   _/home/ubuntu/vllm_env/lib/python3.10/site-packages/mistral_common/protocol/instruct/messages.pyr      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.
    rolereturnc                 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_openai.   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 .)r   r   clsr#   r   r   r   from_openai5   s   zBaseMessage.from_openaiN)r   r   r   r   r   r   r   r   r   r   __annotations__dictstrlistr   r"   classmethodr'   r   r   r   r   r   %   s   
 2<r   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?")
    r   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.r   r.   c                 S      g | ]}|  qS r   r"   .0chunkr   r   r   
<listcomp>O       z)UserMessage.to_openai.<locals>.<listcomp>)
isinstancer.   r*   r   r    r   r   r   r"   K   s   zUserMessage.to_openair#   c                 C   s:   t |d tr| |S | |d dd |d D dS )2Converts the OpenAI message to the Mistral format.r.   r   c                 S      g | ]}t |qS r   r   r3   r   r   r   r6   Y   r7   z+UserMessage.from_openai.<locals>.<listcomp>r0   )r8   r*   model_validater%   r   r   r   r'   Q   s   
zUserMessage.from_openaiN)r   r   r   r   r   r   r   r   r(   r*   r+   r   r)   r   r"   r,   r'   r   r   r   r   r-   >   s   
 	2<r-   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.")
    r   r.   r   c                 C   s   |   S )r/   )
model_dumpr    r   r   r   r"   k   s   zSystemMessage.to_openair#   c                 C   s
   |  |S )r9   )r<   r%   r   r   r   r'   o   s   
zSystemMessage.from_openaiN)r   r   r   r   r   r   r   r   r(   r*   r+   r   r   r)   r   r"   r,   r'   r   r   r   r   r=   ^   s   
 	2<r=   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?")
    r   Nr.   
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 )r/   r   Nr.   c                 S   r1   r   r2   r3   r   r   r   r6      r7   z.AssistantMessage.to_openai.<locals>.<listcomp>c                 S   r1   r   r2   )r4   	tool_callr   r   r   r6      r7   r@   )r   r.   r8   r*   r@   )r!   out_dictr   r   r   r"      s   

zAssistantMessage.to_openair#   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 )
r9   r@   Nztool_calls must be a list, got r.   c                 S   r:   r   r;   r3   r   r   r   r6      r7   z0AssistantMessage.from_openai.<locals>.<listcomp>zUnknown content type: r   )r   r.   r@   )
getr8   r+   appendr   r'   
ValueErrorr   r*   r<   )r&   r#   openai_tool_callstools_callsopenai_tool_callopenai_contentr.   r   r   r   r'      s,   

zAssistantMessage.from_openai)r   r   r   r   r   r   r   r   r(   r.   r*   r+   r   r   r@   r   rA   boolr)   r   r"   r,   r'   r   r   r   r   r?   u   s   
 2<r?   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   r   r   rM   floatr(   r   r   r   r   rL      s   
 	rL   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")
    r.   r   Ntool_call_idnamer   c                 C   s    | j dus	J d| jdhdS )r/   N0tool_call_id must be provided for tool messages.rQ   )exclude)rP   r>   r    r   r   r   r"      s   zToolMessage.to_openaimessagesc                 C   s    |  |}|jdusJ d|S )r9   NrR   )r<   rP   )r&   rT   tool_messager   r   r   r'      s   
zToolMessage.from_openai)r   r   r   r   r*   r+   r   r(   r   r   r   r   rP   rQ   r)   r   r"   r,   r'   r   r   r   r   rO      s   
 2<rO   r   )discriminatorChatMessageType)boundUserMessageTypeAssistantMessageTypeToolMessageTypeSystemMessageTypeUATSN)&enumr   typingr   r   r   pydanticr   typing_extensionsr   r   mistral_common.baser	   &mistral_common.protocol.instruct.chunkr
   r   r   r   r   +mistral_common.protocol.instruct.tool_callsr   r*   r   r   r-   r=   r?   rL   rO   ChatMessageFinetuningMessagerW   rY   rZ   r[   r\   r]   r(   r   r   r   r   <module>   s4     @ 