[Bugfix] fix tool_parser error handling when serve a model not support it (#8709)

This commit is contained in:
Yanyi Liu
2024-10-06 20:51:08 +08:00
committed by GitHub
parent b22b798471
commit fdf59d30ea
3 changed files with 31 additions and 10 deletions

View File

@@ -302,10 +302,6 @@ class OpenAIServingChat(OpenAIServing):
finish_reason_sent = [False] * num_choices
num_prompt_tokens = 0
tool_parsers: List[Optional[ToolParser]] = [
self.tool_parser(tokenizer) if self.tool_parser else None
] * num_choices
if isinstance(request.tool_choice, ChatCompletionNamedToolChoiceParam):
tool_choice_function_name = request.tool_choice.function.name
else:
@@ -324,6 +320,21 @@ class OpenAIServingChat(OpenAIServing):
else:
previous_texts, all_previous_token_ids = None, None
# Prepare the tool parser if it's needed
try:
if tool_choice_auto and self.tool_parser:
tool_parsers: List[Optional[ToolParser]] = [
self.tool_parser(tokenizer)
] * num_choices
else:
tool_parsers = [None] * num_choices
except RuntimeError as e:
logger.error("Error in tool parser creation: %s", e)
data = self.create_streaming_error_response(str(e))
yield f"data: {data}\n\n"
yield "data: [DONE]\n\n"
return
try:
async for res in result_generator:
if res.prompt_token_ids is not None:
@@ -704,7 +715,12 @@ class OpenAIServingChat(OpenAIServing):
or request.tool_choice is None) and self.enable_auto_tools \
and self.tool_parser:
tool_parser = self.tool_parser(tokenizer)
try:
tool_parser = self.tool_parser(tokenizer)
except RuntimeError as e:
logger.error("Error in tool parser creation: %s", e)
return self.create_error_response(str(e))
tool_call_info = tool_parser.extract_tool_calls(
output.text, request=request)
tools_called = tool_call_info.tools_called