
    .`i/                         d dl Z d dlZd dlmZ 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 d d	lmZ  ee          Z G d
 de          ZdS )    N)Sequence)Any)ChatCompletionRequest)DeltaFunctionCallDeltaMessageDeltaToolCallExtractedToolCallInformationFunctionCallToolCall)init_logger)TokenizerLike)
ToolParser)random_uuidc                   F    e Zd ZdZdZdZdZdZdZeeeegZ	de
f fdZd	ed
ef fdZeded
eedz  eeef         dz  f         fd            Zdedeeef         d	ed
eeef         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ded	ed
efdZ xZS )Step3ToolParserz
    Tool parser for a model that uses a specific XML-like format for tool calls.
    This version uses a robust, stateful, cursor-based streaming parser and
    consolidates tool arguments into a single message.
    u   <｜tool_calls_begin｜>u   <｜tool_calls_end｜>u   <｜tool_call_begin｜>u   <｜tool_call_end｜>u   <｜tool_sep｜>	tokenizerc                 t    t                                          |           d| _        d| _        d| _        d S )Nr   F)super__init__positiontool_block_startedtool_block_finished)selfr   	__class__s     w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/tool_parsers/step3_tool_parser.pyr   zStep3ToolParser.__init__.   s8    ###"'#(       requestreturnc                 |    t                                          |          }|j        r|j        dk    rd|_        |S )NnoneF)r   adjust_requesttoolstool_choiceskip_special_tokens)r   r   r   s     r   r!   zStep3ToolParser.adjust_request5   s>    ''((11= 	0W0F::*/G'r   action_textNc                     t          j        d|           }|sdS |                    d          }i }t          j        d|           }|D ]\  }}|                                ||<   ||fS )Nz<steptml:invoke name="([^"]+)">)NN   z=<steptml:parameter name="([^"]+)">([^<]*)</steptml:parameter>)researchgroupfindallstrip)r%   func_name_match	func_nameparamsparam_matchesnamevalues          r   _parse_steptml_invokez%Step3ToolParser._parse_steptml_invoke;   s     )$FTT 	:#))!,,	!#
L
 
 ) 	) 	)KD% ;;==F4LL&  r   r.   r/   c                 N   |j         pg D ]}|j        j        |k    r|j        j        pi }|                    di           }|                                D ]I\  }}t          |t                    s|                    |i           }	|	                    d          }
|
dk    r|                                ||<   e|
dk    rDt          j
        t                    5  t          |          ||<   d d d            n# 1 swxY w Y   |
dk    rDt          j
        t                    5  t          |          ||<   d d d            n# 1 swxY w Y   |
dk    r%|                                }|dv r|dk    n|||<   $|
d	k    r|                                d	k    rd n|||<   K n|S )
N
propertiestypestringintegernumberboolean)truefalser;   null)r"   functionr1   
parametersgetitems
isinstancestrr,   
contextlibsuppress
ValueErrorintfloatlower)r   r.   r/   r   toolschemar5   keyr2   proptyp	lower_vals               r   _cast_argumentszStep3ToolParser._cast_argumentsM   s;    M'R 	 	D}!Y..17R#ZZb99
"(,,.. Q QJC%eS11 ! %>>#r22D((6**Ch&+kkmms	))'0<< 5 5*-e**F3K5 5 5 5 5 5 5 5 5 5 5 5 5 5 5'0<< 7 7*/,,F3K7 7 7 7 7 7 7 7 7 7 7 7 7 7 7	))$)KKMM	  ),=== &//!& s
 .3kkmmv.E.Edd5s3 /4 s$   C99C= C=$EE
Eprevious_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsc           
      $
   	 | j         t          |          k    rd S || j         d          }| j        r$t          |          | _         t          |          S | j        s|                    | j                  r*| xj         t          | j                  z  c_         d| _        |                    | j                  }	|	dk    rT| j                            |                                          r|rd S t          |          | _         t          |          S |d |	         }
| xj         t          |
          z  c_         t          |
          S t          |          t          |	                                          z
  }|	                                }| xj         |z  c_         |                    | j
                  r2| xj         t          | j
                  z  c_         d| _        d| _        | j        dk    o$| j        | j                                     d          }| j        dk    s|r|                    | j                  r| xj         t          | j                  z  c_         | j        dk    rd| _        n| xj        dz  c_        d| _        t          | j                  | j        k    r7| j                            i            t          | j                  | j        k    7d| j        | j                 d<   | j                            |          rd S | j        dk    r| j        | j                                     dd          s|                    | j                  }|dk    r|}n
|d |         }|dk    r| j                            |          rd S |                     |          \  }}|sd S ||pi d}| j        spd| _        | j        | j                                     |           t          t)          | j        d	d
t+                       t-          |                    g          S | j        | j                                     |           |dk    r| xj         |t          | j                  z   z  c_         d| j        | j                 d<   |                     ||                    di           |          }|rIt1          j        |d          }t          t)          | j        t-          |                    g          S d S d S )NT)contentfinishedr   r'   F)r1   r?   r>   zchatcmpl-tool-)r1   )indexr6   idr>   )
tool_callsr?   ensure_ascii)	arguments)r[   r>   )r   lenr   r   r   
startswithTOOL_CALLS_BEGINfindr,   lstripTOOL_CALLS_ENDcurrent_tool_idprev_tool_call_arrr@   TOOL_CALL_BEGINcurrent_tool_name_sentappendTOOL_CALL_ENDr3   updater   r   r   rP   jsondumps)r   rQ   rR   rS   rT   rU   rV   r   unprocessed_text	start_posrX   offsettool_finishedend_tool_pos	tool_bodyfunction_namer`   tool_call_arr
final_argsfinal_args_jsons                       r   extract_tool_calls_streamingz,Step3ToolParser.extract_tool_calls_streamingp   sx   @	}L 1 111t+DMOO< ' > #L 1 1#,<==== * 9#..t/DEE MMS)>%?%??MM.2D+,11$2GHH	??-889I9O9O9Q9QRR$,$  $t$'$5$5DM'0@AAAA.z	z:GMMS\\1MM'8888 )**S1A1H1H1J1J-K-KKF/6688MMV#MM**4+>?? T%8!9!99+/(')$ !0B6 4;R$<c*oo  #r))])#..t/CDD 
MMS)=%>%>>MM+r11/0,,,,1,,27D/d566$:NNN/66r::: d566$:NNNPUD+D,@A*M'223CDD  4 #r))$2I$3c*e$$ *  044T5GHH2%% 0II 0, ?I2%%$*<*G*G	*R*R%4+/+E+Ei+P+P(y$  4)6ioSU V V 2 26D/+D,@AHHWWW')&*&:%/#CKMM#C#C):)N)N)N	  $	 	 	 	 '(<=DD!  
  2%%MM\C8J4K4K%KKMMPTD+D,@A*M!%!5!5%%)),;;" "J
 " *.*Ze*T*T*T+ -*.*>->2A.& .& .&!" !" !"(	  	  	  	 t4r   model_outputc           	      |   | j         |vrt          dg |          S |                    | j         d          \  }}| j        |vrt          dg |          S |                    | j        d          \  }}||z                                   }g }|                    | j                  }	|	D ]}
|
r	| j        |
vr|
                    | j        d          d         }| j        |vr9|                    | j        d          \  }}|                                dk    rp|                     |          \  }}|ra|_| 	                    |||          }t          j        |d          }|                    t          t          ||                               |rt          d	||r|nd           S t          dg |          S )
NF)tools_calledr]   rX   r'   r   r>   r^   )r1   r`   )r>   T)rc   r	   splitrf   r,   ri   rl   TOOL_SEPr3   rP   rn   ro   rk   r   r
   )r   r{   r   pre_textrest
tool_block	post_textrX   r]   
call_partspartcall_content	type_partinvoke_partrv   params_dict
params_strs                    r   extract_tool_callsz"Step3ToolParser.extract_tool_calls   s,   
  44/"r<    &++D,A1EE$d**/"r<    !%

4+> B B
Ii'..00%'
%%d&:;;
 	 	D 4-T99::d&8!<<Q?L}L00%1%7%7q%I%I"I{  J..)-)C)CK)P)P&M; !8"22=+wWW!Z%HHH
!!!-=J!W!W!W    
  	/!%#*4   
 ,2|
 
 
 	
r   )__name__
__module____qualname____doc__rc   rf   ri   rl   r   SPECIAL_TOKENSr   r   r   r!   staticmethodrC   tupledictr3   r   rP   r   rG   r   rz   r	   r   __classcell__)r   s   @r   r   r       s         2-N/O+M!H&WN)- ) ) ) ) ) )&; @U       !!	sTz4S>D00	1! ! ! \!"!! S#X! '	!
 
c3h! ! ! !FKK K 	K
 %SMK $C=K "#K 'K 
	K K K KZ4
4
 '4
 
&	4
 4
 4
 4
 4
 4
 4
 4
r   r   )rD   rn   collections.abcr   typingr   regexr(   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   
vllm.utilsr   r   loggerr    r   r   <module>r      s_        $ $ $ $ $ $                               $ # # # # # ) ) ) ) ) )      # " " " " "	X		Q
 Q
 Q
 Q
 Q
j Q
 Q
 Q
 Q
 Q
r   