glm47_moe_tool_parser.py 1.51 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
"""
GLM-4.7 Tool Call Parser.
5

6
7
8
9
10
11
12
13
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.
"""
14
15
16
17
18

import regex as re

from vllm.logger import init_logger
from vllm.tokenizers import TokenizerLike
19
from vllm.tool_parsers.abstract_tool_parser import Tool
20
21
22
23
24
25
from vllm.tool_parsers.glm4_moe_tool_parser import Glm4MoeModelToolParser

logger = init_logger(__name__)


class Glm47MoeModelToolParser(Glm4MoeModelToolParser):
26
27
    supports_required_and_named = False

28
29
    def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
        super().__init__(tokenizer, tools)
30
31
32
33
        # 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.
34
        self.func_detail_regex = re.compile(
35
            r"<tool_call>\s*(\S+?)\s*(<arg_key>.*)?</tool_call>", re.DOTALL
36
37
        )
        self.func_arg_regex = re.compile(
38
            r"<arg_key>(.*?)</arg_key>\s*<arg_value>(.*?)</arg_value>",
39
40
            re.DOTALL,
        )