Support GLM-4.7 function calling via structural tags (#28149)
Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com> Co-authored-by: Xinyuan Tong <xinyuantong.cs@gmail.com>
This commit is contained in:
co-authored by
Xinyuan Tong
Xinyuan Tong
parent
37ed10bd24
commit
f79a6b5c33
@@ -3,10 +3,15 @@ import json
|
||||
import logging
|
||||
import re
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
from functools import lru_cache
|
||||
from typing import Any, Dict, List, Literal, Optional, Tuple, Union
|
||||
|
||||
from sglang.srt.entrypoints.openai.protocol import Tool
|
||||
from sglang.srt.function_call.base_format_detector import BaseFormatDetector
|
||||
from sglang.srt.entrypoints.openai.protocol import Tool, ToolChoice
|
||||
from sglang.srt.function_call.base_format_detector import (
|
||||
BaseFormatDetector,
|
||||
StructuralTag,
|
||||
get_model_structural_tag,
|
||||
)
|
||||
from sglang.srt.function_call.core_types import (
|
||||
StreamingParseResult,
|
||||
ToolCallItem,
|
||||
@@ -17,6 +22,21 @@ from sglang.srt.function_call.utils import infer_type_from_json_schema
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def _glm47_native_structural_tag_available() -> bool:
|
||||
# "glm_4_7" is only registered in newer xgrammar, so the import can succeed
|
||||
# while the model name stays unknown. Probe once and fall back if absent.
|
||||
if get_model_structural_tag is None:
|
||||
return False
|
||||
try:
|
||||
get_model_structural_tag(
|
||||
model="glm_4_7", tools=[], tool_choice="auto", reasoning=False
|
||||
)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
class StreamState(str, Enum):
|
||||
"""State machine states for XML to JSON streaming conversion."""
|
||||
|
||||
@@ -781,7 +801,22 @@ class Glm47MoeDetector(BaseFormatDetector):
|
||||
return arguments
|
||||
|
||||
def supports_structural_tag(self) -> bool:
|
||||
return False
|
||||
return _glm47_native_structural_tag_available()
|
||||
|
||||
def get_structural_tag(
|
||||
self,
|
||||
tools: Union[List[Tool], None] = None,
|
||||
tool_choice: Union[ToolChoice, Literal["auto", "required"]] = "auto",
|
||||
thinking_mode: bool = False,
|
||||
) -> Optional[StructuralTag]:
|
||||
if not self.supports_structural_tag():
|
||||
return None
|
||||
return super().get_structural_tag(
|
||||
tools=tools, tool_choice=tool_choice, thinking_mode=thinking_mode
|
||||
)
|
||||
|
||||
def structure_info(self) -> _GetInfoFunc:
|
||||
raise NotImplementedError()
|
||||
raise NotImplementedError
|
||||
|
||||
def get_structural_tag_name(self) -> str:
|
||||
return "glm_4_7"
|
||||
|
||||
Reference in New Issue
Block a user