[Bugfix][Frontend] Accept Anthropic tool_addition and tool_removal content blocks (#57693)
Signed-off-by: Anurag Singh <dr.anuragkr@gmail.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Antigravity <antigravity@google.com> Co-authored-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
co-authored by
Antigravity
sfeng33
parent
e6674262f1
commit
28c57456db
@@ -31,6 +31,8 @@ from vllm.entrypoints.anthropic.protocol import (
|
||||
AnthropicOutputConfig,
|
||||
AnthropicStreamEvent,
|
||||
AnthropicThinkingConfig,
|
||||
AnthropicTool,
|
||||
AnthropicToolChangeDefinition,
|
||||
AnthropicUsage,
|
||||
)
|
||||
from vllm.entrypoints.chat_utils import ChatTemplateContentFormatOption
|
||||
@@ -710,6 +712,51 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _resolve_tools(
|
||||
cls,
|
||||
anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
|
||||
) -> list[AnthropicTool] | None:
|
||||
"""Apply tool_addition/tool_removal blocks in ``messages`` to ``tools``.
|
||||
|
||||
Chat templates see a single tool list, so this resolves the tools in
|
||||
effect at the end of the conversation: a tool added by reference is no
|
||||
longer deferred, a tool added by value is declared, and a removed tool
|
||||
is dropped.
|
||||
"""
|
||||
changes = [
|
||||
block
|
||||
for msg in anthropic_request.messages
|
||||
if not isinstance(msg.content, str)
|
||||
for block in msg.content
|
||||
if block.type in ("tool_addition", "tool_removal")
|
||||
]
|
||||
if not changes:
|
||||
return anthropic_request.tools
|
||||
|
||||
tools = {tool.name: tool for tool in anthropic_request.tools or []}
|
||||
removed: set[str] = set()
|
||||
for block in changes:
|
||||
change = block.tool
|
||||
assert change is not None
|
||||
if isinstance(change, AnthropicToolChangeDefinition):
|
||||
name = change.definition.name
|
||||
tools[name] = change.definition
|
||||
else:
|
||||
name = change.name
|
||||
if name not in tools:
|
||||
raise ValueError(
|
||||
f"{block.type} references tool {name!r}, "
|
||||
"which is not declared in tools"
|
||||
)
|
||||
if block.type == "tool_addition":
|
||||
tools[name] = tools[name].model_copy(update={"defer_loading": None})
|
||||
if block.type == "tool_removal":
|
||||
removed.add(name)
|
||||
else:
|
||||
removed.discard(name)
|
||||
return [tool for name, tool in tools.items() if name not in removed]
|
||||
|
||||
@classmethod
|
||||
def _convert_tools(
|
||||
cls,
|
||||
@@ -717,11 +764,12 @@ class AnthropicServingMessages(OpenAIServingChat):
|
||||
req: ChatCompletionRequest,
|
||||
) -> None:
|
||||
"""Convert Anthropic tools to OpenAI format."""
|
||||
if anthropic_request.tools is None:
|
||||
anthropic_tools = cls._resolve_tools(anthropic_request)
|
||||
if anthropic_tools is None:
|
||||
return
|
||||
|
||||
tools = []
|
||||
for tool in anthropic_request.tools:
|
||||
for tool in anthropic_tools:
|
||||
tools.append(
|
||||
ChatCompletionToolsParam.model_validate(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user