[Bugfix] Internal Server Error when tool_choice is incorrect. (#10567)

Signed-off-by: Varun Shenoy <varun.vinayak.shenoy@oracle.com>
This commit is contained in:
Varun Vinayak Shenoy
2024-11-22 21:13:29 -08:00
committed by GitHub
parent 4aba6e3d1a
commit 7d8ffb344f
2 changed files with 20 additions and 6 deletions

View File

@@ -478,17 +478,17 @@ class ChatCompletionRequest(OpenAIBaseModel):
# it matches a valid tool
if isinstance(data["tool_choice"], dict):
valid_tool = False
specified_function = data["tool_choice"]["function"]
specified_function = data["tool_choice"].get("function")
if not specified_function:
raise ValueError(
"Incorrectly formatted `tool_choice`. Should be like "
"`{\"type\": \"function\","
"Expected field `function` in `tool_choice`."
" Correct usage: `{\"type\": \"function\","
" \"function\": {\"name\": \"my_function\"}}`")
specified_function_name = specified_function["name"]
specified_function_name = specified_function.get("name")
if not specified_function_name:
raise ValueError(
"Incorrectly formatted `tool_choice`. Should be like "
"`{\"type\": \"function\", "
"Expected field `name` in `function` in `tool_choice`."
"Correct usage: `{\"type\": \"function\", "
"\"function\": {\"name\": \"my_function\"}}`")
for tool in data["tools"]:
if tool["function"]["name"] == specified_function_name: