
    :i4                     "   S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
JrJrJr  S SKJr  S SKJrJr  S S	KJr  S S
KJr   " S S\\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\\5      r " S S\5      r  " S S\5      r! " S S \5      r" " S! S"\5      r#\\"\-  \-  \!-  \ -  \#-  \" S#S$94   r$\\"\-  \-  \!-  \ -  \" S#S$94   r%S%\&\\\&\\4   -  4   S&\$4S' jr'g)(    N)Enum)Path)Literal)urlparse)
ConfigDictFieldValidationErrorfield_validator)	Annotated)EXPECTED_FORMAT_VALUESAudio)MistralBase)SerializableImagec                   0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)
ChunkTypes   aZ  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thinking N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   __static_attributes__r       `/home/james-whalen/.local/lib/python3.13/site-packages/mistral_common/protocol/instruct/chunk.pyr   r      s'     DEIKIHr    r   c                      \ rS rSr% Sr\\R                  \R                  \R                  \R                  \R                  \R                  4   \S'   S\\\\\\4   -  4   4S jr\S\\\\\\4   -  4   SS 4S j5       rSrg	)
BaseContentChunk&   zBase 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                 D    [        S[        U 5      R                   35      e)zOConverts the chunk to the OpenAI format.

Should be implemented by subclasses.
z%to_openai method not implemented for )NotImplementedErrorr%   r   selfs    r!   	to_openaiBaseContentChunk.to_openai8   s"    
 "$I$t*J]J]I^"_``r    openai_chunkc                 2    [        SU R                   35      e)zWConverts 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_openaiBaseContentChunk.from_openai?   s     "$KCLL>"Z[[r    r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   __annotations__dictstrr+   classmethodr1   r   r   r    r!   r#   r#   &   s     	 a4S4S>%9 9: a \tCtCH~1E,E'F \K] \ \r    r#   c                       \ rS rSr% Sr\R                  r\\R                     \	S'   \
\	S'   \" SS9rS\\\\\\4   -  4   4S jr\S	\\\\\\4   -  4   SS 4S
 j5       rSrg)
ImageChunkH   zImage 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                 <    U R                  S1SS0S9S   nSSU0S.$ )(Converts the chunk to the OpenAI format.r   add_format_prefixT)includecontextr   urlr%   r   
model_dump)r*   base64_images     r!   r+   ImageChunk.to_openaiW   s3    yCVX\B]^_fg#5,2GHHr    r-   c                 &   UR                  S5      S:X  d   U5       eUS   n[        U[        5      (       a  SU;   d   U5       e[        R                  " SUS   5      (       a  US   R                  S5      S   US'   U R                  SUS   05      $ )0Converts the OpenAI chunk to the Mistral format.r%   r   rA   z^data:image/\w+;base64,,   r   )get
isinstancer4   rematchsplitmodel_validate)r0   r-   image_url_dicts      r!   r1   ImageChunk.from_openai\   s     ';6DD6%k2.$//E^4K[^[K88.u0EFF$25$9$?$?$DQ$GN5!!!7N5,A"BCCr    r   N)r   r   r   r   r   r   r   r%   r   r3   r   r   model_configr4   r5   r+   r6   r1   r   r   r    r!   r8   r8   H   s     '1&6&6D'*""
#6d;LI4S4S>%9 9: I
 
DtCtCH~1E,E'F 
D< 
D 
Dr    r8   c                   8    \ rS rSr% Sr\\S'   Sr\S-  \S'   Srg)ImageURLj   zImage 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")
rA   Ndetailr   )	r   r   r   r   r   r5   r3   rW   r   r   r    r!   rU   rU   j   s     
HFC$Jr    rU   c                       \ rS rSr% Sr\R                  r\\R                     \	S'   \
\-  \	S'   \" SS9rS\4S jrS\\\\\\4   -  4   4S	 jr\S
\\\\\\4   -  4   SS 4S j5       rSrg)ImageURLChunky   zImage 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   Tr:   r&   c                     [        U R                  [        5      (       a  U R                  R                  $ U R                  $ N)rL   r   rU   rA   r)   s    r!   get_urlImageURLChunk.get_url   s-    dnnh//>>%%%~~r    c                     SU R                  5       0n[        U R                  [        5      (       a0  U R                  R                  b  U R                  R                  US'   SUS.nU$ )r=   rA   rW   r   rB   )r]   rL   r   rU   rW   )r*   rQ   out_dicts      r!   r+   ImageURLChunk.to_openai   s\    0dnnh//DNN4I4I4U'+~~'<'<N8$  '5
 r    r-   c                 .    U R                  SUS   05      $ )rH   r   rP   r/   s     r!   r1   ImageURLChunk.from_openai   s     !!;[0I"JKKr    r   N)r   r   r   r   r   r   r   r%   r   r3   rU   r5   r   rS   r]   r4   r+   r6   r1   r   r   r    r!   rY   rY   y   s     +5*>*>D'*&&
'>#~d;L 

4S4S>%9 9: 
 LtCtCH~1E,E'F L? L Lr    rY   c                   x    \ rS rSr% Sr\\-  \S'   \\S'   \S\	SS 4S j5       r
\" S5      S\S\4S	 j5       rS
rg)RawAudio   a/  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                 J    UR                   nUR                  US5      nU " X2S9$ )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rh   ri   )ri   	to_base64)r0   rj   ri   rh   s       r!   
from_audioRawAudio.from_audio   s(     vu-,,r    vc                 F    U[         ;  a  [        S[          SU S35      eU$ )Nz`format` should be one of z. Got: `)r   r	   r0   rp   s     r!   should_not_be_emptyRawAudio.should_not_be_empty   s1    **!$>?U>VV]^_]``a"bccr    r   N)r   r   r   r   r   r5   bytesr3   r6   r   rn   r
   rt   r   r   r    r!   rf   rf      s_    
 +K-u - - - XC C  r    rf   c                   $    \ rS rSr% Sr\\S'   Srg)AudioURL   z<Audio URL.

Attributes:
    url: The URL of the audio file.
rA   r   N)r   r   r   r   r   r5   r3   r   r   r    r!   rx   rx      s     
Hr    rx   c                   (    \ rS rSrSrSrSrSrSrSr	g)	AudioURLType   zEnum 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`).
rA   base64filefile_urir   N)
r   r   r   r   r   rA   r}   r~   r   r   r   r    r!   r{   r{      s     CFDHr    r{   c                       \ rS rSr% Sr\R                  r\\R                     \	S'   \
\-  \	S'   \S\
4S j5       rS\4S jrS\\
\
\\
\
4   -  4   4S jr\S	\\
\
\\
\
4   -  4   SS 4S
 j5       rSrg)AudioURLChunk   zAudio 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                     [        U R                  [        5      (       a  U R                  R                  $ U R                  $ r\   )rL   r   rx   rA   r)   s    r!   rA   AudioURLChunk.url   s-    dnnh//>>%%%~~r    c                    [        U R                  5      R                  nUS;   a  [        R                  $ US:X  a  [        R                  $ US:X  a  [        R
                  $  [        U R                  5      nUR                  5       nU(       a  [        R                  $ [        R                  $ ! [         a    Sn N5f = f)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httpsrh   r~   F)
r   rA   schemer{   r}   r   r   existsOSErrorr~   )r*   
url_schemeurl_path
exist_paths       r!   get_url_typeAudioURLChunk.get_url_type   s     dhh'..
**###6!&&&6!(((	DHH~H!*J $$$"""  	J	s   #%B/ /B>=B>c                     [        U R                  [        5      (       a  U R                  5       $ U R                  SU R                  0S.$ )r=   rA   )r%   r   )rL   r   rx   rD   r%   r)   s    r!   r+   AudioURLChunk.to_openai  s8    dnnh//??$$ IIUDNN4KLLr    r-   c                 $    U R                  U5      $ rH   rc   r/   s     r!   r1   AudioURLChunk.from_openai       !!,//r    r   N)r   r   r   r   r   r   r   r%   r   r3   r5   rx   propertyrA   r{   r   r4   r+   r6   r1   r   r   r    r!   r   r      s     +5*>*>D'*&&
'>X~S  
#l #@M4S4S>%9 9: M 0tCtCH~1E,E'F 0? 0 0r    r   c                      \ rS rSr% Sr\R                  r\\R                     \	S'   \
\	S'   \" S5      S\
S\
4S j5       r\S\SS 4S	 j5       rS\\\\\\4   -  4   4S
 jr\S\\\\\\4   -  4   SS 4S j5       rSrg)
AudioChunki  aw  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   rp   r&   c                 b    UR                   R                  5       (       d  [        SU S35      eU$ )Nz'`InputAudio` should not be empty. Got: rr   )rh   stripr	   rs   s     r!   rt   AudioChunk.should_not_be_empty.  s+    vv||~~!$KA3a"PQQr    rj   c                 4    U " [         R                  U5      S9$ )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   )rf   rn   )r0   rj   s     r!   rn   AudioChunk.from_audio5  s     x2259::r    c                 :   [        U R                  R                  [        5      (       a%  U R                  R                  R	                  S5      OU R                  R                  nU R
                  [        XR                  R                  S9R                  5       S.$ )zwConverts the chunk to the OpenAI format.

Returns:
    A dictionary representing the audio chunk in the OpenAI format.
zutf-8rl   )r%   r   )	rL   r   rh   rv   decoder%   rf   ri   rD   )r*   contents     r!   r+   AudioChunk.to_openaiA  s|     6@@P@P@U@UW\5]5]D!!((1cgcscscxcx 	 II#9I9I9P9PQ\\^
 	
r    r-   c                 $    U R                  U5      $ )zConverts 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.
rc   r/   s     r!   r1   AudioChunk.from_openaiO  s     !!,//r    r   N)r   r   r   r   r   r   r   r%   r   r3   rf   r
   rt   r6   r   rn   r4   r5   r+   r1   r   r   r    r!   r   r     s    
 -7,B,BD'*((
)B]#H   $ 	;u 	; 	; 	;
4S4S>%9 9: 
 	0tCtCH~1E,E'F 	0< 	0 	0r    r   c                       \ rS rSr% Sr\R                  r\\R                     \	S'   \
\	S'   S\\
\
\\
\
4   -  4   4S jr\S\\
\
\\
\
4   -  4   SS 4S j5       rS	rg
)	TextChunki\  zText 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                 "    U R                  5       $ r=   rC   r)   s    r!   r+   TextChunk.to_openaii        r    r-   c                 $    U R                  U5      $ r   rc   r/   s     r!   r1   TextChunk.from_openaim  r   r    r   N)r   r   r   r   r   r   r   r%   r   r3   r5   r4   r+   r6   r1   r   r   r    r!   r   r   \  s     &0__D'*//
"4
I!4S4S>%9 9: ! 0tCtCH~1E,E'F 0; 0 0r    r   c                       \ rS rSr% Sr\R                  r\\R                     \	S'   \
\	S'   \" SSS9r\\	S'   S	\\
\
\\
\
4   -  4   4S
 jr\S\\
\
\\
\
4   -  4   S	S 4S j5       rSrg)
ThinkChunkis  zThinking 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                 "    U R                  5       $ r   rC   r)   s    r!   r+   ThinkChunk.to_openai  r   r    r-   c                 $    U R                  U5      $ r   rc   r/   s     r!   r1   ThinkChunk.from_openai  r   r    r   N)r   r   r   r   r   r   r   r%   r   r3   r5   r   r   boolr4   r+   r6   r1   r   r   r    r!   r   r   s  s     *4)<)<D'*%%
&<M3abFDb!4S4S>%9 9: ! 0tCtCH~1E,E'F 0< 0 0r    r   r%   )discriminatoropenai_content_chunksr&   c                 ^   U R                  S5      nUc  [        S5      e[        U5      nU[        R                  :X  a  [        R                  U 5      $ U[        R                  :X  a  [        R                  U 5      $ U[        R                  :X  a  [        R                  U 5      $ U[        R                  :X  a  [        R                  U 5      $ U[        R                  :X  a  [        R                  U 5      $ U[        R                  :X  a  [        R                  U 5      $ [        SU 35      e)Nr%   z%Content chunk must have a type field.zUnknown content chunk type: )rK   
ValueErrorr   r   r   r1   r   rY   r   r8   r   r   r   r   r   r   )r   content_type_strcontent_types      r!   _convert_openai_content_chunksr     s   ,008@AA./Lz&$$%:;;	--	-(()>??	))	)%%&;<<	//	/%%&;<<	--	-(()>??	,,	,%%&;<<7~FGGr    )(rM   enumr   pathlibr   typingr   urllib.parser   pydanticr   r   r	   r
   typing_extensionsr   mistral_common.audior   r   mistral_common.baser   mistral_common.imager   r5   r   r#   r8   rU   rY   rf   rx   r{   r   r   r   r   ContentChunkUserContentChunkr4   r   r   r    r!   <module>r      s^   	    ! H H ' > + 2d .\{ \DD! DD{ #L$ #LL${ $N{ 3  ;0$ ;0|;0! ;0|0  0.0! 0. 
]*Z7-G*TV[jpVqq 
]*Z7-G]cIdd 
H$sC$sTWx.DX?X:Y H^j Hr    