fix(glm47): improve tool call parsing and content normalization (#37386)
Signed-off-by: karanb192 <karan@example.com> Co-authored-by: karanb192 <karan@example.com>
This commit is contained in:
co-authored by
karanb192
parent
8c31f47c63
commit
fad09e8a1f
@@ -1,6 +1,16 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
GLM-4.7 Tool Call Parser.
|
||||
|
||||
GLM-4.7 uses a slightly different tool call format compared to GLM-4.5:
|
||||
- The function name may appear on the same line as ``<tool_call>`` without
|
||||
a newline separator before the first ``<arg_key>``.
|
||||
- Tool calls may have zero arguments
|
||||
(e.g. ``<tool_call>func</tool_call>``).
|
||||
|
||||
This parser overrides the parent regex patterns to handle both formats.
|
||||
"""
|
||||
|
||||
import regex as re
|
||||
|
||||
@@ -14,10 +24,14 @@ logger = init_logger(__name__)
|
||||
class Glm47MoeModelToolParser(Glm4MoeModelToolParser):
|
||||
def __init__(self, tokenizer: TokenizerLike):
|
||||
super().__init__(tokenizer)
|
||||
# GLM-4.7 format: <tool_call>func_name[<arg_key>...]*</tool_call>
|
||||
# The function name can be followed by a newline, whitespace, or
|
||||
# directly by <arg_key> tags (no separator). The arg section is
|
||||
# optional so that zero-argument calls are supported.
|
||||
self.func_detail_regex = re.compile(
|
||||
r"<tool_call>(.*?)(<arg_key>.*?)?</tool_call>", re.DOTALL
|
||||
r"<tool_call>\s*(\S+?)\s*(<arg_key>.*)?</tool_call>", re.DOTALL
|
||||
)
|
||||
self.func_arg_regex = re.compile(
|
||||
r"<arg_key>(.*?)</arg_key>(?:\\n|\s)*<arg_value>(.*?)</arg_value>",
|
||||
r"<arg_key>(.*?)</arg_key>\s*<arg_value>(.*?)</arg_value>",
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
@@ -206,7 +206,12 @@ class Glm4MoeModelToolParser(ToolParser):
|
||||
)
|
||||
else:
|
||||
if len(tool_calls) > 0:
|
||||
content = model_output[: model_output.find(self.tool_calls_start_token)]
|
||||
content: str | None = model_output[
|
||||
: model_output.find(self.tool_calls_start_token)
|
||||
]
|
||||
# Normalize empty/whitespace-only content to None
|
||||
if not content or not content.strip():
|
||||
content = None
|
||||
return ExtractedToolCallInformation(
|
||||
tools_called=True, tool_calls=tool_calls, content=content
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user