Elffuss BraidHub

kikocis/llama-stack public

371459e1ee27 · 3.1 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
12from termcolor import cprint
13
14from llama_toolchain.cli.subcommand import Subcommand
15from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
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 BuildType
38
39 allowed_ids = [d.distribution_type for d in available_distribution_specs()]
40 self.parser.add_argument(
41 "distribution",
42 type=str,
43 help='Distribution ("adhoc" or one of: {})'.format(allowed_ids),
44 )
45 self.parser.add_argument(
46 "--name",
47 type=str,
48 help="Name of the build",
49 required=True,
50 )
51 self.parser.add_argument(
52 "--type",
53 type=str,
54 default="conda_env",
55 choices=[v.value for v in BuildType],
56 )
57
58 def _run_stack_configure_cmd(self, args: argparse.Namespace) -> None:
59 from llama_toolchain.core.package import BuildType
60
61 build_type = BuildType(args.type)
62 name = args.name
63 config_file = (
64 BUILDS_BASE_DIR
65 / args.distribution
66 / build_type.descriptor()
67 / f"{name}.yaml"
68 )
69 if not config_file.exists():
70 self.parser.error(
71 f"Could not find {config_file}. Please run `llama stack build` first"
72 )
73 return
74
75 configure_llama_distribution(config_file)
76
77
78def configure_llama_distribution(config_file: Path) -> None:
79 from llama_toolchain.common.serialize import EnumEncoder
80 from llama_toolchain.core.configure import configure_api_providers
81
82 with open(config_file, "r") as f:
83 config = PackageConfig(**yaml.safe_load(f))
84
85 if config.providers:
86 cprint(
87 f"Configuration already exists for {config.distribution_type}. Will overwrite...",
88 "yellow",
89 attrs=["bold"],
90 )
91
92 config.providers = configure_api_providers(config.providers)
93
94 with open(config_file, "w") as fp:
95 to_write = json.loads(json.dumps(config.dict(), cls=EnumEncoder))
96 fp.write(yaml.dump(to_write, sort_keys=False))
97
98 print(f"YAML configuration has been written to {config_file}")