Elffuss BraidHub

kikocis/llama-stack public

7b7b9b1ba243 · 3.4 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 yaml
12
13from llama_stack.cli.subcommand import Subcommand
14from llama_stack.distribution.datatypes import * # noqa: F403
15
16
17class StackRun(Subcommand):
18 def __init__(self, subparsers: argparse._SubParsersAction):
19 super().__init__()
20 self.parser = subparsers.add_parser(
21 "run",
22 prog="llama stack run",
23 description="""start the server for a Llama Stack Distribution. You should have already built (or downloaded) and configured the distribution.""",
24 formatter_class=argparse.RawTextHelpFormatter,
25 )
26 self._add_arguments()
27 self.parser.set_defaults(func=self._run_stack_run_cmd)
28
29 def _add_arguments(self):
30 self.parser.add_argument(
31 "config",
32 type=str,
33 help="Path to config file to use for the run",
34 )
35 self.parser.add_argument(
36 "--port",
37 type=int,
38 help="Port to run the server on. Defaults to 5000",
39 default=5000,
40 )
41 self.parser.add_argument(
42 "--disable-ipv6",
43 action="store_true",
44 help="Disable IPv6 support",
45 default=False,
46 )
47
48 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
49 import pkg_resources
50 from llama_stack.distribution.build import ImageType
51 from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR
52
53 from llama_stack.distribution.utils.exec import run_with_pty
54
55 if not args.config:
56 self.parser.error("Must specify a config file to run")
57 return
58
59 config_file = Path(args.config)
60 if not config_file.exists() and not args.config.endswith(".yaml"):
61 # check if it's a build config saved to conda dir
62 config_file = Path(
63 BUILDS_BASE_DIR / ImageType.conda.value / f"{args.config}-run.yaml"
64 )
65
66 if not config_file.exists() and not args.config.endswith(".yaml"):
67 # check if it's a build config saved to docker dir
68 config_file = Path(
69 BUILDS_BASE_DIR / ImageType.docker.value / f"{args.config}-run.yaml"
70 )
71
72 if not config_file.exists():
73 self.parser.error(
74 f"File {str(config_file)} does not exist. Please run `llama stack build` and `llama stack configure <name>` to generate a run.yaml file"
75 )
76 return
77
78 with open(config_file, "r") as f:
79 config = StackRunConfig(**yaml.safe_load(f))
80
81 if config.docker_image:
82 script = pkg_resources.resource_filename(
83 "llama_stack",
84 "distribution/start_container.sh",
85 )
86 run_args = [script, config.docker_image]
87 else:
88 script = pkg_resources.resource_filename(
89 "llama_stack",
90 "distribution/start_conda_env.sh",
91 )
92 run_args = [
93 script,
94 config.conda_env,
95 ]
96
97 run_args.extend([str(config_file), str(args.port)])
98 if args.disable_ipv6:
99 run_args.append("--disable-ipv6")
100
101 run_with_pty(run_args)