
    *`i4                        d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
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  G d dee          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d dee          Z G d de          Z  G d de          Z! G d d e          Z" G d! d"e          Z#ee"ez  ez  e!z  e z  e#z   ed#$          f         Z$ee"ez  ez  e!z  e z   ed#$          f         Z%d%e&eee&eef         z  f         d&e$fd'Z'dS )(    N)Enum)Path)Literal)urlparse)
ConfigDictFieldValidationErrorfield_validator)	Annotated)EXPECTED_FORMAT_VALUESAudio)MistralBase)SerializableImagec                   *    e Zd ZdZdZdZdZdZdZdZ	dS )	
ChunkTypesa  Enum for the types of chunks that can be sent to the model.

    Attributes:
       text: A text chunk.
       image: An image chunk.
       image_url: An image url chunk.
       input_audio: An input audio chunk.
       audio_url: An audio url chunk.

    Examples:
        >>> from mistral_common.protocol.instruct.chunk import ChunkTypes
        >>> chunk_type = ChunkTypes.text
    textimage	image_urlinput_audio	audio_urlthinkingN)
__name__
__module____qualname____doc__r   r   r   r   r   r        z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mistral_common/protocol/instruct/chunk.pyr   r      s8          DEIKIHHHr   r   c                       e Zd ZU dZeej        ej        ej        ej	        ej
        ej        f         ed<   deeeeeef         z  f         fdZedeeeeeef         z  f         dd fd            ZdS )BaseContentChunkzBase class for all content chunks.

    Content chunks are used to send different types of content to the model.

    Attributes:
       type: The type of the chunk.
    typereturnc                 J    t          dt          |           j                   )z_Converts the chunk to the OpenAI format.

        Should be implemented by subclasses.
        z%to_openai method not implemented for )NotImplementedErrorr!   r   selfs    r   	to_openaizBaseContentChunk.to_openai8   s$    
 ""_$t**J]"_"_```r   openai_chunkc                 0    t          d| j                   )zgConverts the OpenAI chunk to the Mistral format.

        Should be implemented by subclasses.
        z'from_openai method not implemented for )r$   r   clsr(   s     r   from_openaizBaseContentChunk.from_openai?   s     ""ZCL"Z"Z[[[r   N)r   r   r   r   r   r   r   r   r   r   r   r   __annotations__dictstrr'   classmethodr,   r   r   r   r    r    &   s           	   a4S4S>%9 9: a a a a \tCtCH~1E,E'F \K] \ \ \ [\ \ \r   r    c                       e Zd ZU dZej        Zeej                 ed<   e	ed<    e
d          Zdeeeeeef         z  f         fdZedeeeeeef         z  f         dd fd	            Zd
S )
ImageChunkzImage chunk.

    Attributes:
       image: The image to be sent to the model.

    Examples:
        >>> from PIL import Image
        >>> image_chunk = ImageChunk(image=Image.new('RGB', (200, 200), color='blue'))
    r!   r   Tarbitrary_types_allowedr"   c                 P    |                      dhddi          d         }dd|idS )(Converts the chunk to the OpenAI format.r   add_format_prefixT)includecontextr   urlr!   r   
model_dump)r&   base64_images     r   r'   zImageChunk.to_openaiW   s9    yCVX\B]^^_fg#5,2GHHHr   r(   c                 V   |                     d          dk    s
J |            |d         }t          |t                    rd|v s
J |            t          j        d|d                   r$|d                             d          d         |d<   |                     d|d         i          S )0Converts the OpenAI chunk to the Mistral format.r!   r   r:   z^data:image/\w+;base64,,   r   )get
isinstancer.   rematchsplitmodel_validate)r+   r(   image_url_dicts      r   r,   zImageChunk.from_openai\   s     '';666666%k2.$//[E^4K4K4K^4K4KK8.u0EFF 	H$25$9$?$?$D$DQ$GN5!!!7N5,A"BCCCr   N)r   r   r   r   r   r   r!   r   r-   r   r   model_configr.   r/   r'   r0   r,   r   r   r   r2   r2   H   s           '1&6D'*"
#666:d;;;LI4S4S>%9 9: I I I I
 
DtCtCH~1E,E'F 
D< 
D 
D 
D [
D 
D 
Dr   r2   c                   2    e Zd ZU dZeed<   dZedz  ed<   dS )ImageURLzImage URL or a base64 encoded image.

    Attributes:
       url: The URL of the image.
       detail: The detail of the image.

    Examples:
       >>> image_url = ImageURL(url="https://example.com/image.png")
    r:   Ndetail)r   r   r   r   r/   r-   rM   r   r   r   rL   rL   j   s;           
HHHFC$Jr   rL   c                       e Zd ZU dZej        Zeej                 ed<   e	e
z  ed<    ed          Zde
fdZdee
e
ee
e
f         z  f         fdZed	ee
e
ee
e
f         z  f         dd fd
            ZdS )ImageURLChunkzImage URL chunk.

    Attributes:
       image_url: The URL of the image or a base64 encoded image to be sent to the model.

    Examples:
        >>> image_url_chunk = ImageURLChunk(image_url="data:image/png;base64,iVBORw0")
    r!   r   Tr3   r"   c                 \    t          | j        t                    r| j        j        S | j        S N)rD   r   rL   r:   r%   s    r   get_urlzImageURLChunk.get_url   s)    dnh// 	&>%%~r   c                     d|                                  i}t          | j        t                    r| j        j        | j        j        |d<   d|d}|S )r6   r:   NrM   r   r;   )rR   rD   r   rL   rM   )r&   rI   out_dicts      r   r'   zImageURLChunk.to_openai   s\    0dnh// 	=DN4I4U'+~'<N8$  '5
 5
 r   r(   c                 <    |                      d|d         i          S )r@   r   rH   r*   s     r   r,   zImageURLChunk.from_openai   s"     !!;[0I"JKKKr   N)r   r   r   r   r   r   r!   r   r-   rL   r/   r   rJ   rR   r.   r'   r0   r,   r   r   r   rO   rO   y   s           +5*>D'*&
'>>>#~:d;;;L    

4S4S>%9 9: 
 
 
 
 LtCtCH~1E,E'F L? L L L [L L Lr   rO   c                       e Zd ZU dZeez  ed<   eed<   ededd fd            Z	 e
d          dedefd            Zd	S )
RawAudioaK  Base64 encoded audio data.

    This class represents raw audio data encoded in base64 format.

    Attributes:
        data: The base64 encoded audio data, which can be a string or bytes.
        format: The format of the audio data.

    Examples:
        >>> audio = RawAudio(data="base64_encoded_audio_data", format="mp3")
    dataformataudior"   c                 V    |j         }|                    |d          } | ||          S )zCreates a RawAudio instance from an Audio object.

        Args:
            audio: An Audio object containing audio data, format, and duration.

        Returns:
            An AudioChunk instance initialized with the audio data.
        FrY   rZ   )rZ   	to_base64)r+   r[   rZ   rY   s       r   
from_audiozRawAudio.from_audio   s3     vu--sV,,,,r   vc                 N    |t           vrt          dt            d| d          |S )Nz`format` should be one of z. Got: `)r   r	   r+   r`   s     r   should_not_be_emptyzRawAudio.should_not_be_empty   s7    ***!"b?U"b"b^_"b"b"bcccr   N)r   r   r   r   r/   bytesr-   r0   r   r_   r
   rd   r   r   r   rX   rX      s         
 
 +KKK-u - - - - [- _XC C      r   rX   c                       e Zd ZU dZeed<   dS )AudioURLzHAudio URL.

    Attributes:
        url: The URL of the audio file.
    r:   N)r   r   r   r   r/   r-   r   r   r   rg   rg      s%           
HHHHHr   rg   c                   "    e Zd ZdZdZdZdZdZdS )AudioURLTypezEnum for the types of audio URLs.

    Attributes:
        url: A URL.
        base64: A base64 encoded audio. Can be prefixed with `data:audio/<format>;base64,`.
        file: A file path.
        file_uri: A file URI (eg. `file:///path/to/file`).
    r:   base64filefile_uriN)r   r   r   r   r:   rj   rk   rl   r   r   r   ri   ri      s.          CFDHHHr   ri   c                       e Zd ZU dZej        Zeej                 ed<   e	e
z  ed<   ede	fd            ZdefdZdee	e	ee	e	f         z  f         fdZedee	e	ee	e	f         z  f         dd fd	            Zd
S )AudioURLChunkzAudio URL chunk.

    Attributes:
        type: The type of the chunk, which is always `ChunkTypes.audio_url`.
        audio_url: The URL of the audio file.
    r!   r   r"   c                 \    t          | j        t                    r| j        j        S | j        S rQ   )rD   r   rg   r:   r%   s    r   r:   zAudioURLChunk.url   s)    dnh// 	&>%%~r   c                 H   t          | j                  j        }|dv rt          j        S |dk    rt          j        S |dk    rt          j        S 	 t          | j                  }|                                }n# t          $ r d}Y nw xY w|rt          j	        S t          j        S )a  Returns the type of the audio URL.

        Note:
            URLs should be either:
            - a valid URL (http:// or https://)
            - a valid file path (e.g. /path/to/file)
            - a valid file URI (e.g. file:///path/to/file)
            - a base64 encoded audio. It is assumed to be base64 encoded if it is not a valid URL or file path.

        Returns:
            The type of the audio URL.
        >   httphttpsrY   rk   F)
r   r:   schemeri   rj   rl   r   existsOSErrorrk   )r&   
url_schemeurl_path
exist_paths       r   get_url_typezAudioURLChunk.get_url_type   s     dh''.
***##6!!&&6!!((	DH~~H!**JJ 	 	 	JJJ	  	%$$""s   (A8 8BBc                     t          | j        t                    r|                                 S | j        d| j        idS )r6   r:   )r!   r   )rD   r   rg   r=   r!   r%   s    r   r'   zAudioURLChunk.to_openai  s>    dnh// 	M??$$$ IUDN4KLLLr   r(   c                 ,    |                      |          S r@   rV   r*   s     r   r,   zAudioURLChunk.from_openai       !!,///r   N)r   r   r   r   r   r   r!   r   r-   r/   rg   propertyr:   ri   ry   r.   r'   r0   r,   r   r   r   rn   rn      s
          +5*>D'*&
'>>>X~S    X
#l # # # #@M4S4S>%9 9: M M M M 0tCtCH~1E,E'F 0? 0 0 0 [0 0 0r   rn   c                      e Zd ZU dZej        Zeej                 ed<   e	ed<    e
d          de	de	fd            Zededd fd            Zdeeeeeef         z  f         fd	Zed
eeeeeef         z  f         dd fd            ZdS )
AudioChunka  Audio chunk containing raw audio data.

    This class represents a chunk of audio data that can be used as input.

    Attributes:
        type: The type of the chunk, which is always ChunkTypes.input_audio.
        input_audio: The RawAudio object containing the audio data.

    Examples:
        >>> audio_chunk = AudioChunk(input_audio=RawAudio(data="base64_encoded_audio_data", format="mp3"))
    r!   r   r`   r"   c                 ^    |j                                         st          d| d          |S )Nz'`InputAudio` should not be empty. Got: rb   )rY   stripr	   rc   s     r   rd   zAudioChunk.should_not_be_empty.  s4    v||~~ 	R!"PA"P"P"PQQQr   r[   c                 J     | t                               |                    S )zCreates an AudioChunk instance from an Audio object.

        Args:
            audio: An Audio object containing audio data.

        Returns:
            An AudioChunk instance initialized with the audio data.
        )r   )rX   r_   )r+   r[   s     r   r_   zAudioChunk.from_audio5  s%     sx22599::::r   c                     t          | j        j        t                    r| j        j                            d          n| j        j        }| j        t          || j        j                                                  dS )zConverts the chunk to the OpenAI format.

        Returns:
            A dictionary representing the audio chunk in the OpenAI format.
        zutf-8r]   )r!   r   )	rD   r   rY   re   decoder!   rX   rZ   r=   )r&   contents     r   r'   zAudioChunk.to_openaiA  s{     6@@P@UW\5]5]xD!((111cgcscx 	 I#9I9PQQQ\\^^
 
 	
r   r(   c                 ,    |                      |          S )a  Converts the OpenAI chunk to the Mistral format.

        Args:
            openai_chunk: A dictionary representing the audio chunk in the OpenAI format.

        Returns:
            An AudioChunk instance initialized with the data from the OpenAI chunk.
        rV   r*   s     r   r,   zAudioChunk.from_openaiO  s     !!,///r   N)r   r   r   r   r   r   r!   r   r-   rX   r
   rd   r0   r   r_   r.   r/   r'   r,   r   r   r   r   r     s%        
 
 -7,BD'*(
)BBB_]##H     $# 	;u 	; 	; 	; 	; [	;
4S4S>%9 9: 
 
 
 
 	0tCtCH~1E,E'F 	0< 	0 	0 	0 [	0 	0 	0r   r   c                       e Zd ZU dZej        Zeej                 ed<   e	ed<   de
e	e	e
e	e	f         z  f         fdZede
e	e	e
e	e	f         z  f         dd fd            ZdS )		TextChunkzText chunk.

    Attributes:
      text: The text to be sent to the model.

    Examples:
        >>> text_chunk = TextChunk(text="Hello, how can I help you?")
    r!   r   r"   c                 *    |                                  S r6   r<   r%   s    r   r'   zTextChunk.to_openaii         r   r(   c                 ,    |                      |          S r|   rV   r*   s     r   r,   zTextChunk.from_openaim  r}   r   N)r   r   r   r   r   r   r!   r   r-   r/   r.   r'   r0   r,   r   r   r   r   r   \  s           &0_D'*/
"444
III!4S4S>%9 9: ! ! ! ! 0tCtCH~1E,E'F 0; 0 0 0 [0 0 0r   r   c                       e Zd ZU dZej        Zeej                 ed<   e	ed<    e
dd          Zeed<   dee	e	ee	e	f         z  f         fd	Zed
ee	e	ee	e	f         z  f         dd fd            ZdS )
ThinkChunkzThinking chunk.

    Attributes:
        type: The type of the chunk, which is always ChunkTypes.thinking.
        thinking: The list of text chunks of the thinking.
        closed: Whether the thinking chunk is closed or not.
    r!   r   Tz,Whether the thinking chunk is closed or not.)defaultdescriptionclosedr"   c                 *    |                                  S r   r<   r%   s    r   r'   zThinkChunk.to_openai  r   r   r(   c                 ,    |                      |          S r|   rV   r*   s     r   r,   zThinkChunk.from_openai  r}   r   N)r   r   r   r   r   r   r!   r   r-   r/   r   r   boolr.   r'   r0   r,   r   r   r   r   r   s  s           *4)<D'*%
&<<<MMM53abbbFDbbb!4S4S>%9 9: ! ! ! ! 0tCtCH~1E,E'F 0< 0 0 0 [0 0 0r   r   r!   )discriminatoropenai_content_chunksr"   c                    |                      d          }|t          d          t          |          }|t          j        k    rt                              |           S |t          j        k    rt                              |           S |t          j        k    rt                              |           S |t          j
        k    rt                              |           S |t          j        k    rt                              |           S |t          j        k    rt                              |           S t          d|           )Nr!   z%Content chunk must have a type field.zUnknown content chunk type: )rC   
ValueErrorr   r   r   r,   r   rO   r   r2   r   r   r   rn   r   r   )r   content_type_strcontent_types      r   _convert_openai_content_chunksr     s!   ,0088@AAA.//Lz&&$$%:;;;	-	-	-(()>???	)	)	)%%&;<<<	/	/	/%%&;<<<	-	-	-(()>???	,	,	,%%&;<<<FFFGGGr   )(rE   enumr   pathlibr   typingr   urllib.parser   pydanticr   r   r	   r
   typing_extensionsr   mistral_common.audior   r   mistral_common.baser   mistral_common.imager   r/   r   r    r2   rL   rO   rX   rg   ri   rn   r   r   r   ContentChunkUserContentChunkr.   r   r   r   r   <module>r      s   				                   ! ! ! ! ! ! H H H H H H H H H H H H ' ' ' ' ' ' > > > > > > > > + + + + + + 2 2 2 2 2 2    d   .\ \ \ \ \{ \ \ \DD D D D D! D D DD    {   #L #L #L #L #L$ #L #L #LL$ $ $ $ ${ $ $ $N    {       3    ;0 ;0 ;0 ;0 ;0$ ;0 ;0 ;0|;0 ;0 ;0 ;0 ;0! ;0 ;0 ;0|0 0 0 0 0  0 0 0.0 0 0 0 0! 0 0 0. 
]*Z7-G*TV[V[jpVqVqVqq 
]*Z7-G]cIdIdIdd 
H$sC$sTWx.DX?X:Y H^j H H H H H Hr   