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