| 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 pathlib import Path |
| 25 | |
| 26 | from llama_toolchain.core.distribution import api_providers, SERVER_DEPENDENCIES |
| 27 | |
| 28 | |
| 29 | class ImageType(Enum): |
| 30 | docker = "docker" |
| 31 | conda = "conda" |
| 32 | |
| 33 | |
| 34 | class Dependencies(BaseModel): |
| 35 | pip_packages: List[str] |
| 36 | docker_image: Optional[str] = None |
| 37 | |
| 38 | |
| 39 | class ApiInput(BaseModel): |
| 40 | api: Api |
| 41 | provider: str |
| 42 | |
| 43 | |
| 44 | def build_package(build_config: BuildConfig, build_file_path: Path): |
| 45 | package_deps = Dependencies( |
| 46 | docker_image=build_config.distribution_spec.docker_image or "python:3.10-slim", |
| 47 | pip_packages=SERVER_DEPENDENCIES, |
| 48 | ) |
| 49 | |
| 50 | # extend package dependencies based on providers spec |
| 51 | all_providers = api_providers() |
| 52 | for api_str, provider in build_config.distribution_spec.providers.items(): |
| 53 | providers_for_api = all_providers[Api(api_str)] |
| 54 | if provider not in providers_for_api: |
| 55 | raise ValueError( |
| 56 | f"Provider `{provider}` is not available for API `{api_str}`" |
| 57 | ) |
| 58 | |
| 59 | provider_spec = providers_for_api[provider] |
| 60 | package_deps.pip_packages.extend(provider_spec.pip_packages) |
| 61 | if provider_spec.docker_image: |
| 62 | raise ValueError("A stack's dependencies cannot have a docker image") |
| 63 | |
| 64 | if build_config.image_type == ImageType.docker.value: |
| 65 | script = pkg_resources.resource_filename( |
| 66 | "llama_toolchain", "core/build_container.sh" |
| 67 | ) |
| 68 | args = [ |
| 69 | script, |
| 70 | build_config.name, |
| 71 | package_deps.docker_image, |
| 72 | str(build_file_path), |
| 73 | " ".join(package_deps.pip_packages), |
| 74 | ] |
| 75 | else: |
| 76 | script = pkg_resources.resource_filename( |
| 77 | "llama_toolchain", "core/build_conda_env.sh" |
| 78 | ) |
| 79 | args = [ |
| 80 | script, |
| 81 | build_config.name, |
| 82 | " ".join(package_deps.pip_packages), |
| 83 | ] |
| 84 | |
| 85 | return_code = run_with_pty(args) |
| 86 | if return_code != 0: |
| 87 | cprint( |
| 88 | f"Failed to build target {build_config.name} with return code {return_code}", |
| 89 | color="red", |
| 90 | ) |
| 91 | return |