| 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 termcolor import cprint |
| 13 | |
| 14 | from llama_stack.distribution.distribution import api_providers, stack_apis |
| 15 | from llama_stack.distribution.utils.dynamic import instantiate_class_type |
| 16 | |
| 17 | from llama_stack.distribution.utils.prompt_for_config import prompt_for_config |
| 18 | |
| 19 | |
| 20 | # These are hacks so we can re-use the `prompt_for_config` utility |
| 21 | # This needs a bunch of work to be made very user friendly. |
| 22 | class ReqApis(BaseModel): |
| 23 | apis_to_serve: List[str] |
| 24 | |
| 25 | |
| 26 | def make_routing_entry_type(config_class: Any): |
| 27 | class BaseModelWithConfig(BaseModel): |
| 28 | routing_key: str |
| 29 | config: config_class |
| 30 | |
| 31 | return BaseModelWithConfig |
| 32 | |
| 33 | |
| 34 | # TODO: make sure we can deal with existing configuration values correctly |
| 35 | # instead of just overwriting them |
| 36 | def configure_api_providers( |
| 37 | config: StackRunConfig, spec: DistributionSpec |
| 38 | ) -> StackRunConfig: |
| 39 | cprint("Configuring APIs to serve...", "white", attrs=["bold"]) |
| 40 | print("Enter comma-separated list of APIs to serve:") |
| 41 | |
| 42 | apis = config.apis_to_serve or list(spec.providers.keys()) |
| 43 | apis = [a for a in apis if a != "telemetry"] |
| 44 | req_apis = ReqApis( |
| 45 | apis_to_serve=apis, |
| 46 | ) |
| 47 | req_apis = prompt_for_config(ReqApis, req_apis) |
| 48 | config.apis_to_serve = req_apis.apis_to_serve |
| 49 | print("") |
| 50 | |
| 51 | apis = [v.value for v in stack_apis()] |
| 52 | all_providers = api_providers() |
| 53 | |
| 54 | for api_str in spec.providers.keys(): |
| 55 | if api_str not in apis: |
| 56 | raise ValueError(f"Unknown API `{api_str}`") |
| 57 | |
| 58 | cprint(f"Configuring API `{api_str}`...\n", "white", attrs=["bold"]) |
| 59 | api = Api(api_str) |
| 60 | |
| 61 | provider_or_providers = spec.providers[api_str] |
| 62 | if isinstance(provider_or_providers, list) and len(provider_or_providers) > 1: |
| 63 | print( |
| 64 | "You have specified multiple providers for this API. We will configure a routing table now. For each provider, provide a routing key followed by provider configuration.\n" |
| 65 | ) |
| 66 | |
| 67 | routing_entries = [] |
| 68 | for p in provider_or_providers: |
| 69 | print(f"Configuring provider `{p}`...") |
| 70 | provider_spec = all_providers[api][p] |
| 71 | config_type = instantiate_class_type(provider_spec.config_class) |
| 72 | |
| 73 | # TODO: we need to validate the routing keys, and |
| 74 | # perhaps it is better if we break this out into asking |
| 75 | # for a routing key separately from the associated config |
| 76 | wrapper_type = make_routing_entry_type(config_type) |
| 77 | rt_entry = prompt_for_config(wrapper_type, None) |
| 78 | |
| 79 | routing_entries.append( |
| 80 | ProviderRoutingEntry( |
| 81 | provider_id=p, |
| 82 | routing_key=rt_entry.routing_key, |
| 83 | config=rt_entry.config.dict(), |
| 84 | ) |
| 85 | ) |
| 86 | config.provider_map[api_str] = routing_entries |
| 87 | else: |
| 88 | p = ( |
| 89 | provider_or_providers[0] |
| 90 | if isinstance(provider_or_providers, list) |
| 91 | else provider_or_providers |
| 92 | ) |
| 93 | print(f"Configuring provider `{p}`...") |
| 94 | provider_spec = all_providers[api][p] |
| 95 | config_type = instantiate_class_type(provider_spec.config_class) |
| 96 | try: |
| 97 | provider_config = config.provider_map.get(api_str) |
| 98 | if provider_config: |
| 99 | existing = config_type(**provider_config.config) |
| 100 | else: |
| 101 | existing = None |
| 102 | except Exception: |
| 103 | existing = None |
| 104 | cfg = prompt_for_config(config_type, existing) |
| 105 | config.provider_map[api_str] = GenericProviderConfig( |
| 106 | provider_id=p, |
| 107 | config=cfg.dict(), |
| 108 | ) |
| 109 | |
| 110 | return config |