|
| 1 | +# Adapted from https://github.com/sgl-project/sglang/blob/94e1251131ca27260cb0e8938aeb7b4a4e630b19/python/sglang/srt/function_call/deepseekv31_detector.py |
| 2 | +import json |
| 3 | +import re |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from tensorrt_llm.logger import logger |
| 7 | +from tensorrt_llm.serve.openai_protocol import ChatCompletionToolsParam as Tool |
| 8 | +from tensorrt_llm.serve.tool_parser.base_tool_parser import BaseToolParser |
| 9 | +from tensorrt_llm.serve.tool_parser.core_types import ( |
| 10 | + StreamingParseResult, |
| 11 | + StructureInfo, |
| 12 | + ToolCallItem, |
| 13 | + _GetInfoFunc, |
| 14 | +) |
| 15 | + |
| 16 | +from .utils import is_complete_json |
| 17 | + |
| 18 | + |
| 19 | +class DeepSeekV31Parser(BaseToolParser): |
| 20 | + ( |
| 21 | + """Tool parser for DeepSeek V3 model function call format. |
| 22 | +
|
| 23 | + The DeepSeek V3 format uses special Unicode tokens to delimit function calls |
| 24 | + with JSON code blocks for arguments. |
| 25 | +
|
| 26 | + Format Structure: |
| 27 | + ``` |
| 28 | + <|tool▁calls▁begin|><|tool▁call▁begin|>{function_name}<|tool▁sep|>{json_arguments}<|tool▁calls▁end|><|end▁of▁sentence|> |
| 29 | + ``` |
| 30 | + Examples: |
| 31 | + ``` |
| 32 | + """ |
| 33 | + """<|tool▁calls▁begin|>""" |
| 34 | + """<|tool▁call▁begin|>get_current_weather<|tool▁sep|>{"location": "Tokyo"}<|tool▁call▁end|>""" |
| 35 | + """<|tool▁call▁begin|>get_current_weather<|tool▁sep|>{"location": "Paris"}<|tool▁call▁end|>""" |
| 36 | + """<|tool▁calls▁end|><|end▁of▁sentence|> |
| 37 | + ``` |
| 38 | +
|
| 39 | + Key Components: |
| 40 | + - Tool Calls Section: Wrapped between `<|tool▁calls▁begin|>` and `<|tool▁calls▁end|>` |
| 41 | + - Individual Tool Call: Wrapped between `<|tool▁call▁begin|>` and `<|tool▁call▁end|>` |
| 42 | + - Function Declaration: `<|tool▁call▁begin|>{function_name}<|tool▁sep|>` |
| 43 | + - Arguments: JSON code block between `<|tool▁sep|>` and `<|tool▁call▁end|>` |
| 44 | + - Supports multiple tool calls |
| 45 | +
|
| 46 | + Reference: https://www.modelscope.cn/models/deepseek-ai/DeepSeek-V3.1 |
| 47 | + """ |
| 48 | + ) |
| 49 | + |
| 50 | + def __init__(self): |
| 51 | + super().__init__() |
| 52 | + self.bot_token = "<|tool▁calls▁begin|>" # nosec B105 |
| 53 | + self.eot_token = "<|tool▁calls▁end|>" # nosec B105 |
| 54 | + self.func_call_regex = r"<|tool▁call▁begin|>.*?<|tool▁call▁end|>" |
| 55 | + self.func_detail_regex = r"<|tool▁call▁begin|>(.*)<|tool▁sep|>(.*)<|tool▁call▁end|>" |
| 56 | + self._last_arguments = "" |
| 57 | + self.current_tool_id = -1 |
| 58 | + |
| 59 | + def has_tool_call(self, text: str) -> bool: |
| 60 | + """Check if the text contains a deepseek format tool call.""" |
| 61 | + return self.bot_token in text |
| 62 | + |
| 63 | + def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult: |
| 64 | + """One-time parsing: Detects and parses tool calls in the provided text. |
| 65 | +
|
| 66 | + :param text: The complete text to parse. |
| 67 | + :param tools: List of available tools. |
| 68 | + :return: ParseResult indicating success or failure, consumed text, leftover text, and parsed calls. |
| 69 | + """ |
| 70 | + idx = text.find(self.bot_token) |
| 71 | + normal_text = text[:idx].strip() if idx != -1 else text |
| 72 | + if self.bot_token not in text: |
| 73 | + return StreamingParseResult(normal_text=normal_text, calls=[]) |
| 74 | + match_result_list = re.findall(self.func_call_regex, text, re.DOTALL) |
| 75 | + calls = [] |
| 76 | + try: |
| 77 | + for match_result in match_result_list: |
| 78 | + # Get function name |
| 79 | + func_detail = re.search(self.func_detail_regex, match_result, re.DOTALL) |
| 80 | + func_name = func_detail.group(1) |
| 81 | + func_args = func_detail.group(2) |
| 82 | + func_args = json.loads(func_args) |
| 83 | + # construct match_result for parse_base_json |
| 84 | + match_result = {"name": func_name, "parameters": func_args} |
| 85 | + calls.extend(self.parse_base_json(match_result, tools)) |
| 86 | + return StreamingParseResult(normal_text=normal_text, calls=calls) |
| 87 | + except Exception as e: |
| 88 | + logger.error(f"Error in detect_and_parse: {e}") |
| 89 | + # return the normal text if parsing fails |
| 90 | + return StreamingParseResult(normal_text=text) |
| 91 | + |
| 92 | + def parse_streaming_increment(self, new_text: str, tools: List[Tool]) -> StreamingParseResult: |
| 93 | + """Streaming incremental parsing tool calls for DeepSeekV3 format.""" |
| 94 | + self._buffer += new_text |
| 95 | + current_text = self._buffer |
| 96 | + |
| 97 | + # Check if we have a tool call (either the start token or individual tool call) |
| 98 | + has_tool_call = self.bot_token in current_text or "<|tool▁call▁begin|>" in current_text |
| 99 | + |
| 100 | + if not has_tool_call: |
| 101 | + if any( |
| 102 | + e_token.startswith(new_text) |
| 103 | + for e_token in [self.bot_token, "<|tool▁call▁begin|>"] |
| 104 | + ): |
| 105 | + return StreamingParseResult() |
| 106 | + self._buffer = "" |
| 107 | + for e_token in [self.eot_token, "<|tool▁call▁end|>"]: |
| 108 | + if e_token in new_text: |
| 109 | + new_text = new_text.replace(e_token, "") |
| 110 | + return StreamingParseResult(normal_text=new_text) |
| 111 | + |
| 112 | + if not hasattr(self, "_tool_indices"): |
| 113 | + self._tool_indices = self._get_tool_indices(tools) |
| 114 | + |
| 115 | + calls: list[ToolCallItem] = [] |
| 116 | + try: |
| 117 | + partial_match = re.search( |
| 118 | + pattern=r"<|tool▁call▁begin|>(.*)<|tool▁sep|>(.*?)(<|tool▁call▁end|>|$)", |
| 119 | + string=current_text, |
| 120 | + flags=re.DOTALL, |
| 121 | + ) |
| 122 | + if partial_match: |
| 123 | + func_name = partial_match.group(1).strip() |
| 124 | + func_args_raw = partial_match.group(2).strip() |
| 125 | + is_tool_end = partial_match.group(3) |
| 126 | + |
| 127 | + # Initialize state if this is the first tool call |
| 128 | + if self.current_tool_id == -1: |
| 129 | + self.current_tool_id = 0 |
| 130 | + self.prev_tool_call_arr = [] |
| 131 | + self.streamed_args_for_tool = [""] |
| 132 | + |
| 133 | + # Ensure we have enough entries in our tracking arrays |
| 134 | + while len(self.prev_tool_call_arr) <= self.current_tool_id: |
| 135 | + self.prev_tool_call_arr.append({}) |
| 136 | + while len(self.streamed_args_for_tool) <= self.current_tool_id: |
| 137 | + self.streamed_args_for_tool.append("") |
| 138 | + |
| 139 | + if not self.current_tool_name_sent: |
| 140 | + calls.append( |
| 141 | + ToolCallItem( |
| 142 | + tool_index=self.current_tool_id, |
| 143 | + name=func_name, |
| 144 | + parameters="", |
| 145 | + ) |
| 146 | + ) |
| 147 | + self.current_tool_name_sent = True |
| 148 | + # Store the tool call info for serving layer completions endpoint |
| 149 | + self.prev_tool_call_arr[self.current_tool_id] = { |
| 150 | + "name": func_name, |
| 151 | + "arguments": {}, |
| 152 | + } |
| 153 | + else: |
| 154 | + argument_diff = ( |
| 155 | + func_args_raw[len(self._last_arguments) :] |
| 156 | + if func_args_raw.startswith(self._last_arguments) |
| 157 | + else func_args_raw |
| 158 | + ) |
| 159 | + |
| 160 | + if argument_diff: |
| 161 | + calls.append( |
| 162 | + ToolCallItem( |
| 163 | + tool_index=self.current_tool_id, |
| 164 | + name=None, |
| 165 | + parameters=argument_diff, |
| 166 | + ) |
| 167 | + ) |
| 168 | + self._last_arguments += argument_diff |
| 169 | + self.streamed_args_for_tool[self.current_tool_id] += argument_diff |
| 170 | + |
| 171 | + if is_complete_json(func_args_raw): |
| 172 | + # Update the stored arguments |
| 173 | + try: |
| 174 | + parsed_args = json.loads(func_args_raw) |
| 175 | + self.prev_tool_call_arr[self.current_tool_id]["arguments"] = parsed_args |
| 176 | + except json.JSONDecodeError: |
| 177 | + pass |
| 178 | + |
| 179 | + # Find the end of the current tool call and remove only that part from buffer |
| 180 | + if is_tool_end: |
| 181 | + # Remove the completed tool call from buffer, keep any remaining content |
| 182 | + self._buffer = current_text[partial_match.end(3) :] |
| 183 | + else: |
| 184 | + self._buffer = "" |
| 185 | + |
| 186 | + result = StreamingParseResult(normal_text="", calls=calls) |
| 187 | + self.current_tool_id += 1 |
| 188 | + self._last_arguments = "" |
| 189 | + self.current_tool_name_sent = False |
| 190 | + return result |
| 191 | + |
| 192 | + return StreamingParseResult(normal_text="", calls=calls) |
| 193 | + |
| 194 | + except Exception as e: |
| 195 | + logger.error(f"Error in parse_streaming_increment: {e}") |
| 196 | + return StreamingParseResult(normal_text=current_text) |
| 197 | + |
| 198 | + def structure_info(self) -> _GetInfoFunc: |
| 199 | + return lambda name: StructureInfo( |
| 200 | + begin="<|tool▁call▁begin|>" + name + "<|tool▁sep|>", |
| 201 | + end="<|tool▁call▁end|>", |
| 202 | + trigger="<|tool▁call▁begin|>" + name + "<|tool▁sep|>", |
| 203 | + ) |
0 commit comments