fix function calling for Trinity (#17364)

This commit is contained in:
Raghav Ravishankar
2026-01-20 08:03:17 +05:30
committed by GitHub
parent 17c04b109d
commit 84aef37808
2 changed files with 45 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ from sglang.srt.function_call.pythonic_detector import PythonicDetector
from sglang.srt.function_call.qwen3_coder_detector import Qwen3CoderDetector
from sglang.srt.function_call.qwen25_detector import Qwen25Detector
from sglang.srt.function_call.step3_detector import Step3Detector
from sglang.srt.function_call.trinity_detector import TrinityDetector
from sglang.srt.function_call.utils import get_json_schema_constraint
logger = logging.getLogger(__name__)
@@ -59,6 +60,7 @@ class FunctionCallParser:
"qwen3_coder": Qwen3CoderDetector,
"step3": Step3Detector,
"minimax-m2": MinimaxM2Detector,
"trinity": TrinityDetector,
"interns1": InternlmDetector,
}

View File

@@ -0,0 +1,43 @@
import logging
from typing import List
from sglang.srt.entrypoints.openai.protocol import Tool
from sglang.srt.function_call.core_types import StreamingParseResult
from sglang.srt.function_call.qwen25_detector import Qwen25Detector
logger = logging.getLogger(__name__)
class TrinityDetector(Qwen25Detector):
"""
Detector for Trinity models using Qwen-style function call format.
This detector extends Qwen25Detector to handle tool calls that may appear
inside <think> sections by stripping the think tags before parsing.
Reference: https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct?chat_template=default
"""
def _strip_think_tags(self, text: str) -> str:
"""Remove <think> and </think> tags, keeping the content inside."""
return text.replace("<think>", "").replace("</think>", "")
def has_tool_call(self, text: str) -> bool:
"""Check if the text contains a tool call."""
return super().has_tool_call(self._strip_think_tags(text))
def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult:
"""
One-time parsing: Detects and parses tool calls in the provided text.
"""
return super().detect_and_parse(self._strip_think_tags(text), tools)
def parse_streaming_increment(
self, new_text: str, tools: List[Tool]
) -> StreamingParseResult:
"""
Streaming incremental parsing for tool calls.
"""
return super().parse_streaming_increment(
self._strip_think_tags(new_text), tools
)