
    .`i@                         d dl mZ d dl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 d dlmZ  ee          Z G d	 d
e          ZdS )    )SequenceN)make_tool_call_id)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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 )DeepSeekV3ToolParser	tokenizerc                    t                                          |           d| _        g | _        d| _        g | _        d| _        d| _        d| _        d| _	        t          j        d          | _        t          j        d          | _        t          j        d	          | _        | j        st!          d
          | j                            | j                  | _        | j                            | j                  | _        | j                            | j                  | _        | j                            | j	                  | _        | j        | j        t/          d          d S )NFu   <｜tool▁calls▁begin｜>u   <｜tool▁calls▁end｜>u   <｜tool▁call▁begin｜>u   <｜tool▁call▁end｜>u   <｜tool▁call▁begin｜>(?P<type>.*)<｜tool▁sep｜>(?P<function_name>.*)\n```json\n(?P<function_arguments>.*)\n```<｜tool▁call▁end｜>u^   (?P<type>.*)<｜tool▁sep｜>(?P<function_name>.*)\n```json\n(?P<function_arguments>.*[^\n`])u5   (?P<type>.*)<｜tool▁sep｜>(?P<function_name>.*)\nzUThe model tokenizer must be passed to the ToolParser constructor during construction.zUDeepSeek-V3 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tool_calls_start_tokentool_calls_end_tokentool_call_start_tokentool_call_end_tokenrecompiletool_call_regexstream_tool_call_portion_regexstream_tool_call_name_regexmodel_tokenizer
ValueErrorvocabgettool_calls_start_token_idtool_calls_end_token_idtool_call_start_token_idtool_call_end_token_idRuntimeError)selfr   	__class__s     |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/tool_parsers/deepseekv3_tool_parser.pyr   zDeepSeekV3ToolParser.__init__   sl   ###,1#.0$&  	# ,J#)E!*G"(C !z a 
  
 /1jm/
 /
+ ,.:D,
 ,
( # 	3   *.8S)T)T&'+z~~d6O'P'P$(,
t7Q(R(R%&*jnnT5M&N&N# *2+3+   43    model_outputrequestreturnc           
         | j         |vrt          dg |          S 	 | j                            |          }g }|D ];}|\  }}}|                    t          |t          ||                               <|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content)name	arguments)typefunctionTz,Error in extracting tool call from response.)r   r	   r    findallappendr   r
   find	Exceptionlogger	exception)
r,   r0   r1   function_call_tuplesr5   match	tool_typefunction_namefunction_argsr6   s
             r.   extract_tool_callsz'DeepSeekV3ToolParser.extract_tool_callsN   s]    &l::/"r<   

 (,';'C'CL'Q'Q$
1 	 	E>C;I}m%% !*%1%2m& & &      ''W):):4;V)W)W'WX3!%)'.8GGD         !OPPP3!&2|     s   BB/ /6C('C(previous_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsNc           
         t                               d|           t                               d|           | j        |vr*t                               d           t          |          S |                    | j        d                              | j        d          }	 |                    | j                  }|                    | j	                  }	|                    | j                  }
|                    | j	                  }d }d }|
|k    r9|	|k    r3| j
        |vr*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 }n|
|k    rA||	k    r:| j        t          | j                  d	k    rt                               d           d S | j        | j                                     d          }|r|t,          u r(|                    d                              d          n|}d|vrd S |                    d          }|d |         dz   }t                               d|           | j        | j        xx         |z  cc<   t          t5          | j        t7          |                              d                    g          S nI|                    | j        d          }|                    | j
        d          }t          g |          }|S t;                      }|r| j                            |          }|r#|                                 \  }}}||d<   ||d<   nZ| j!                            |          }|r"|                                 \  }}||d<   d|d<   nt                               d           d S | j        sy|d S |                    d          }|r\d| _        t          t5          | j        dtE                      t7          |                              d                    g          S 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           #                    d"           d }n:|rY|sWt          t5          | j        t7          |                              d                    g          }|| j        | j        <   n|r|rtI          |t,                    r||k    rt          |          t          |          k    r|%                    |          r|t          |          d          }t                               d#|           t          t5          | j        t7          |                              d                    g          }|| j        | j        <   nd }| j        t          | j                  d
z
  k    r|| j        | j        <   n| j                            |           |S # tL          $ r t           '                    d$           Y d S w xY w)%Nzdelta_text: %szdelta_token_ids: %szNo tool call tokens found!)r6    z/Generating text content! skipping tool parsing.z!tool_call_end_token in delta_textr   r      FzStarting on a new tool %sz/attempting to close tool call, but no tool callr8   zutf-8unicode_escapez"}z@Finishing tool and found diff that had not been streamed yet: %s)r8   T)exclude_none)indexr:   )r5   )r5   r6   r7   zNot enough tokenr:   )r7   )rR   r9   idr:   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.)(r?   debugr'   r   replacer   r   countr)   r*   r   splitr   rstriplstriplenr   r   r   r<   r   r&   strencodedecoderindexr   r   
model_dumpdictr!   rB   groupsr"   r   error
isinstance
startswithr>   r@   )r,   rG   rH   rI   rJ   rK   rL   r1   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_loctextcurrent_tool_callcurrent_tool_call_matchesrC   	tool_name	tool_argscurrent_tool_call_name_matchesrD   prev_argumentscur_argumentsdelta_argumentss                                r.   extract_tool_calls_streamingz1DeepSeekV3ToolParser.extract_tool_calls_streamingz   s	    	%z222*O<<<)1BBBLL5666
3333''(CRHHPP%r
 

{	 %7$<$<-% %! #5":":4;V"W"W#4#:#:-$ $  "3!8!89T!U!U $L %(:::'+===,J>>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.t/CDHHUU   3;; G,,334DEEE! 
 :--#t(//55G%hwh/$6DLL0  
 /0DEEEMEEE')&*&:):T)J)J)J)U)U15 *V *" *"  $	 	 	 	!: "))$*DbII||D$<bAA$DAAA $  $,0,O,U,U%- -) - $6O6V6V6X6X3Iy)09%f-5>%k22 8>>?PQQ 3 6 $/M/T/T/V/V,	94=)&19;)+66%7888#t .  $,4,=,A,A&,I,I   26D/')&*&:%/#4#6#6):)6*" *" *"",*$*"?"?  	$     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	sP   B*a 
G,a 8Aa Ba 6A	a  B'a )	a 4A2a )a Ja $a;:a;)__name__
__module____qualname__r   r   r[   r   r	   rF   r   intr   rx   __classcell__)r-   s   @r.   r   r      s        .- . . . . . .`** '* 
&	* * * *XNN N 	N
 %SMN $C=N "#N 'N 
	N N N N N N N Nr/   r   )collections.abcr   regexr   vllm.entrypoints.chat_utilsr   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   ry   r?   r    r/   r.   <module>r      s8   % $ $ $ $ $     9 9 9 9 9 9                     $ # # # # # ) ) ) ) ) )      
X		k k k k k: k k k k kr/   