vlm: refactor engine vlm params and support processor output as input (#14091)

Co-authored-by: Mick <mickjagger19@icloud.com>
Co-authored-by: zhaochenyang20 <zhaochenyang20@gmail.com>
Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com>
Co-authored-by: BenYao21 <cyao22@asu.edu>
Co-authored-by: minleminzui <minleminzui@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: 赵晨阳 <zhaochen20@outlook.com>
This commit is contained in:
mlmz
2025-12-20 18:31:24 +08:00
committed by GitHub
parent 165f5c04cb
commit 1f1f05a85e
16 changed files with 783 additions and 305 deletions

View File

@@ -1,5 +1,7 @@
import ast
import json
import logging
import re
from typing import List
from sglang.srt.entrypoints.openai.protocol import Tool
@@ -32,6 +34,16 @@ class Llama32Detector(BaseFormatDetector):
# if users define to use a different separator in their prompt
self.tool_call_separator = ";"
def _convert_python_dict_to_json(self, text: str) -> str:
"""Convert Python dict strings to JSON format."""
try:
parsed = ast.literal_eval(text.strip())
if isinstance(parsed, dict):
return json.dumps(parsed, ensure_ascii=False)
except:
pass
return text
def has_tool_call(self, text: str) -> bool:
"""Check if the text contains a Llama 3.2 format tool call."""
# depending on the prompt format the Llama model may or may not
@@ -59,16 +71,36 @@ class Llama32Detector(BaseFormatDetector):
all_actions.append(obj)
idx += end + len(self.tool_call_separator)
safe_idx = idx
except json.JSONDecodeError as e:
# Find where next `{"name"` appears and try again
logger.warning(
f"Failed to parse JSON part: {action_text[idx:]}, JSON parse error: {str(e)}"
)
except json.JSONDecodeError:
# Try Python dict conversion as fallback
try:
dict_end = idx
brace_count = 0
for i in range(idx, action_text_len):
if action_text[i] == "{":
brace_count += 1
elif action_text[i] == "}":
brace_count -= 1
if brace_count == 0:
dict_end = i + 1
break
if dict_end > idx:
potential_dict = action_text[idx:dict_end]
json_version = self._convert_python_dict_to_json(potential_dict)
if json_version != potential_dict:
obj, _ = decoder.raw_decode(json_version)
all_actions.append(obj)
idx = dict_end + len(self.tool_call_separator)
safe_idx = idx
continue
except:
pass
next_obj_start = action_text.find('{"name":', idx + 1)
if next_obj_start == -1:
break
idx = next_obj_start
continue
# Only process if we found valid JSON objects
calls = self.parse_base_json(all_actions, tools) if all_actions else []
@@ -80,6 +112,30 @@ class Llama32Detector(BaseFormatDetector):
normal_text=normal_text + trailing_text, calls=calls
)
def parse_streaming_increment(
self, new_text: str, tools: List[Tool]
) -> StreamingParseResult:
"""Override to handle Python dict format in streaming."""
# First try with converted Python dict
self._buffer += new_text
converted_buffer = self._buffer
# Convert Python dict syntax to JSON
converted_buffer = re.sub(r"'([^']*)':", r'"\1":', converted_buffer)
converted_buffer = re.sub(r":\s*'([^']*)'", r': "\1"', converted_buffer)
# Temporarily replace buffer for parsing
original_buffer = self._buffer
self._buffer = converted_buffer
try:
result = super().parse_streaming_increment("", tools)
return result
except:
# Fall back to original buffer
self._buffer = original_buffer
return super().parse_streaming_increment(new_text, tools)
def structure_info(self) -> _GetInfoFunc:
return lambda name: StructureInfo(
begin='<|python_tag|>{"name":"' + name + '", "arguments":',