| 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 | 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 | default=None, |
| 33 | nargs="*", |
| 34 | help="Path to a config file to use for the build. You can find example configs in llama_stack/distribution/example_configs. If this argument is not provided, you will be prompted to enter information interactively", |
| 35 | ) |
| 36 | |
| 37 | self.parser.add_argument( |
| 38 | "--name", |
| 39 | type=str, |
| 40 | help="Name of the llama stack build to override from template config", |
| 41 | ) |
| 42 | |
| 43 | def _run_stack_build_command_from_build_config( |
| 44 | self, build_config: BuildConfig |
| 45 | ) -> None: |
| 46 | import json |
| 47 | import os |
| 48 | |
| 49 | from llama_stack.distribution.build import ApiInput, build_image, ImageType |
| 50 | |
| 51 | from llama_stack.distribution.utils.config_dirs import DISTRIBS_BASE_DIR |
| 52 | from llama_stack.distribution.utils.serialize import EnumEncoder |
| 53 | from termcolor import cprint |
| 54 | |
| 55 | # save build.yaml spec for building same distribution again |
| 56 | if build_config.image_type == ImageType.docker.value: |
| 57 | # docker needs build file to be in the llama-stack repo dir to be able to copy over to the image |
| 58 | llama_stack_path = Path(os.path.relpath(__file__)).parent.parent.parent |
| 59 | build_dir = ( |
| 60 | llama_stack_path / "configs/distributions" / build_config.image_type |
| 61 | ) |
| 62 | else: |
| 63 | build_dir = ( |
| 64 | Path(os.getenv("CONDA_PREFIX")).parent |
| 65 | / f"llamastack-{build_config.name}" |
| 66 | ) |
| 67 | |
| 68 | os.makedirs(build_dir, exist_ok=True) |
| 69 | build_file_path = build_dir / f"{build_config.name}-build.yaml" |
| 70 | |
| 71 | with open(build_file_path, "w") as f: |
| 72 | to_write = json.loads(json.dumps(build_config.dict(), cls=EnumEncoder)) |
| 73 | f.write(yaml.dump(to_write, sort_keys=False)) |
| 74 | |
| 75 | build_image(build_config, build_file_path) |
| 76 | |
| 77 | cprint( |
| 78 | f"Build spec configuration saved at {str(build_file_path)}", |
| 79 | color="blue", |
| 80 | ) |
| 81 | cprint( |
| 82 | f"You may now run `llama stack configure {build_config.name}` or `llama stack configure {str(build_file_path)}`", |
| 83 | color="green", |
| 84 | ) |
| 85 | |
| 86 | def _run_stack_build_command(self, args: argparse.Namespace) -> None: |
| 87 | from llama_stack.distribution.distribution import Api, api_providers |
| 88 | from llama_stack.distribution.utils.dynamic import instantiate_class_type |
| 89 | from prompt_toolkit import prompt |
| 90 | from prompt_toolkit.validation import Validator |
| 91 | from termcolor import cprint |
| 92 | |
| 93 | if not args.config: |
| 94 | name = prompt( |
| 95 | "> Enter a unique name for identifying your Llama Stack build (e.g. my-local-stack): " |
| 96 | ) |
| 97 | image_type = prompt( |
| 98 | "> Enter the image type you want your Llama Stack to be built as (docker or conda): ", |
| 99 | validator=Validator.from_callable( |
| 100 | lambda x: x in ["docker", "conda"], |
| 101 | error_message="Invalid image type, please enter conda or docker", |
| 102 | ), |
| 103 | default="conda", |
| 104 | ) |
| 105 | |
| 106 | cprint( |
| 107 | f"\n Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs.", |
| 108 | color="green", |
| 109 | ) |
| 110 | |
| 111 | providers = dict() |
| 112 | for api in Api: |
| 113 | all_providers = api_providers() |
| 114 | providers_for_api = all_providers[api] |
| 115 | |
| 116 | api_provider = prompt( |
| 117 | "> Enter the API provider for the {} API: (default=meta-reference): ".format( |
| 118 | api.value |
| 119 | ), |
| 120 | validator=Validator.from_callable( |
| 121 | lambda x: x in providers_for_api, |
| 122 | error_message="Invalid provider, please enter one of the following: {}".format( |
| 123 | providers_for_api.keys() |
| 124 | ), |
| 125 | ), |
| 126 | default=( |
| 127 | "meta-reference" |
| 128 | if "meta-reference" in providers_for_api |
| 129 | else list(providers_for_api.keys())[0] |
| 130 | ), |
| 131 | ) |
| 132 | |
| 133 | providers[api.value] = api_provider |
| 134 | |
| 135 | description = prompt( |
| 136 | "\n > (Optional) Enter a short description for your Llama Stack: ", |
| 137 | default="", |
| 138 | ) |
| 139 | |
| 140 | distribution_spec = DistributionSpec( |
| 141 | providers=providers, |
| 142 | description=description, |
| 143 | ) |
| 144 | |
| 145 | build_config = BuildConfig( |
| 146 | name=name, image_type=image_type, distribution_spec=distribution_spec |
| 147 | ) |
| 148 | self._run_stack_build_command_from_build_config(build_config) |
| 149 | return |
| 150 | |
| 151 | with open(args.config, "r") as f: |
| 152 | try: |
| 153 | build_config = BuildConfig(**yaml.safe_load(f)) |
| 154 | except Exception as e: |
| 155 | self.parser.error(f"Could not parse config file {args.config}: {e}") |
| 156 | return |
| 157 | if args.name: |
| 158 | build_config.name = args.name |
| 159 | self._run_stack_build_command_from_build_config(build_config) |