mirror of
https://git.datalinker.icu/vllm-project/vllm.git
synced 2025-12-18 08:35:01 +08:00
[Frontend] Improve error message in tool_choice validation (#19239)
Signed-off-by: 22quinn <33176974+22quinn@users.noreply.github.com>
This commit is contained in:
parent
3f6341bf7f
commit
7e3e74c97c
@ -700,22 +700,26 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
|||||||
|
|
||||||
# ensure that if "tool_choice" is specified as an object,
|
# ensure that if "tool_choice" is specified as an object,
|
||||||
# it matches a valid tool
|
# it matches a valid tool
|
||||||
|
correct_usage_message = 'Correct usage: `{"type": "function",' \
|
||||||
|
' "function": {"name": "my_function"}}`'
|
||||||
if isinstance(data["tool_choice"], dict):
|
if isinstance(data["tool_choice"], dict):
|
||||||
valid_tool = False
|
valid_tool = False
|
||||||
specified_function = data["tool_choice"].get("function")
|
function = data["tool_choice"].get("function")
|
||||||
if not specified_function:
|
if not isinstance(function, dict):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Expected field `function` in `tool_choice`."
|
f"Invalid value for `function`: `{function}` in "
|
||||||
" Correct usage: `{\"type\": \"function\","
|
f"`tool_choice`! {correct_usage_message}")
|
||||||
" \"function\": {\"name\": \"my_function\"}}`")
|
if "name" not in function:
|
||||||
specified_function_name = specified_function.get("name")
|
raise ValueError(f"Expected field `name` in `function` in "
|
||||||
if not specified_function_name:
|
f"`tool_choice`! {correct_usage_message}")
|
||||||
|
function_name = function["name"]
|
||||||
|
if not isinstance(function_name,
|
||||||
|
str) or len(function_name) == 0:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Expected field `name` in `function` in `tool_choice`."
|
f"Invalid `name` in `function`: `{function_name}`"
|
||||||
"Correct usage: `{\"type\": \"function\", "
|
f" in `tool_choice`! {correct_usage_message}")
|
||||||
"\"function\": {\"name\": \"my_function\"}}`")
|
|
||||||
for tool in data["tools"]:
|
for tool in data["tools"]:
|
||||||
if tool["function"]["name"] == specified_function_name:
|
if tool["function"]["name"] == function_name:
|
||||||
valid_tool = True
|
valid_tool = True
|
||||||
break
|
break
|
||||||
if not valid_tool:
|
if not valid_tool:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user