| 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, Union |
| 10 | |
| 11 | from llama_models.schema_utils import json_schema_type |
| 12 | |
| 13 | from pydantic import BaseModel, Field, validator |
| 14 | |
| 15 | |
| 16 | @json_schema_type |
| 17 | class Api(Enum): |
| 18 | inference = "inference" |
| 19 | safety = "safety" |
| 20 | agents = "agents" |
| 21 | memory = "memory" |
| 22 | telemetry = "telemetry" |
| 23 | |
| 24 | |
| 25 | @json_schema_type |
| 26 | class ApiEndpoint(BaseModel): |
| 27 | route: str |
| 28 | method: str |
| 29 | name: str |
| 30 | |
| 31 | |
| 32 | @json_schema_type |
| 33 | class ProviderSpec(BaseModel): |
| 34 | api: Api |
| 35 | provider_id: str |
| 36 | config_class: str = Field( |
| 37 | ..., |
| 38 | description="Fully-qualified classname of the config for this provider", |
| 39 | ) |
| 40 | api_dependencies: List[Api] = Field( |
| 41 | default_factory=list, |
| 42 | description="Higher-level API surfaces may depend on other providers to provide their functionality", |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | @json_schema_type |
| 47 | class RouterProviderSpec(ProviderSpec): |
| 48 | provider_id: str = "router" |
| 49 | config_class: str = "" |
| 50 | |
| 51 | docker_image: Optional[str] = None |
| 52 | |
| 53 | inner_specs: List[ProviderSpec] |
| 54 | module: str = Field( |
| 55 | ..., |
| 56 | description=""" |
| 57 | Fully-qualified name of the module to import. The module is expected to have: |
| 58 | |
| 59 | - `get_router_impl(config, provider_specs, deps)`: returns the router implementation |
| 60 | """, |
| 61 | ) |
| 62 | |
| 63 | @property |
| 64 | def pip_packages(self) -> List[str]: |
| 65 | raise AssertionError("Should not be called on RouterProviderSpec") |
| 66 | |
| 67 | |
| 68 | class GenericProviderConfig(BaseModel): |
| 69 | provider_id: str |
| 70 | config: Dict[str, Any] |
| 71 | |
| 72 | |
| 73 | @json_schema_type |
| 74 | class AdapterSpec(BaseModel): |
| 75 | adapter_id: str = Field( |
| 76 | ..., |
| 77 | description="Unique identifier for this adapter", |
| 78 | ) |
| 79 | module: str = Field( |
| 80 | ..., |
| 81 | description=""" |
| 82 | Fully-qualified name of the module to import. The module is expected to have: |
| 83 | |
| 84 | - `get_adapter_impl(config, deps)`: returns the adapter implementation |
| 85 | """, |
| 86 | ) |
| 87 | pip_packages: List[str] = Field( |
| 88 | default_factory=list, |
| 89 | description="The pip dependencies needed for this implementation", |
| 90 | ) |
| 91 | config_class: Optional[str] = Field( |
| 92 | default=None, |
| 93 | description="Fully-qualified classname of the config for this provider", |
| 94 | ) |
| 95 | |
| 96 | |
| 97 | @json_schema_type |
| 98 | class InlineProviderSpec(ProviderSpec): |
| 99 | pip_packages: List[str] = Field( |
| 100 | default_factory=list, |
| 101 | description="The pip dependencies needed for this implementation", |
| 102 | ) |
| 103 | docker_image: Optional[str] = Field( |
| 104 | default=None, |
| 105 | description=""" |
| 106 | The docker image to use for this implementation. If one is provided, pip_packages will be ignored. |
| 107 | If a provider depends on other providers, the dependencies MUST NOT specify a docker image. |
| 108 | """, |
| 109 | ) |
| 110 | module: str = Field( |
| 111 | ..., |
| 112 | description=""" |
| 113 | Fully-qualified name of the module to import. The module is expected to have: |
| 114 | |
| 115 | - `get_provider_impl(config, deps)`: returns the local implementation |
| 116 | """, |
| 117 | ) |
| 118 | |
| 119 | |
| 120 | class RemoteProviderConfig(BaseModel): |
| 121 | url: str = Field(..., description="The URL for the provider") |
| 122 | |
| 123 | @validator("url") |
| 124 | @classmethod |
| 125 | def validate_url(cls, url: str) -> str: |
| 126 | if not url.startswith("http"): |
| 127 | raise ValueError(f"URL must start with http: {url}") |
| 128 | return url.rstrip("/") |
| 129 | |
| 130 | |
| 131 | def remote_provider_id(adapter_id: str) -> str: |
| 132 | return f"remote::{adapter_id}" |
| 133 | |
| 134 | |
| 135 | @json_schema_type |
| 136 | class RemoteProviderSpec(ProviderSpec): |
| 137 | adapter: Optional[AdapterSpec] = Field( |
| 138 | default=None, |
| 139 | description=""" |
| 140 | If some code is needed to convert the remote responses into Llama Stack compatible |
| 141 | API responses, specify the adapter here. If not specified, it indicates the remote |
| 142 | as being "Llama Stack compatible" |
| 143 | """, |
| 144 | ) |
| 145 | |
| 146 | @property |
| 147 | def docker_image(self) -> Optional[str]: |
| 148 | return None |
| 149 | |
| 150 | @property |
| 151 | def module(self) -> str: |
| 152 | if self.adapter: |
| 153 | return self.adapter.module |
| 154 | return f"llama_stack.apis.{self.api.value}.client" |
| 155 | |
| 156 | @property |
| 157 | def pip_packages(self) -> List[str]: |
| 158 | if self.adapter: |
| 159 | return self.adapter.pip_packages |
| 160 | return [] |
| 161 | |
| 162 | |
| 163 | # Can avoid this by using Pydantic computed_field |
| 164 | def remote_provider_spec( |
| 165 | api: Api, adapter: Optional[AdapterSpec] = None |
| 166 | ) -> RemoteProviderSpec: |
| 167 | config_class = ( |
| 168 | adapter.config_class |
| 169 | if adapter and adapter.config_class |
| 170 | else "llama_stack.distribution.datatypes.RemoteProviderConfig" |
| 171 | ) |
| 172 | provider_id = remote_provider_id(adapter.adapter_id) if adapter else "remote" |
| 173 | |
| 174 | return RemoteProviderSpec( |
| 175 | api=api, provider_id=provider_id, config_class=config_class, adapter=adapter |
| 176 | ) |
| 177 | |
| 178 | |
| 179 | @json_schema_type |
| 180 | class DistributionSpec(BaseModel): |
| 181 | description: Optional[str] = Field( |
| 182 | default="", |
| 183 | description="Description of the distribution", |
| 184 | ) |
| 185 | docker_image: Optional[str] = None |
| 186 | providers: Dict[str, Union[str, List[str]]] = Field( |
| 187 | default_factory=dict, |
| 188 | description=""" |
| 189 | Provider Types for each of the APIs provided by this distribution. If you |
| 190 | select multiple providers, you should provide an appropriate 'routing_map' |
| 191 | in the runtime configuration to help route to the correct provider.""", |
| 192 | ) |
| 193 | |
| 194 | |
| 195 | @json_schema_type |
| 196 | class ProviderRoutingEntry(GenericProviderConfig): |
| 197 | routing_key: str |
| 198 | |
| 199 | |
| 200 | ProviderMapEntry = Union[GenericProviderConfig, List[ProviderRoutingEntry]] |
| 201 | |
| 202 | |
| 203 | @json_schema_type |
| 204 | class StackRunConfig(BaseModel): |
| 205 | built_at: datetime |
| 206 | |
| 207 | image_name: str = Field( |
| 208 | ..., |
| 209 | description=""" |
| 210 | Reference to the distribution this package refers to. For unregistered (adhoc) packages, |
| 211 | this could be just a hash |
| 212 | """, |
| 213 | ) |
| 214 | docker_image: Optional[str] = Field( |
| 215 | default=None, |
| 216 | description="Reference to the docker image if this package refers to a container", |
| 217 | ) |
| 218 | conda_env: Optional[str] = Field( |
| 219 | default=None, |
| 220 | description="Reference to the conda environment if this package refers to a conda environment", |
| 221 | ) |
| 222 | apis_to_serve: List[str] = Field( |
| 223 | description=""" |
| 224 | The list of APIs to serve. If not specified, all APIs specified in the provider_map will be served""", |
| 225 | ) |
| 226 | provider_map: Dict[str, ProviderMapEntry] = Field( |
| 227 | description=""" |
| 228 | Provider configurations for each of the APIs provided by this package. |
| 229 | |
| 230 | Given an API, you can specify a single provider or a "routing table". Each entry in the routing |
| 231 | table has a (routing_key, provider_config) tuple. How the key is interpreted is API-specific. |
| 232 | |
| 233 | As examples: |
| 234 | - the "inference" API interprets the routing_key as a "model" |
| 235 | - the "memory" API interprets the routing_key as the type of a "memory bank" |
| 236 | |
| 237 | The key may support wild-cards alsothe routing_key to route to the correct provider.""", |
| 238 | ) |
| 239 | |
| 240 | |
| 241 | @json_schema_type |
| 242 | class BuildConfig(BaseModel): |
| 243 | name: str |
| 244 | distribution_spec: DistributionSpec = Field( |
| 245 | description="The distribution spec to build including API providers. " |
| 246 | ) |
| 247 | image_type: str = Field( |
| 248 | default="conda", |
| 249 | description="Type of package to build (conda | container)", |
| 250 | ) |