| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import warnings |
| 4 | from argparse import Namespace |
| 5 | |
| 6 | from fastapi import FastAPI |
| 7 | |
| 8 | from vllm.config import ModelConfig |
| 9 | from vllm.entrypoints.serve.exception_handling.register import init_exception_handler |
| 10 | from vllm.entrypoints.serve.middleware.register import init_entrypoints_middleware |
| 11 | from vllm.entrypoints.serve.sagemaker.api_router import sagemaker_standards_bootstrap |
| 12 | from vllm.plugins.endpoint_plugins.interface import attach_endpoint_plugins |
| 13 | from vllm.tasks import FALLBACK_SUPPORTED_TASKS, SupportedTask |
| 14 | |
| 15 | from .api_server.routers import register_api_routers |
| 16 | from .utils.server_utils import lifespan |
| 17 | |
| 18 | |
| 19 | def build_app( |
| 20 | args: Namespace, |
| 21 | supported_tasks: tuple["SupportedTask", ...] | None = None, |
| 22 | model_config: ModelConfig | None = None, |
| 23 | ) -> FastAPI: |
| 24 | if supported_tasks is None: |
| 25 | warnings.warn( |
| 26 | "The 'supported_tasks' parameter was not provided to " |
| 27 | "build_app and will be required in a future version. " |
| 28 | "Defaulting to ('generate',).", |
| 29 | DeprecationWarning, |
| 30 | stacklevel=2, |
| 31 | ) |
| 32 | supported_tasks = FALLBACK_SUPPORTED_TASKS |
| 33 | |
| 34 | if args.disable_fastapi_docs: |
| 35 | app = FastAPI( |
| 36 | openapi_url=None, docs_url=None, redoc_url=None, lifespan=lifespan |
| 37 | ) |
| 38 | elif args.enable_offline_docs: |
| 39 | app = FastAPI(docs_url=None, redoc_url=None, lifespan=lifespan) |
| 40 | else: |
| 41 | app = FastAPI(lifespan=lifespan) |
| 42 | app.state.args = args |
| 43 | app.root_path = args.root_path |
| 44 | |
| 45 | register_api_routers(args, app, supported_tasks, model_config) |
| 46 | |
| 47 | # Endpoint plugins are attached last so their routes are registered after all core |
| 48 | # routers. This runs even for the CPU only render server. A plugin eligible for |
| 49 | # the `render` task still gets its routes registered. It receives |
| 50 | # `engine_client=None` at Phase B (see `_init_endpoint_plugins_state`). |
| 51 | attach_endpoint_plugins(app, supported_tasks) |
| 52 | |
| 53 | init_exception_handler(app) |
| 54 | init_entrypoints_middleware(args, app, supported_tasks) |
| 55 | app = sagemaker_standards_bootstrap(app) |
| 56 | return app |