[Bugfix][Frontend] Expose completion response schemas in OpenAPI (#60926)
Signed-off-by: Guan Luo <gluo@nvidia.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -6,13 +6,15 @@ import dataclasses
|
||||
import functools
|
||||
import os
|
||||
from argparse import Namespace
|
||||
from http import HTTPStatus
|
||||
from logging import Logger
|
||||
from string import Template
|
||||
from typing import Any
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from fastapi.responses import JSONResponse, Response, StreamingResponse
|
||||
from pydantic import BaseModel
|
||||
from starlette.background import BackgroundTask, BackgroundTasks
|
||||
|
||||
from vllm import envs
|
||||
@@ -88,11 +90,80 @@ def with_cancellation(handler_func):
|
||||
|
||||
if handler_task in done:
|
||||
return handler_task.result()
|
||||
return None
|
||||
# The client is gone; an empty response skips response_model validation.
|
||||
return Response()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
_ERROR_REF = {"$ref": "#/components/schemas/ErrorResponse"}
|
||||
|
||||
|
||||
def _sse_event_schema(chunk_ref: dict[str, str]) -> dict[str, Any]:
|
||||
# One SSE event: `data` is a JSON chunk, a JSON error, or "[DONE]". This is
|
||||
# the envelope FastAPI emits for EventSourceResponse routes, but published
|
||||
# under `schema` because OpenAPI 3.1 has no `itemSchema`.
|
||||
return {
|
||||
"type": "object",
|
||||
"required": ["data"],
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "string",
|
||||
"anyOf": [
|
||||
{"const": "[DONE]"},
|
||||
{
|
||||
"contentMediaType": "application/json",
|
||||
"contentSchema": chunk_ref,
|
||||
},
|
||||
{
|
||||
"contentMediaType": "application/json",
|
||||
"contentSchema": _ERROR_REF,
|
||||
},
|
||||
],
|
||||
},
|
||||
"event": {"type": "string"},
|
||||
"id": {"type": "string"},
|
||||
"retry": {"type": "integer", "minimum": 0},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def json_or_sse_responses(
|
||||
unary_model: type[BaseModel],
|
||||
chunk_model: type[BaseModel],
|
||||
error_responses: dict[int, dict[str, Any]],
|
||||
) -> dict[str, Any]:
|
||||
"""Route kwargs for an endpoint answering with ``unary_model`` as JSON, or
|
||||
with SSE ``data`` events carrying ``chunk_model`` when ``stream=true``.
|
||||
|
||||
FastAPI only registers models reachable from ``response_model`` or an
|
||||
additional response's ``model``. The chunk model rides on the 200 entry,
|
||||
which also overwrites the JSON schema, so ``openapi_extra`` restores it.
|
||||
"""
|
||||
unary_ref = {"$ref": f"#/components/schemas/{unary_model.__name__}"}
|
||||
chunk_ref = {"$ref": f"#/components/schemas/{chunk_model.__name__}"}
|
||||
return {
|
||||
"response_model": unary_model,
|
||||
"responses": {
|
||||
HTTPStatus.OK.value: {
|
||||
"model": chunk_model,
|
||||
"description": (
|
||||
"JSON body when stream=false; SSE events when stream=true."
|
||||
),
|
||||
"content": {
|
||||
"text/event-stream": {"schema": _sse_event_schema(chunk_ref)}
|
||||
},
|
||||
},
|
||||
**error_responses,
|
||||
},
|
||||
"openapi_extra": {
|
||||
"responses": {
|
||||
"200": {"content": {"application/json": {"schema": unary_ref}}}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def decrement_server_load(request: Request):
|
||||
request.app.state.server_load_metrics -= 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user