[Chore] Consolidate reasoning/tool parser attributes into unified Parser in chat serving (#45548)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -60,7 +60,6 @@ from vllm.logprobs import Logprob
|
||||
from vllm.outputs import RequestOutput
|
||||
from vllm.parser import ParserManager
|
||||
from vllm.parser.abstract_parser import Parser
|
||||
from vllm.reasoning import ReasoningParser
|
||||
from vllm.renderers import ChatParams
|
||||
from vllm.sampling_params import BeamSearchParams, SamplingParams
|
||||
from vllm.tokenizers import TokenizerLike
|
||||
@@ -145,17 +144,7 @@ class OpenAIServingChat(OpenAIServing):
|
||||
self.enable_log_outputs = enable_log_outputs
|
||||
self.enable_log_deltas = enable_log_deltas
|
||||
|
||||
# set up reasoning parser
|
||||
self.reasoning_parser_cls = ParserManager.get_reasoning_parser(
|
||||
reasoning_parser_name=reasoning_parser
|
||||
)
|
||||
# set up tool use
|
||||
self.enable_auto_tools: bool = enable_auto_tools
|
||||
self.tool_parser = ParserManager.get_tool_parser(
|
||||
tool_parser_name=tool_parser,
|
||||
enable_auto_tools=enable_auto_tools,
|
||||
model_name=self.model_config.model,
|
||||
)
|
||||
self.parser_cls = ParserManager.get_parser(
|
||||
tool_parser_name=tool_parser,
|
||||
reasoning_parser_name=reasoning_parser,
|
||||
@@ -164,8 +153,9 @@ class OpenAIServingChat(OpenAIServing):
|
||||
is_harmony=self.model_config.hf_config.model_type == "gpt_oss",
|
||||
)
|
||||
if (
|
||||
is_mistral_tool_parser(self.tool_parser)
|
||||
and self.reasoning_parser_cls is not None
|
||||
self.parser_cls is not None
|
||||
and is_mistral_tool_parser(self.parser_cls.tool_parser_cls)
|
||||
and self.parser_cls.reasoning_parser_cls is not None
|
||||
):
|
||||
from vllm.tool_parsers.mistral_tool_parser import MistralToolParser
|
||||
|
||||
@@ -267,11 +257,12 @@ class OpenAIServingChat(OpenAIServing):
|
||||
tokenizer = self.renderer.tokenizer
|
||||
assert tokenizer is not None
|
||||
chat_template_kwargs = self._effective_chat_template_kwargs(request)
|
||||
reasoning_parser: ReasoningParser | None = None
|
||||
if self.reasoning_parser_cls:
|
||||
reasoning_parser = self.reasoning_parser_cls(
|
||||
parser: Parser | None = None
|
||||
if self.parser_cls is not None:
|
||||
parser = self.parser_cls(
|
||||
tokenizer,
|
||||
chat_template_kwargs=chat_template_kwargs, # type: ignore[call-arg]
|
||||
request.tools,
|
||||
chat_template_kwargs=chat_template_kwargs,
|
||||
)
|
||||
result = await self.render_chat_request(request)
|
||||
if isinstance(result, ErrorResponse):
|
||||
@@ -359,10 +350,8 @@ class OpenAIServingChat(OpenAIServing):
|
||||
# `think?` rule that handles both reasoning and
|
||||
# non-reasoning outputs.
|
||||
reasoning_ended = True
|
||||
elif reasoning_parser:
|
||||
reasoning_ended = reasoning_parser.is_reasoning_end(
|
||||
prompt_token_ids or []
|
||||
)
|
||||
elif parser is not None and parser.reasoning_parser is not None:
|
||||
reasoning_ended = parser.is_reasoning_end(prompt_token_ids or [])
|
||||
else:
|
||||
reasoning_ended = None
|
||||
|
||||
@@ -378,7 +367,7 @@ class OpenAIServingChat(OpenAIServing):
|
||||
reasoning_parser_kwargs={
|
||||
"chat_template_kwargs": chat_template_kwargs,
|
||||
}
|
||||
if reasoning_parser
|
||||
if parser is not None and parser.reasoning_parser is not None
|
||||
else None,
|
||||
)
|
||||
|
||||
@@ -408,7 +397,7 @@ class OpenAIServingChat(OpenAIServing):
|
||||
conversation,
|
||||
tokenizer,
|
||||
request_metadata,
|
||||
chat_template_kwargs=chat_template_kwargs,
|
||||
parser=parser,
|
||||
mm_token_counts=mm_token_counts,
|
||||
)
|
||||
|
||||
@@ -835,7 +824,7 @@ class OpenAIServingChat(OpenAIServing):
|
||||
conversation: list[ConversationMessage],
|
||||
tokenizer: TokenizerLike,
|
||||
request_metadata: RequestResponseMetadata,
|
||||
chat_template_kwargs: dict[str, Any] | None = None,
|
||||
parser: Parser | None = None,
|
||||
mm_token_counts: dict[str, int] | None = None,
|
||||
) -> ErrorResponse | ChatCompletionResponse:
|
||||
created_time = int(time.time())
|
||||
@@ -861,6 +850,9 @@ class OpenAIServingChat(OpenAIServing):
|
||||
history_tool_call_cnt = 0
|
||||
|
||||
role = self.get_chat_request_role(request)
|
||||
tool_parser_cls = (
|
||||
self.parser_cls.tool_parser_cls if self.parser_cls is not None else None
|
||||
)
|
||||
for output in final_res.outputs:
|
||||
# check for error finish reason and raise GenerationError
|
||||
# finish_reason='error' indicates a retryable request-level internal error
|
||||
@@ -880,14 +872,6 @@ class OpenAIServingChat(OpenAIServing):
|
||||
else:
|
||||
logprobs = None
|
||||
|
||||
parser: Parser | None = None
|
||||
if self.parser_cls is not None:
|
||||
parser = self.parser_cls(
|
||||
tokenizer,
|
||||
request.tools,
|
||||
chat_template_kwargs=chat_template_kwargs,
|
||||
)
|
||||
|
||||
if parser is not None:
|
||||
reasoning, content, tool_calls = parser.parse(
|
||||
output.text,
|
||||
@@ -904,7 +888,7 @@ class OpenAIServingChat(OpenAIServing):
|
||||
|
||||
auto_tools_called = False
|
||||
|
||||
if (not self.enable_auto_tools or not self.tool_parser) and (
|
||||
if (not self.enable_auto_tools or not tool_parser_cls) and (
|
||||
not isinstance(request.tool_choice, ChatCompletionNamedToolChoiceParam)
|
||||
and request.tool_choice != "required"
|
||||
):
|
||||
@@ -963,7 +947,7 @@ class OpenAIServingChat(OpenAIServing):
|
||||
request.tools
|
||||
and (request.tool_choice == "auto" or request.tool_choice is None)
|
||||
and self.enable_auto_tools
|
||||
and self.tool_parser
|
||||
and tool_parser_cls
|
||||
):
|
||||
auto_tools_called = tool_calls is not None and len(tool_calls) > 0
|
||||
if tool_calls:
|
||||
|
||||
Reference in New Issue
Block a user