Convert formatting to use ruff instead of yapf + isort (#26247)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-10-05 07:06:22 -07:00
committed by GitHub
parent 17edd8a807
commit d6953beb91
1508 changed files with 115205 additions and 94107 deletions
@@ -8,13 +8,19 @@ from typing import Any, Optional, Union
import regex as re
from vllm.entrypoints.chat_utils import make_tool_call_id
from vllm.entrypoints.openai.protocol import (ChatCompletionRequest,
DeltaFunctionCall, DeltaMessage,
DeltaToolCall,
ExtractedToolCallInformation,
FunctionCall, ToolCall)
from vllm.entrypoints.openai.protocol import (
ChatCompletionRequest,
DeltaFunctionCall,
DeltaMessage,
DeltaToolCall,
ExtractedToolCallInformation,
FunctionCall,
ToolCall,
)
from vllm.entrypoints.openai.tool_parsers.abstract_tool_parser import (
ToolParser, ToolParserManager)
ToolParser,
ToolParserManager,
)
from vllm.logger import init_logger
from vllm.transformers_utils.tokenizer import AnyTokenizer
from vllm.utils import random_uuid
@@ -24,7 +30,6 @@ logger = init_logger(__name__)
@ToolParserManager.register_module("xlam")
class xLAMToolParser(ToolParser):
def __init__(self, tokenizer: AnyTokenizer):
super().__init__(tokenizer)
@@ -32,8 +37,7 @@ class xLAMToolParser(ToolParser):
self.prev_tool_calls: list[dict] = []
self.current_tool_id = -1
self.current_tool_name_sent = False
self.streamed_args: list[str] = [
] # Track arguments sent for each tool
self.streamed_args: list[str] = [] # Track arguments sent for each tool
# For backward compatibility with tests
self.current_tools_sent: list[bool] = []
@@ -57,7 +61,8 @@ class xLAMToolParser(ToolParser):
}
def preprocess_model_output(
self, model_output: str) -> tuple[Optional[str], Optional[str]]:
self, model_output: str
) -> tuple[Optional[str], Optional[str]]:
"""
Preprocess the model output to extract content and potential tool calls.
Returns:
@@ -66,8 +71,7 @@ class xLAMToolParser(ToolParser):
# Check for thinking tag
thinking_match = re.search(self.thinking_tag_pattern, model_output)
if thinking_match:
content = model_output[:thinking_match.start() +
len("</think>")].strip()
content = model_output[: thinking_match.start() + len("</think>")].strip()
thinking_content = thinking_match.group(1).strip()
# Try to parse the thinking content as JSON
@@ -94,8 +98,7 @@ class xLAMToolParser(ToolParser):
try:
json.loads(json_str)
# Extract content by removing the JSON code block
content = re.sub(json_pattern, "",
model_output).strip()
content = re.sub(json_pattern, "", model_output).strip()
return content, json_str
except json.JSONDecodeError:
continue
@@ -107,28 +110,30 @@ class xLAMToolParser(ToolParser):
return None, model_output
except json.JSONDecodeError:
# Even if it's not valid JSON yet, it might be a tool call in progress
if ("{" in model_output and "name" in model_output
and "arguments" in model_output):
if (
"{" in model_output
and "name" in model_output
and "arguments" in model_output
):
return None, model_output
# If no tool calls found, return the original output as content
return model_output, None
def extract_tool_calls(
self, model_output: str,
request: ChatCompletionRequest) -> ExtractedToolCallInformation:
self, model_output: str, request: ChatCompletionRequest
) -> ExtractedToolCallInformation:
"""
Extract tool calls from a complete model output.
"""
try:
# Preprocess the model output
content, potential_tool_calls = self.preprocess_model_output(
model_output)
content, potential_tool_calls = self.preprocess_model_output(model_output)
if not potential_tool_calls:
return ExtractedToolCallInformation(tools_called=False,
tool_calls=[],
content=content)
return ExtractedToolCallInformation(
tools_called=False, tool_calls=[], content=content
)
# Parse the potential tool calls as JSON
tool_calls_data = json.loads(potential_tool_calls)
@@ -145,8 +150,11 @@ class xLAMToolParser(ToolParser):
tool_calls: list[ToolCall] = []
for idx, call in enumerate(tool_calls_data):
if (not isinstance(call, dict) or "name" not in call
or "arguments" not in call):
if (
not isinstance(call, dict)
or "name" not in call
or "arguments" not in call
):
logger.debug("Invalid tool call format at index %d", idx)
continue
@@ -155,8 +163,11 @@ class xLAMToolParser(ToolParser):
type="function",
function=FunctionCall(
name=call["name"],
arguments=(json.dumps(call["arguments"]) if isinstance(
call["arguments"], dict) else call["arguments"]),
arguments=(
json.dumps(call["arguments"])
if isinstance(call["arguments"], dict)
else call["arguments"]
),
),
)
tool_calls.append(tool_call)
@@ -169,9 +180,9 @@ class xLAMToolParser(ToolParser):
except Exception as e:
logger.exception("Error extracting tool calls: %s", str(e))
return ExtractedToolCallInformation(tools_called=False,
tool_calls=[],
content=model_output)
return ExtractedToolCallInformation(
tools_called=False, tool_calls=[], content=model_output
)
def extract_tool_calls_streaming(
self,
@@ -189,26 +200,36 @@ class xLAMToolParser(ToolParser):
# First, check for a definitive start of a tool call block.
# This prevents premature parsing of incomplete output.
stripped_text = current_text.strip()
preprocessed_content, preprocessed_tool_calls = (
self.preprocess_model_output(current_text))
preprocessed_content, preprocessed_tool_calls = self.preprocess_model_output(
current_text
)
# For JSON code blocks, we need to detect them earlier, even if incomplete
has_potential_json_block = ("```json" in current_text
or "```\n[" in current_text
or "[TOOL_CALLS]" in current_text
or "<tool_call>" in current_text)
has_potential_json_block = (
"```json" in current_text
or "```\n[" in current_text
or "[TOOL_CALLS]" in current_text
or "<tool_call>" in current_text
)
is_tool_call_block = (
stripped_text.startswith("[")
or stripped_text.startswith("<tool_call>")
or stripped_text.startswith("[TOOL_CALLS]") or
or stripped_text.startswith("[TOOL_CALLS]")
or
# Check if we have thinking tags with JSON-like content following
("</think>[" in current_text) or
("</think>[" in current_text)
or
# Check if the text contains a JSON array after preprocessing
preprocessed_tool_calls is not None or
preprocessed_tool_calls is not None
or
# For JSON code blocks, detect early if we see enough structure
(has_potential_json_block and '"name"' in current_text
and '"arguments"' in current_text))
(
has_potential_json_block
and '"name"' in current_text
and '"arguments"' in current_text
)
)
if not is_tool_call_block:
return DeltaMessage(content=delta_text)
@@ -225,8 +246,9 @@ class xLAMToolParser(ToolParser):
# Try parsing as JSON to check for complete tool calls
try:
# Use preprocessed tool calls if available
tool_calls_text = (preprocessed_tool_calls if
preprocessed_tool_calls else current_text)
tool_calls_text = (
preprocessed_tool_calls if preprocessed_tool_calls else current_text
)
parsed_tools = json.loads(tool_calls_text)
if isinstance(parsed_tools, list):
# Update our tool array for next time
@@ -237,11 +259,15 @@ class xLAMToolParser(ToolParser):
# Check for test-specific state setup (current_tools_sent)
# This handles the case where tests manually set current_tools_sent
if (hasattr(self, "current_tools_sent") # type: ignore
and len(self.current_tools_sent) > 0):
if (
hasattr(self, "current_tools_sent") # type: ignore
and len(self.current_tools_sent) > 0
):
# If current_tools_sent is set to [False], it means the test wants us to send the name
if (len(self.current_tools_sent) == 1
and self.current_tools_sent[0] is False):
if (
len(self.current_tools_sent) == 1
and self.current_tools_sent[0] is False
):
# Extract the function name using regex
name_pattern = r'"name"\s*:\s*"([^"]+)"'
name_match = re.search(name_pattern, current_text)
@@ -250,51 +276,53 @@ class xLAMToolParser(ToolParser):
# The test expects us to send just the name first
tool_id = make_tool_call_id()
delta = DeltaMessage(tool_calls=[
DeltaToolCall(
index=0,
type="function",
id=tool_id,
function=DeltaFunctionCall(
name=function_name).model_dump(
exclude_none=True), # type: ignore
)
])
delta = DeltaMessage(
tool_calls=[
DeltaToolCall(
index=0,
type="function",
id=tool_id,
function=DeltaFunctionCall(
name=function_name
).model_dump(exclude_none=True), # type: ignore
)
]
)
# Update state to reflect that we've sent the name
self.current_tools_sent = [True]
self.current_tool_id = 0
self.streaming_state["current_tool_index"] = 0
if len(self.streaming_state["sent_tools"]) == 0:
self.streaming_state["sent_tools"].append({
"sent_name":
True,
"sent_arguments_prefix":
False,
"sent_arguments":
"",
})
self.streaming_state["sent_tools"].append(
{
"sent_name": True,
"sent_arguments_prefix": False,
"sent_arguments": "",
}
)
else:
self.streaming_state["sent_tools"][0][
"sent_name"] = True
self.streaming_state["sent_tools"][0]["sent_name"] = True
self.current_tool_name_sent = True
return delta
# Use regex to identify tool calls in the output
# Use preprocessed tool calls text for better parsing, but also try to extract from incomplete JSON blocks
search_text = (preprocessed_tool_calls
if preprocessed_tool_calls else current_text)
search_text = (
preprocessed_tool_calls if preprocessed_tool_calls else current_text
)
# For JSON code blocks that aren't complete yet, try to extract the JSON content
if not preprocessed_tool_calls and has_potential_json_block:
# Try to extract the JSON array from within the code block
json_match = re.search(r"```(?:json)?\s*([\s\S]*?)(?:```|$)",
current_text)
json_match = re.search(
r"```(?:json)?\s*([\s\S]*?)(?:```|$)", current_text
)
if json_match:
potential_json = json_match.group(1).strip()
# Use this as search text even if it's incomplete
if potential_json.startswith("[") and (
'"name"' in potential_json
and '"arguments"' in potential_json):
'"name"' in potential_json and '"arguments"' in potential_json
):
search_text = potential_json
# Try to find complete tool names first
@@ -306,8 +334,7 @@ class xLAMToolParser(ToolParser):
if tool_count == 0:
# Check if we're in the middle of parsing a tool name
partial_name_pattern = r'"name"\s*:\s*"([^"]*)'
partial_matches = list(
re.finditer(partial_name_pattern, search_text))
partial_matches = list(re.finditer(partial_name_pattern, search_text))
if partial_matches:
# We have a partial tool name - not ready to emit yet
return None
@@ -317,14 +344,13 @@ class xLAMToolParser(ToolParser):
# Ensure our state arrays are large enough
while len(self.streaming_state["sent_tools"]) < tool_count:
self.streaming_state["sent_tools"].append({
"sent_name":
False,
"sent_arguments_prefix":
False,
"sent_arguments":
"",
})
self.streaming_state["sent_tools"].append(
{
"sent_name": False,
"sent_arguments_prefix": False,
"sent_arguments": "",
}
)
while len(self.streaming_state["tool_ids"]) < tool_count:
self.streaming_state["tool_ids"].append(None)
@@ -337,14 +363,13 @@ class xLAMToolParser(ToolParser):
next_idx = current_idx + 1
# If tool at next_idx has not been sent yet
if (next_idx < tool_count
and not self.streaming_state["sent_tools"][next_idx]
["sent_name"]):
if (
next_idx < tool_count
and not self.streaming_state["sent_tools"][next_idx]["sent_name"]
):
# Update indexes
self.streaming_state["current_tool_index"] = next_idx
self.current_tool_id = (
next_idx # For backward compatibility
)
self.current_tool_id = next_idx # For backward compatibility
current_idx = next_idx
# Extract the tool name
@@ -354,21 +379,20 @@ class xLAMToolParser(ToolParser):
tool_id = f"call_{current_idx}_{random_uuid()}"
self.streaming_state["tool_ids"][current_idx] = tool_id
delta = DeltaMessage(tool_calls=[
DeltaToolCall(
index=current_idx,
type="function",
id=tool_id,
function=DeltaFunctionCall(
name=tool_name).model_dump(
exclude_none=True), # type: ignore
)
])
self.streaming_state["sent_tools"][current_idx][
"sent_name"] = True
self.current_tool_name_sent = (
True # For backward compatibility
delta = DeltaMessage(
tool_calls=[
DeltaToolCall(
index=current_idx,
type="function",
id=tool_id,
function=DeltaFunctionCall(name=tool_name).model_dump(
exclude_none=True
), # type: ignore
)
]
)
self.streaming_state["sent_tools"][current_idx]["sent_name"] = True
self.current_tool_name_sent = True # For backward compatibility
# Keep track of streamed args for backward compatibility
while len(self.streamed_args) <= current_idx:
@@ -381,7 +405,8 @@ class xLAMToolParser(ToolParser):
# Support both regular and empty argument objects
# First, check for the empty arguments case: "arguments": {}
empty_args_pattern = (
r'"name"\s*:\s*"[^"]+"\s*,\s*"arguments"\s*:\s*\{\s*\}')
r'"name"\s*:\s*"[^"]+"\s*,\s*"arguments"\s*:\s*\{\s*\}'
)
empty_args_match = re.search(empty_args_pattern, search_text)
# Check if this tool has empty arguments
@@ -391,36 +416,39 @@ class xLAMToolParser(ToolParser):
for i in range(tool_count):
if i == current_idx:
# If this is our current tool and it has empty arguments
if not self.streaming_state["sent_tools"][
current_idx]["sent_arguments_prefix"]:
if not self.streaming_state["sent_tools"][current_idx][
"sent_arguments_prefix"
]:
# Send empty object
self.streaming_state["sent_tools"][
current_idx][
"sent_arguments_prefix"] = True
self.streaming_state["sent_tools"][
current_idx]["sent_arguments"] = "{}"
self.streaming_state["sent_tools"][current_idx][
"sent_arguments_prefix"
] = True
self.streaming_state["sent_tools"][current_idx][
"sent_arguments"
] = "{}"
# Update streamed_args for backward compatibility
while len(self.streamed_args) <= current_idx:
self.streamed_args.append("")
self.streamed_args[current_idx] += "{}"
delta = DeltaMessage(tool_calls=[
DeltaToolCall(
index=current_idx,
function=DeltaFunctionCall(
arguments="{}").
model_dump(
exclude_none=True), # type: ignore
)
])
delta = DeltaMessage(
tool_calls=[
DeltaToolCall(
index=current_idx,
function=DeltaFunctionCall(
arguments="{}"
).model_dump(exclude_none=True), # type: ignore
)
]
)
# Move to next tool if available
if current_idx < tool_count - 1:
self.streaming_state[
"current_tool_index"] += 1
self.streaming_state["current_tool_index"] += 1
self.current_tool_id = self.streaming_state[
"current_tool_index"]
"current_tool_index"
]
return delta
@@ -439,72 +467,77 @@ class xLAMToolParser(ToolParser):
# Parse the entire JSON structure to properly extract arguments for each tool
try:
parsed_tools = json.loads(search_text)
if isinstance(
parsed_tools,
list) and current_idx < len(parsed_tools):
if isinstance(parsed_tools, list) and current_idx < len(
parsed_tools
):
current_tool = parsed_tools[current_idx]
if isinstance(current_tool.get("arguments"),
dict):
args_text = json.dumps(
current_tool["arguments"])
if isinstance(current_tool.get("arguments"), dict):
args_text = json.dumps(current_tool["arguments"])
else:
args_text = str(
current_tool.get("arguments", "{}"))
args_text = str(current_tool.get("arguments", "{}"))
except (json.JSONDecodeError, KeyError, IndexError):
# Fallback to regex-based extraction
pass
# If arguments haven't been sent yet
sent_args = self.streaming_state["sent_tools"][
current_idx]["sent_arguments"]
sent_args = self.streaming_state["sent_tools"][current_idx][
"sent_arguments"
]
# If we haven't sent the opening bracket yet
if not self.streaming_state["sent_tools"][current_idx][
"sent_arguments_prefix"] and args_text.startswith(
"{"):
"sent_arguments_prefix"
] and args_text.startswith("{"):
self.streaming_state["sent_tools"][current_idx][
"sent_arguments_prefix"] = True
"sent_arguments_prefix"
] = True
self.streaming_state["sent_tools"][current_idx][
"sent_arguments"] = "{"
"sent_arguments"
] = "{"
# Update streamed_args for backward compatibility
while len(self.streamed_args) <= current_idx:
self.streamed_args.append("")
self.streamed_args[current_idx] += "{"
delta = DeltaMessage(tool_calls=[
DeltaToolCall(
index=current_idx,
function=DeltaFunctionCall(
arguments="{").model_dump(
exclude_none=True), # type: ignore
)
])
delta = DeltaMessage(
tool_calls=[
DeltaToolCall(
index=current_idx,
function=DeltaFunctionCall(
arguments="{"
).model_dump(exclude_none=True), # type: ignore
)
]
)
return delta
# If we need to send more arguments
if args_text.startswith(sent_args):
# Calculate what part of arguments we need to send
args_diff = args_text[len(sent_args):]
args_diff = args_text[len(sent_args) :]
if args_diff:
# Update our state
self.streaming_state["sent_tools"][current_idx][
"sent_arguments"] = args_text
"sent_arguments"
] = args_text
# Update streamed_args for backward compatibility
while len(self.streamed_args) <= current_idx:
self.streamed_args.append("")
self.streamed_args[current_idx] += args_diff
delta = DeltaMessage(tool_calls=[
DeltaToolCall(
index=current_idx,
function=DeltaFunctionCall(
arguments=args_diff).model_dump(
exclude_none=True), # type: ignore
)
])
delta = DeltaMessage(
tool_calls=[
DeltaToolCall(
index=current_idx,
function=DeltaFunctionCall(
arguments=args_diff
).model_dump(exclude_none=True), # type: ignore
)
]
)
return delta
# If the tool's arguments are complete, check if we need to move to the next tool
@@ -513,7 +546,8 @@ class xLAMToolParser(ToolParser):
if current_idx < tool_count - 1:
self.streaming_state["current_tool_index"] += 1
self.current_tool_id = self.streaming_state[
"current_tool_index"] # For compatibility
"current_tool_index"
] # For compatibility
# If we got here, we couldn't determine what to stream next
return None