Lazy-import third-party backends (#794)
This commit is contained in:
@@ -1,3 +1,27 @@
|
||||
import importlib
|
||||
|
||||
|
||||
class LazyImport:
|
||||
def __init__(self, module_name, class_name):
|
||||
self.module_name = module_name
|
||||
self.class_name = class_name
|
||||
self._module = None
|
||||
|
||||
def _load(self):
|
||||
if self._module is None:
|
||||
module = importlib.import_module(self.module_name)
|
||||
self._module = getattr(module, self.class_name)
|
||||
return self._module
|
||||
|
||||
def __getattr__(self, name):
|
||||
module = self._load()
|
||||
return getattr(module, name)
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
module = self._load()
|
||||
return module(*args, **kwargs)
|
||||
|
||||
|
||||
# SGL API Components
|
||||
from sglang.api import (
|
||||
Runtime,
|
||||
@@ -24,11 +48,12 @@ from sglang.api import (
|
||||
from sglang.global_config import global_config
|
||||
|
||||
# SGL Backends
|
||||
from sglang.lang.backend.anthropic import Anthropic
|
||||
from sglang.lang.backend.litellm import LiteLLM
|
||||
from sglang.lang.backend.openai import OpenAI
|
||||
from sglang.lang.backend.runtime_endpoint import RuntimeEndpoint
|
||||
from sglang.lang.backend.vertexai import VertexAI
|
||||
|
||||
Anthropic = LazyImport("sglang.lang.backend.anthropic", "Anthropic")
|
||||
LiteLLM = LazyImport("sglang.lang.backend.litellm", "LiteLLM")
|
||||
OpenAI = LazyImport("sglang.lang.backend.openai", "OpenAI")
|
||||
VertexAI = LazyImport("sglang.lang.backend.vertexai", "VertexAI")
|
||||
|
||||
from .version import __version__
|
||||
|
||||
|
||||
Reference in New Issue
Block a user