|
1 | | -from typing import Dict, List, Optional, Type |
| 1 | +from typing import Optional |
2 | 2 |
|
3 | | -from dstack._internal.core.models.routers import AnyRouterConfig, RouterType |
4 | | -from dstack._internal.utils.logging import get_logger |
| 3 | +from dstack._internal.core.models.routers import AnyRouterConfig |
| 4 | +from dstack._internal.proxy.gateway.model_routers.sglang import SglangRouter |
5 | 5 |
|
6 | 6 | from .base import Replica, Router, RouterContext |
7 | 7 |
|
8 | | -logger = get_logger(__name__) |
9 | | - |
10 | | -"""This provides a registry of available router implementations.""" |
11 | | - |
12 | | -_ROUTER_CLASSES: List[Type[Router]] = [] |
13 | | - |
14 | | -try: |
15 | | - from dstack._internal.proxy.gateway.model_routers.sglang import SglangRouter |
16 | | - |
17 | | - _ROUTER_CLASSES.append(SglangRouter) |
18 | | - logger.debug("Registered SglangRouter") |
19 | | -except ImportError as e: |
20 | | - logger.warning("SGLang router not available: %s", e) |
21 | | - |
22 | | -_ROUTER_TYPE_TO_CLASS_MAP: Dict[RouterType, Type[Router]] = {} |
23 | | - |
24 | | -for router_class in _ROUTER_CLASSES: |
25 | | - router_type_str = getattr(router_class, "TYPE", None) |
26 | | - if router_type_str is None: |
27 | | - logger.warning(f"Router class {router_class.__name__} missing TYPE attribute, skipping") |
28 | | - continue |
29 | | - router_type = RouterType(router_type_str) |
30 | | - _ROUTER_TYPE_TO_CLASS_MAP[router_type] = router_class |
31 | | - |
32 | | -_AVAILABLE_ROUTER_TYPES = list(_ROUTER_TYPE_TO_CLASS_MAP.keys()) |
33 | | - |
34 | | - |
35 | | -def get_router_class(router_type: RouterType) -> Optional[Type[Router]]: |
36 | | - """Get the router class for a given router type.""" |
37 | | - return _ROUTER_TYPE_TO_CLASS_MAP.get(router_type) |
38 | | - |
39 | 8 |
|
40 | 9 | def get_router(router: AnyRouterConfig, context: Optional[RouterContext] = None) -> Router: |
41 | 10 | """Factory function to create a router instance from router configuration.""" |
42 | | - router_type = RouterType(router.type) |
43 | | - router_class = get_router_class(router_type) |
44 | | - |
45 | | - if router_class is None: |
46 | | - available_types = [rt.value for rt in _AVAILABLE_ROUTER_TYPES] |
47 | | - raise ValueError( |
48 | | - f"Router type '{router_type.value}' is not available. " |
49 | | - f"Available types: {available_types}" |
50 | | - ) |
51 | | - |
52 | | - # Router implementations may have different constructor signatures |
53 | | - # SglangRouter takes (router, context), others might differ |
54 | | - return router_class(router=router, context=context) |
| 11 | + if router.type == "sglang": |
| 12 | + return SglangRouter(router=router, context=context) |
| 13 | + raise ValueError(f"Router type '{router.type}' is not available") |
55 | 14 |
|
56 | 15 |
|
57 | 16 | __all__ = [ |
|
0 commit comments