
    .`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 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  ee          Z G d	 d
e          Z G d de          Zde j        defdZde j         defdZ!de"de#e"e"f         dz  fdZ$de"dede%de"dedz  f
dZ&dS )    N)Sequence)Any)PreTrainedTokenizerBase)ChatCompletionRequest)DeltaFunctionCallDeltaMessageDeltaToolCallExtractedToolCallInformationFunctionCallToolCall)init_logger)
ToolParserc                       e Zd ZdS )_UnexpectedAstErrorN)__name__
__module____qualname__     z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/tool_parsers/pythonic_tool_parser.pyr   r       s        Dr   r   c                        e Zd ZdZ ej        dej                  Zdef fdZ	e
defd            Zej        deddf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 )PythonicToolParserz
    Tool call parser for models that produce tool calls in a pythonic style,
    such as Llama 3.2 and Llama 4 models.

    Used when --enable-auto-tool-choice --tool-call-parser pythonic are all set
    z\[([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*,\s*)*([a-zA-Z]+\w*=.*\s)?\),\s*)*([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*,\s*)*([a-zA-Z]+\w*=.*\s*)?\)\s*)+\]	tokenizerc                 J    t                                          |           d S N)super__init__)selfr   	__class__s     r   r   zPythonicToolParser.__init__8   s!    #####r   returnc                     | j         S r   current_tool_id)r   s    r   current_tool_indexz%PythonicToolParser.current_tool_index<   s    ##r   valueNc                     || _         d S r   r"   )r   r%   s     r   r$   z%PythonicToolParser.current_tool_index@   s    $r   model_outputrequestc                    d}	 | j                             |t          j                  du}nE# t          $ r8 t
                              d           t
                              d|           Y nw xY w|st          dg |          S 	 t          j
        |          }t          |j        d         dd          }t          |t          j                  r?t          d	 |j        D                       r!t          d
d |j        D             d          S t#          d          # t$          $ r/ t
                              d           t          dg |          cY S w xY w)zH
        Extract the tool calls from a complete model response.
        F)timeoutNz7Regex timeout occurred when matching tool call pattern.z3Regex timeout occurred when matching user input: %s)tools_called
tool_callscontentr   r%   c              3   J   K   | ]}t          |t          j                  V  d S r   
isinstanceastCall.0es     r   	<genexpr>z8PythonicToolParser.extract_tool_calls.<locals>.<genexpr>`   s?       4 4,-
1ch''4 4 4 4 4 4r   Tc                 ,    g | ]}t          |          S r   _handle_single_toolr3   s     r   
<listcomp>z9PythonicToolParser.extract_tool_calls.<locals>.<listcomp>e   s0           ,A..     r   ,Tool output must be a list of function callsz,Error in extracting tool call from response.)TOOL_CALL_REGEXmatchenvs%VLLM_TOOL_PARSE_REGEX_TIMEOUT_SECONDSTimeoutErrorloggerwarningdebugr
   r1   parsegetattrbodyr0   Listalleltsr   	Exception	exception)r   r'   r(   is_tool_call_patternmoduleparseds         r   extract_tool_callsz%PythonicToolParser.extract_tool_callsD   s     %	$** $*T +    !   	 	 	NNTUUULLE|    	 $ 	/"r<   	Y|,,FV[^Wd;;F&#(++  4 4174 4 4 1 1  4!%   !'      !    *B    	 	 	KLLL/"r<     	s(   (- ?A/.A/BD D 6EEprevious_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsc                    |                     d          st          |          S 	 t          |          }|d S |\  }	}
t          j        |	          }t          |j        d         dd           }t          |t          j                  rt          d |j
        D                       st          d          d |j
        D             }g }t          |          D ]\  }}|| j        k     r|| _        t          | j                  |k    r| j                            d           |t          |          d	z
  k     pd
|
v}|r| xj        d	z  c_        |s
|
d d         nd}|s|
d         dk    r|dz   }|                    dd          }t%          | j        |         |||          }|G|                    |           |j        +|j        j        | j        |xx         |j        j        z  cc<   |r| j        s
di ig| _        |rt          |          S |
s| j        dk    rt          d          S d S # t.          $ r8 t0                              d           t0                              d           Y d S w xY w)N[)r-   r   r%   c              3   J   K   | ]}t          |t          j                  V  d S r   r/   r3   s     r   r6   zBPythonicToolParser.extract_tool_calls_streaming.<locals>.<genexpr>   s?       ; ;,-
1ch''; ; ; ; ; ;r   r;   c                 ,    g | ]}t          |          S r   r8   r3   s     r   r:   zCPythonicToolParser.extract_tool_calls_streaming.<locals>.<listcomp>   s0        $A&&  r       z)])}'"	arguments)r,   z+Error trying to handle streaming tool call.z=Skipping chunk as a result of tool streaming extraction error)
startswithr   _make_valid_pythonr1   rD   rE   rF   r0   rG   rH   rI   r   	enumerater$   lenstreamed_args_for_toolappendreplace_compute_tool_deltafunctionra   prev_tool_call_arrr#   rJ   rA   rK   rC   )r   rP   rQ   rR   rS   rT   rU   r(   valid_and_added_text
valid_text
added_textrM   rN   r,   tool_deltasindexnew_callnew_call_completewithheld_suffixdeltas                       r   extract_tool_calls_streamingz/PythonicToolParser.extract_tool_calls_streamingv   s    &&s++ 	4
3333J	#5l#C#C #+t%9"J
Yz**FV[^Wd;;Ffch// s ; ;17; ; ; 8 8  *B     J
 K#,Z#8#8 W Wx4222*/'t233u<</66r::: C
OOa//I4z3I " % 1++q0++9J"R*SbS//PR( <Z^s-B-B&5&;O #2"9"9#s"C"C+/6%  $&&u---2!N4@3E:::en>VV:::  >4#: >,7+<*=' #{;;;; D$81$<$< $B////t 	 	 	JKKKLLO   44	s   H2 GH2 H2 2>I43I4)r   r   r   __doc__recompileDOTALLr<   r   r   propertyintr$   setterstrr   r
   rO   r   r   ru   __classcell__)r   s   @r   r   r   $   sx         !bj 	R
	 O
$"9 $ $ $ $ $ $ $C $ $ $ X$ % % % % % %00*?0	%0 0 0 0dWW W 	W
 %SMW $C=W "#W 'W 
	W W W W W W W Wr   r   valr    c                    t          | t          j                  r| j        S t          | t          j                  rQt          d | j        D                       st          d          d t          | j        | j	                  D             S t          | t          j
                  rd | j        D             S t          d          )Nc              3   J   K   | ]}t          |t          j                  V  d S r   )r0   r1   Constant)r4   ks     r   r6   z'_get_parameter_value.<locals>.<genexpr>   s.      AA1:a..AAAAAAr   z/Dict tool call arguments must have literal keysc                 >    i | ]\  }}|j         t          |          S r   )r%   _get_parameter_value)r4   r   vs      r   
<dictcomp>z(_get_parameter_value.<locals>.<dictcomp>   s9     
 
 
1 G)!,,
 
 
r   c                 ,    g | ]}t          |          S r   )r   )r4   r   s     r   r:   z(_get_parameter_value.<locals>.<listcomp>   s!    :::A$Q'':::r   z$Tool call arguments must be literals)r0   r1   r   r%   DictrH   keysr   zipvaluesrG   rI   )r   s    r   r   r      s    #s|$$ Jy	C	"	" 
JAAAAAAA 	Y%&WXXX
 
CHcj11
 
 
 	
 
C	"	" J::::::!"HIIIr   callc           
      .   t          | j        t          j                  st	          d          | j        j        }i }| j        D ]}t          |j                  ||j	        <   t          dt          |t          j        |d                              S )NzInvalid tool call namerj   F)ensure_asciinamera   )typerj   )r0   funcr1   Namer   idkeywordsr   r%   argr   r   jsondumps)r   function_namera   keywords       r   r9   r9      s    di** <!":;;;ILMI= E E!5gm!D!D	'+$*YU*S*S*S
 
 
   r   textc                    g }t          |           D ]\  }}|dv r|                    |            |dk    r*|r|                                dk    rt          d          P|dk    r*|r|                                dk    rt          d          |dk    r*|r|                                d	k    rt          d
          |dv r[|r7|d         |k    r+|dk    r| |dz
           dk    r|                                 |r|d         dv r|                    |           |                                 } |                     d          s|                     d          rd S |r[|d         d	k    rO| d |                     d	                   }|                    d          }|                    d          }||k    rd S |r[|d         dk    rO| d |                     d                   }|                    d          }|                    d          }	||	k    rd S |                     d          r
| d d         } |r8|d         dk    r,|                     d          s|                     d          sd S d}
t          |          D ]=}|dk    r|
dz  }
|dk    r|
dz  }
|d	k    r|
dz  }
&|dk    r|
dz  }
2|dk    r|
dz  }
>| |
z   |
fS )N>   {(rW   ]rW   zMismatched square bracketsr]   r   zMismatched parenthesesr^   r   zMismatched curly braces>   r`   r_   r   r[   \=:,rZ   r_   r`   )	rd   rg   popr   rstripendswithrfindcountreversed)r   bracket_stackrp   chartrailing_dict_textnum_keys
num_valuestrailing_params_textnum_full_param_namesnum_full_param_valuesrn   s              r   rc   rc      s   M  + +t?""  &&&&S[[  HM$5$5$7$73$>$>)*FGGG %?S[[  DM$5$5$7$73$>$>)*BCCC %?S[[  EM$5$5$7$73$>$>)*CDDD %?Z 
+r!2d!:!:199eaiD!8!8!%%'''' +=#4
#B#B$$T***;;==D}}S T]]3//  t r*c11!"3DJJsOO"34%++C00'--c22
z!!4 r*c11#$5djjoo$56399#>> 4 : :3 ? ?#8884}}S CRCy"$$c"" %c"" % tJ'' 
 
3;;#JJS[[#JJS[[#JJS[[#JJS[[#J*j((r   previously_sent_argsrq   rp   rs   c           	      X   |j         j        }|r/|                    |          sJ |d t          |                    }| s1t	          |j        d|t          |j         j        |                    S |t          |           d          }|r t	          d |t          |                    nd S )Nrj   r   )r   r   rp   rj   )ra   )r   rp   rj   )rj   ra   r   re   r	   r   r   r   )r   rq   rp   rs   new_call_argsarg_diffs         r   ri   ri   5  s     %/M ?%%o66666%&=_)=)=(=&=> 	
{&&+'  	
 
 
 	
 S!566889H
 	5+<x+P+P+P	
 	
 	
 	
 r   )'r1   r   collections.abcr   typingr   regexrw   transformersr   	vllm.envsr>   0vllm.entrypoints.openai.chat_completion.protocolr   'vllm.entrypoints.openai.engine.protocolr   r   r	   r
   r   r   vllm.loggerr   &vllm.tool_parsers.abstract_tool_parserr   r   rA   rJ   r   r   exprr   r2   r9   r}   tuplerc   r{   ri   r   r   r   <module>r      sJ   


  $ $ $ $ $ $           0 0 0 0 0 0                           $ # # # # #      
X			 	 	 	 	) 	 	 	i i i i i i i iXJch J3 J J J J ch 8    C)S C)U38_t%; C) C) C) C)L)1:=PST     r   