Elffuss BraidHub

kikocis/llama-stack public

6513a722f4e4 · 2.9 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 ImageType
33
34 self.parser.add_argument(
35 "config",
36 type=str,
37 help="Path to config file to use for the run",
38 )
39 self.parser.add_argument(
40 "--port",
41 type=int,
42 help="Port to run the server on. Defaults to 5000",
43 default=5000,
44 )
45 self.parser.add_argument(
46 "--disable-ipv6",
47 action="store_true",
48 help="Disable IPv6 support",
49 default=False,
50 )
51
52 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
53 from llama_toolchain.common.exec import run_with_pty
54 from llama_toolchain.core.package import ImageType
55
56 if not args.config:
57 self.parser.error("Must specify a config file to run")
58 return
59
60 path = args.config
61 config_file = Path(path)
62
63 if not config_file.exists():
64 self.parser.error(
65 f"File {str(config_file)} does not exist. Did you run `llama stack build`?"
66 )
67 return
68
69 with open(config_file, "r") as f:
70 config = PackageConfig(**yaml.safe_load(f))
71
72 if not config.distribution_type:
73 raise ValueError("Build config appears to be corrupt.")
74
75 if config.docker_image:
76 script = pkg_resources.resource_filename(
77 "llama_toolchain",
78 "core/start_container.sh",
79 )
80 run_args = [script, config.docker_image]
81 else:
82 script = pkg_resources.resource_filename(
83 "llama_toolchain",
84 "core/start_conda_env.sh",
85 )
86 run_args = [
87 script,
88 config.conda_env,
89 ]
90
91 run_args.extend([str(config_file), str(args.port)])
92 if args.disable_ipv6:
93 run_args.append("--disable-ipv6")
94
95 run_with_pty(run_args)