Elffuss BraidHub

kikocis/llama-stack public

566afbf5da2b · 3.5 KiBRawPermalink
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
7from typing import Any
8
9from pydantic import BaseModel
10
11from llama_stack.distribution.datatypes import * # noqa: F403
12from termcolor import cprint
13
14from llama_stack.distribution.distribution import api_providers, stack_apis
15from llama_stack.distribution.utils.dynamic import instantiate_class_type
16
17from llama_stack.distribution.utils.prompt_for_config import prompt_for_config
18
19
20def make_routing_entry_type(config_class: Any):
21 class BaseModelWithConfig(BaseModel):
22 routing_key: str
23 config: config_class
24
25 return BaseModelWithConfig
26
27
28# TODO: make sure we can deal with existing configuration values correctly
29# instead of just overwriting them
30def configure_api_providers(
31 config: StackRunConfig, spec: DistributionSpec
32) -> StackRunConfig:
33 apis = config.apis_to_serve or list(spec.providers.keys())
34 config.apis_to_serve = [a for a in apis if a != "telemetry"]
35
36 apis = [v.value for v in stack_apis()]
37 all_providers = api_providers()
38
39 for api_str in spec.providers.keys():
40 if api_str not in apis:
41 raise ValueError(f"Unknown API `{api_str}`")
42
43 cprint(f"Configuring API `{api_str}`...\n", "white", attrs=["bold"])
44 api = Api(api_str)
45
46 provider_or_providers = spec.providers[api_str]
47 if isinstance(provider_or_providers, list) and len(provider_or_providers) > 1:
48 print(
49 "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"
50 )
51
52 routing_entries = []
53 for p in provider_or_providers:
54 print(f"Configuring provider `{p}`...")
55 provider_spec = all_providers[api][p]
56 config_type = instantiate_class_type(provider_spec.config_class)
57
58 # TODO: we need to validate the routing keys, and
59 # perhaps it is better if we break this out into asking
60 # for a routing key separately from the associated config
61 wrapper_type = make_routing_entry_type(config_type)
62 rt_entry = prompt_for_config(wrapper_type, None)
63
64 routing_entries.append(
65 ProviderRoutingEntry(
66 provider_id=p,
67 routing_key=rt_entry.routing_key,
68 config=rt_entry.config.dict(),
69 )
70 )
71 config.provider_map[api_str] = routing_entries
72 else:
73 p = (
74 provider_or_providers[0]
75 if isinstance(provider_or_providers, list)
76 else provider_or_providers
77 )
78 print(f"Configuring provider `{p}`...")
79 provider_spec = all_providers[api][p]
80 config_type = instantiate_class_type(provider_spec.config_class)
81 try:
82 provider_config = config.provider_map.get(api_str)
83 if provider_config:
84 existing = config_type(**provider_config.config)
85 else:
86 existing = None
87 except Exception:
88 existing = None
89 cfg = prompt_for_config(config_type, existing)
90 config.provider_map[api_str] = GenericProviderConfig(
91 provider_id=p,
92 config=cfg.dict(),
93 )
94
95 return config