| 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 | |
| 12 | |
| 13 | def parse_api_provider_tuples( |
| 14 | tuples: str, parser: argparse.ArgumentParser |
| 15 | ) -> Dict[str, ProviderSpec]: |
| 16 | from llama_toolchain.core.distribution import api_providers |
| 17 | |
| 18 | all_providers = api_providers() |
| 19 | |
| 20 | deps = {} |
| 21 | for dep in tuples.split(","): |
| 22 | dep = dep.strip() |
| 23 | if not dep: |
| 24 | continue |
| 25 | api_str, provider = dep.split("=") |
| 26 | api = Api(api_str) |
| 27 | |
| 28 | provider = provider.strip() |
| 29 | if provider not in all_providers[api]: |
| 30 | parser.error(f"Provider `{provider}` is not available for API `{api}`") |
| 31 | return |
| 32 | deps[api] = all_providers[api][provider] |
| 33 | |
| 34 | return deps |
| 35 | |
| 36 | |
| 37 | class StackBuild(Subcommand): |
| 38 | def __init__(self, subparsers: argparse._SubParsersAction): |
| 39 | super().__init__() |
| 40 | self.parser = subparsers.add_parser( |
| 41 | "build", |
| 42 | prog="llama stack build", |
| 43 | description="Build a Llama stack container", |
| 44 | formatter_class=argparse.RawTextHelpFormatter, |
| 45 | ) |
| 46 | self._add_arguments() |
| 47 | self.parser.set_defaults(func=self._run_stack_build_command) |
| 48 | |
| 49 | def _add_arguments(self): |
| 50 | from llama_toolchain.core.distribution_registry import available_distribution_specs |
| 51 | from llama_toolchain.core.package import ( |
| 52 | BuildType, |
| 53 | ) |
| 54 | |
| 55 | allowed_ids = [d.distribution_type for d in available_distribution_specs()] |
| 56 | self.parser.add_argument( |
| 57 | "distribution", |
| 58 | type=str, |
| 59 | help="Distribution to build (either \"adhoc\" OR one of: {})".format(allowed_ids), |
| 60 | ) |
| 61 | self.parser.add_argument( |
| 62 | "api_providers", |
| 63 | nargs='?', |
| 64 | help="Comma separated list of (api=provider) tuples", |
| 65 | ) |
| 66 | |
| 67 | self.parser.add_argument( |
| 68 | "--name", |
| 69 | type=str, |
| 70 | help="Name of the build target (image, conda env)", |
| 71 | required=True, |
| 72 | ) |
| 73 | self.parser.add_argument( |
| 74 | "--type", |
| 75 | type=str, |
| 76 | default="conda_env", |
| 77 | choices=[v.value for v in BuildType], |
| 78 | ) |
| 79 | |
| 80 | def _run_stack_build_command(self, args: argparse.Namespace) -> None: |
| 81 | from llama_toolchain.core.distribution_registry import resolve_distribution_spec |
| 82 | from llama_toolchain.core.package import ( |
| 83 | ApiInput, |
| 84 | BuildType, |
| 85 | build_package, |
| 86 | ) |
| 87 | |
| 88 | api_inputs = [] |
| 89 | if args.distribution == "adhoc": |
| 90 | if not args.api_providers: |
| 91 | self.parser.error("You must specify API providers with (api=provider,...) for building an adhoc distribution") |
| 92 | return |
| 93 | |
| 94 | parsed = parse_api_provider_tuples(args.api_providers, self.parser) |
| 95 | for api, provider_spec in parsed.items(): |
| 96 | for dep in provider_spec.api_dependencies: |
| 97 | if dep not in parsed: |
| 98 | self.parser.error(f"API {api} needs dependency {dep} provided also") |
| 99 | return |
| 100 | |
| 101 | api_inputs.append( |
| 102 | ApiInput( |
| 103 | api=api, |
| 104 | provider=provider_spec.provider_type, |
| 105 | ) |
| 106 | ) |
| 107 | docker_image = None |
| 108 | else: |
| 109 | if args.api_providers: |
| 110 | self.parser.error("You cannot specify API providers for pre-registered distributions") |
| 111 | return |
| 112 | |
| 113 | dist = resolve_distribution_spec(args.distribution) |
| 114 | if dist is None: |
| 115 | self.parser.error(f"Could not find distribution {args.distribution}") |
| 116 | return |
| 117 | |
| 118 | for api, provider_type in dist.providers.items(): |
| 119 | api_inputs.append( |
| 120 | ApiInput( |
| 121 | api=api, |
| 122 | provider=provider_type, |
| 123 | ) |
| 124 | ) |
| 125 | docker_image = dist.docker_image |
| 126 | |
| 127 | build_package( |
| 128 | api_inputs, |
| 129 | build_type=BuildType(args.type), |
| 130 | name=args.name, |
| 131 | distribution_type=args.distribution, |
| 132 | docker_image=docker_image, |
| 133 | ) |