| 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 pathlib import Path |
| 10 | |
| 11 | import pkg_resources |
| 12 | import yaml |
| 13 | |
| 14 | from llama_toolchain.cli.subcommand import Subcommand |
| 15 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 16 | from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR |
| 17 | |
| 18 | |
| 19 | class 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 config.docker_image: |
| 73 | script = pkg_resources.resource_filename( |
| 74 | "llama_toolchain", |
| 75 | "core/start_container.sh", |
| 76 | ) |
| 77 | run_args = [script, config.docker_image] |
| 78 | else: |
| 79 | script = pkg_resources.resource_filename( |
| 80 | "llama_toolchain", |
| 81 | "core/start_conda_env.sh", |
| 82 | ) |
| 83 | run_args = [ |
| 84 | script, |
| 85 | config.conda_env, |
| 86 | ] |
| 87 | |
| 88 | run_args.extend([str(config_file), str(args.port)]) |
| 89 | if args.disable_ipv6: |
| 90 | run_args.append("--disable-ipv6") |
| 91 | |
| 92 | run_with_pty(run_args) |