From 7aa40bf05fa7cc502c4648d89eeff26b8c97918e Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Wed, 18 Sep 2024 20:44:06 +0530 Subject: [PATCH 1/4] Only set tool_choice `auto` if at least one tool is provided --- vllm/entrypoints/openai/protocol.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vllm/entrypoints/openai/protocol.py b/vllm/entrypoints/openai/protocol.py index 7e9f53b1816d..d20243bba9ec 100644 --- a/vllm/entrypoints/openai/protocol.py +++ b/vllm/entrypoints/openai/protocol.py @@ -381,11 +381,12 @@ def check_tool_usage(cls, data): # if "tool_choice" is not specified but tools are provided, # default to "auto" tool_choice - if "tool_choice" not in data and "tools" in data: - data["tool_choice"] = "auto" + if "tool_choice" not in data: + if data.get("tools"): + data["tool_choice"] = "auto" # if "tool_choice" is specified -- validation - if "tool_choice" in data: + elif "tool_choice" in data: # ensure that if "tool choice" is specified, tools are present if "tools" not in data or data["tools"] is None: From d2bbf5e69518c648824c21e1d70f9be3c7702fe2 Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Thu, 19 Sep 2024 15:09:15 +0530 Subject: [PATCH 2/4] Apply suggestion from code review --- vllm/entrypoints/openai/protocol.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/vllm/entrypoints/openai/protocol.py b/vllm/entrypoints/openai/protocol.py index d20243bba9ec..359012611a34 100644 --- a/vllm/entrypoints/openai/protocol.py +++ b/vllm/entrypoints/openai/protocol.py @@ -381,12 +381,11 @@ def check_tool_usage(cls, data): # if "tool_choice" is not specified but tools are provided, # default to "auto" tool_choice - if "tool_choice" not in data: - if data.get("tools"): - data["tool_choice"] = "auto" + if "tool_choice" not in data and data.get("tools"): + data["tool_choice"] = "auto" # if "tool_choice" is specified -- validation - elif "tool_choice" in data: + if "tool_choice" in data: # ensure that if "tool choice" is specified, tools are present if "tools" not in data or data["tools"] is None: From 203d07e30b737f87bc7218b7520b394f15c9cff5 Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Wed, 25 Sep 2024 00:55:42 +0530 Subject: [PATCH 3/4] Add tests --- ...est_chat_completion_request_validations.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/tool_use/test_chat_completion_request_validations.py diff --git a/tests/tool_use/test_chat_completion_request_validations.py b/tests/tool_use/test_chat_completion_request_validations.py new file mode 100644 index 000000000000..2c33c2409db6 --- /dev/null +++ b/tests/tool_use/test_chat_completion_request_validations.py @@ -0,0 +1,70 @@ +import pytest +from vllm.entrypoints.openai.protocol import ChatCompletionRequest + + +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' + + +def test_chat_completion_request_with_tool_choice_but_no_tools(): + 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': + 'auto' + }) + + 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': + 'auto', + 'tools': + None + }) From eae161c33f47a0b760701769f56fc249563f8ed0 Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Wed, 25 Sep 2024 02:18:19 +0530 Subject: [PATCH 4/4] Fix isort lint issues --- tests/tool_use/test_chat_completion_request_validations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/tool_use/test_chat_completion_request_validations.py b/tests/tool_use/test_chat_completion_request_validations.py index 2c33c2409db6..3d0fe8f06089 100644 --- a/tests/tool_use/test_chat_completion_request_validations.py +++ b/tests/tool_use/test_chat_completion_request_validations.py @@ -1,4 +1,5 @@ import pytest + from vllm.entrypoints.openai.protocol import ChatCompletionRequest