Convert formatting to use ruff instead of yapf + isort (#26247)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -8,8 +8,10 @@ import pytest
|
||||
import regex as re
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
from vllm.entrypoints.openai.protocol import (ChatCompletionRequest,
|
||||
ChatCompletionToolsParam)
|
||||
from vllm.entrypoints.openai.protocol import (
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionToolsParam,
|
||||
)
|
||||
from vllm.entrypoints.openai.serving_chat import OpenAIServingChat
|
||||
|
||||
pytestmark = pytest.mark.cpu_test
|
||||
@@ -24,18 +26,16 @@ EXAMPLE_TOOLS = [
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type":
|
||||
"string",
|
||||
"description":
|
||||
"The city to find the weather for"
|
||||
"type": "string",
|
||||
"description": "The city to find the weather for"
|
||||
", e.g. 'San Francisco'",
|
||||
},
|
||||
},
|
||||
"required": ["city"],
|
||||
"additionalProperties": False
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
"strict": True
|
||||
"strict": True,
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
@@ -46,35 +46,33 @@ EXAMPLE_TOOLS = [
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type":
|
||||
"string",
|
||||
"description":
|
||||
"The city to get the forecast for, e.g. 'New York'",
|
||||
"type": "string",
|
||||
"description": "The city to get the forecast for, e.g. 'New York'",
|
||||
},
|
||||
"days": {
|
||||
"type":
|
||||
"integer",
|
||||
"description":
|
||||
"Number of days to get the forecast for (1-7)",
|
||||
"type": "integer",
|
||||
"description": "Number of days to get the forecast for (1-7)",
|
||||
},
|
||||
},
|
||||
"required": ["city", "days"],
|
||||
"additionalProperties": False
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
"strict": True
|
||||
"strict": True,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def _compile_and_check(tools: list[ChatCompletionToolsParam], sample_output,
|
||||
should_match: bool):
|
||||
def _compile_and_check(
|
||||
tools: list[ChatCompletionToolsParam], sample_output, should_match: bool
|
||||
):
|
||||
self = MagicMock(tool_choice="required", tools=tools)
|
||||
schema = ChatCompletionRequest._get_json_schema_from_tool(self)
|
||||
assert isinstance(schema, dict)
|
||||
|
||||
# use build_regex_from_schema used in JSONLogitsProcessor to create Guide
|
||||
from outlines_core.json_schema import build_regex_from_schema
|
||||
|
||||
regex = build_regex_from_schema(json.dumps(schema))
|
||||
compiled = re.compile(regex)
|
||||
matches = compiled.fullmatch(json.dumps(sample_output)) is not None
|
||||
@@ -83,65 +81,31 @@ def _compile_and_check(tools: list[ChatCompletionToolsParam], sample_output,
|
||||
|
||||
|
||||
VALID_TOOL_OUTPUTS = [
|
||||
([{
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
}
|
||||
}], True),
|
||||
([{
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
}
|
||||
}, {
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Berlin"
|
||||
}
|
||||
}], True),
|
||||
([{
|
||||
"name": "get_forecast",
|
||||
"parameters": {
|
||||
"city": "Vienna",
|
||||
"days": 7
|
||||
}
|
||||
}], True),
|
||||
([{
|
||||
"name": "get_forecast",
|
||||
"parameters": {
|
||||
"city": "Vienna",
|
||||
"days": 7
|
||||
}
|
||||
}, {
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
}
|
||||
}], True),
|
||||
([{
|
||||
"name": "get_forecast",
|
||||
"parameters": {
|
||||
"city": "Vienna",
|
||||
"days": 7
|
||||
}
|
||||
}, {
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
}
|
||||
}, {
|
||||
"name": "get_forecast",
|
||||
"parameters": {
|
||||
"city": "Berlin",
|
||||
"days": 7
|
||||
}
|
||||
}, {
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Berlin"
|
||||
}
|
||||
}], True),
|
||||
([{"name": "get_current_weather", "parameters": {"city": "Vienna"}}], True),
|
||||
(
|
||||
[
|
||||
{"name": "get_current_weather", "parameters": {"city": "Vienna"}},
|
||||
{"name": "get_current_weather", "parameters": {"city": "Berlin"}},
|
||||
],
|
||||
True,
|
||||
),
|
||||
([{"name": "get_forecast", "parameters": {"city": "Vienna", "days": 7}}], True),
|
||||
(
|
||||
[
|
||||
{"name": "get_forecast", "parameters": {"city": "Vienna", "days": 7}},
|
||||
{"name": "get_current_weather", "parameters": {"city": "Vienna"}},
|
||||
],
|
||||
True,
|
||||
),
|
||||
(
|
||||
[
|
||||
{"name": "get_forecast", "parameters": {"city": "Vienna", "days": 7}},
|
||||
{"name": "get_current_weather", "parameters": {"city": "Vienna"}},
|
||||
{"name": "get_forecast", "parameters": {"city": "Berlin", "days": 7}},
|
||||
{"name": "get_current_weather", "parameters": {"city": "Berlin"}},
|
||||
],
|
||||
True,
|
||||
),
|
||||
]
|
||||
|
||||
VALID_TOOLS = [t[0] for t in VALID_TOOL_OUTPUTS]
|
||||
@@ -149,92 +113,100 @@ VALID_TOOLS = [t[0] for t in VALID_TOOL_OUTPUTS]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"sample_output, should_match",
|
||||
VALID_TOOL_OUTPUTS + [
|
||||
VALID_TOOL_OUTPUTS
|
||||
+ [
|
||||
(None, False),
|
||||
([], False), # empty list cannot be generated
|
||||
({}, False), # empty object cannot be generated
|
||||
([{}], False), # list with empty object cannot be generated
|
||||
(
|
||||
[{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather"
|
||||
}],
|
||||
False),
|
||||
[
|
||||
{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather"
|
||||
}
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {}
|
||||
}],
|
||||
False),
|
||||
[
|
||||
{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {},
|
||||
}
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": None
|
||||
}],
|
||||
False),
|
||||
[
|
||||
{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": None,
|
||||
}
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ # tool call without lists cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
}
|
||||
"parameters": {"city": "Vienna"},
|
||||
},
|
||||
False),
|
||||
False,
|
||||
),
|
||||
(
|
||||
[{ # tool call with extra parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna",
|
||||
"extra": "value"
|
||||
[
|
||||
{ # tool call with extra parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {"city": "Vienna", "extra": "value"},
|
||||
}
|
||||
}],
|
||||
False),
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[{ # tool call where parameters are first cannot be generated
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
},
|
||||
"name": "get_current_weather"
|
||||
}],
|
||||
False),
|
||||
(
|
||||
[{ # tool call without all required parameters cannot be generated
|
||||
"name": "get_forecast",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
[
|
||||
{ # tool call where parameters are first cannot be generated
|
||||
"parameters": {"city": "Vienna"},
|
||||
"name": "get_current_weather",
|
||||
}
|
||||
}],
|
||||
False),
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[
|
||||
{ # tool call without all required parameters cannot be generated
|
||||
"name": "get_forecast",
|
||||
"parameters": {"city": "Vienna"},
|
||||
}
|
||||
],
|
||||
False,
|
||||
),
|
||||
( # tool call with incorrect name/parameters cannot be generated
|
||||
[{
|
||||
"name": "get_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna",
|
||||
"days": 7
|
||||
}
|
||||
}], False),
|
||||
[{"name": "get_weather", "parameters": {"city": "Vienna", "days": 7}}],
|
||||
False,
|
||||
),
|
||||
( # tool call with both valid and empty function cannot be generated
|
||||
[{
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"city": "Vienna"
|
||||
}
|
||||
}, {}], False),
|
||||
])
|
||||
[{"name": "get_current_weather", "parameters": {"city": "Vienna"}}, {}],
|
||||
False,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_structured_outputs_json(sample_output, should_match):
|
||||
_compile_and_check(tools=TypeAdapter(
|
||||
list[ChatCompletionToolsParam]).validate_python(EXAMPLE_TOOLS),
|
||||
sample_output=sample_output,
|
||||
should_match=should_match)
|
||||
_compile_and_check(
|
||||
tools=TypeAdapter(list[ChatCompletionToolsParam]).validate_python(
|
||||
EXAMPLE_TOOLS
|
||||
),
|
||||
sample_output=sample_output,
|
||||
should_match=should_match,
|
||||
)
|
||||
|
||||
|
||||
def update_parameters_none(
|
||||
tool: ChatCompletionToolsParam) -> ChatCompletionToolsParam:
|
||||
def update_parameters_none(tool: ChatCompletionToolsParam) -> ChatCompletionToolsParam:
|
||||
tool.function.parameters = None
|
||||
return tool
|
||||
|
||||
|
||||
def update_parameters_empty_dict(
|
||||
tool: ChatCompletionToolsParam) -> ChatCompletionToolsParam:
|
||||
tool: ChatCompletionToolsParam,
|
||||
) -> ChatCompletionToolsParam:
|
||||
tool.function.parameters = {}
|
||||
return tool
|
||||
|
||||
@@ -247,48 +219,60 @@ def update_parameters_empty_dict(
|
||||
({}, False), # empty object cannot be generated
|
||||
([{}], False), # list with empty object cannot be generated
|
||||
(
|
||||
[{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather"
|
||||
}],
|
||||
False),
|
||||
(
|
||||
[{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": None
|
||||
}],
|
||||
False),
|
||||
(
|
||||
[{ # function with extra parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {
|
||||
"extra": "value"
|
||||
[
|
||||
{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather"
|
||||
}
|
||||
}],
|
||||
False),
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[{ # only function with empty parameters object is valid
|
||||
"name": "get_current_weather",
|
||||
"parameters": {}
|
||||
}],
|
||||
True),
|
||||
])
|
||||
[
|
||||
{ # function without required parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": None,
|
||||
}
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[
|
||||
{ # function with extra parameters cannot be generated
|
||||
"name": "get_current_weather",
|
||||
"parameters": {"extra": "value"},
|
||||
}
|
||||
],
|
||||
False,
|
||||
),
|
||||
(
|
||||
[
|
||||
{ # only function with empty parameters object is valid
|
||||
"name": "get_current_weather",
|
||||
"parameters": {},
|
||||
}
|
||||
],
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"update_parameters",
|
||||
[update_parameters_none, update_parameters_empty_dict])
|
||||
def test_structured_outputs_json_without_parameters(sample_output,
|
||||
should_match,
|
||||
update_parameters):
|
||||
"update_parameters", [update_parameters_none, update_parameters_empty_dict]
|
||||
)
|
||||
def test_structured_outputs_json_without_parameters(
|
||||
sample_output, should_match, update_parameters
|
||||
):
|
||||
updated_tools = [deepcopy(EXAMPLE_TOOLS[0])]
|
||||
tools = TypeAdapter(
|
||||
list[ChatCompletionToolsParam]).validate_python(updated_tools)
|
||||
tools = TypeAdapter(list[ChatCompletionToolsParam]).validate_python(updated_tools)
|
||||
tools = list(map(update_parameters, tools))
|
||||
assert all([
|
||||
tool.function.parameters is None or tool.function.parameters == {}
|
||||
for tool in tools
|
||||
])
|
||||
_compile_and_check(tools=tools,
|
||||
sample_output=sample_output,
|
||||
should_match=should_match)
|
||||
assert all(
|
||||
[
|
||||
tool.function.parameters is None or tool.function.parameters == {}
|
||||
for tool in tools
|
||||
]
|
||||
)
|
||||
_compile_and_check(
|
||||
tools=tools, sample_output=sample_output, should_match=should_match
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("output", VALID_TOOLS)
|
||||
@@ -306,7 +290,7 @@ def test_streaming_output_valid(output, empty_params, delta_len):
|
||||
function_name_returned = False
|
||||
messages = []
|
||||
for i in range(0, len(output_json), delta_len):
|
||||
delta_text = output_json[i:i + delta_len]
|
||||
delta_text = output_json[i : i + delta_len]
|
||||
current_text = previous_text + delta_text
|
||||
|
||||
delta_message, function_name_returned = (
|
||||
@@ -315,7 +299,9 @@ def test_streaming_output_valid(output, empty_params, delta_len):
|
||||
previous_text=previous_text,
|
||||
current_text=current_text,
|
||||
delta_text=delta_text,
|
||||
function_name_returned=function_name_returned))
|
||||
function_name_returned=function_name_returned,
|
||||
)
|
||||
)
|
||||
|
||||
if delta_message:
|
||||
messages.append(delta_message)
|
||||
@@ -329,10 +315,12 @@ def test_streaming_output_valid(output, empty_params, delta_len):
|
||||
if len(combined_messages) > 1:
|
||||
combined_messages += "},"
|
||||
|
||||
combined_messages += '{"name": "' + \
|
||||
message.tool_calls[0].function.name + \
|
||||
'", "parameters": ' + \
|
||||
message.tool_calls[0].function.arguments
|
||||
combined_messages += (
|
||||
'{"name": "'
|
||||
+ message.tool_calls[0].function.name
|
||||
+ '", "parameters": '
|
||||
+ message.tool_calls[0].function.arguments
|
||||
)
|
||||
else:
|
||||
combined_messages += message.tool_calls[0].function.arguments
|
||||
combined_messages += "}]"
|
||||
|
||||
Reference in New Issue
Block a user