Elffuss BraidHub

kikocis/llama-stack public

371459e1ee27 · 3.3 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
8
9from pathlib import Path
10
11import pkg_resources
12import yaml
13
14from llama_toolchain.cli.subcommand import Subcommand
15from llama_toolchain.core.datatypes import * # noqa: F403
16from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
17
18
19class StackRun(Subcommand):
20 def __init__(self, subparsers: argparse._SubParsersAction):
21 super().__init__()
22 self.parser = subparsers.add_parser(
23 "run",
24 prog="llama stack run",
25 description="""start the server for a Llama Stack Distribution. You should have already built (or downloaded) and configured the distribution.""",
26 formatter_class=argparse.RawTextHelpFormatter,
27 )
28 self._add_arguments()
29 self.parser.set_defaults(func=self._run_stack_run_cmd)
30
31 def _add_arguments(self):
32 from llama_toolchain.core.package import BuildType
33
34 self.parser.add_argument(
35 "distribution",
36 type=str,
37 help="Distribution whose build you want to start",
38 )
39 self.parser.add_argument(
40 "--name",
41 type=str,
42 help="Name of the build you want to start",
43 required=True,
44 )
45 self.parser.add_argument(
46 "--type",
47 type=str,
48 default="conda_env",
49 choices=[v.value for v in BuildType],
50 )
51 self.parser.add_argument(
52 "--port",
53 type=int,
54 help="Port to run the server on. Defaults to 5000",
55 default=5000,
56 )
57 self.parser.add_argument(
58 "--disable-ipv6",
59 action="store_true",
60 help="Disable IPv6 support",
61 default=False,
62 )
63
64 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
65 from llama_toolchain.common.exec import run_with_pty
66 from llama_toolchain.core.package import BuildType
67
68 build_type = BuildType(args.type)
69 build_dir = BUILDS_BASE_DIR / args.distribution / build_type.descriptor()
70 path = build_dir / f"{args.name}.yaml"
71
72 config_file = Path(path)
73
74 if not config_file.exists():
75 self.parser.error(
76 f"File {str(config_file)} does not exist. Did you run `llama stack build`?"
77 )
78 return
79
80 with open(config_file, "r") as f:
81 config = PackageConfig(**yaml.safe_load(f))
82
83 if not config.distribution_type:
84 raise ValueError("Build config appears to be corrupt.")
85
86 if config.docker_image:
87 script = pkg_resources.resource_filename(
88 "llama_toolchain",
89 "core/start_container.sh",
90 )
91 run_args = [script, config.docker_image]
92 else:
93 script = pkg_resources.resource_filename(
94 "llama_toolchain",
95 "core/start_conda_env.sh",
96 )
97 run_args = [
98 script,
99 config.conda_env,
100 ]
101
102 run_args.extend([str(config_file), str(args.port)])
103 if args.disable_ipv6:
104 run_args.append("--disable-ipv6")
105
106 run_with_pty(run_args)