| 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_toolchain.cli.subcommand import Subcommand |
| 10 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import yaml |
| 14 | |
| 15 | |
| 16 | class StackBuild(Subcommand): |
| 17 | def __init__(self, subparsers: argparse._SubParsersAction): |
| 18 | super().__init__() |
| 19 | self.parser = subparsers.add_parser( |
| 20 | "build", |
| 21 | prog="llama stack build", |
| 22 | description="Build a Llama stack container", |
| 23 | formatter_class=argparse.RawTextHelpFormatter, |
| 24 | ) |
| 25 | self._add_arguments() |
| 26 | self.parser.set_defaults(func=self._run_stack_build_command) |
| 27 | |
| 28 | def _add_arguments(self): |
| 29 | self.parser.add_argument( |
| 30 | "config", |
| 31 | type=str, |
| 32 | help="Path to a config file to use for the build. You may find example configs in llama_toolchain/configs/distributions", |
| 33 | ) |
| 34 | |
| 35 | self.parser.add_argument( |
| 36 | "--name", |
| 37 | type=str, |
| 38 | help="Name of the llama stack build to override from template config", |
| 39 | ) |
| 40 | |
| 41 | def _run_stack_build_command_from_build_config( |
| 42 | self, build_config: BuildConfig |
| 43 | ) -> None: |
| 44 | import json |
| 45 | import os |
| 46 | |
| 47 | from llama_toolchain.common.config_dirs import DISTRIBS_BASE_DIR |
| 48 | from llama_toolchain.common.serialize import EnumEncoder |
| 49 | from llama_toolchain.core.package import ApiInput, build_package, ImageType |
| 50 | from termcolor import cprint |
| 51 | |
| 52 | # save build.yaml spec for building same distribution again |
| 53 | if build_config.image_type == ImageType.docker.value: |
| 54 | # docker needs build file to be in the llama-stack repo dir to be able to copy over to the image |
| 55 | llama_toolchain_path = Path(os.path.relpath(__file__)).parent.parent.parent |
| 56 | build_dir = ( |
| 57 | llama_toolchain_path / "configs/distributions" / build_config.image_type |
| 58 | ) |
| 59 | else: |
| 60 | build_dir = DISTRIBS_BASE_DIR / build_config.image_type |
| 61 | |
| 62 | os.makedirs(build_dir, exist_ok=True) |
| 63 | build_file_path = build_dir / f"{build_config.name}-build.yaml" |
| 64 | |
| 65 | with open(build_file_path, "w") as f: |
| 66 | to_write = json.loads(json.dumps(build_config.dict(), cls=EnumEncoder)) |
| 67 | f.write(yaml.dump(to_write, sort_keys=False)) |
| 68 | |
| 69 | build_package(build_config, build_file_path) |
| 70 | |
| 71 | cprint( |
| 72 | f"Build spec configuration saved at {str(build_file_path)}", |
| 73 | color="green", |
| 74 | ) |
| 75 | |
| 76 | def _run_stack_build_command(self, args: argparse.Namespace) -> None: |
| 77 | from llama_toolchain.common.prompt_for_config import prompt_for_config |
| 78 | from llama_toolchain.core.dynamic import instantiate_class_type |
| 79 | |
| 80 | if not args.config: |
| 81 | self.parser.error( |
| 82 | "No config file specified. Please use `llama stack build /path/to/*-build.yaml`. Example config files can be found in llama_toolchain/configs/distributions" |
| 83 | ) |
| 84 | return |
| 85 | |
| 86 | with open(args.config, "r") as f: |
| 87 | try: |
| 88 | build_config = BuildConfig(**yaml.safe_load(f)) |
| 89 | except Exception as e: |
| 90 | self.parser.error(f"Could not parse config file {args.config}: {e}") |
| 91 | return |
| 92 | if args.name: |
| 93 | build_config.name = args.name |
| 94 | self._run_stack_build_command_from_build_config(build_config) |