[Frontend] Move async logic outside of constructor (#4674)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import asyncio
|
||||
import codecs
|
||||
import time
|
||||
from typing import (AsyncGenerator, AsyncIterator, Awaitable, Iterable, List,
|
||||
@@ -8,6 +7,7 @@ from fastapi import Request
|
||||
from openai.types.chat import (ChatCompletionContentPartParam,
|
||||
ChatCompletionRole)
|
||||
|
||||
from vllm.config import ModelConfig
|
||||
from vllm.engine.async_llm_engine import AsyncLLMEngine
|
||||
from vllm.entrypoints.openai.protocol import (
|
||||
ChatCompletionRequest, ChatCompletionResponse,
|
||||
@@ -35,17 +35,47 @@ class OpenAIServingChat(OpenAIServing):
|
||||
|
||||
def __init__(self,
|
||||
engine: AsyncLLMEngine,
|
||||
model_config: ModelConfig,
|
||||
served_model_names: List[str],
|
||||
response_role: str,
|
||||
lora_modules: Optional[List[LoRAModulePath]] = None,
|
||||
chat_template: Optional[str] = None):
|
||||
super().__init__(engine=engine,
|
||||
model_config=model_config,
|
||||
served_model_names=served_model_names,
|
||||
lora_modules=lora_modules,
|
||||
await_post_init=self._load_chat_template(
|
||||
chat_template=chat_template))
|
||||
lora_modules=lora_modules)
|
||||
|
||||
self.response_role = response_role
|
||||
self._load_chat_template(chat_template)
|
||||
|
||||
def _load_chat_template(self, chat_template: Optional[str]):
|
||||
tokenizer = self.tokenizer
|
||||
|
||||
if chat_template is not None:
|
||||
try:
|
||||
with open(chat_template, "r") as f:
|
||||
tokenizer.chat_template = f.read()
|
||||
except OSError as e:
|
||||
JINJA_CHARS = "{}\n"
|
||||
if not any(c in chat_template for c in JINJA_CHARS):
|
||||
msg = (f"The supplied chat template ({chat_template}) "
|
||||
f"looks like a file path, but it failed to be "
|
||||
f"opened. Reason: {e}")
|
||||
raise ValueError(msg) from e
|
||||
|
||||
# If opening a file fails, set chat template to be args to
|
||||
# ensure we decode so our escape are interpreted correctly
|
||||
tokenizer.chat_template = codecs.decode(
|
||||
chat_template, "unicode_escape")
|
||||
|
||||
logger.info("Using supplied chat template:\n%s",
|
||||
tokenizer.chat_template)
|
||||
elif tokenizer.chat_template is not None:
|
||||
logger.info("Using default chat template:\n%s",
|
||||
tokenizer.chat_template)
|
||||
else:
|
||||
logger.warning(
|
||||
"No chat template provided. Chat API will not work.")
|
||||
|
||||
def _parse_chat_message_content(
|
||||
self,
|
||||
@@ -357,36 +387,4 @@ class OpenAIServingChat(OpenAIServing):
|
||||
usage=usage,
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
async def _load_chat_template(self, chat_template: Optional[str]):
|
||||
while self.tokenizer is None:
|
||||
# Give the parent class time to load the tokenizer
|
||||
await asyncio.sleep(0.1)
|
||||
tokenizer = self.tokenizer
|
||||
|
||||
if chat_template is not None:
|
||||
try:
|
||||
with open(chat_template, "r") as f:
|
||||
tokenizer.chat_template = f.read()
|
||||
except OSError as e:
|
||||
JINJA_CHARS = "{}\n"
|
||||
if not any(c in chat_template for c in JINJA_CHARS):
|
||||
msg = (f"The supplied chat template ({chat_template}) "
|
||||
f"looks like a file path, but it failed to be "
|
||||
f"opened. Reason: {e}")
|
||||
raise ValueError(msg) from e
|
||||
|
||||
# If opening a file fails, set chat template to be args to
|
||||
# ensure we decode so our escape are interpreted correctly
|
||||
tokenizer.chat_template = codecs.decode(
|
||||
chat_template, "unicode_escape")
|
||||
|
||||
logger.info("Using supplied chat template:\n%s",
|
||||
tokenizer.chat_template)
|
||||
elif tokenizer.chat_template is not None:
|
||||
logger.info("Using default chat template:\n%s",
|
||||
tokenizer.chat_template)
|
||||
else:
|
||||
logger.warning(
|
||||
"No chat template provided. Chat API will not work.")
|
||||
return response
|
||||
Reference in New Issue
Block a user