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|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Dict, Optional, List
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from starlette.requests import Request
|
| 7 |
+
from starlette.responses import StreamingResponse, JSONResponse
|
| 8 |
+
|
| 9 |
+
from ray import serve
|
| 10 |
+
|
| 11 |
+
from vllm.engine.arg_utils import AsyncEngineArgs
|
| 12 |
+
from vllm.engine.async_llm_engine import AsyncLLMEngine
|
| 13 |
+
from vllm.entrypoints.openai.cli_args import make_arg_parser
|
| 14 |
+
from vllm.entrypoints.openai.protocol import (
|
| 15 |
+
ChatCompletionRequest,
|
| 16 |
+
ChatCompletionResponse,
|
| 17 |
+
ErrorResponse,
|
| 18 |
+
)
|
| 19 |
+
from vllm.entrypoints.openai.serving_chat import OpenAIServingChat, BaseModelPath
|
| 20 |
+
from vllm.entrypoints.openai.serving_engine import LoRAModulePath
|
| 21 |
+
from vllm.utils import FlexibleArgumentParser
|
| 22 |
+
|
| 23 |
+
logger = logging.getLogger("ray.serve")
|
| 24 |
+
|
| 25 |
+
app = FastAPI()
|
| 26 |
+
|
| 27 |
+
@serve.deployment(name="VLLMDeployment")
|
| 28 |
+
@serve.ingress(app)
|
| 29 |
+
class VLLMDeployment:
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
engine_client: AsyncLLMEngine,
|
| 33 |
+
model_config: Dict,
|
| 34 |
+
base_model_paths: List[BaseModelPath],
|
| 35 |
+
response_role: str,
|
| 36 |
+
lora_modules: Optional[List[LoRAModulePath]] = None,
|
| 37 |
+
prompt_adapters: Optional[List[str]] = None,
|
| 38 |
+
request_logger: Optional[logging.Logger] = None,
|
| 39 |
+
chat_template: Optional[str] = None,
|
| 40 |
+
chat_template_content_format: str = "json",
|
| 41 |
+
return_tokens_as_token_ids: bool = False,
|
| 42 |
+
enable_auto_tools: bool = False,
|
| 43 |
+
tool_parser: Optional[str] = None,
|
| 44 |
+
enable_prompt_tokens_details: bool = False,
|
| 45 |
+
):
|
| 46 |
+
logger.info(f"Starting with engine client: {engine_client}")
|
| 47 |
+
self.openai_serving_chat = None
|
| 48 |
+
self.engine_client = engine_client
|
| 49 |
+
self.model_config = model_config
|
| 50 |
+
self.base_model_paths = base_model_paths
|
| 51 |
+
self.response_role = response_role
|
| 52 |
+
self.lora_modules = lora_modules
|
| 53 |
+
self.prompt_adapters = prompt_adapters
|
| 54 |
+
self.request_logger = request_logger
|
| 55 |
+
self.chat_template = chat_template
|
| 56 |
+
self.chat_template_content_format = chat_template_content_format
|
| 57 |
+
self.return_tokens_as_token_ids = return_tokens_as_token_ids
|
| 58 |
+
self.enable_auto_tools = enable_auto_tools
|
| 59 |
+
self.tool_parser = tool_parser
|
| 60 |
+
self.enable_prompt_tokens_details = enable_prompt_tokens_details
|
| 61 |
+
|
| 62 |
+
@app.post("/v1/chat/completions")
|
| 63 |
+
async def create_chat_completion(
|
| 64 |
+
self, request: ChatCompletionRequest, raw_request: Request
|
| 65 |
+
):
|
| 66 |
+
"""OpenAI-compatible HTTP endpoint.
|
| 67 |
+
|
| 68 |
+
API reference:
|
| 69 |
+
- https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html
|
| 70 |
+
"""
|
| 71 |
+
if not self.openai_serving_chat:
|
| 72 |
+
model_config = await self.engine_client.get_model_config()
|
| 73 |
+
self.openai_serving_chat = OpenAIServingChat(
|
| 74 |
+
self.engine_client,
|
| 75 |
+
model_config,
|
| 76 |
+
self.base_model_paths,
|
| 77 |
+
self.response_role,
|
| 78 |
+
lora_modules=self.lora_modules,
|
| 79 |
+
prompt_adapters=self.prompt_adapters,
|
| 80 |
+
request_logger=self.request_logger,
|
| 81 |
+
chat_template=self.chat_template,
|
| 82 |
+
chat_template_content_format=self.chat_template_content_format,
|
| 83 |
+
return_tokens_as_token_ids=self.return_tokens_as_token_ids,
|
| 84 |
+
enable_auto_tools=self.enable_auto_tools,
|
| 85 |
+
tool_parser=self.tool_parser,
|
| 86 |
+
enable_prompt_tokens_details=self.enable_prompt_tokens_details,
|
| 87 |
+
)
|
| 88 |
+
logger.info(f"Request: {request}")
|
| 89 |
+
generator = await self.openai_serving_chat.create_chat_completion(
|
| 90 |
+
request, raw_request
|
| 91 |
+
)
|
| 92 |
+
if isinstance(generator, ErrorResponse):
|
| 93 |
+
return JSONResponse(
|
| 94 |
+
content=generator.model_dump(), status_code=generator.code
|
| 95 |
+
)
|
| 96 |
+
if request.stream:
|
| 97 |
+
return StreamingResponse(content=generator, media_type="text/event-stream")
|
| 98 |
+
else:
|
| 99 |
+
assert isinstance(generator, ChatCompletionResponse)
|
| 100 |
+
return JSONResponse(content=generator.model_dump())
|
| 101 |
+
|
| 102 |
+
def parse_vllm_args(cli_args: Dict[str, str]):
|
| 103 |
+
"""Parses vLLM args based on CLI inputs.
|
| 104 |
+
|
| 105 |
+
Currently uses argparse because vLLM doesn't expose Python models for all of the
|
| 106 |
+
config options we want to support.
|
| 107 |
+
"""
|
| 108 |
+
parser = FlexibleArgumentParser(description="vLLM CLI")
|
| 109 |
+
parser = make_arg_parser(parser)
|
| 110 |
+
arg_strings = []
|
| 111 |
+
for key, value in cli_args.items():
|
| 112 |
+
arg_strings.extend([f"--{key}", str(value)])
|
| 113 |
+
logger.info(arg_strings)
|
| 114 |
+
parsed_args = parser.parse_args(args=arg_strings)
|
| 115 |
+
return parsed_args
|
| 116 |
+
|
| 117 |
+
def build_app(cli_args: Dict[str, str], base_model_paths: List[BaseModelPath]) -> serve.Application:
|
| 118 |
+
"""Builds the Serve app based on CLI arguments.
|
| 119 |
+
|
| 120 |
+
See https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html#command-line-arguments-for-the-server
|
| 121 |
+
for the complete set of arguments.
|
| 122 |
+
|
| 123 |
+
Supported engine arguments: https://docs.vllm.ai/en/latest/models/engine_args.html.
|
| 124 |
+
"""
|
| 125 |
+
parsed_args = parse_vllm_args(cli_args)
|
| 126 |
+
engine_args = AsyncEngineArgs.from_cli_args(parsed_args)
|
| 127 |
+
engine_args.worker_use_ray = True
|
| 128 |
+
|
| 129 |
+
model_config = {} # Load your model config here
|
| 130 |
+
|
| 131 |
+
return VLLMDeployment.bind(
|
| 132 |
+
AsyncLLMEngine.from_engine_args(engine_args),
|
| 133 |
+
model_config,
|
| 134 |
+
base_model_paths,
|
| 135 |
+
parsed_args.response_role,
|
| 136 |
+
lora_modules=parsed_args.lora_modules,
|
| 137 |
+
prompt_adapters=parsed_args.prompt_adapters,
|
| 138 |
+
request_logger=logging.getLogger("request"),
|
| 139 |
+
chat_template=parsed_args.chat_template,
|
| 140 |
+
chat_template_content_format=parsed_args.chat_template_content_format,
|
| 141 |
+
return_tokens_as_token_ids=parsed_args.return_tokens_as_token_ids,
|
| 142 |
+
enable_auto_tools=parsed_args.enable_auto_tools,
|
| 143 |
+
tool_parser=parsed_args.tool_call_parser,
|
| 144 |
+
enable_prompt_tokens_details=parsed_args.enable_prompt_tokens_details,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
def convert_paths_to_base_model_paths(path_str: str) -> List[BaseModelPath]:
|
| 148 |
+
"""Chuyển đổi chuỗi các đường dẫn thành danh sách các đối tượng BaseModelPath."""
|
| 149 |
+
paths = path_str.split(',')
|
| 150 |
+
base_model_paths = [BaseModelPath(name=f"model{i+1}", model_path=path) for i, path in enumerate(paths)]
|
| 151 |
+
return base_model_paths
|
| 152 |
+
|
| 153 |
+
# Sử dụng build_app để tạox model
|
| 154 |
+
model_paths_str = "path/to/your/model1,path/to/your/model2"
|
| 155 |
+
base_model_paths = convert_paths_to_base_model_paths(model_paths_str)
|
| 156 |
+
|
| 157 |
+
model = build_app(
|
| 158 |
+
{
|
| 159 |
+
"model": os.environ['MODEL_ID'],
|
| 160 |
+
"tensor-parallel-size": os.environ['TENSOR_PARALLELISM'],
|
| 161 |
+
"pipeline-parallel-size": os.environ['PIPELINE_PARALLELISM']
|
| 162 |
+
},
|
| 163 |
+
base_model_paths
|
| 164 |
+
)
|