| 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 datetime import datetime |
| 8 | from enum import Enum |
| 9 | from typing import Any, Dict, List, Optional, Protocol, Union |
| 10 | |
| 11 | from llama_models.schema_utils import json_schema_type |
| 12 | |
| 13 | from pydantic import BaseModel, Field |
| 14 | |
| 15 | |
| 16 | @json_schema_type |
| 17 | class Api(Enum): |
| 18 | inference = "inference" |
| 19 | safety = "safety" |
| 20 | agents = "agents" |
| 21 | memory = "memory" |
| 22 | |
| 23 | telemetry = "telemetry" |
| 24 | |
| 25 | models = "models" |
| 26 | shields = "shields" |
| 27 | memory_banks = "memory_banks" |
| 28 | |
| 29 | |
| 30 | @json_schema_type |
| 31 | class ApiEndpoint(BaseModel): |
| 32 | route: str |
| 33 | method: str |
| 34 | name: str |
| 35 | |
| 36 | |
| 37 | @json_schema_type |
| 38 | class ProviderSpec(BaseModel): |
| 39 | api: Api |
| 40 | provider_id: str |
| 41 | config_class: str = Field( |
| 42 | ..., |
| 43 | description="Fully-qualified classname of the config for this provider", |
| 44 | ) |
| 45 | api_dependencies: List[Api] = Field( |
| 46 | default_factory=list, |
| 47 | description="Higher-level API surfaces may depend on other providers to provide their functionality", |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | class RoutingTable(Protocol): |
| 52 | def get_routing_keys(self) -> List[str]: ... |
| 53 | |
| 54 | def get_provider_impl(self, routing_key: str) -> Any: ... |
| 55 | |
| 56 | |
| 57 | class GenericProviderConfig(BaseModel): |
| 58 | provider_id: str |
| 59 | config: Dict[str, Any] |
| 60 | |
| 61 | |
| 62 | class PlaceholderProviderConfig(BaseModel): |
| 63 | """Placeholder provider config for API whose provider are defined in routing_table""" |
| 64 | |
| 65 | providers: List[str] |
| 66 | |
| 67 | |
| 68 | class RoutableProviderConfig(GenericProviderConfig): |
| 69 | routing_key: str |
| 70 | |
| 71 | |
| 72 | # Example: /inference, /safety |
| 73 | @json_schema_type |
| 74 | class AutoRoutedProviderSpec(ProviderSpec): |
| 75 | provider_id: str = "router" |
| 76 | config_class: str = "" |
| 77 | |
| 78 | docker_image: Optional[str] = None |
| 79 | routing_table_api: Api |
| 80 | module: str = Field( |
| 81 | ..., |
| 82 | description=""" |
| 83 | Fully-qualified name of the module to import. The module is expected to have: |
| 84 | |
| 85 | - `get_router_impl(config, provider_specs, deps)`: returns the router implementation |
| 86 | """, |
| 87 | ) |
| 88 | provider_data_validator: Optional[str] = Field( |
| 89 | default=None, |
| 90 | ) |
| 91 | |
| 92 | @property |
| 93 | def pip_packages(self) -> List[str]: |
| 94 | raise AssertionError("Should not be called on AutoRoutedProviderSpec") |
| 95 | |
| 96 | |
| 97 | # Example: /models, /shields |
| 98 | @json_schema_type |
| 99 | class RoutingTableProviderSpec(ProviderSpec): |
| 100 | provider_id: str = "routing_table" |
| 101 | config_class: str = "" |
| 102 | docker_image: Optional[str] = None |
| 103 | |
| 104 | inner_specs: List[ProviderSpec] |
| 105 | module: str = Field( |
| 106 | ..., |
| 107 | description=""" |
| 108 | Fully-qualified name of the module to import. The module is expected to have: |
| 109 | |
| 110 | - `get_router_impl(config, provider_specs, deps)`: returns the router implementation |
| 111 | """, |
| 112 | ) |
| 113 | pip_packages: List[str] = Field(default_factory=list) |
| 114 | |
| 115 | |
| 116 | @json_schema_type |
| 117 | class AdapterSpec(BaseModel): |
| 118 | adapter_id: str = Field( |
| 119 | ..., |
| 120 | description="Unique identifier for this adapter", |
| 121 | ) |
| 122 | module: str = Field( |
| 123 | ..., |
| 124 | description=""" |
| 125 | Fully-qualified name of the module to import. The module is expected to have: |
| 126 | |
| 127 | - `get_adapter_impl(config, deps)`: returns the adapter implementation |
| 128 | """, |
| 129 | ) |
| 130 | pip_packages: List[str] = Field( |
| 131 | default_factory=list, |
| 132 | description="The pip dependencies needed for this implementation", |
| 133 | ) |
| 134 | config_class: Optional[str] = Field( |
| 135 | default=None, |
| 136 | description="Fully-qualified classname of the config for this provider", |
| 137 | ) |
| 138 | provider_data_validator: Optional[str] = Field( |
| 139 | default=None, |
| 140 | ) |
| 141 | |
| 142 | |
| 143 | @json_schema_type |
| 144 | class InlineProviderSpec(ProviderSpec): |
| 145 | pip_packages: List[str] = Field( |
| 146 | default_factory=list, |
| 147 | description="The pip dependencies needed for this implementation", |
| 148 | ) |
| 149 | docker_image: Optional[str] = Field( |
| 150 | default=None, |
| 151 | description=""" |
| 152 | The docker image to use for this implementation. If one is provided, pip_packages will be ignored. |
| 153 | If a provider depends on other providers, the dependencies MUST NOT specify a docker image. |
| 154 | """, |
| 155 | ) |
| 156 | module: str = Field( |
| 157 | ..., |
| 158 | description=""" |
| 159 | Fully-qualified name of the module to import. The module is expected to have: |
| 160 | |
| 161 | - `get_provider_impl(config, deps)`: returns the local implementation |
| 162 | """, |
| 163 | ) |
| 164 | provider_data_validator: Optional[str] = Field( |
| 165 | default=None, |
| 166 | ) |
| 167 | |
| 168 | |
| 169 | class RemoteProviderConfig(BaseModel): |
| 170 | host: str = "localhost" |
| 171 | port: int |
| 172 | |
| 173 | @property |
| 174 | def url(self) -> str: |
| 175 | return f"http://{self.host}:{self.port}" |
| 176 | |
| 177 | |
| 178 | def remote_provider_id(adapter_id: str) -> str: |
| 179 | return f"remote::{adapter_id}" |
| 180 | |
| 181 | |
| 182 | @json_schema_type |
| 183 | class RemoteProviderSpec(ProviderSpec): |
| 184 | adapter: Optional[AdapterSpec] = Field( |
| 185 | default=None, |
| 186 | description=""" |
| 187 | If some code is needed to convert the remote responses into Llama Stack compatible |
| 188 | API responses, specify the adapter here. If not specified, it indicates the remote |
| 189 | as being "Llama Stack compatible" |
| 190 | """, |
| 191 | ) |
| 192 | |
| 193 | @property |
| 194 | def docker_image(self) -> Optional[str]: |
| 195 | return None |
| 196 | |
| 197 | @property |
| 198 | def module(self) -> str: |
| 199 | if self.adapter: |
| 200 | return self.adapter.module |
| 201 | return f"llama_stack.apis.{self.api.value}.client" |
| 202 | |
| 203 | @property |
| 204 | def pip_packages(self) -> List[str]: |
| 205 | if self.adapter: |
| 206 | return self.adapter.pip_packages |
| 207 | return [] |
| 208 | |
| 209 | @property |
| 210 | def provider_data_validator(self) -> Optional[str]: |
| 211 | if self.adapter: |
| 212 | return self.adapter.provider_data_validator |
| 213 | return None |
| 214 | |
| 215 | |
| 216 | # Can avoid this by using Pydantic computed_field |
| 217 | def remote_provider_spec( |
| 218 | api: Api, adapter: Optional[AdapterSpec] = None |
| 219 | ) -> RemoteProviderSpec: |
| 220 | config_class = ( |
| 221 | adapter.config_class |
| 222 | if adapter and adapter.config_class |
| 223 | else "llama_stack.distribution.datatypes.RemoteProviderConfig" |
| 224 | ) |
| 225 | provider_id = remote_provider_id(adapter.adapter_id) if adapter else "remote" |
| 226 | |
| 227 | return RemoteProviderSpec( |
| 228 | api=api, provider_id=provider_id, config_class=config_class, adapter=adapter |
| 229 | ) |
| 230 | |
| 231 | |
| 232 | @json_schema_type |
| 233 | class DistributionSpec(BaseModel): |
| 234 | description: Optional[str] = Field( |
| 235 | default="", |
| 236 | description="Description of the distribution", |
| 237 | ) |
| 238 | docker_image: Optional[str] = None |
| 239 | providers: Dict[str, Union[str, List[str]]] = Field( |
| 240 | default_factory=dict, |
| 241 | description=""" |
| 242 | Provider Types for each of the APIs provided by this distribution. If you |
| 243 | select multiple providers, you should provide an appropriate 'routing_map' |
| 244 | in the runtime configuration to help route to the correct provider.""", |
| 245 | ) |
| 246 | |
| 247 | |
| 248 | @json_schema_type |
| 249 | class StackRunConfig(BaseModel): |
| 250 | built_at: datetime |
| 251 | |
| 252 | image_name: str = Field( |
| 253 | ..., |
| 254 | description=""" |
| 255 | Reference to the distribution this package refers to. For unregistered (adhoc) packages, |
| 256 | this could be just a hash |
| 257 | """, |
| 258 | ) |
| 259 | docker_image: Optional[str] = Field( |
| 260 | default=None, |
| 261 | description="Reference to the docker image if this package refers to a container", |
| 262 | ) |
| 263 | conda_env: Optional[str] = Field( |
| 264 | default=None, |
| 265 | description="Reference to the conda environment if this package refers to a conda environment", |
| 266 | ) |
| 267 | apis_to_serve: List[str] = Field( |
| 268 | description=""" |
| 269 | The list of APIs to serve. If not specified, all APIs specified in the provider_map will be served""", |
| 270 | ) |
| 271 | |
| 272 | api_providers: Dict[ |
| 273 | str, Union[GenericProviderConfig, PlaceholderProviderConfig] |
| 274 | ] = Field( |
| 275 | description=""" |
| 276 | Provider configurations for each of the APIs provided by this package. |
| 277 | """, |
| 278 | ) |
| 279 | routing_table: Dict[str, List[RoutableProviderConfig]] = Field( |
| 280 | default_factory=dict, |
| 281 | description=""" |
| 282 | |
| 283 | E.g. The following is a ProviderRoutingEntry for models: |
| 284 | - routing_key: Meta-Llama3.1-8B-Instruct |
| 285 | provider_id: meta-reference |
| 286 | config: |
| 287 | model: Meta-Llama3.1-8B-Instruct |
| 288 | quantization: null |
| 289 | torch_seed: null |
| 290 | max_seq_len: 4096 |
| 291 | max_batch_size: 1 |
| 292 | """, |
| 293 | ) |
| 294 | |
| 295 | |
| 296 | @json_schema_type |
| 297 | class BuildConfig(BaseModel): |
| 298 | name: str |
| 299 | distribution_spec: DistributionSpec = Field( |
| 300 | description="The distribution spec to build including API providers. " |
| 301 | ) |
| 302 | image_type: str = Field( |
| 303 | default="conda", |
| 304 | description="Type of package to build (conda | container)", |
| 305 | ) |