[Refactor][Parser] Unify Response API to use parser.parse() like Chat Completion API (#45190)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -16,6 +16,7 @@ from vllm.entrypoints.openai.responses.protocol import (
|
||||
ResponseInputOutputItem,
|
||||
ResponsesRequest,
|
||||
)
|
||||
from vllm.entrypoints.openai.responses.utils import build_response_output_items
|
||||
from vllm.entrypoints.serve.utils.constants import MCP_PREFIX
|
||||
from vllm.outputs import CompletionOutput
|
||||
from vllm.parser.abstract_parser import Parser
|
||||
@@ -73,11 +74,15 @@ class ResponsesParser:
|
||||
self.finish_reason = output.finish_reason
|
||||
|
||||
if self.parser_instance is not None:
|
||||
output_items = self.parser_instance.extract_response_outputs(
|
||||
model_output=output.text,
|
||||
model_output_token_ids=output.token_ids,
|
||||
request=self.request,
|
||||
reasoning, content, tool_calls = self.parser_instance.parse(
|
||||
output.text,
|
||||
self.request,
|
||||
enable_auto_tools=self.enable_auto_tools,
|
||||
)
|
||||
output_items = build_response_output_items(
|
||||
reasoning=reasoning,
|
||||
content=content,
|
||||
tool_calls=tool_calls,
|
||||
tool_call_id_type=self.tool_call_id_type,
|
||||
)
|
||||
self.response_messages.extend(output_items)
|
||||
|
||||
@@ -86,6 +86,7 @@ from vllm.entrypoints.openai.responses.streaming_events import (
|
||||
split_delta,
|
||||
)
|
||||
from vllm.entrypoints.openai.responses.utils import (
|
||||
build_response_output_items,
|
||||
construct_input_messages,
|
||||
construct_tool_dicts,
|
||||
extract_function_tool_names,
|
||||
@@ -1028,19 +1029,23 @@ class OpenAIServingResponses(OpenAIServing):
|
||||
top_logprobs=request.top_logprobs,
|
||||
)
|
||||
|
||||
# Use parser to extract and create response output items
|
||||
# Use parser to extract reasoning, content, and tool calls
|
||||
if self.parser:
|
||||
chat_template_kwargs = self._effective_chat_template_kwargs(request)
|
||||
parser = self.parser(
|
||||
tokenizer, request.tools, chat_template_kwargs=chat_template_kwargs
|
||||
)
|
||||
return parser.extract_response_outputs(
|
||||
model_output=final_output.text,
|
||||
model_output_token_ids=final_output.token_ids,
|
||||
request=request,
|
||||
reasoning, content, tool_calls = parser.parse(
|
||||
final_output.text,
|
||||
request,
|
||||
enable_auto_tools=self.enable_auto_tools,
|
||||
tool_call_id_type=self.tool_call_id_type,
|
||||
)
|
||||
return build_response_output_items(
|
||||
reasoning=reasoning,
|
||||
content=content,
|
||||
tool_calls=tool_calls,
|
||||
logprobs=logprobs,
|
||||
tool_call_id_type=self.tool_call_id_type,
|
||||
)
|
||||
|
||||
# Fallback when no parser is configured
|
||||
|
||||
@@ -12,23 +12,96 @@ from openai.types.chat import (
|
||||
from openai.types.chat.chat_completion_message_tool_call_param import (
|
||||
Function as FunctionCallTool,
|
||||
)
|
||||
from openai.types.responses import ResponseFunctionToolCall, ResponseOutputItem
|
||||
from openai.types.responses import (
|
||||
ResponseFunctionToolCall,
|
||||
ResponseOutputItem,
|
||||
ResponseOutputMessage,
|
||||
ResponseOutputText,
|
||||
ResponseReasoningItem,
|
||||
)
|
||||
from openai.types.responses.response import ToolChoice
|
||||
from openai.types.responses.response_function_tool_call_output_item import (
|
||||
ResponseFunctionToolCallOutputItem,
|
||||
)
|
||||
from openai.types.responses.response_output_message import ResponseOutputMessage
|
||||
from openai.types.responses.response_reasoning_item import ResponseReasoningItem
|
||||
from openai.types.responses.response_output_text import Logprob
|
||||
from openai.types.responses.response_reasoning_item import (
|
||||
Content as ResponseReasoningTextContent,
|
||||
)
|
||||
from openai.types.responses.tool import Tool
|
||||
|
||||
from vllm import envs
|
||||
from vllm.entrypoints.chat_utils import make_tool_call_id
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionMessageParam
|
||||
from vllm.entrypoints.openai.engine.protocol import FunctionCall
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponseInputOutputItem
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils import random_uuid
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def build_response_output_items(
|
||||
reasoning: str | None,
|
||||
content: str | None,
|
||||
tool_calls: list[FunctionCall] | None,
|
||||
logprobs: list[Logprob] | None = None,
|
||||
tool_call_id_type: str = "random",
|
||||
) -> list[ResponseOutputItem]:
|
||||
outputs: list[ResponseOutputItem] = []
|
||||
|
||||
if reasoning:
|
||||
outputs.append(
|
||||
ResponseReasoningItem(
|
||||
id=f"rs_{random_uuid()}",
|
||||
summary=[],
|
||||
type="reasoning",
|
||||
content=[
|
||||
ResponseReasoningTextContent(text=reasoning, type="reasoning_text")
|
||||
],
|
||||
status=None,
|
||||
)
|
||||
)
|
||||
|
||||
if content:
|
||||
outputs.append(
|
||||
ResponseOutputMessage(
|
||||
id=f"msg_{random_uuid()}",
|
||||
content=[
|
||||
ResponseOutputText(
|
||||
text=content,
|
||||
annotations=[],
|
||||
type="output_text",
|
||||
logprobs=logprobs,
|
||||
)
|
||||
],
|
||||
role="assistant",
|
||||
status="completed",
|
||||
type="message",
|
||||
)
|
||||
)
|
||||
|
||||
if tool_calls:
|
||||
for idx, tool_call in enumerate(tool_calls):
|
||||
outputs.append(
|
||||
ResponseFunctionToolCall(
|
||||
id=f"fc_{random_uuid()}",
|
||||
call_id=tool_call.id
|
||||
if tool_call.id
|
||||
else make_tool_call_id(
|
||||
id_type=tool_call_id_type,
|
||||
func_name=tool_call.name,
|
||||
idx=idx,
|
||||
),
|
||||
type="function_call",
|
||||
status="completed",
|
||||
name=tool_call.name,
|
||||
arguments=tool_call.arguments,
|
||||
)
|
||||
)
|
||||
|
||||
return outputs
|
||||
|
||||
|
||||
def should_continue_final_message(
|
||||
request_input: str | list[ResponseInputOutputItem],
|
||||
) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user