| 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 |
| 8 | |
| 9 | from pydantic import BaseModel |
| 10 | |
| 11 | from llama_stack.distribution.datatypes import * # noqa: F403 |
| 12 | from llama_stack.apis.memory.memory import MemoryBankType |
| 13 | from llama_stack.distribution.distribution import ( |
| 14 | api_providers, |
| 15 | builtin_automatically_routed_apis, |
| 16 | stack_apis, |
| 17 | ) |
| 18 | from llama_stack.distribution.utils.dynamic import instantiate_class_type |
| 19 | |
| 20 | from llama_stack.distribution.utils.prompt_for_config import prompt_for_config |
| 21 | from llama_stack.providers.impls.meta_reference.safety.config import ( |
| 22 | MetaReferenceShieldType, |
| 23 | ) |
| 24 | from prompt_toolkit import prompt |
| 25 | from prompt_toolkit.validation import Validator |
| 26 | from termcolor import cprint |
| 27 | |
| 28 | |
| 29 | def make_routing_entry_type(config_class: Any): |
| 30 | class BaseModelWithConfig(BaseModel): |
| 31 | routing_key: str |
| 32 | config: config_class |
| 33 | |
| 34 | return BaseModelWithConfig |
| 35 | |
| 36 | |
| 37 | def get_builtin_apis(provider_backed_apis: List[str]) -> List[str]: |
| 38 | """Get corresponding builtin APIs given provider backed APIs""" |
| 39 | res = [] |
| 40 | for inf in builtin_automatically_routed_apis(): |
| 41 | if inf.router_api.value in provider_backed_apis: |
| 42 | res.append(inf.routing_table_api.value) |
| 43 | |
| 44 | return res |
| 45 | |
| 46 | |
| 47 | # TODO: make sure we can deal with existing configuration values correctly |
| 48 | # instead of just overwriting them |
| 49 | def configure_api_providers( |
| 50 | config: StackRunConfig, spec: DistributionSpec |
| 51 | ) -> StackRunConfig: |
| 52 | apis = config.apis_to_serve or list(spec.providers.keys()) |
| 53 | # append the bulitin routing APIs |
| 54 | apis += get_builtin_apis(apis) |
| 55 | |
| 56 | router_api2builtin_api = { |
| 57 | inf.router_api.value: inf.routing_table_api.value |
| 58 | for inf in builtin_automatically_routed_apis() |
| 59 | } |
| 60 | |
| 61 | config.apis_to_serve = list(set([a for a in apis if a != "telemetry"])) |
| 62 | |
| 63 | apis = [v.value for v in stack_apis()] |
| 64 | all_providers = api_providers() |
| 65 | |
| 66 | # configure simple case for with non-routing providers to api_providers |
| 67 | for api_str in spec.providers.keys(): |
| 68 | if api_str not in apis: |
| 69 | raise ValueError(f"Unknown API `{api_str}`") |
| 70 | |
| 71 | cprint(f"Configuring API `{api_str}`...", "green", attrs=["bold"]) |
| 72 | api = Api(api_str) |
| 73 | |
| 74 | p = spec.providers[api_str] |
| 75 | cprint(f"=== Configuring provider `{p}` for API {api_str}...", "green") |
| 76 | |
| 77 | if isinstance(p, list): |
| 78 | cprint( |
| 79 | f"[WARN] Interactive configuration of multiple providers {p} is not supported, configuring {p[0]} only, please manually configure {p[1:]} in routing_table of run.yaml", |
| 80 | "yellow", |
| 81 | ) |
| 82 | p = p[0] |
| 83 | |
| 84 | provider_spec = all_providers[api][p] |
| 85 | config_type = instantiate_class_type(provider_spec.config_class) |
| 86 | try: |
| 87 | provider_config = config.api_providers.get(api_str) |
| 88 | if provider_config: |
| 89 | existing = config_type(**provider_config.config) |
| 90 | else: |
| 91 | existing = None |
| 92 | except Exception: |
| 93 | existing = None |
| 94 | cfg = prompt_for_config(config_type, existing) |
| 95 | |
| 96 | if api_str in router_api2builtin_api: |
| 97 | # a routing api, we need to infer and assign it a routing_key and put it in the routing_table |
| 98 | routing_key = "<PLEASE_FILL_ROUTING_KEY>" |
| 99 | routing_entries = [] |
| 100 | if api_str == "inference": |
| 101 | if hasattr(cfg, "model"): |
| 102 | routing_key = cfg.model |
| 103 | else: |
| 104 | routing_key = prompt( |
| 105 | "> Please enter the supported model your provider has for inference: ", |
| 106 | default="Meta-Llama3.1-8B-Instruct", |
| 107 | ) |
| 108 | routing_entries.append( |
| 109 | RoutableProviderConfig( |
| 110 | routing_key=routing_key, |
| 111 | provider_id=p, |
| 112 | config=cfg.dict(), |
| 113 | ) |
| 114 | ) |
| 115 | |
| 116 | if api_str == "safety": |
| 117 | # TODO: add support for other safety providers, and simplify safety provider config |
| 118 | if p == "meta-reference": |
| 119 | for shield_type in MetaReferenceShieldType: |
| 120 | routing_entries.append( |
| 121 | RoutableProviderConfig( |
| 122 | routing_key=shield_type.value, |
| 123 | provider_id=p, |
| 124 | config=cfg.dict(), |
| 125 | ) |
| 126 | ) |
| 127 | else: |
| 128 | cprint( |
| 129 | f"[WARN] Interactive configuration of safety provider {p} is not supported, please manually configure safety shields types in routing_table of run.yaml", |
| 130 | "yellow", |
| 131 | ) |
| 132 | routing_entries.append( |
| 133 | RoutableProviderConfig( |
| 134 | routing_key=routing_key, |
| 135 | provider_id=p, |
| 136 | config=cfg.dict(), |
| 137 | ) |
| 138 | ) |
| 139 | |
| 140 | if api_str == "memory": |
| 141 | bank_types = list([x.value for x in MemoryBankType]) |
| 142 | routing_key = prompt( |
| 143 | "> Please enter the supported memory bank type your provider has for memory: ", |
| 144 | default="vector", |
| 145 | validator=Validator.from_callable( |
| 146 | lambda x: x in bank_types, |
| 147 | error_message="Invalid provider, please enter one of the following: {}".format( |
| 148 | bank_types |
| 149 | ), |
| 150 | ), |
| 151 | ) |
| 152 | routing_entries.append( |
| 153 | RoutableProviderConfig( |
| 154 | routing_key=routing_key, |
| 155 | provider_id=p, |
| 156 | config=cfg.dict(), |
| 157 | ) |
| 158 | ) |
| 159 | |
| 160 | config.routing_table[api_str] = routing_entries |
| 161 | config.api_providers[api_str] = PlaceholderProviderConfig( |
| 162 | providers=p if isinstance(p, list) else [p] |
| 163 | ) |
| 164 | else: |
| 165 | config.api_providers[api_str] = GenericProviderConfig( |
| 166 | provider_id=p, |
| 167 | config=cfg.dict(), |
| 168 | ) |
| 169 | |
| 170 | print("") |
| 171 | |
| 172 | return config |