Elffuss BraidHub

kikocis/llama-stack public

d510759522c8 · 2.7 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
16
17
18class StackRun(Subcommand):
19 def __init__(self, subparsers: argparse._SubParsersAction):
20 super().__init__()
21 self.parser = subparsers.add_parser(
22 "run",
23 prog="llama stack run",
24 description="""start the server for a Llama Stack Distribution. You should have already built (or downloaded) and configured the distribution.""",
25 formatter_class=argparse.RawTextHelpFormatter,
26 )
27 self._add_arguments()
28 self.parser.set_defaults(func=self._run_stack_run_cmd)
29
30 def _add_arguments(self):
31 self.parser.add_argument(
32 "config",
33 type=str,
34 help="Path to config file to use for the run",
35 )
36 self.parser.add_argument(
37 "--port",
38 type=int,
39 help="Port to run the server on. Defaults to 5000",
40 default=5000,
41 )
42 self.parser.add_argument(
43 "--disable-ipv6",
44 action="store_true",
45 help="Disable IPv6 support",
46 default=False,
47 )
48
49 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
50 from llama_toolchain.common.exec import run_with_pty
51
52 if not args.config:
53 self.parser.error("Must specify a config file to run")
54 return
55
56 path = args.config
57 config_file = Path(path)
58
59 if not config_file.exists():
60 self.parser.error(
61 f"File {str(config_file)} does not exist. Did you run `llama stack build`?"
62 )
63 return
64
65 with open(config_file, "r") as f:
66 config = StackRunConfig(**yaml.safe_load(f))
67
68 if config.docker_image:
69 script = pkg_resources.resource_filename(
70 "llama_toolchain",
71 "core/start_container.sh",
72 )
73 run_args = [script, config.docker_image]
74 else:
75 script = pkg_resources.resource_filename(
76 "llama_toolchain",
77 "core/start_conda_env.sh",
78 )
79 run_args = [
80 script,
81 config.conda_env,
82 ]
83
84 run_args.extend([str(config_file), str(args.port)])
85 if args.disable_ipv6:
86 run_args.append("--disable-ipv6")
87
88 run_with_pty(run_args)