| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import time |
| 5 | from typing import Annotated, TypeAlias |
| 6 | |
| 7 | from pydantic import BeforeValidator, Field |
| 8 | |
| 9 | from vllm import PoolingParams |
| 10 | from vllm.entrypoints.serve.engine.protocol import OpenAIBaseModel, UsageInfo |
| 11 | from vllm.logger import init_logger |
| 12 | from vllm.utils import random_uuid |
| 13 | |
| 14 | from ..base.protocol import ( |
| 15 | ChatRequestMixin, |
| 16 | ClassifyRequestMixin, |
| 17 | CompletionRequestMixin, |
| 18 | FixedMaxLenTokenizeParamsMixin, |
| 19 | PoolingBasicRequestMixin, |
| 20 | reject_removed_pooling_parameters, |
| 21 | ) |
| 22 | |
| 23 | logger = init_logger(__name__) |
| 24 | |
| 25 | |
| 26 | class ClassificationCompletionRequest( |
| 27 | PoolingBasicRequestMixin, |
| 28 | CompletionRequestMixin, |
| 29 | ClassifyRequestMixin, |
| 30 | FixedMaxLenTokenizeParamsMixin, |
| 31 | ): |
| 32 | def to_pooling_params(self): |
| 33 | return PoolingParams( |
| 34 | task="classify", |
| 35 | use_activation=self.use_activation, |
| 36 | ) |
| 37 | |
| 38 | |
| 39 | class ClassificationChatRequest( |
| 40 | PoolingBasicRequestMixin, |
| 41 | ChatRequestMixin, |
| 42 | ClassifyRequestMixin, |
| 43 | FixedMaxLenTokenizeParamsMixin, |
| 44 | ): |
| 45 | def to_pooling_params(self): |
| 46 | return PoolingParams( |
| 47 | task="classify", |
| 48 | use_activation=self.use_activation, |
| 49 | ) |
| 50 | |
| 51 | |
| 52 | ClassificationRequest: TypeAlias = Annotated[ |
| 53 | ClassificationCompletionRequest | ClassificationChatRequest, |
| 54 | BeforeValidator(reject_removed_pooling_parameters), |
| 55 | ] |
| 56 | |
| 57 | |
| 58 | class ClassificationData(OpenAIBaseModel): |
| 59 | index: int |
| 60 | label: str | None |
| 61 | probs: list[float] |
| 62 | num_classes: int |
| 63 | |
| 64 | |
| 65 | class ClassificationResponse(OpenAIBaseModel): |
| 66 | id: str = Field(default_factory=lambda: f"classify-{random_uuid()}") |
| 67 | object: str = "list" |
| 68 | created: int = Field(default_factory=lambda: int(time.time())) |
| 69 | model: str |
| 70 | data: list[ClassificationData] |
| 71 | usage: UsageInfo |