Elffuss BraidHub

kikocis/llama-stack public

main · 3.0 KiBRawPermalink
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
7from enum import Enum
8from typing import List, Optional
9
10import pkg_resources
11from pydantic import BaseModel
12
13from termcolor import cprint
14
15from llama_stack.distribution.utils.exec import run_with_pty
16
17from llama_stack.distribution.datatypes import * # noqa: F403
18from pathlib import Path
19
20from llama_stack.distribution.distribution import api_providers, SERVER_DEPENDENCIES
21
22
23class ImageType(Enum):
24 docker = "docker"
25 conda = "conda"
26
27
28class Dependencies(BaseModel):
29 pip_packages: List[str]
30 docker_image: Optional[str] = None
31
32
33class ApiInput(BaseModel):
34 api: Api
35 provider: str
36
37
38def build_image(build_config: BuildConfig, build_file_path: Path):
39 package_deps = Dependencies(
40 docker_image=build_config.distribution_spec.docker_image or "python:3.10-slim",
41 pip_packages=SERVER_DEPENDENCIES,
42 )
43
44 # extend package dependencies based on providers spec
45 all_providers = api_providers()
46 for (
47 api_str,
48 provider_or_providers,
49 ) in build_config.distribution_spec.providers.items():
50 providers_for_api = all_providers[Api(api_str)]
51
52 providers = (
53 provider_or_providers
54 if isinstance(provider_or_providers, list)
55 else [provider_or_providers]
56 )
57
58 for provider in providers:
59 if provider not in providers_for_api:
60 raise ValueError(
61 f"Provider `{provider}` is not available for API `{api_str}`"
62 )
63
64 provider_spec = providers_for_api[provider]
65 package_deps.pip_packages.extend(provider_spec.pip_packages)
66 if provider_spec.docker_image:
67 raise ValueError("A stack's dependencies cannot have a docker image")
68
69 special_deps = []
70 deps = []
71 for package in package_deps.pip_packages:
72 if "--no-deps" in package or "--index-url" in package:
73 special_deps.append(package)
74 else:
75 deps.append(package)
76
77 if build_config.image_type == ImageType.docker.value:
78 script = pkg_resources.resource_filename(
79 "llama_stack", "distribution/build_container.sh"
80 )
81 args = [
82 script,
83 build_config.name,
84 package_deps.docker_image,
85 str(build_file_path),
86 " ".join(deps),
87 ]
88 else:
89 script = pkg_resources.resource_filename(
90 "llama_stack", "distribution/build_conda_env.sh"
91 )
92 args = [
93 script,
94 build_config.name,
95 " ".join(deps),
96 ]
97
98 if special_deps:
99 args.append("#".join(special_deps))
100
101 return_code = run_with_pty(args)
102 if return_code != 0:
103 cprint(
104 f"Failed to build target {build_config.name} with return code {return_code}",
105 color="red",
106 )
107
108 return return_code