[Frontend] Support OpenAI Responses API namespace tools (#47024)
Signed-off-by: zhongjing123 <jimzhong5193@gmail.com> Co-authored-by: zhongjing123 <jimzhong5193@gmail.com>
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import json
|
||||
|
||||
import openai # use the official client for correctness check
|
||||
import pytest
|
||||
|
||||
MODEL_NAME = "Qwen/Qwen3-1.7B"
|
||||
NAMESPACE = "mcp__computer_use"
|
||||
TOOL_NAME = "get_app_state"
|
||||
FLAT_TOOL_NAME = f"{NAMESPACE}__{TOOL_NAME}"
|
||||
|
||||
tools = [
|
||||
{
|
||||
"type": "namespace",
|
||||
"name": NAMESPACE,
|
||||
"description": "Computer control tools.",
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": TOOL_NAME,
|
||||
"description": "Get the current state of a desktop application.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"app": {
|
||||
"type": "string",
|
||||
"description": "Application name, for example Chrome.",
|
||||
}
|
||||
},
|
||||
"required": ["app"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
prompt = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Use the computer app state tool to inspect Google Chrome.",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def _assert_namespace_tool_call(tool_call) -> None:
|
||||
assert tool_call.type == "function_call"
|
||||
assert tool_call.name == TOOL_NAME
|
||||
assert tool_call.namespace == NAMESPACE
|
||||
assert tool_call.name != FLAT_TOOL_NAME
|
||||
|
||||
args = json.loads(tool_call.arguments)
|
||||
assert args["app"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
||||
async def test_namespace_tool_separator(client: openai.AsyncOpenAI, model_name: str):
|
||||
response = await client.responses.create(
|
||||
model=model_name,
|
||||
input=prompt,
|
||||
tools=tools,
|
||||
temperature=0.0,
|
||||
)
|
||||
|
||||
assert len(response.output) >= 1
|
||||
tool_call = next(
|
||||
(out for out in response.output if out.type == "function_call"), None
|
||||
)
|
||||
assert tool_call is not None
|
||||
_assert_namespace_tool_call(tool_call)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
||||
async def test_namespace_tool_separator_streaming(
|
||||
client: openai.AsyncOpenAI, model_name: str
|
||||
):
|
||||
stream = await client.responses.create(
|
||||
model=model_name,
|
||||
input=prompt,
|
||||
tools=tools,
|
||||
temperature=0.0,
|
||||
stream=True,
|
||||
)
|
||||
events = [event async for event in stream]
|
||||
|
||||
added_call = next(
|
||||
(
|
||||
event.item
|
||||
for event in events
|
||||
if event.type == "response.output_item.added"
|
||||
and getattr(event.item, "type", None) == "function_call"
|
||||
),
|
||||
None,
|
||||
)
|
||||
done_call = next(
|
||||
(
|
||||
event.item
|
||||
for event in events
|
||||
if event.type == "response.output_item.done"
|
||||
and getattr(event.item, "type", None) == "function_call"
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
assert added_call is not None
|
||||
assert added_call.name == TOOL_NAME
|
||||
assert added_call.namespace == NAMESPACE
|
||||
|
||||
assert done_call is not None
|
||||
_assert_namespace_tool_call(done_call)
|
||||
@@ -7,12 +7,16 @@ import json
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from openai.types.responses import ResponseFunctionToolCall
|
||||
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionToolsParam,
|
||||
FunctionDefinition,
|
||||
)
|
||||
from vllm.entrypoints.openai.engine.protocol import FunctionCall
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
from vllm.entrypoints.openai.responses.utils import build_response_output_items
|
||||
from vllm.tokenizers import get_tokenizer
|
||||
from vllm.tool_parsers.glm47_moe_tool_parser import Glm47MoeModelToolParser
|
||||
|
||||
@@ -58,7 +62,69 @@ def mock_request(sample_tools) -> ChatCompletionRequest:
|
||||
return request
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def namespace_tool_request() -> ResponsesRequest:
|
||||
return ResponsesRequest.model_validate(
|
||||
{
|
||||
"input": "hi",
|
||||
"tools": [
|
||||
{
|
||||
"type": "namespace",
|
||||
"name": "mcp__computer_use",
|
||||
"description": "Computer use tools.",
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "get_app_state",
|
||||
"description": "Get app state.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"app": {"type": "string"},
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class TestGlm47ExtractToolCalls:
|
||||
def test_namespace_tool_call_round_trip_to_responses_output(
|
||||
self, glm47_tokenizer, namespace_tool_request
|
||||
):
|
||||
parser = Glm47MoeModelToolParser(
|
||||
glm47_tokenizer, tools=namespace_tool_request.tools
|
||||
)
|
||||
out = (
|
||||
"<tool_call>mcp__computer_use__get_app_state"
|
||||
"<arg_key>app</arg_key>"
|
||||
"<arg_value>Google Chrome</arg_value>"
|
||||
"</tool_call>"
|
||||
)
|
||||
|
||||
result = parser.extract_tool_calls(out, request=namespace_tool_request)
|
||||
|
||||
assert result.tools_called
|
||||
tool_call = result.tool_calls[0].function
|
||||
assert tool_call == FunctionCall(
|
||||
name="mcp__computer_use__get_app_state",
|
||||
arguments='{"app": "Google Chrome"}',
|
||||
)
|
||||
|
||||
output_items = build_response_output_items(
|
||||
reasoning=None,
|
||||
content=None,
|
||||
tool_calls=[tool_call],
|
||||
tools=namespace_tool_request.tools,
|
||||
)
|
||||
output_tool_call = output_items[0]
|
||||
assert isinstance(output_tool_call, ResponseFunctionToolCall)
|
||||
assert output_tool_call.name == "get_app_state"
|
||||
assert output_tool_call.namespace == "mcp__computer_use"
|
||||
|
||||
def test_no_tool_call(self, glm47_tool_parser, mock_request):
|
||||
out = "This is a plain response."
|
||||
r = glm47_tool_parser.extract_tool_calls(out, request=mock_request)
|
||||
|
||||
Reference in New Issue
Block a user