[Tool] adjust_request to reasoning parser, and Gemma4 fixes (#39027)
Signed-off-by: Ben Browning <bbrownin@redhat.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
Cursor
parent
e24e0a43a4
commit
8477fe427d
@@ -6,7 +6,7 @@ import os
|
||||
from abc import abstractmethod
|
||||
from collections.abc import Callable, Iterable, Sequence
|
||||
from functools import cached_property
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, cast
|
||||
|
||||
from vllm.entrypoints.mcp.tool_server import ToolServer
|
||||
from vllm.logger import init_logger
|
||||
@@ -150,6 +150,12 @@ class ReasoningParser:
|
||||
previously been parsed and extracted (see constructor)
|
||||
"""
|
||||
|
||||
def adjust_request(
|
||||
self, request: "ChatCompletionRequest | ResponsesRequest"
|
||||
) -> "ChatCompletionRequest | ResponsesRequest":
|
||||
"""Adjust request parameters; override in subclasses as needed."""
|
||||
return request
|
||||
|
||||
def prepare_structured_tag(
|
||||
self,
|
||||
original_tag: str | None,
|
||||
@@ -298,7 +304,7 @@ class ReasoningParserManager:
|
||||
if isinstance(name, str):
|
||||
names = [name]
|
||||
elif is_list_of(name, str):
|
||||
names = name
|
||||
names = cast(list[str], name)
|
||||
else:
|
||||
names = [class_name]
|
||||
|
||||
|
||||
@@ -52,6 +52,16 @@ class Gemma4ReasoningParser(BaseThinkingReasoningParser):
|
||||
# skip_special_tokens=True).
|
||||
self._reasoning_text: str = ""
|
||||
self._prefix_stripped: bool = False
|
||||
self.new_turn_token_id = self.vocab["<|turn>"]
|
||||
self.tool_call_token_id = self.vocab["<|tool_call>"]
|
||||
self.tool_response_token_id = self.vocab["<|tool_response>"]
|
||||
|
||||
def adjust_request(
|
||||
self, request: "ChatCompletionRequest | ResponsesRequest"
|
||||
) -> "ChatCompletionRequest | ResponsesRequest":
|
||||
"""Disable special-token stripping to preserve boundary tokens."""
|
||||
request.skip_special_tokens = False
|
||||
return request
|
||||
|
||||
@property
|
||||
def start_token(self) -> str:
|
||||
@@ -63,6 +73,29 @@ class Gemma4ReasoningParser(BaseThinkingReasoningParser):
|
||||
"""The token that ends reasoning content."""
|
||||
return "<channel|>"
|
||||
|
||||
def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
|
||||
start_token_id = self.start_token_id
|
||||
end_token_id = self.end_token_id
|
||||
new_turn_token_id = self.new_turn_token_id
|
||||
tool_call_token_id = self.tool_call_token_id
|
||||
tool_response_token_id = self.tool_response_token_id
|
||||
|
||||
# Search from the end of input_ids to find the last match.
|
||||
for i in range(len(input_ids) - 1, -1, -1):
|
||||
if input_ids[i] == start_token_id:
|
||||
return False
|
||||
if input_ids[i] == tool_call_token_id:
|
||||
# We're generating a tool call, so reasoning must be ended.
|
||||
return True
|
||||
if input_ids[i] in (new_turn_token_id, tool_response_token_id):
|
||||
# We found a new turn or tool response token so don't consider
|
||||
# reasoning ended yet, since the model starts new reasoning
|
||||
# after these tokens.
|
||||
return False
|
||||
if input_ids[i] == end_token_id:
|
||||
return True
|
||||
return False
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Non-streaming path
|
||||
# ------------------------------------------------------------------
|
||||
@@ -159,11 +192,10 @@ class Gemma4ReasoningParser(BaseThinkingReasoningParser):
|
||||
result.reasoning = stripped
|
||||
return result
|
||||
else:
|
||||
# This entire delta was prefix — suppress it.
|
||||
# Don't set _prefix_stripped yet; there may be more
|
||||
# prefix chars to consume in the next delta.
|
||||
if len(self._reasoning_text) >= prefix_len:
|
||||
self._prefix_stripped = True
|
||||
result.reasoning = ""
|
||||
return result
|
||||
return None
|
||||
|
||||
# Case 2: Accumulated text is a strict prefix of
|
||||
|
||||
Reference in New Issue
Block a user