
    :i:                         S SK Jr  S SKJr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Jr  S SKJrJrJr   " S	 S
\\5      r " S S\5      r " S S\
\\   5      r " S S\\\\4   5      rg)    )Enum)AnyGeneric)Field)MistralBase)BaseCompletionRequest)_check_openai_fields_names_is_openai_field_nameconvert_openai_messagesconvert_openai_tools)ChatMessageChatMessageType)Tool
ToolChoiceToolTypec                        \ rS rSrSrSrSrSrg)ResponseFormats   zEnum of the different formats of an instruct response.

Attributes:
    text: The response is a plain text.
    json: The response is a JSON object.

Examples:
    >>> response_format = ResponseFormats.text
textjson_object N)__name__
__module____qualname____firstlineno____doc__r   json__static_attributes__r       b/home/james-whalen/.local/lib/python3.13/site-packages/mistral_common/protocol/instruct/request.pyr   r      s     DDr   r   c                   <    \ rS rSr% Sr\R                  r\\S'   Sr	g)ResponseFormat$   zThe format of the response.

Attributes:
    type: The type of the response.

Examples:
    >>> response_format = ResponseFormat(type=ResponseFormats.text)
typer   N)
r   r   r   r   r   r   r   r$   __annotations__r   r   r   r    r"   r"   $   s     ,00D/0r   r"   c                   T   \ rS rSr% SrSr\S-  \S'   \\	   \S'   \
" \S9r\\S'   Sr\\   S-  \S'   \R                   r\\S	'   S
r\\S'   S
r\\S'   S\S\\\\\\4      4   4S jr\  SS\\\\\\\\\\\4   -  4      -  4      S\\\\4      S-  S\S\SS 4
S jj5       rSrg)ChatCompletionRequest1   ao  Request for a chat completion.

Attributes:
    model: The model to use for the chat completion.
    messages: The messages to use for the chat completion.
    response_format: The format of the response.
    tools: The tools to use for the chat completion.
    tool_choice: The tool choice to use for the chat completion.
    truncate_for_context_length: Whether to truncate the messages for the context length.
    continue_final_message: Whether to continue the final message.

Examples:
    >>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
    >>> from mistral_common.protocol.instruct.tool_calls import ToolTypes, Function
    >>> request = ChatCompletionRequest(
    ...     messages=[
    ...         UserMessage(content="Hello!"),
    ...         AssistantMessage(content="Hi! How can I help you?"),
    ...     ],
    ...     response_format=ResponseFormat(type=ResponseFormats.text),
    ...     tools=[Tool(type=ToolTypes.function, function=Function(name="get_weather", parameters={}))],
    ...     tool_choice=ToolChoice.auto,
    ...     truncate_for_context_length=True,
    ... )
Nmodelmessages)default_factoryresponse_formattoolstool_choiceFtruncate_for_context_lengthcontinue_final_messagekwargsreturnc                 `   U R                  1 SkSS9nUR                  SS5      nUb  X2S'   U R                  (       a  [        S5      eU HX  nXB;   a  [	        SU 35      eU[
        R                  ;   a  [	        S	U S
35      e[        U5      (       a  MK  [	        SU S35      e   / nU R                   H"  nUR                  UR                  5       5        M$     XRS'   U R                  b-  U R                   Vs/ s H  owR                  5       PM     snUS'   UR                  U5        U$ s  snf )a  Convert the request messages and tools into the OpenAI format.

Args:
    kwargs: Additional parameters to be added to the request.

Returns:
    The request in the OpenAI format.

Examples:
    >>> from mistral_common.protocol.instruct.messages import UserMessage
    >>> from mistral_common.protocol.instruct.tool_calls import Tool, Function
    >>> request = ChatCompletionRequest(messages=[UserMessage(content="Hello, how are you?")], temperature=0.15)
    >>> request.to_openai(stream=True)
    {'temperature': 0.15, 'top_p': 1.0, 'response_format': {'type': 'text'}, 'tool_choice': 'auto', 'continue_final_message': False, 'messages': [{'role': 'user', 'content': 'Hello, how are you?'}], 'stream': True}
    >>> request = ChatCompletionRequest(messages=[UserMessage(content="Hello, how are you?")], tools=[
    ...     Tool(function=Function(
    ...         name="get_current_weather",
    ...         description="Get the current weather in a given location",
    ...         parameters={
    ...             "type": "object",
    ...             "properties": {
    ...                 "location": {
    ...                     "type": "string",
    ...                     "description": "The city and state, e.g. San Francisco, CA",
    ...                 },
    ...                 "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
    ...             },
    ...             "required": ["location"],
    ...         },
    ...     ),
    ... )])
    >>> request.to_openai()
    {'temperature': 0.7, 'top_p': 1.0, 'response_format': {'type': 'text'}, 'tool_choice': 'auto', 'continue_final_message': False, 'messages': [{'role': 'user', 'content': 'Hello, how are you?'}], 'tools': [{'type': 'function', 'function': {'name': 'get_current_weather', 'description': 'Get the current weather in a given location', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'}, 'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}}, 'required': ['location']}}}]}
>   r-   r*   r/   Texcludeexclude_nonerandom_seedNseedzETruncating for context length is not implemented for OpenAI requests.Duplicate keyword argument: Keyword argument  is already set in the request.Invalid keyword argument: $, it should be an OpenAI field name.r*   r-   )
model_dumppopr/   NotImplementedError
ValueErrorr'   model_fieldsr
   r*   append	to_openair-   update)selfr1   openai_requestr8   kwargopenai_messagesmessagetools           r    rD   ChatCompletionRequest.to_openaiT   s<   J *.HW[ *9 *

 !!-6%)6"++%&mnnE& #?w!GHH /<<< #4UG;Z![\\*511 #=eWDh!ijj  }}G""7#4#4#67 % &5z"::!DHJJ&OJD~~'7J&ON7#f%	 'Ps   9D+c                 ^   SU;   a  SU;   a  [        S5      eUR                  SS5      =(       d    UR                  SS5      n[        [        U R                  R                  5       5      [        UR                  5       5      5        [        U5      nUb  [        U5      OSnU " SUUUUS.UD6$ )a  Create a chat completion request from the OpenAI format.

Args:
    messages: The messages in the OpenAI format.
    tools: The tools in the OpenAI format.
    continue_final_message: Whether to continue the final message.
    **kwargs: Additional keyword arguments to pass to the constructor. These should be the same as the fields
        of the request class or the OpenAI API equivalent.


Returns:
    The chat completion request.
r8   r7   z-Cannot specify both `seed` and `random_seed`.N)r*   r-   r7   r0   r   )rA   r?   r	   setrB   keysr   r   )clsr*   r-   r0   r1   r7   converted_messagesconverted_toolss           r    from_openai!ChatCompletionRequest.from_openai   s    * V 7LMMjj.Q&**]D2Q"3s'7'7'<'<'>#?V[[]AST0G0Q9>9J.u5PT 
'!##9	

 
 	
r   r   NF)r   r   r   r   r   r)   strr%   listr   r   r"   r,   r-   r   r   autor.   r/   boolr0   r   dictrD   classmethodrS   r   r   r   r    r'   r'   1   s$   4 E3:?##&+N&KO^K#E4:#(ooK-(--#(D(G# G$sDc3h4H/H*I GR  .2',	%
tCtDcDcN6J1J,K'L!LLMN%
 DcN#d*%
 !%	%

 %
 
!%
 %
r   r'   c                   "   \ rS rSr% Sr\\   \S'   Sr\	S-  \S'   Sr
\\   S-  \S'   Sr\S-  \S'   Sr\\S	'   S
\S\\	\\\	\4      4   4S jr\  SS\\\	\	\\\	\	\\	\4   -  4      -  4      S\\\	\4      S-  S	\S
\SS 4
S jj5       rSrg)InstructRequest   a  A valid Instruct request to be tokenized.

Attributes:
    messages: The history of the conversation.
    system_prompt: The system prompt to be used for the conversation.
    available_tools: The tools available to the assistant.
    truncate_at_max_tokens: The maximum number of tokens to truncate the conversation at.
    continue_final_message: Whether to continue the final message.

Examples:
    >>> from mistral_common.protocol.instruct.messages import UserMessage, SystemMessage
    >>> request = InstructRequest(
    ...     messages=[UserMessage(content="Hello, how are you?")], system_prompt="You are a helpful assistant."
    ... )
r*   Nsystem_promptavailable_toolstruncate_at_max_tokensFr0   r1   r2   c                 |   U R                  1 SkSS9nU HX  nX2;   a  [        SU 35      eU[        R                  ;   a  [        SU S35      e[	        U5      (       a  MK  [        SU S35      e   / nU R
                  b  UR                  S	U R
                  S
.5        U R                   H"  nUR                  UR                  5       5        M$     XBS'   U R                  b-  U R                   Vs/ s H  ofR                  5       PM     snUS'   U R                  b  [        S5      eUR                  U5        U$ s  snf )a  Convert the request messages and tools into the OpenAI format.

Args:
    kwargs: Additional parameters to be added to the request.

Returns:
    The request in the OpenAI format.

Examples:
    >>> from mistral_common.protocol.instruct.messages import UserMessage
    >>> from mistral_common.protocol.instruct.tool_calls import Tool, Function
    >>> request = InstructRequest(messages=[UserMessage(content="Hello, how are you?")])
    >>> request.to_openai(temperature=0.15, stream=True)
    {'continue_final_message': False, 'messages': [{'role': 'user', 'content': 'Hello, how are you?'}], 'temperature': 0.15, 'stream': True}
    >>> request = InstructRequest(
    ...     messages=[UserMessage(content="Hello, how are you?")],
    ...     available_tools=[
    ...     Tool(function=Function(
    ...         name="get_current_weather",
    ...         description="Get the current weather in a given location",
    ...         parameters={
    ...             "type": "object",
    ...             "properties": {
    ...                 "location": {
    ...                     "type": "string",
    ...                     "description": "The city and state, e.g. San Francisco, CA",
    ...                 },
    ...                 "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
    ...             },
    ...             "required": ["location"],
    ...         },
    ...     ),
    ... )])
    >>> request.to_openai()
    {'continue_final_message': False, 'messages': [{'role': 'user', 'content': 'Hello, how are you?'}], 'tools': [{'type': 'function', 'function': {'name': 'get_current_weather', 'description': 'Get the current weather in a given location', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'}, 'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}}, 'required': ['location']}}}]}
>   r*   r`   ra   Tr4   r9   r:   r;   r<   r=   system)rolecontentr*   r-   z@Truncating at max tokens is not implemented for OpenAI requests.)r>   rA   r]   rB   r
   r_   rC   r*   rD   r`   ra   r@   rE   )rF   r1   rG   rH   rI   rJ   rK   s          r    rD   InstructRequest.to_openai   sI   N *.M\` *9 *
 E& #?w!GHH /666 #4UG;Z![\\*511 #=eWDh!ijj  13)""HASAS#TU}}G""7#4#4#67 % &5z"+DHDXDX&YDXD~~'7DX&YN7#&&2%&hiif% 'Zs   /D9r-   c                 $   SU;   a   Uc  UR                  S5      nO[        S5      e[        [        U R                  R                  5       5      [        UR                  5       5      5        [        U5      nUb  [        U5      OSnU " SUUUS.UD6$ )a  Create an instruct request from the OpenAI format.

Args:
    messages: The messages in the OpenAI format.
    tools: The tools in the OpenAI format.
    continue_final_message: Whether to continue the final message.
    **kwargs: Additional keyword arguments to pass to the constructor. These should be the same as the fields
        of the request class or the OpenAI API equivalent.

Returns:
    The instruct request.
r`   Nz2Cannot specify both `tools` and `available_tools`.)r*   r`   r0   r   )r?   rA   r	   rN   rB   rO   r   r   )rP   r*   r-   r0   r1   rQ   rR   s          r    rS   InstructRequest.from_openai'  s    , &}

#45 !UVV"3s'7'7'<'<'>#?V[[]AST0G0Q9>9J.u5PT 
'+#9
 	
 	
r   r   rU   )r   r   r   r   r   rW   r   r%   r_   rV   r`   r   ra   intr0   rY   r   rZ   rD   r[   rS   r   r   r   r    r]   r]      s     ?## $M3:$-1OT(^d*1)-C$J-#(D(H# H$sDc3h4H/H*I HT  .2',	&
tCtDcDcN6J1J,K'L!LLMN&
 DcN#d*&
 !%	&

 &
 
&
 &
r   r]   N)enumr   typingr   r   pydanticr   mistral_common.baser   mistral_common.protocol.baser   +mistral_common.protocol.instruct.convertersr	   r
   r   r   )mistral_common.protocol.instruct.messagesr   r   +mistral_common.protocol.instruct.tool_callsr   r   r   rV   r   r"   r'   r]   r   r   r    <module>rr      sw       + >  S Rc4 
1[ 
1R
17?3K R
jH
k7?H+D#E H
r   