2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-02-02 14:58:18 -05:00
|
|
|
|
2024-09-27 04:53:17 +05:30
|
|
|
import pytest
|
|
|
|
|
|
2026-01-13 21:01:39 +08:00
|
|
|
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
2024-09-27 04:53:17 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_completion_request_with_no_tools():
|
|
|
|
|
# tools key is not present
|
|
|
|
|
request = ChatCompletionRequest.model_validate(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
|
|
|
"model": "facebook/opt-125m",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert request.tool_choice == "none"
|
|
|
|
|
|
|
|
|
|
# tools key is None
|
|
|
|
|
request = ChatCompletionRequest.model_validate(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
|
|
|
"model": "facebook/opt-125m",
|
|
|
|
|
"tools": None,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert request.tool_choice == "none"
|
|
|
|
|
|
|
|
|
|
# tools key present but empty
|
|
|
|
|
request = ChatCompletionRequest.model_validate(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
|
|
|
"model": "facebook/opt-125m",
|
|
|
|
|
"tools": [],
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert request.tool_choice == "none"
|
|
|
|
|
|
|
|
|
|
|
2025-04-02 16:45:45 +02:00
|
|
|
@pytest.mark.parametrize("tool_choice", ["auto", "required"])
|
|
|
|
|
def test_chat_completion_request_with_tool_choice_but_no_tools(tool_choice):
|
2024-09-27 04:53:17 +05:30
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError, match="When using `tool_choice`, `tools` must be set."
|
|
|
|
|
):
|
|
|
|
|
ChatCompletionRequest.model_validate(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
|
|
|
"model": "facebook/opt-125m",
|
|
|
|
|
"tool_choice": tool_choice,
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-10-05 15:06:22 +01:00
|
|
|
|
2024-09-27 04:53:17 +05:30
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError, match="When using `tool_choice`, `tools` must be set."
|
|
|
|
|
):
|
|
|
|
|
ChatCompletionRequest.model_validate(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
|
|
|
"model": "facebook/opt-125m",
|
2025-04-02 16:45:45 +02:00
|
|
|
"tool_choice": tool_choice,
|
2024-09-27 04:53:17 +05:30
|
|
|
"tools": None,
|
|
|
|
|
}
|
|
|
|
|
)
|