o
    -it@                     @   s   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
 d
eZdS )    )SequenceN)make_tool_call_id)ChatCompletionRequest)DeltaFunctionCallDeltaMessageDeltaToolCallExtractedToolCallInformationFunctionCallToolCall)init_logger)TokenizerLike)
ToolParserc                       sr   e Zd Zdef fddZdededefddZd	ed
edede	e
 de	e
 de	e
 dededB fddZ  ZS )DeepSeekV31ToolParser	tokenizerc                    s   t  | d| _g | _d| _g | _d| _d| _d| _d| _	t
d| _t
d| _t
d	| _| js7td
| j| j| _| j| j| _| j| j| _| j| j	| _| jd u sa| jd u retdd S )NFu   <｜tool▁calls▁begin｜>u   <｜tool▁calls▁end｜>u   <｜tool▁call▁begin｜>u   <｜tool▁call▁end｜>uw   <｜tool▁call▁begin｜>(?P<function_name>.*?)<｜tool▁sep｜>(?P<function_arguments>.*?)<｜tool▁call▁end｜>uA   (?P<function_name>.*)<｜tool▁sep｜>(?P<function_arguments>.*)u'   (?P<function_name>.*)<｜tool▁sep｜>zUThe model tokenizer must be passed to the ToolParser constructor during construction.zWDeepSeek-V3.1 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__ f/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/tool_parsers/deepseekv31_tool_parser.pyr      sB   

zDeepSeekV31ToolParser.__init__model_outputrequestreturnc           	   	   C   s   | j |vrtdg |dS z7| j|}g }|D ]}|\}}|tdt||dd q|d || j  }td||r?|dW S d dW S  tyX   t	
d tdg |d Y S w )NF)tools_called
tool_callscontentfunction)name	arguments)typer4   Tz,Error in extracting tool call from response.)r   r   r   findallappendr
   r	   find	Exceptionlogger	exception)	r)   r.   r/   function_call_tuplesr2   matchfunction_namefunction_argsr3   r,   r,   r-   extract_tool_callsL   s>   
	

z(DeepSeekV31ToolParser.extract_tool_callsprevious_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsNc                 C   s  t d| t d| | j|vrt d t|dS || jd| jd}z|| j}|| j	}	|| j}
|| j	}d }d }|
|kr]|	|kr]| j
|vr]t d t|dW S | j
|v rt d || }|| jd | j
d	  }|| j
d	  }|| j
d  }|
|kr|
|krt|d
kr|| jd }nd }d }d }|  jd
7  _d| _| jd t d| j n|
|kr|
|kr|| jd }d }n|
|krI||	krI| jd u st| jd	krt d W d S | j| j d}|rH|tu r|ddn|}d|vrW d S |d}|d | d }t d| | j| j  |7  < tt| jt|djdddgdW S n|| jd}|| j
d}tg |d}|W S t }|r| j|}|r~|  \}}||d< ||d< n| j!|}|r|  }||d< d|d< nt d W d S | js|d u rW d S |d}|rd| _tt| jdt" t|djdddgdW S W d S |d u r|d urt|d}|W S d }|W S t d| j t| j| jkr| ji  | j| j d}|d}t d| t d | |s"|s"t d!| d }nv|s0|r0t #d" d }nh|rO|sOtt| jt|djdddgd}|| j| j< nI|r|rt$|tr||krt|t|kr|%|r|t|d  }t d#| tt| jt|djdddgd}|| j| j< nd }| jt| jd
 kr|| j| j< |W S | j| |W S  t&y   t 'd$ Y d S w )%Nzdelta_text: %szdelta_token_ids: %szNo tool call tokens found!)r3    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 callr6   zutf-8unicode_escapez"}z@Finishing tool and found diff that had not been streamed yet: %s)r6   T)exclude_none)indexr4   )r2   )r2   r3   r5   zNot enough tokenr4   )r5   )rM   r7   idr4   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   r9   r   r#   strencodedecoderindexr   r   
model_dumpdictr   r?   groupsr   r   error
isinstance
startswithr;   r=   )r)   rC   rD   rE   rF   rG   rH   r/   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_matches	tool_name	tool_argscurrent_tool_call_name_matchesr@   prev_argumentscur_argumentsdelta_argumentsr,   r,   r-   extract_tool_calls_streamingx   s  





























z2DeepSeekV31ToolParser.extract_tool_calls_streaming)__name__
__module____qualname__r   r   rV   r   r   rB   r   intr   rs   __classcell__r,   r,   r*   r-   r      s4    0
,	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   rt   r<   r   r,   r,   r,   r-   <module>   s    