94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
203 lines
7.0 KiB
Python
203 lines
7.0 KiB
Python
import json
|
|
import logging
|
|
import re
|
|
from typing import List
|
|
|
|
from sglang.srt.entrypoints.openai.protocol import Tool
|
|
from sglang.srt.function_call.base_format_detector import BaseFormatDetector
|
|
from sglang.srt.function_call.core_types import (
|
|
StreamingParseResult,
|
|
ToolCallItem,
|
|
_GetInfoFunc,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
REGEX_FUNCTION_CALL = re.compile(
|
|
r"(?:function call<\|role_sep\|>\n|<\|function_call\|>)(.*)",
|
|
re.DOTALL,
|
|
)
|
|
|
|
REGEX_CONTENT_PATTERN = re.compile(
|
|
r"^(.*?)(?:<\|message_sep\|>|<\|function_call\|>)",
|
|
re.DOTALL,
|
|
)
|
|
|
|
NAME_REGEX = re.compile(
|
|
r'"name"\s*:\s*"([^"]*)"',
|
|
re.DOTALL,
|
|
)
|
|
|
|
ARGS_REGEX = re.compile(
|
|
r'"arguments"\s*:\s*(.*)',
|
|
re.DOTALL,
|
|
)
|
|
|
|
|
|
class GigaChat3Detector(BaseFormatDetector):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.tool_started: bool = False
|
|
self.tool_name_sent: bool = False
|
|
self.end_content: bool = False
|
|
self._buffer: str = ""
|
|
self.prev_tool_call_arr: list[dict] = []
|
|
|
|
def has_tool_call(self, text: str) -> bool:
|
|
"""Check if text contains a tool call marker"""
|
|
return "function call<|role_sep|>\n" in text or "<|function_call|>" in text
|
|
|
|
def detect_and_parse(
|
|
self,
|
|
text: str,
|
|
tools: List[Tool],
|
|
) -> StreamingParseResult:
|
|
"""
|
|
Non-streaming parsing of complete model output.
|
|
Extracts tool calls and content from the full text.
|
|
"""
|
|
logger.debug(f"[GigaChat3] detect_and_parse: {text}")
|
|
model_output = text
|
|
function_call = None
|
|
content = None
|
|
if model_output.rstrip().endswith("</s>"):
|
|
model_output = model_output[: model_output.rfind("</s>")]
|
|
m_func = REGEX_FUNCTION_CALL.search(model_output)
|
|
if m_func:
|
|
try:
|
|
function_call = json.loads(m_func.group(1), strict=False)
|
|
if not (
|
|
isinstance(function_call, dict)
|
|
and "name" in function_call
|
|
and "arguments" in function_call
|
|
):
|
|
function_call = None
|
|
elif not isinstance(function_call["arguments"], dict):
|
|
function_call = None
|
|
except json.JSONDecodeError as e:
|
|
logger.warning(f"[GigaChat3] JSON decode error: {e}")
|
|
return StreamingParseResult(
|
|
normal_text=model_output,
|
|
calls=[],
|
|
)
|
|
m_content = REGEX_CONTENT_PATTERN.search(model_output)
|
|
if m_content:
|
|
content = m_content.group(1)
|
|
else:
|
|
content = model_output
|
|
if not function_call:
|
|
return StreamingParseResult(normal_text=content, calls=[])
|
|
name = function_call["name"]
|
|
args = function_call["arguments"]
|
|
match_result = {"name": name, "arguments": args}
|
|
calls = self.parse_base_json(match_result, tools)
|
|
return StreamingParseResult(normal_text=content, calls=calls)
|
|
|
|
def parse_streaming_increment(
|
|
self,
|
|
new_text: str,
|
|
tools: List[Tool],
|
|
) -> StreamingParseResult:
|
|
"""
|
|
Streaming parser for incremental text chunks.
|
|
Maintains state across calls to build complete tool calls.
|
|
"""
|
|
if not new_text:
|
|
return StreamingParseResult()
|
|
logger.debug(f"[GigaChat3] parse_streaming_increment: '{new_text}'")
|
|
self._buffer += new_text
|
|
current_text = self._buffer
|
|
delta_text = new_text
|
|
content = None
|
|
func_name = None
|
|
cur_args = None
|
|
m_func = REGEX_FUNCTION_CALL.search(current_text)
|
|
if not self.tool_started:
|
|
m_content = REGEX_CONTENT_PATTERN.search(delta_text)
|
|
if m_content:
|
|
content = m_content.group(1)
|
|
self.end_content = True
|
|
else:
|
|
if not self.end_content:
|
|
content = delta_text
|
|
if m_func:
|
|
self.tool_started = True
|
|
logger.debug("[GigaChat3] Tool call started")
|
|
if content:
|
|
return StreamingParseResult(normal_text=content)
|
|
if not m_func:
|
|
return StreamingParseResult()
|
|
json_tail = m_func.group(1).strip()
|
|
name_match = NAME_REGEX.search(json_tail)
|
|
if name_match:
|
|
func_name = name_match.group(1)
|
|
args_match = ARGS_REGEX.search(json_tail)
|
|
if args_match:
|
|
cur_args = args_match.group(1).strip()
|
|
if cur_args.endswith("</s>"):
|
|
cur_args = cur_args[: -len("</s>")]
|
|
if cur_args.endswith("}"):
|
|
try:
|
|
candidate = cur_args[:-1].strip()
|
|
json.loads(candidate, strict=False)
|
|
cur_args = candidate
|
|
except json.JSONDecodeError:
|
|
pass
|
|
calls: List[ToolCallItem] = []
|
|
if not self.prev_tool_call_arr:
|
|
self.prev_tool_call_arr.append({})
|
|
if not self.tool_name_sent:
|
|
if not func_name:
|
|
return StreamingParseResult()
|
|
self.tool_name_sent = True
|
|
self.prev_tool_call_arr[0]["name"] = func_name
|
|
logger.debug(f"[GigaChat3] Sending tool name: {func_name}")
|
|
calls.append(
|
|
ToolCallItem(
|
|
tool_index=0,
|
|
name=func_name,
|
|
parameters="",
|
|
)
|
|
)
|
|
return StreamingParseResult(calls=calls)
|
|
if cur_args is None:
|
|
return StreamingParseResult()
|
|
prev_args = self.prev_tool_call_arr[0].get("arguments_str", "")
|
|
if not prev_args:
|
|
delta_args = cur_args
|
|
elif cur_args.startswith(prev_args):
|
|
delta_args = cur_args[len(prev_args) :]
|
|
else:
|
|
logger.warning(
|
|
f"[GigaChat3] Arguments overlap mismatch. "
|
|
f"prev='{prev_args[:50]}...' cur='{cur_args[:50]}...'"
|
|
)
|
|
return StreamingParseResult()
|
|
if not delta_args:
|
|
return StreamingParseResult()
|
|
self.prev_tool_call_arr[0]["arguments_str"] = cur_args
|
|
try:
|
|
args_dict = json.loads(cur_args, strict=False)
|
|
self.prev_tool_call_arr[0]["arguments"] = args_dict
|
|
except json.JSONDecodeError:
|
|
self.prev_tool_call_arr[0]["arguments"] = {}
|
|
logger.debug(f"[GigaChat3] Sending args delta: '{delta_args[:100]}...'")
|
|
calls.append(
|
|
ToolCallItem(
|
|
tool_index=0,
|
|
name=None,
|
|
parameters=delta_args,
|
|
)
|
|
)
|
|
return StreamingParseResult(calls=calls)
|
|
|
|
def supports_structural_tag(self) -> bool:
|
|
"""GigaChat3 does not use structural tags"""
|
|
return False
|
|
|
|
def structure_info(self) -> _GetInfoFunc:
|
|
"""Not applicable for GigaChat3"""
|
|
raise NotImplementedError(
|
|
"GigaChat3Detector does not support structural_tag format."
|
|
)
|