[Frontend] Add site-packages support for reasoning/tool parser plugins (#45241)
Signed-off-by: odashi <yusuke.oda@predicate.jp> Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import importlib
|
||||
import os
|
||||
from abc import abstractmethod
|
||||
from collections.abc import Callable, Iterable, Sequence
|
||||
from functools import cached_property
|
||||
@@ -11,7 +10,7 @@ from typing import TYPE_CHECKING, cast
|
||||
from vllm.entrypoints.mcp.tool_server import ToolServer
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.collection_utils import is_list_of
|
||||
from vllm.utils.import_utils import import_from_path
|
||||
from vllm.utils.import_utils import import_plugin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import ModelConfig
|
||||
@@ -359,16 +358,5 @@ class ReasoningParserManager:
|
||||
|
||||
@classmethod
|
||||
def import_reasoning_parser(cls, plugin_path: str) -> None:
|
||||
"""
|
||||
Import a user-defined reasoning parser by the path
|
||||
of the reasoning parser define file.
|
||||
"""
|
||||
module_name = os.path.splitext(os.path.basename(plugin_path))[0]
|
||||
|
||||
try:
|
||||
import_from_path(module_name, plugin_path)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to load module '%s' from %s.", module_name, plugin_path
|
||||
)
|
||||
return
|
||||
"""Import a user-defined reasoning parser."""
|
||||
import_plugin(plugin_path)
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
from collections.abc import Callable, Sequence
|
||||
from functools import cached_property
|
||||
from typing import Any
|
||||
@@ -33,7 +32,7 @@ from vllm.sampling_params import (
|
||||
from vllm.tokenizers import TokenizerLike
|
||||
from vllm.tool_parsers.utils import Tool, get_json_schema_from_tools
|
||||
from vllm.utils.collection_utils import is_list_of
|
||||
from vllm.utils.import_utils import import_from_path
|
||||
from vllm.utils.import_utils import import_plugin
|
||||
|
||||
__all__ = ["Tool"]
|
||||
|
||||
@@ -367,12 +366,5 @@ class ToolParserManager:
|
||||
|
||||
@classmethod
|
||||
def import_tool_parser(cls, plugin_path: str) -> None:
|
||||
"""Import a user-defined parser file from arbitrary path."""
|
||||
|
||||
module_name = os.path.splitext(os.path.basename(plugin_path))[0]
|
||||
try:
|
||||
import_from_path(module_name, plugin_path)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to load module '%s' from %s.", module_name, plugin_path
|
||||
)
|
||||
"""Import a user-defined tool parser."""
|
||||
import_plugin(plugin_path)
|
||||
|
||||
@@ -82,7 +82,28 @@ def import_triton_kernels():
|
||||
)
|
||||
|
||||
|
||||
def import_from_path(module_name: str, file_path: str | os.PathLike):
|
||||
def import_plugin(plugin_path: str) -> ModuleType | None:
|
||||
"""
|
||||
Import a user-defined plugin.
|
||||
|
||||
Plugin can be either:
|
||||
* a module in site-packages
|
||||
* a Python file specified by its path
|
||||
"""
|
||||
try:
|
||||
return importlib.import_module(plugin_path)
|
||||
except ModuleNotFoundError:
|
||||
module_name = os.path.splitext(os.path.basename(plugin_path))[0]
|
||||
try:
|
||||
return import_from_path(module_name, plugin_path)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to load module '%s' from %s.", module_name, plugin_path
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def import_from_path(module_name: str, file_path: str | os.PathLike) -> ModuleType:
|
||||
"""
|
||||
Import a Python file according to its file path.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user