| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from typing import Any, List, Optional, Tuple |
| 8 | |
| 9 | from llama_models.sku_list import resolve_model |
| 10 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 11 | |
| 12 | from llama_stack.apis.models import * # noqa: F403 |
| 13 | from llama_stack.apis.shields import * # noqa: F403 |
| 14 | from llama_stack.apis.memory_banks import * # noqa: F403 |
| 15 | |
| 16 | from llama_stack.distribution.datatypes import * # noqa: F403 |
| 17 | |
| 18 | |
| 19 | class CommonRoutingTableImpl(RoutingTable): |
| 20 | def __init__( |
| 21 | self, |
| 22 | inner_impls: List[Tuple[str, Any]], |
| 23 | routing_table_config: Dict[str, List[RoutableProviderConfig]], |
| 24 | ) -> None: |
| 25 | self.providers = {k: v for k, v in inner_impls} |
| 26 | self.routing_keys = list(self.providers.keys()) |
| 27 | self.routing_table_config = routing_table_config |
| 28 | |
| 29 | async def initialize(self) -> None: |
| 30 | pass |
| 31 | |
| 32 | async def shutdown(self) -> None: |
| 33 | for p in self.providers.values(): |
| 34 | await p.shutdown() |
| 35 | |
| 36 | def get_provider_impl(self, routing_key: str) -> Any: |
| 37 | if routing_key not in self.providers: |
| 38 | raise ValueError(f"Could not find provider for {routing_key}") |
| 39 | return self.providers[routing_key] |
| 40 | |
| 41 | def get_routing_keys(self) -> List[str]: |
| 42 | return self.routing_keys |
| 43 | |
| 44 | def get_provider_config(self, routing_key: str) -> Optional[GenericProviderConfig]: |
| 45 | for entry in self.routing_table_config: |
| 46 | if entry.routing_key == routing_key: |
| 47 | return entry |
| 48 | return None |
| 49 | |
| 50 | |
| 51 | class ModelsRoutingTable(CommonRoutingTableImpl, Models): |
| 52 | |
| 53 | async def list_models(self) -> List[ModelServingSpec]: |
| 54 | specs = [] |
| 55 | for entry in self.routing_table_config: |
| 56 | model_id = entry.routing_key |
| 57 | specs.append( |
| 58 | ModelServingSpec( |
| 59 | llama_model=resolve_model(model_id), |
| 60 | provider_config=entry, |
| 61 | ) |
| 62 | ) |
| 63 | return specs |
| 64 | |
| 65 | async def get_model(self, core_model_id: str) -> Optional[ModelServingSpec]: |
| 66 | for entry in self.routing_table_config: |
| 67 | if entry.routing_key == core_model_id: |
| 68 | return ModelServingSpec( |
| 69 | llama_model=resolve_model(core_model_id), |
| 70 | provider_config=entry, |
| 71 | ) |
| 72 | return None |
| 73 | |
| 74 | |
| 75 | class ShieldsRoutingTable(CommonRoutingTableImpl, Shields): |
| 76 | |
| 77 | async def list_shields(self) -> List[ShieldSpec]: |
| 78 | specs = [] |
| 79 | for entry in self.routing_table_config: |
| 80 | specs.append( |
| 81 | ShieldSpec( |
| 82 | shield_type=entry.routing_key, |
| 83 | provider_config=entry, |
| 84 | ) |
| 85 | ) |
| 86 | return specs |
| 87 | |
| 88 | async def get_shield(self, shield_type: str) -> Optional[ShieldSpec]: |
| 89 | for entry in self.routing_table_config: |
| 90 | if entry.routing_key == shield_type: |
| 91 | return ShieldSpec( |
| 92 | shield_type=entry.routing_key, |
| 93 | provider_config=entry, |
| 94 | ) |
| 95 | return None |
| 96 | |
| 97 | |
| 98 | class MemoryBanksRoutingTable(CommonRoutingTableImpl, MemoryBanks): |
| 99 | |
| 100 | async def list_available_memory_banks(self) -> List[MemoryBankSpec]: |
| 101 | specs = [] |
| 102 | for entry in self.routing_table_config: |
| 103 | specs.append( |
| 104 | MemoryBankSpec( |
| 105 | bank_type=entry.routing_key, |
| 106 | provider_config=entry, |
| 107 | ) |
| 108 | ) |
| 109 | return specs |
| 110 | |
| 111 | async def get_serving_memory_bank(self, bank_type: str) -> Optional[MemoryBankSpec]: |
| 112 | for entry in self.routing_table_config: |
| 113 | if entry.routing_key == bank_type: |
| 114 | return MemoryBankSpec( |
| 115 | bank_type=entry.routing_key, |
| 116 | provider_config=entry, |
| 117 | ) |
| 118 | return None |