api_router.py 1.23 KB
Newer Older
1
2
3
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

4
from fastapi import APIRouter, Depends, Request
5
from fastapi.responses import JSONResponse, Response
6
7

from vllm.entrypoints.openai.utils import validate_json_request
8
from vllm.entrypoints.pooling.classify.protocol import ClassificationRequest
9
from vllm.entrypoints.pooling.classify.serving import ServingClassification
10
11
12
13
14
from vllm.entrypoints.utils import (
    create_error_response,
    load_aware_call,
    with_cancellation,
)
15
16
17
18
19

router = APIRouter()


def classify(request: Request) -> ServingClassification | None:
20
    return request.app.state.serving_classification
21
22
23
24
25


@router.post("/classify", dependencies=[Depends(validate_json_request)])
@with_cancellation
@load_aware_call
26
27
async def create_classify(
    request: ClassificationRequest, raw_request: Request
28
) -> Response:
29
30
    handler = classify(raw_request)
    if handler is None:
31
        error_response = create_error_response(
32
33
34
            message="The model does not support Classification API"
        )
        return JSONResponse(
35
36
            content=error_response.model_dump(),
            status_code=error_response.error.code,
37
38
        )

39
    return await handler(request, raw_request)