Add pydocstyle to the ruff rules (#52136)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Harry Mellor <hej.mellor@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
Tool call parser for Apertus models.
|
||||
"""Tool call parser for Apertus models.
|
||||
|
||||
Extracts tool calls from the format:
|
||||
<|tools_prefix|>[{"function_name": {"arg1": "value1", ...}}, ...]<|tools_suffix|>
|
||||
@@ -45,8 +44,7 @@ TOOL_CALLS_SUFFIX = "<|tools_suffix|>"
|
||||
|
||||
|
||||
class ApertusToolParser(ToolParser):
|
||||
"""
|
||||
Tool call parser for Apertus models.
|
||||
"""Tool call parser for Apertus models.
|
||||
|
||||
Handles the extraction of tool calls from text in both non-streaming
|
||||
(complete string) and streaming (chunked token) environments.
|
||||
@@ -68,11 +66,11 @@ class ApertusToolParser(ToolParser):
|
||||
"get_weather"
|
||||
>>> info.tool_calls[0].function.arguments
|
||||
'{"city": "Paris"}'
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
|
||||
"""
|
||||
Initializes the ApertusToolParser.
|
||||
"""Initializes the ApertusToolParser.
|
||||
|
||||
Args:
|
||||
tokenizer: The model's tokenizer.
|
||||
@@ -82,6 +80,7 @@ class ApertusToolParser(ToolParser):
|
||||
Raises:
|
||||
ValueError: If the `model_tokenizer`
|
||||
is not successfully passed to the base class.
|
||||
|
||||
"""
|
||||
super().__init__(tokenizer, tools)
|
||||
|
||||
@@ -101,8 +100,7 @@ class ApertusToolParser(ToolParser):
|
||||
self._reset_streaming_state()
|
||||
|
||||
def _reset_streaming_state(self) -> None:
|
||||
"""
|
||||
Resets all streaming state variables for a new completion request.
|
||||
"""Resets all streaming state variables for a new completion request.
|
||||
|
||||
This clears the delta text buffer and resets the pointers used to
|
||||
track the currently streaming tool index and arguments. Called implicitly
|
||||
@@ -116,8 +114,7 @@ class ApertusToolParser(ToolParser):
|
||||
def adjust_request(
|
||||
self, request: ChatCompletionRequest | ResponsesRequest
|
||||
) -> ChatCompletionRequest | ResponsesRequest:
|
||||
"""
|
||||
Adjusts the generation request to ensure special tool tokens are not skipped.
|
||||
"""Adjusts the generation request to ensure special tool tokens are not skipped.
|
||||
|
||||
Forces `skip_special_tokens=False` if tools are actively being evaluated,
|
||||
ensuring the tools special tokens are surfaced to the engine for parsing.
|
||||
@@ -127,6 +124,7 @@ class ApertusToolParser(ToolParser):
|
||||
|
||||
Returns:
|
||||
The potentially modified chat completion request.
|
||||
|
||||
"""
|
||||
request = super().adjust_request(request)
|
||||
if request.tools and request.tool_choice != "none":
|
||||
@@ -134,8 +132,7 @@ class ApertusToolParser(ToolParser):
|
||||
return request
|
||||
|
||||
def _buffer_delta_text(self, delta_text: str) -> str:
|
||||
"""
|
||||
Buffers incoming delta chunks to prevent
|
||||
"""Buffers incoming delta chunks to prevent
|
||||
fragmentation of multi-token special tags.
|
||||
|
||||
If a chunk ends with a partial match of
|
||||
@@ -155,6 +152,7 @@ class ApertusToolParser(ToolParser):
|
||||
"Let me check " # "<|tool" is buffered internally
|
||||
>>> parser._buffer_delta_text("s_prefix|>" \
|
||||
"<|tools_prefix|>" # Buffer released on completion
|
||||
|
||||
"""
|
||||
self.buffered_delta_text += delta_text
|
||||
text = self.buffered_delta_text
|
||||
@@ -178,8 +176,7 @@ class ApertusToolParser(ToolParser):
|
||||
model_output: str,
|
||||
request: ChatCompletionRequest,
|
||||
) -> ExtractedToolCallInformation:
|
||||
"""
|
||||
Extracts tool calls from a completely generated model response (Non-Streaming).
|
||||
"""Extract tool calls from a complete model response (non-streaming).
|
||||
|
||||
Args:
|
||||
model_output: The full completion string generated by the model.
|
||||
@@ -200,6 +197,7 @@ class ApertusToolParser(ToolParser):
|
||||
'Let me see.'
|
||||
>>> info.tool_calls[0].function.name
|
||||
'get_weather'
|
||||
|
||||
"""
|
||||
match = self.tool_call_regex.search(model_output)
|
||||
if not match:
|
||||
@@ -280,8 +278,7 @@ class ApertusToolParser(ToolParser):
|
||||
delta_token_ids: Sequence[int],
|
||||
request: ChatCompletionRequest,
|
||||
) -> DeltaMessage | None:
|
||||
"""
|
||||
Handles streaming chunks
|
||||
"""Handles streaming chunks.
|
||||
|
||||
Args:
|
||||
previous_text: The complete model text generated prior to this chunk.
|
||||
@@ -305,6 +302,7 @@ class ApertusToolParser(ToolParser):
|
||||
... )
|
||||
>>> msg.tool_calls[0].function.arguments
|
||||
'ation": "Paris"}'
|
||||
|
||||
"""
|
||||
delta_text = self._buffer_delta_text(delta_text)
|
||||
if not delta_text:
|
||||
@@ -323,8 +321,7 @@ class ApertusToolParser(ToolParser):
|
||||
def _extract_streaming(
|
||||
self, current_text: str, delta_text: str
|
||||
) -> DeltaMessage | None:
|
||||
"""
|
||||
Core streaming logic.
|
||||
"""Core streaming logic.
|
||||
Separates visible chat text from JSON blocks and computes diffs.
|
||||
|
||||
Args:
|
||||
@@ -333,6 +330,7 @@ class ApertusToolParser(ToolParser):
|
||||
|
||||
Returns:
|
||||
A `DeltaMessage` containing the `content` delta and/or `tool_calls` delta.
|
||||
|
||||
"""
|
||||
prefix_idx = current_text.rfind(TOOL_CALLS_PREFIX)
|
||||
suffix_idx = current_text.rfind(TOOL_CALLS_SUFFIX)
|
||||
@@ -404,8 +402,7 @@ class ApertusToolParser(ToolParser):
|
||||
def _parse_and_diff_json(
|
||||
self, json_str: str, is_final: bool
|
||||
) -> list[DeltaToolCall]:
|
||||
"""
|
||||
Parses an isolated, potentially incomplete streaming JSON array and returns
|
||||
"""Parses an isolated, potentially incomplete streaming JSON array and returns
|
||||
newly accumulated tool call diffs.
|
||||
|
||||
Args:
|
||||
@@ -417,6 +414,7 @@ class ApertusToolParser(ToolParser):
|
||||
A list of `DeltaToolCall`
|
||||
items representing string diffs in function arguments
|
||||
to stream back to the client.
|
||||
|
||||
"""
|
||||
try:
|
||||
parsed, _ = partial_json_loads(json_str, Allow.ALL)
|
||||
@@ -460,8 +458,7 @@ class ApertusToolParser(ToolParser):
|
||||
def _emit_tool_name(
|
||||
self, parsed: list, index: int, tool_calls: list[DeltaToolCall]
|
||||
) -> None:
|
||||
"""
|
||||
Extracts and emits the function name mapped to a new tool call ID.
|
||||
"""Extracts and emits the function name mapped to a new tool call ID.
|
||||
|
||||
Args:
|
||||
parsed: The partially parsed JSON list containing tool dictionaries.
|
||||
@@ -472,6 +469,7 @@ class ApertusToolParser(ToolParser):
|
||||
Appends `DeltaToolCall(index=0,
|
||||
function=DeltaFunctionCall(name="get_weather", ...))`
|
||||
to the `tool_calls` list and marks the name as sent.
|
||||
|
||||
"""
|
||||
obj = parsed[index]
|
||||
if isinstance(obj, dict) and obj:
|
||||
@@ -491,8 +489,7 @@ class ApertusToolParser(ToolParser):
|
||||
def _get_tool_diff(
|
||||
self, parsed: list, index: int, is_final: bool
|
||||
) -> DeltaToolCall | None:
|
||||
"""
|
||||
Calculates the exact string difference to safely append new tool parameters.
|
||||
"""Calculates the exact string difference to safely append new tool parameters.
|
||||
|
||||
This ensures characters like `{`, `}`, and `"` don't jump around unevenly
|
||||
in the UI frontend while streaming incomplete JSON arguments.
|
||||
@@ -513,6 +510,7 @@ class ApertusToolParser(ToolParser):
|
||||
>>> # Returns diff (closing bracket suppressed until final):
|
||||
>>> parser._get_tool_diff(parsed, index=0, is_final=False)
|
||||
DeltaToolCall(index=0, function=DeltaFunctionCall(arguments='s'))
|
||||
|
||||
"""
|
||||
obj = parsed[index]
|
||||
if not isinstance(obj, dict) or not obj:
|
||||
|
||||
Reference in New Issue
Block a user