[Bugfix][Frontend] Report named Anthropic tool calls as tool_use (#47598)
Signed-off-by: Ting Sun <suntcrick@gmail.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
@@ -37,6 +37,8 @@ from vllm.entrypoints.generate.base.protocol import (
|
||||
DeltaFunctionCall,
|
||||
DeltaMessage,
|
||||
DeltaToolCall,
|
||||
FunctionCall,
|
||||
ToolCall,
|
||||
)
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionResponse,
|
||||
@@ -999,11 +1001,6 @@ class TestInlineSystemMessageInMessagesArray:
|
||||
|
||||
def _make_stream_converter():
|
||||
obj = MagicMock(spec=AnthropicServingMessages)
|
||||
obj.stop_reason_map = {
|
||||
"stop": "end_turn",
|
||||
"length": "max_tokens",
|
||||
"tool_calls": "tool_use",
|
||||
}
|
||||
obj.message_stream_converter = (
|
||||
AnthropicServingMessages.message_stream_converter.__get__(obj)
|
||||
)
|
||||
@@ -1671,6 +1668,78 @@ class TestStopSequenceReason:
|
||||
assert msg_deltas[0]["delta"]["stop_sequence"] is None
|
||||
|
||||
|
||||
class TestToolUseStopReason:
|
||||
"""A response carrying a tool_use block reports ``stop_reason="tool_use"``
|
||||
even when the chat layer says ``finish_reason="stop"``, as it does for a
|
||||
forced (named) tool. A call cut short by max_tokens or a stop string does not.
|
||||
"""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("finish_reason", "stop_reason", "expected"),
|
||||
[
|
||||
("stop", None, "tool_use"),
|
||||
("tool_calls", "</x>", "tool_use"),
|
||||
],
|
||||
)
|
||||
def test_non_streaming(self, finish_reason, stop_reason, expected):
|
||||
tool_call = ToolCall(
|
||||
function=FunctionCall(name="get_weather", arguments='{"city": "Paris"}')
|
||||
)
|
||||
response = ChatCompletionResponse(
|
||||
id="chatcmpl-test",
|
||||
model="test-model",
|
||||
choices=[
|
||||
ChatCompletionResponseChoice(
|
||||
index=0,
|
||||
message=ChatMessage(role="assistant", tool_calls=[tool_call]),
|
||||
finish_reason=finish_reason,
|
||||
stop_reason=stop_reason,
|
||||
)
|
||||
],
|
||||
usage=UsageInfo(prompt_tokens=5, total_tokens=8, completion_tokens=3),
|
||||
)
|
||||
|
||||
result = _make_full_converter().messages_full_converter(response)
|
||||
|
||||
assert result.content[0].type == "tool_use"
|
||||
assert result.stop_reason == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("args", "finish_reason", "stop_reason", "expected"),
|
||||
[
|
||||
('{"city": "Paris"}', "stop", None, "tool_use"),
|
||||
('{"city": "Pa', "length", None, "max_tokens"),
|
||||
('{"city": "Paris"}', "tool_calls", "</x>", "tool_use"),
|
||||
('{"city": "Pa', "tool_calls", "</x>", "stop_sequence"),
|
||||
],
|
||||
)
|
||||
async def test_streaming(self, args, finish_reason, stop_reason, expected):
|
||||
async def sse_input():
|
||||
yield _make_stream_chunk(delta=DeltaMessage(role="assistant"))
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(
|
||||
tool_calls=[_tc(id="call_1", name="get_weather", args=args)]
|
||||
)
|
||||
)
|
||||
yield _make_stream_chunk(
|
||||
finish_reason=finish_reason, stop_reason=stop_reason
|
||||
)
|
||||
yield _make_stream_chunk(
|
||||
choices=[],
|
||||
usage=UsageInfo(prompt_tokens=5, total_tokens=8, completion_tokens=3),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
events = _parse_sse_events(
|
||||
[e async for e in converter.message_stream_converter(sse_input())]
|
||||
)
|
||||
|
||||
msg_deltas = [data for ev_type, data in events if ev_type == "message_delta"]
|
||||
assert msg_deltas[0]["delta"]["stop_reason"] == expected
|
||||
|
||||
|
||||
# ======================================================================
|
||||
# Client-caused errors are 4xx, not 500 (Issue #52088)
|
||||
# ======================================================================
|
||||
|
||||
Reference in New Issue
Block a user