| 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, Dict |
| 8 | |
| 9 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 10 | from termcolor import cprint |
| 11 | |
| 12 | from llama_toolchain.common.prompt_for_config import prompt_for_config |
| 13 | from llama_toolchain.core.distribution import api_providers |
| 14 | from llama_toolchain.core.dynamic import instantiate_class_type |
| 15 | |
| 16 | |
| 17 | def configure_api_providers(existing_configs: Dict[str, Any]) -> None: |
| 18 | all_providers = api_providers() |
| 19 | |
| 20 | provider_configs = {} |
| 21 | for api_str, stub_config in existing_configs.items(): |
| 22 | api = Api(api_str) |
| 23 | providers = all_providers[api] |
| 24 | provider_id = stub_config["provider_id"] |
| 25 | if provider_id not in providers: |
| 26 | raise ValueError( |
| 27 | f"Unknown provider `{provider_id}` is not available for API `{api_str}`" |
| 28 | ) |
| 29 | |
| 30 | provider_spec = providers[provider_id] |
| 31 | cprint(f"Configuring API: {api_str} ({provider_id})", "white", attrs=["bold"]) |
| 32 | config_type = instantiate_class_type(provider_spec.config_class) |
| 33 | |
| 34 | try: |
| 35 | existing_provider_config = config_type(**stub_config) |
| 36 | except Exception: |
| 37 | existing_provider_config = None |
| 38 | |
| 39 | provider_config = prompt_for_config( |
| 40 | config_type, |
| 41 | existing_provider_config, |
| 42 | ) |
| 43 | print("") |
| 44 | |
| 45 | provider_configs[api_str] = { |
| 46 | "provider_id": provider_id, |
| 47 | **provider_config.dict(), |
| 48 | } |
| 49 | |
| 50 | return provider_configs |