| 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 json |
| 8 | import os |
| 9 | from datetime import datetime |
| 10 | from enum import Enum |
| 11 | from typing import List, Optional |
| 12 | |
| 13 | import pkg_resources |
| 14 | import yaml |
| 15 | |
| 16 | from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR |
| 17 | from llama_toolchain.common.exec import run_with_pty |
| 18 | from llama_toolchain.common.serialize import EnumEncoder |
| 19 | from pydantic import BaseModel |
| 20 | |
| 21 | from termcolor import cprint |
| 22 | |
| 23 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 24 | from llama_toolchain.core.distribution import api_providers, SERVER_DEPENDENCIES |
| 25 | |
| 26 | |
| 27 | class ImageType(Enum): |
| 28 | docker = "docker" |
| 29 | conda = "conda" |
| 30 | |
| 31 | |
| 32 | class Dependencies(BaseModel): |
| 33 | pip_packages: List[str] |
| 34 | docker_image: Optional[str] = None |
| 35 | |
| 36 | |
| 37 | class ApiInput(BaseModel): |
| 38 | api: Api |
| 39 | provider: str |
| 40 | |
| 41 | |
| 42 | def build_package( |
| 43 | api_inputs: List[ApiInput], |
| 44 | image_type: ImageType, |
| 45 | name: str, |
| 46 | distribution_type: Optional[str] = None, |
| 47 | docker_image: Optional[str] = None, |
| 48 | ): |
| 49 | if not distribution_type: |
| 50 | distribution_type = "adhoc" |
| 51 | |
| 52 | build_dir = BUILDS_BASE_DIR / distribution_type / image_type.value |
| 53 | os.makedirs(build_dir, exist_ok=True) |
| 54 | |
| 55 | package_name = name.replace("::", "-") |
| 56 | package_file = build_dir / f"{package_name}.yaml" |
| 57 | |
| 58 | all_providers = api_providers() |
| 59 | |
| 60 | package_deps = Dependencies( |
| 61 | docker_image=docker_image or "python:3.10-slim", |
| 62 | pip_packages=SERVER_DEPENDENCIES, |
| 63 | ) |
| 64 | |
| 65 | stub_config = {} |
| 66 | for api_input in api_inputs: |
| 67 | api = api_input.api |
| 68 | providers_for_api = all_providers[api] |
| 69 | if api_input.provider not in providers_for_api: |
| 70 | raise ValueError( |
| 71 | f"Provider `{api_input.provider}` is not available for API `{api}`" |
| 72 | ) |
| 73 | |
| 74 | provider = providers_for_api[api_input.provider] |
| 75 | package_deps.pip_packages.extend(provider.pip_packages) |
| 76 | if provider.docker_image: |
| 77 | raise ValueError("A stack's dependencies cannot have a docker image") |
| 78 | |
| 79 | stub_config[api.value] = {"provider_type": api_input.provider} |
| 80 | |
| 81 | if package_file.exists(): |
| 82 | cprint( |
| 83 | f"Build `{package_name}` exists; will reconfigure", |
| 84 | color="yellow", |
| 85 | ) |
| 86 | c = PackageConfig(**yaml.safe_load(package_file.read_text())) |
| 87 | for api_str, new_config in stub_config.items(): |
| 88 | if api_str not in c.providers: |
| 89 | c.providers[api_str] = new_config |
| 90 | else: |
| 91 | existing_config = c.providers[api_str] |
| 92 | if existing_config["provider_type"] != new_config["provider_type"]: |
| 93 | cprint( |
| 94 | f"Provider `{api_str}` has changed from `{existing_config}` to `{new_config}`", |
| 95 | color="yellow", |
| 96 | ) |
| 97 | c.providers[api_str] = new_config |
| 98 | else: |
| 99 | c = PackageConfig( |
| 100 | built_at=datetime.now(), |
| 101 | package_name=package_name, |
| 102 | providers=stub_config, |
| 103 | ) |
| 104 | |
| 105 | c.distribution_type = distribution_type |
| 106 | c.docker_image = package_name if image_type == ImageType.docker else None |
| 107 | c.conda_env = package_name if image_type == ImageType.conda else None |
| 108 | |
| 109 | with open(package_file, "w") as f: |
| 110 | to_write = json.loads(json.dumps(c.dict(), cls=EnumEncoder)) |
| 111 | f.write(yaml.dump(to_write, sort_keys=False)) |
| 112 | |
| 113 | if image_type == ImageType.docker: |
| 114 | script = pkg_resources.resource_filename( |
| 115 | "llama_toolchain", "core/build_container.sh" |
| 116 | ) |
| 117 | args = [ |
| 118 | script, |
| 119 | distribution_type, |
| 120 | package_name, |
| 121 | package_deps.docker_image, |
| 122 | str(package_file), |
| 123 | " ".join(package_deps.pip_packages), |
| 124 | ] |
| 125 | else: |
| 126 | script = pkg_resources.resource_filename( |
| 127 | "llama_toolchain", "core/build_conda_env.sh" |
| 128 | ) |
| 129 | args = [ |
| 130 | script, |
| 131 | distribution_type, |
| 132 | package_name, |
| 133 | str(package_file), |
| 134 | " ".join(package_deps.pip_packages), |
| 135 | ] |
| 136 | |
| 137 | return_code = run_with_pty(args) |
| 138 | if return_code != 0: |
| 139 | cprint( |
| 140 | f"Failed to build target {package_name} with return code {return_code}", |
| 141 | color="red", |
| 142 | ) |
| 143 | return |
| 144 | |
| 145 | cprint( |
| 146 | f"Target `{package_name}` built with configuration at {str(package_file)}", |
| 147 | color="green", |
| 148 | ) |