
    *`i=              
          d dl Z d dlmZ d dlmZmZ d dlmZmZ d dl	m
Z
mZmZ  G d de          Z G d d	e          Zd
ee         dedeee         eee         df         f         fdZdee         dedee         fdZdee         dedefdZdee         dedefdZdeee                  dedee         fdZdS )    N)Sequence)_split_integer_list_by_value,_split_tokens_by_one_occurence_control_token)FunctionCallToolCall)SpecialTokenPolicy	TokenizerTokenizerVersionc                       e Zd ZdS )InvalidToolCallErrorN__name__
__module____qualname__     u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mistral_common/experimental/tools.pyr   r              Dr   r   c                       e Zd ZdS )InvalidArgsToolCallErrorNr   r   r   r   r   r      r   r   r   tokenstool_call_token_idreturn.c                     | sg dfS t          | |          }|d         d         |k    }|r|d         }|dd         }ng }|}||fS )a  Split the content and tool calls from a list of tokens.

    The content is the first sequence of tokens that does not start with the tool call token ID.
    The tool calls are the remaining sequences of tokens that start with the tool call token ID.

    Args:
        tokens: The list of tokens.
        tool_call_token_id: The token ID that indicates the start of a tool call.

    Returns:
        A tuple containing the content and tool calls.
    r   r      N)r   )r   r   maybe_content_and_tools_callshas_contentcontent_tokenstools_calls_tokenss         r   _split_content_and_tool_callsr       sv      2v$@I[$\$\!/2159KKK ;6q9:122>:---r   tool_call_tokens	tokenizerc                    |                     | t          j                  }	 t          j        |          }|D ]0}d|vst          |d         t                    st          d          1n'# t          j        $ r}t          d          |d}~ww xY wt          |t                    st          d          d |D             S )a  Decode a list of tool call tokens into a list of tool calls for tokenizer versions v2 to v7.

    Note:
        Expects the tool call tokens to be in the format:

        `[TOOL_CALLS][{"id": "call_id", "name": "name", "arguments": {"arg1": "value1", "arg2": "value2"}}, ...]`
        or

        `[TOOL_CALLS][{"name": "name", "arguments": {"arg1": "value1", "arg2": "value2"}}, ...]`
    special_token_policy	argumentsz:Invalid tool call arguments tokenization. Expected a dict.zCInvalid tool call tokenization. Expected a JSON list of tool calls.Nz>Invalid tool call tokenization. Expected a list of tool calls.c           
          g | ]B}t          |                    d d          t          |d         |d                             CS )idnullnamer&   r*   r&   r(   function)r   getr   ).0	tool_calls     r   
<listcomp>z2_decode_tool_calls_v2_up_to_v7.<locals>.<listcomp>M   sd       
 	 	}}T6**!y'8IkDZ[[[	
 	
 	
  r   )decoder   IGNOREjsonloads
isinstancedictr   JSONDecodeErrorr   list)r!   r"   tool_calls_list_stringtool_calls_decoded_listr0   es         r   _decode_tool_calls_v2_up_to_v7r=   3   s    '--.>UgUn-oo	"&*-C"D"D0 	m 	mI)++:i>TVZ3[3[+./klll ,	m    "Q
 
	
 -t44 e"#cddd 
 1   s   AA+ +B:B

Bc                    t          | |d          \  }}t          ||d          \  }}	 t          |                    |          t          |                    |          t	          j        |                    |t          j                                                }n'# t          j        $ r}t          d          |d}~ww xY w|S )zDecode a list of tool call tokens into a tool call for tokenizer version v11 with call ID.

    Note:
        Expects the tool call tokens to be in the format:

        `[TOOL_CALLS]name[CALL_ID]call_id[ARGS]{"arg1": "value1", "arg2": "value2"}`

    	[CALL_ID][ARGS]r$   r+   r,   &Invalid tokenized tool call arguments.N)
r   r   r2   r   r4   r5   r   r3   r8   r   )r!   r"   r*   call_id_and_argscall_idargsr0   r<   s           r   "_decode_tool_call_v11_with_call_idrE   V   s     JJZ\egrssD
@AQS\^fggMGT	X((!%%d++*Y%5%5dQcQj%5%k%kll  
 
 
		  X X X&'OPPVWWXs   A7B" "C1CCc                 ^   t          | |d          \  }}	 t          t          |                    |t          j                  t          j        |                    |t          j                                                }n'# t          j        $ r}t          d          |d}~ww xY w|S )zDecode a list of tool call tokens into a tool call for tokenizer version v11 without call ID.

    Note:
        Expects the tool call tokens to be in the format:

        `[TOOL_CALLS]name[ARGS]{"arg1": "value1", "arg2": "value2"}`
    r@   r$   r+   )r-   rA   N)
r   r   r   r2   r   r3   r4   r5   r8   r   )r!   r"   r*   rD   r0   r<   s         r   _decode_tool_call_v11rG   p   s     >>NPY[cddJD$X!%%dASAZ%[[*Y%5%5dQcQj%5%k%kll  
 
 
		  X X X&'OPPVWWXs   A/B B*B%%B*c                    g }| D ]}|j         t          j        k    rt          d          |j         t          j        k    r$|                    t          ||                     _|j         t          j        k    r;|                    d          |v r$|	                    t          ||                     |	                    t          ||                     |S )at  Decode a list of tool call tokens into a list of tool calls.

    Note:
        Each list of tool call tokens are expected to be in the format:
        - v2 to v7: `[TOOL_CALLS][{"name": "name", "arguments": {"arg1": "value1", "arg2": "value2"}}, ...]`
        - v11+ without call ID: `[TOOL_CALLS]name[ARGS]{"arg1": "value1", "arg2": "value2"}`
        - v11+ with call ID: `[TOOL_CALLS]name[CALL_ID]call_id[ARGS]{"arg1": "value1", "arg2": "value2"}`

    Args:
        tool_call_tokens: A list of lists of tokens.
        tokenizer: The tokenizer to use for decoding.

    Returns:
        The list of decoded tool calls.
    z6Tool calls are not supported for tokenizer version v1.r?   )versionr
   v1
ValueErrorv7extendr=   v11get_special_tokenappendrE   rG   )r!   r"   tools_callsr0   s       r   _decode_tool_callsrR      s      K% L L	 0 333UVVV"2"555=iSSTTTT"2"6669;V;VWb;c;cgp;p;pA)YWWXXXX4Y	JJKKKKr   )r4   typingr   !mistral_common.experimental.utilsr   r   +mistral_common.protocol.instruct.tool_callsr   r   %mistral_common.tokens.tokenizers.baser   r	   r
   rK   r   r   r9   inttupler    r=   rE   rG   rR   r   r   r   <module>rY      s                 O N N N N N N N a a a a a a a a a a	 	 	 	 	: 	 	 		 	 	 	 	3 	 	 	.I.+..
49eDIsN++,. . . .> T#Y  9  Y]^fYg        Fc y ]e    4DI ) PX    *$s))<  W[\dWe      r   