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,6 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Pydantic models for Anthropic API protocol"""
|
||||
"""Pydantic models for Anthropic API protocol."""
|
||||
|
||||
import time
|
||||
from typing import Annotated, Any, Literal
|
||||
@@ -11,21 +11,21 @@ import vllm.envs as envs
|
||||
|
||||
|
||||
class AnthropicError(BaseModel):
|
||||
"""Error structure for Anthropic API"""
|
||||
"""Error structure for Anthropic API."""
|
||||
|
||||
type: str
|
||||
message: str
|
||||
|
||||
|
||||
class AnthropicErrorResponse(BaseModel):
|
||||
"""Error response structure for Anthropic API"""
|
||||
"""Error response structure for Anthropic API."""
|
||||
|
||||
type: Literal["error"] = "error"
|
||||
error: AnthropicError
|
||||
|
||||
|
||||
class AnthropicUsage(BaseModel):
|
||||
"""Token usage information"""
|
||||
"""Token usage information."""
|
||||
|
||||
input_tokens: int
|
||||
output_tokens: int
|
||||
@@ -34,7 +34,7 @@ class AnthropicUsage(BaseModel):
|
||||
|
||||
|
||||
class AnthropicContentBlock(BaseModel):
|
||||
"""Content block in message"""
|
||||
"""Content block in message."""
|
||||
|
||||
type: Literal[
|
||||
"text",
|
||||
@@ -65,14 +65,14 @@ class AnthropicContentBlock(BaseModel):
|
||||
|
||||
|
||||
class AnthropicMessage(BaseModel):
|
||||
"""Message structure"""
|
||||
"""Message structure."""
|
||||
|
||||
role: Literal["user", "assistant", "system"]
|
||||
content: str | list[AnthropicContentBlock]
|
||||
|
||||
|
||||
class AnthropicTool(BaseModel):
|
||||
"""Tool definition"""
|
||||
"""Tool definition."""
|
||||
|
||||
name: str
|
||||
description: str | None = None
|
||||
@@ -91,7 +91,7 @@ class AnthropicTool(BaseModel):
|
||||
|
||||
|
||||
class AnthropicToolChoice(BaseModel):
|
||||
"""Tool Choice definition"""
|
||||
"""Tool Choice definition."""
|
||||
|
||||
type: Literal["auto", "any", "tool", "none"]
|
||||
name: str | None = None
|
||||
@@ -105,7 +105,7 @@ class AnthropicToolChoice(BaseModel):
|
||||
|
||||
|
||||
class AnthropicJsonOutputFormat(BaseModel):
|
||||
"""JSON output format configuration"""
|
||||
"""JSON output format configuration."""
|
||||
|
||||
json_schema: dict[str, Any] | None = Field(default=None, alias="schema")
|
||||
type: Literal["json_schema"] = "json_schema"
|
||||
@@ -119,7 +119,7 @@ class AnthropicOutputConfig(BaseModel):
|
||||
|
||||
|
||||
class AnthropicMessagesRequest(BaseModel):
|
||||
"""Anthropic Messages API request"""
|
||||
"""Anthropic Messages API request."""
|
||||
|
||||
model: str
|
||||
messages: list[AnthropicMessage]
|
||||
@@ -192,7 +192,7 @@ class AnthropicMessagesRequest(BaseModel):
|
||||
|
||||
|
||||
class AnthropicDelta(BaseModel):
|
||||
"""Delta for streaming responses"""
|
||||
"""Delta for streaming responses."""
|
||||
|
||||
type: (
|
||||
Literal["text_delta", "input_json_delta", "thinking_delta", "signature_delta"]
|
||||
@@ -211,7 +211,7 @@ class AnthropicDelta(BaseModel):
|
||||
|
||||
|
||||
class AnthropicStreamEvent(BaseModel):
|
||||
"""Streaming event"""
|
||||
"""Streaming event."""
|
||||
|
||||
type: Literal[
|
||||
"message_start",
|
||||
@@ -232,7 +232,7 @@ class AnthropicStreamEvent(BaseModel):
|
||||
|
||||
|
||||
class AnthropicMessagesResponse(BaseModel):
|
||||
"""Anthropic Messages API response"""
|
||||
"""Anthropic Messages API response."""
|
||||
|
||||
id: str
|
||||
type: Literal["message"] = "message"
|
||||
@@ -265,7 +265,7 @@ class AnthropicContextManagement(BaseModel):
|
||||
|
||||
|
||||
class AnthropicCountTokensRequest(BaseModel):
|
||||
"""Anthropic messages.count_tokens request"""
|
||||
"""Anthropic messages.count_tokens request."""
|
||||
|
||||
model: str
|
||||
messages: list[AnthropicMessage]
|
||||
@@ -291,7 +291,7 @@ class AnthropicCountTokensRequest(BaseModel):
|
||||
|
||||
|
||||
class AnthropicCountTokensResponse(BaseModel):
|
||||
"""Anthropic messages.count_tokens response"""
|
||||
"""Anthropic messages.count_tokens response."""
|
||||
|
||||
input_tokens: int
|
||||
context_management: AnthropicContextManagement | None = None
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# Adapted from
|
||||
# https://github.com/vllm-project/vllm/blob/main/vllm/entrypoints/openai/chat_completion/serving.py
|
||||
|
||||
"""Anthropic Messages API serving handler"""
|
||||
"""Anthropic Messages API serving handler."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
@@ -96,7 +96,7 @@ def wrap_data_with_event(data: str, event: str):
|
||||
|
||||
|
||||
class AnthropicServingMessages(OpenAIServingChat):
|
||||
"""Handler for Anthropic Messages API requests"""
|
||||
"""Handler for Anthropic Messages API requests."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -196,7 +196,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
*,
|
||||
merge_inline_system: bool = False,
|
||||
) -> ChatCompletionRequest:
|
||||
"""Convert Anthropic message format to OpenAI format"""
|
||||
"""Convert Anthropic message format to OpenAI format."""
|
||||
openai_messages: list[dict[str, Any]] = []
|
||||
|
||||
cls._convert_system_message(
|
||||
@@ -224,7 +224,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
*,
|
||||
merge_inline_system: bool = False,
|
||||
) -> None:
|
||||
"""Convert Anthropic system message to OpenAI format"""
|
||||
"""Convert Anthropic system message to OpenAI format."""
|
||||
system_parts: list[str] = []
|
||||
|
||||
# Top-level system field
|
||||
@@ -278,7 +278,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
*,
|
||||
merge_inline_system: bool = False,
|
||||
) -> None:
|
||||
"""Convert Anthropic messages to OpenAI format"""
|
||||
"""Convert Anthropic messages to OpenAI format."""
|
||||
for msg in messages:
|
||||
# Handle system messages in-place: extract text, strip billing
|
||||
# headers, and only emit if there is real content. This avoids
|
||||
@@ -310,7 +310,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
openai_msg: dict[str, Any],
|
||||
openai_messages: list[dict[str, Any]],
|
||||
) -> None:
|
||||
"""Convert complex message content blocks"""
|
||||
"""Convert complex message content blocks."""
|
||||
content_parts: list[dict[str, Any]] = []
|
||||
tool_calls: list[dict[str, Any]] = []
|
||||
reasoning_parts: list[str] = []
|
||||
@@ -349,7 +349,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
reasoning_parts: list[str],
|
||||
openai_messages: list[dict[str, Any]],
|
||||
) -> None:
|
||||
"""Convert individual content block"""
|
||||
"""Convert individual content block."""
|
||||
if block.type == "text" and block.text:
|
||||
content_parts.append({"type": "text", "text": block.text})
|
||||
elif block.type == "image" and block.source:
|
||||
@@ -374,7 +374,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
|
||||
@classmethod
|
||||
def _convert_tool_use_block(cls, block, tool_calls: list[dict[str, Any]]) -> None:
|
||||
"""Convert tool_use block to OpenAI function call format"""
|
||||
"""Convert tool_use block to OpenAI function call format."""
|
||||
tool_call = {
|
||||
"id": block.id or f"call_{int(time.time())}",
|
||||
"type": "function",
|
||||
@@ -393,7 +393,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
openai_messages: list[dict[str, Any]],
|
||||
content_parts: list[dict[str, Any]],
|
||||
) -> None:
|
||||
"""Convert tool_result block to OpenAI format"""
|
||||
"""Convert tool_result block to OpenAI format."""
|
||||
if role == "user":
|
||||
cls._convert_user_tool_result(block, openai_messages)
|
||||
else:
|
||||
@@ -406,7 +406,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
def _convert_user_tool_result(
|
||||
cls, block, openai_messages: list[dict[str, Any]]
|
||||
) -> None:
|
||||
"""Convert user tool_result with text and image support"""
|
||||
"""Convert user tool_result with text and image support."""
|
||||
tool_text = ""
|
||||
tool_image_urls: list[str] = []
|
||||
tool_reference: list[dict[str, Any]] = []
|
||||
@@ -468,7 +468,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
|
||||
openai_messages: list[dict[str, Any]],
|
||||
) -> ChatCompletionRequest:
|
||||
"""Build base ChatCompletionRequest"""
|
||||
"""Build base ChatCompletionRequest."""
|
||||
if isinstance(anthropic_request, AnthropicCountTokensRequest):
|
||||
return ChatCompletionRequest(
|
||||
model=anthropic_request.model,
|
||||
@@ -498,7 +498,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
req: ChatCompletionRequest,
|
||||
anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
|
||||
) -> None:
|
||||
"""Handle output configuration such as output format and effort"""
|
||||
"""Handle output configuration such as output format and effort."""
|
||||
if isinstance(anthropic_request, AnthropicCountTokensRequest):
|
||||
return
|
||||
output_config: AnthropicOutputConfig | None = anthropic_request.output_config
|
||||
@@ -519,7 +519,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
req: ChatCompletionRequest,
|
||||
anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
|
||||
) -> None:
|
||||
"""Handle streaming configuration"""
|
||||
"""Handle streaming configuration."""
|
||||
if isinstance(anthropic_request, AnthropicCountTokensRequest):
|
||||
return
|
||||
if anthropic_request.stream:
|
||||
@@ -534,7 +534,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
|
||||
req: ChatCompletionRequest,
|
||||
) -> None:
|
||||
"""Convert Anthropic tool_choice to OpenAI format"""
|
||||
"""Convert Anthropic tool_choice to OpenAI format."""
|
||||
if anthropic_request.tool_choice is None:
|
||||
req.tool_choice = None
|
||||
return
|
||||
@@ -563,7 +563,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
|
||||
req: ChatCompletionRequest,
|
||||
) -> None:
|
||||
"""Convert Anthropic tools to OpenAI format"""
|
||||
"""Convert Anthropic tools to OpenAI format."""
|
||||
if anthropic_request.tools is None:
|
||||
return
|
||||
|
||||
@@ -593,8 +593,7 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
request: AnthropicMessagesRequest,
|
||||
raw_request: Request | None = None,
|
||||
) -> AsyncGenerator[str, None] | AnthropicMessagesResponse | ErrorResponse:
|
||||
"""
|
||||
Messages API similar to Anthropic's API.
|
||||
"""Messages API similar to Anthropic's API.
|
||||
|
||||
See https://docs.anthropic.com/en/api/messages
|
||||
for the API specification. This API mimics the Anthropic messages API.
|
||||
|
||||
Reference in New Issue
Block a user