[Bugfix] Route Step3p5 forced tool choices through XML parser (#51810)
Signed-off-by: taking-lying-flat <1615405@qq.com> Signed-off-by: Flora Feng <4florafeng@gmail.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
co-authored by
Flora Feng
parent
4e7af3b0de
commit
5e887a078b
@@ -22,7 +22,11 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
from vllm.logger import init_logger
|
||||
from vllm.tokenizers import TokenizerLike
|
||||
from vllm.tool_parsers.abstract_tool_parser import Tool, ToolParser
|
||||
from vllm.tool_parsers.utils import safe_literal_eval
|
||||
from vllm.tool_parsers.utils import (
|
||||
find_tool_name,
|
||||
find_tool_properties,
|
||||
safe_literal_eval,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -421,43 +425,14 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
def _validate_function_name(self, func_name: str) -> bool:
|
||||
"""Check if function name exists in tool definitions."""
|
||||
if not self.tools:
|
||||
return False
|
||||
|
||||
for tool in self.tools:
|
||||
if (
|
||||
hasattr(tool, "type")
|
||||
and tool.type == "function"
|
||||
and hasattr(tool, "function")
|
||||
and hasattr(tool.function, "name")
|
||||
and tool.function.name == func_name
|
||||
):
|
||||
return True
|
||||
|
||||
return False
|
||||
return find_tool_name(self.tools, func_name)
|
||||
|
||||
def _validate_parameter_name(self, param_name: str) -> bool:
|
||||
"""Check if parameter exists in current function's tool definition."""
|
||||
if not self.tools or not self.current_function_name:
|
||||
return True
|
||||
|
||||
for tool in self.tools:
|
||||
if (
|
||||
hasattr(tool, "type")
|
||||
and tool.type == "function"
|
||||
and hasattr(tool, "function")
|
||||
and hasattr(tool.function, "name")
|
||||
and tool.function.name == self.current_function_name
|
||||
):
|
||||
if not hasattr(tool.function, "parameters"):
|
||||
return True
|
||||
params = tool.function.parameters
|
||||
if isinstance(params, dict):
|
||||
properties = params.get("properties", params)
|
||||
return param_name in properties
|
||||
break
|
||||
|
||||
return True
|
||||
properties = find_tool_properties(self.tools, self.current_function_name)
|
||||
return not properties or param_name in properties
|
||||
|
||||
def _should_skip_element(self, element: str) -> bool:
|
||||
"""Determine whether an element should be skipped.
|
||||
@@ -1190,34 +1165,10 @@ class StreamingXMLToolCallParser:
|
||||
"""
|
||||
if not self.tools or not self.current_function_name:
|
||||
return "string"
|
||||
|
||||
for tool in self.tools:
|
||||
if not hasattr(tool, "type") or not (
|
||||
hasattr(tool, "function") and hasattr(tool.function, "name")
|
||||
):
|
||||
continue
|
||||
if (
|
||||
tool.type == "function"
|
||||
and tool.function.name == self.current_function_name
|
||||
):
|
||||
if not hasattr(tool.function, "parameters"):
|
||||
return "string"
|
||||
params = tool.function.parameters
|
||||
if isinstance(params, dict) and "properties" in params:
|
||||
properties = params["properties"]
|
||||
if param_name in properties and isinstance(
|
||||
properties[param_name], dict
|
||||
):
|
||||
return self.repair_param_type(
|
||||
str(properties[param_name].get("type", "string"))
|
||||
)
|
||||
elif isinstance(params, dict) and param_name in params:
|
||||
param_config = params[param_name]
|
||||
if isinstance(param_config, dict):
|
||||
return self.repair_param_type(
|
||||
str(param_config.get("type", "string"))
|
||||
)
|
||||
break
|
||||
properties = find_tool_properties(self.tools, self.current_function_name)
|
||||
param_config = properties.get(param_name)
|
||||
if isinstance(param_config, dict):
|
||||
return self.repair_param_type(str(param_config.get("type", "string")))
|
||||
return "string"
|
||||
|
||||
def repair_param_type(self, param_type: str) -> str:
|
||||
@@ -1357,6 +1308,9 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
|
||||
class Step3p5ToolParser(ToolParser):
|
||||
# Step-3.5/3.7 chat templates use the Qwen3-Coder XML tool-call format.
|
||||
structural_tag_model = "qwen_3_coder"
|
||||
|
||||
def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
|
||||
super().__init__(tokenizer, tools)
|
||||
self.parser = StreamingXMLToolCallParser()
|
||||
|
||||
Reference in New Issue
Block a user