
    .`imh                         d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ  ee          Z G d d	e          ZdS )
    )SequenceN)ChatCompletionRequest)DeltaFunctionCallDeltaMessageDeltaToolCallExtractedToolCallInformationFunctionCallToolCall)init_logger)TokenizerLike)
ToolParserc                        e Zd Zdef fdZdedeeeef         fdZddZ	ddZ
d	ed
edefdZdedededee         dee         dee         d
ededz  fdZ xZS )KimiK2ToolParser	tokenizerc                     t                                          |           d _        g  _        d _        g  _        d _        d _        d _        d _	        d _
        d _        d _        d _        dd	g _        dd
g _        d _        d _        t%          j        dt$          j                   _        t%          j        d           _        t%          j        d           _         j        st3          d           j                             j                   _         j                             j                   _         fd j        D              _         fd j        D              _         j                             j                   _          j                             j                   _!         j         j        tE          d          d S )NF i   r   i    z<|tool_calls_section_begin|>z<|tool_calls_section_end|>z<|tool_call_section_begin|>z<|tool_call_section_end|>z<|tool_call_begin|>z<|tool_call_end|>z<\|tool_call_begin\|>\s*(?P<tool_call_id>[^<]+:\d+)\s*<\|tool_call_argument_begin\|>\s*(?P<function_arguments>(?:(?!<\|tool_call_begin\|>).)*?)\s*<\|tool_call_end\|>zV(?P<tool_call_id>.+:\d+)\s*<\|tool_call_argument_begin\|>\s*(?P<function_arguments>.*)z(?P<tool_call_id>.+:\d+)\s*zUThe model tokenizer must be passed to the ToolParser constructor during construction.c                 L    g | ] }j                             |          x!S Nvocabget.0variantselftids     y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/tool_parsers/kimi_k2_tool_parser.py
<listcomp>z-KimiK2ToolParser.__init__.<locals>.<listcomp>T   s<     6
 6
 6
z~~g...; ;;;    c                 L    g | ] }j                             |          x!S r   r   r   s     r   r   z-KimiK2ToolParser.__init__.<locals>.<listcomp>Y   s<     4
 4
 4
z~~g...; ;;;r    zQKimi-K2 Tool parser could not locate tool call start/end tokens in the tokenizer!)#super__init__current_tool_name_sentprev_tool_call_arrcurrent_tool_idstreamed_args_for_toolin_tool_sectiontoken_bufferbuffer_max_sizesection_char_countmax_section_chars_buffer_overflow_loggedtool_calls_start_tokentool_calls_end_tokentool_calls_start_token_variantstool_calls_end_token_variantstool_call_start_tokentool_call_end_tokenrecompileDOTALLtool_call_regexstream_tool_call_portion_regexstream_tool_call_name_regexmodel_tokenizer
ValueErrorr   r   tool_calls_start_token_idtool_calls_end_token_idtool_calls_start_token_idstool_calls_end_token_idstool_call_start_token_idtool_call_end_token_idRuntimeError)r   r   r   	__class__s   ` @r   r#   zKimiK2ToolParser.__init__   s.   ###,1#.0$&  	#
 &+!# %)'(&*-2$ ,J#)E!*);
,
 )'9
*
 +@"(; !z uI 
  

 /1je/
 /
+ ,.:6T+U+U(# 	3   *.8S)T)T&'+z~~d6O'P'P$6
 6
 6
 6
 6
?6
 6
 6
'
4
 4
 4
 4
 4
=4
 4
 4
% )-
t7Q(R(R%&*jnnT5M&N&N# *2+3+   43r    textreturnc                     d}d}|}| j         D ]}||v r|                    |d          }d}| j        D ]}||v r|                    |d          }d}|||fS )z
        Check for section begin/end markers in text and strip them.
        Returns: (cleaned_text, found_section_begin, found_section_end)
        Fr   T)r0   replacer1   )r   rD   found_begin	found_endcleanedr   s         r   _check_and_strip_markersz)KimiK2ToolParser._check_and_strip_markersk   s    
 	 ; 	# 	#G'!!!//'266" 9 	! 	!G'!!!//'266 	Y..r    Nc                 0    d| _         d| _        d| _        dS )z&Reset state when exiting tool section.Fr   r   N)r(   r)   r+   r   s    r   _reset_section_statez%KimiK2ToolParser._reset_section_state   s     $"#r    c                     |                                   d| _        g | _        d| _        g | _        t
                              d           dS )z
        Reset all streaming state. Call this between requests to prevent
        state leakage when parser instance is reused.
        Fr   zStreaming state resetN)rN   r$   r%   r&   r'   loggerdebugrM   s    r   reset_streaming_statez&KimiK2ToolParser.reset_streaming_state   sQ     	!!### ',#"$!&(#,-----r    model_outputrequestc                 t   | j         |vrt          dg |          S 	 | j                            |          }t                              d|           g }|D ]o}|\  }}|                    d          d                             d          d         }|                    t          |dt          ||	          
                     p|d |
                    | j                            }	t          d||	r|	nd           S # t          $ r/ t                              d           t          dg |          cY S w xY w)NF)tools_called
tool_callscontentzfunction_call_tuples: %s:r   .r   function)name	arguments)idtyper[   Tz,Error in extracting tool call from response.)r.   r   r7   findallrP   rQ   splitappendr
   r	   find	Exception	exception)
r   rS   rT   function_call_tuplesrW   matchfunction_idfunction_argsfunction_namerX   s
             r   extract_tool_callsz#KimiK2ToolParser.extract_tool_calls   s    &l::/"r<   
#
 (,';'C'CL'Q'Q$79MNNN
1  E16.K$/$5$5c$:$:1$=$C$CC$H$H$LM%% *!+%1%2m& & &      ''W):):4;V)W)W'WX3!%)'.8GGD         !OPPP3!&2|     s   C C> >6D76D7previous_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsc           
      P   t                               d|           t                               d|           d}| xj        |z  c_        t          | j                  | j        k    rK| j        s't                               d| j                   d| _        | j        | j         dz  d          | _        |                     | j                  \  }	}
}|
r6| j        s/t                               d           d| _        |	| _        d| _	        |r| j        rt                               d	           | j
        |v }|r$d}t                               d
           |	| _        nt                               d           |                                  d}| j        D ]9}||v r3|                    |d          }t          |          dk    r|d         } n:|                                rt          |          S t          d          S |	| _        t!          fd| j        D                       }|s1| j        s*t                               d           t          |          S |                     |          \  }}}| j        r| xj	        t          |          z  c_	        | j	        | j        k    rZt                               d| j                   |                                  t          |                                r|nd          S 	 |                    | j                  }|                    | j
                  }                    | j                  }                    | j
                  }d }d }||k    rq||k    rk| j        |vrb| j        r1|dk    r+t                               d|           t          d          S t                               d           t          |          S | j        |v rt                               d           ||z   }|                    | j                  d                             | j                  d                                         }|                    | j                  d                                         }|                    | j                  d                                         }||k    r||k    rt          |          dk    r!|                    | j                  d         }nd }d }d }| xj        dz  c_        d| _        | j                            d           t                               d| j                   n||k    r*||k    r$|                    | j                  d         }d }ng||k    r||k    r| j        t          | j                  dk    r9t                               d           |r| j        r|                                  d S | j        | j                                     d          }|r+|t>          u r(|                     d          !                    d          n|}d|vr|r| j        r|                                  d S |"                    d          }|d |         dz   }t                               d|           | j        | j        xx         |z  cc<   |r5| j        r.t                               d           |                                  t          tG          | j        tI          |          %                    d                     g!          S n| j        r@t                               d"           |r|                                  t          d          S |&                    | j        d          }|&                    | j        d          }t          g |#          }|r| j        r|                                  |S tO                      }|r	| j(        )                    |          }|r[|*                                \  }} |                    d$          d                             d%          d         }!||d&<   |!|d'<   | |d<   n| j+        )                    |          }"|"rZ|"*                                \  }#|#                    d$          d                             d%          d         }!|#|d&<   |!|d'<   d|d<   nt                               d(           d S | j        s|d S |                    d'          }$|                    d&          }|$rPd| _        t          tG          | j        d)|tI          |$*          %                    d          +          g!          S d S || j        rd S |t          |          nd }|S t                               d,| j                   t          | j                  | j        k    r| j                            i            | j        | j                                     d          }%|                    d          }&t                               d-|%           t                               d.|&           |&s!|%st                               d/|           d }n\|&s |%rt           ,                    d0           d }n:|&rY|%sWt          tG          | j        tI          |&          %                    d                     g!          }|&| j        | j        <   n|&r|%rt[          |t>                    r|&|%k    rt          |&          t          |%          k    r|&.                    |%          r|&t          |%          d          }'t                               d1|           t          tG          | j        tI          |'          %                    d                     g!          }|&| j        | j        <   nd }| j        t          | j                  dz
  k    r|| j        | j        <   n| j                            |           |r5| j        r.t                               d           |                                  |S # t^          $ r t           0                    d2           Y d S w xY w)3Nzdelta_text: %szdelta_token_ids: %sFzxToken buffer exceeded max size (%d bytes), flushing excess. This may indicate very long markers or unusual tokenization.T   zEntering tool sectionr   zDetected section end markerz3Deferring section exit: tool_call_end in same chunkzExiting tool sectionr      )rX   c              3       K   | ]}|v V  	d S r    )r   r   rp   s     r   	<genexpr>z@KimiK2ToolParser.extract_tool_calls_streaming.<locals>.<genexpr>  s:        
  
),C$$ 
  
  
  
  
  
r    zNo tool call tokens found!zdTool section exceeded max length (%d chars), forcing exit. This may indicate malformed model output.z2In tool section before first tool, suppressing: %sz/Generating text content! skipping tool parsing.z!tool_call_end_token in delta_textr   zStarting on a new tool %sz/attempting to close tool call, but no tool callr]   zutf-8unicode_escapez"}z@Finishing tool and found diff that had not been streamed yet: %sz Completing deferred section exit)r]   )exclude_none)indexr[   )rW   z,In tool section, suppressing text generation)rW   rX   rY   rZ   r^   r\   zNot enough tokenr[   )r\   )rz   r_   r^   r[   z,Trying to parse current tool call with ID %szdiffing old arguments: %szagainst new ones: %szSkipping text %s - no argumentszSshould be impossible to have arguments reset mid-call. skipping streaming anything.zgot diff %sz+Error trying to handle streaming tool call.)1rP   rQ   r)   lenr*   r-   warningrK   r(   r+   rA   rN   r1   ra   stripr   anyr>   r,   countr@   r3   r2   rstriplstripr&   r$   r'   rb   r%   r   strencodedecoderindexr   r   
model_dumprG   dictr8   rg   groupsr9   error
isinstance
startswithrd   re   )(r   rl   rm   rn   ro   rp   rq   rT   deferred_section_exitbuffered_textfound_section_beginfound_section_endhas_tool_endpost_section_contentr   partshas_section_token_prev_tool_start_countprev_tool_end_countcur_tool_start_countcur_tool_end_counttool_call_portiontext_portion	full_textdeltadiffend_locrD   current_tool_callcurrent_tool_call_matchestool_id	tool_args	tool_namecurrent_tool_call_name_matchestool_id_strrj   prev_argumentscur_argumentsdelta_argumentss(        `                                  r   extract_tool_calls_streamingz-KimiK2ToolParser.extract_tool_calls_streaming   sj    	%z222*O<<< !& 	Z' t !!D$888/ 4S(  
 04, $ 143G2G12L2N2N OD ))$*;<< 	>*,=
  	(t'; 	(LL0111#'D  -D&'D# 	.!5 	.LL6777  6/IL 0(,%RSSS$1!! 3444))+++ (*$#A  G*,, * 0 0! < <u::>>3880	 -
 (--// F'0DEEEE#B//// -D    
  
  
  
040O 
  
  
 
 

 ! 	4)= 	4LL5666  
3333  88DD
Aq  	V##s:6##&)???@*  
 ))+++ $*:J:J:L:L,TJJRTUUUUf	 %7$<$<-% %! #5":":4;V"W"W#4#:#:-$ $  "3!8!89T!U!U $L %(:::'+===,J>>
 ' 4,@A,E,ELLL"  
 (3333NOOO#J7777':55@AAA(:5	OOD$>??CU4344Q8VXX "
 (--d.FGGJQQSS
)//0HII"MTTVV %'999(+@@@''!++(4(:(:4;U(V(V)%% )-% E# $$)$$.3++2226668$:NOOOO %'999(,AAA %1$6$6t7Q$R$RSU$V!# %(:::&*===*2c$:Q6R6RVW6W6WLL!RSSS, 41E 4113334.t/CDHHUU    3;; G,,334DEEE! 
 :--0 8T5I 8 55777#t(//55G%hwh/$6DLL0  
 /0DEEEMEEE, 41E 4%GHHH11333')&*&:):T)J)J)J)U)U15 *V *" *"  $	 	 	 	/ J ' 4LL!OPPP, 411333'3333!))$*DbII||D$<bAA$DAAA( 0T-A 0--/// $  $,0,O,U,U%- -) - $)B)I)I)K)K&GY 'c 2 21 5 ; ;C @ @ DI.5%d+09%f-5>%k22 8>>?PQQ 3 6 $)G)N)N)P)P$/$5$5c$:$:1$=$C$CC$H$H$L	2=)$/4=)&19;)+66%7888#t .  $,4,=,A,A&,I,I+//55   26D/')&*&:%/#*):)6*" *" *"",*$*"?"?  	$     4
 !( '  4 $/ !4444 
 
 LL>@T   4*++t/CCC'..r222 "4T5IJNN N .11+>>MLL4nEEELL/??? ! 3! 3!>
KKK # -!~ -!=     $!~ $!$%"&"6%6*7& & &(jdj;;	   	 	 	 ER+D,@AA  !> !z3//!%77M**S-@-@@@%00@@ A '4C4G4G4I4I&JOLL
;;;()&*&:):.=*" *" *"",*$*"?"?	  $	 	 	E IVD/0DEE E #s4+B'C'Ca'GGG@Q'(<=='../@AAA % ,)= ,?@@@))+++L 	 	 	JKKK44	sc   B8q= :)q= $H	q= /A<q= -Cq= Aq= 
A%q= 0Dq= 
	q= A;q= 	q= q= 4Kq= =$r%$r%)rE   N)__name__
__module____qualname__r   r#   r   tupleboolrK   rN   rR   r   r   rk   r   intr   r   __classcell__)rC   s   @r   r   r      sU       K- K K K K K KZ/S /U3d?5K / / / /,$ $ $ $. . . . // '/ 
&	/ / / /bPP P 	P
 %SMP $C=P "#P 'P 
	P P P P P P P Pr    r   )collections.abcr   regexr4   0vllm.entrypoints.openai.chat_completion.protocolr   'vllm.entrypoints.openai.engine.protocolr   r   r   r   r	   r
   vllm.loggerr   vllm.tokenizersr   &vllm.tool_parsers.abstract_tool_parserr   r   rP   r   rv   r    r   <module>r      s&  
 % $ $ $ $ $                         $ # # # # # ) ) ) ) ) )      
X		{ { { { {z { { { { {r    