Elffuss BraidHub

kikocis/llama-stack public

6513a722f4e4 · 2.6 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
7import argparse
8import json
9from pathlib import Path
10
11import yaml
12
13from llama_toolchain.cli.subcommand import Subcommand
14from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
15from termcolor import cprint
16from llama_toolchain.core.datatypes import * # noqa: F403
17
18
19class StackConfigure(Subcommand):
20 """Llama cli for configuring llama toolchain configs"""
21
22 def __init__(self, subparsers: argparse._SubParsersAction):
23 super().__init__()
24 self.parser = subparsers.add_parser(
25 "configure",
26 prog="llama stack configure",
27 description="configure a llama stack distribution",
28 formatter_class=argparse.RawTextHelpFormatter,
29 )
30 self._add_arguments()
31 self.parser.set_defaults(func=self._run_stack_configure_cmd)
32
33 def _add_arguments(self):
34 from llama_toolchain.core.distribution_registry import (
35 available_distribution_specs,
36 )
37 from llama_toolchain.core.package import ImageType
38
39 allowed_ids = [d.distribution_type for d in available_distribution_specs()]
40 self.parser.add_argument(
41 "config",
42 type=str,
43 help="Path to the package config file (e.g. ~/.llama/builds/<distribution>/<image_type>/<name>.yaml)",
44 )
45
46 def _run_stack_configure_cmd(self, args: argparse.Namespace) -> None:
47 from llama_toolchain.core.package import ImageType
48
49 config_file = Path(args.config)
50 if not config_file.exists():
51 self.parser.error(
52 f"Could not find {config_file}. Please run `llama stack build` first"
53 )
54 return
55
56 configure_llama_distribution(config_file)
57
58
59def configure_llama_distribution(config_file: Path) -> None:
60 from llama_toolchain.common.serialize import EnumEncoder
61 from llama_toolchain.core.configure import configure_api_providers
62
63 with open(config_file, "r") as f:
64 config = PackageConfig(**yaml.safe_load(f))
65
66 if config.providers:
67 cprint(
68 f"Configuration already exists for {config.distribution_type}. Will overwrite...",
69 "yellow",
70 attrs=["bold"],
71 )
72
73 config.providers = configure_api_providers(config.providers)
74
75 with open(config_file, "w") as fp:
76 to_write = json.loads(json.dumps(config.dict(), cls=EnumEncoder))
77 fp.write(yaml.dump(to_write, sort_keys=False))
78
79 print(f"YAML configuration has been written to {config_file}")