config file for build

This commit is contained in:
Xi Yan 2024-09-10 11:02:46 -07:00
parent 4f021de10f
commit 0981193d78
2 changed files with 94 additions and 25 deletions

View file

@ -8,6 +8,7 @@ import argparse
from llama_toolchain.cli.subcommand import Subcommand from llama_toolchain.cli.subcommand import Subcommand
from llama_toolchain.core.datatypes import * # noqa: F403 from llama_toolchain.core.datatypes import * # noqa: F403
from llama_toolchain.common.config_dirs import DISTRIBS_BASE_DIR
def parse_api_provider_tuples( def parse_api_provider_tuples(
@ -47,20 +48,22 @@ class StackBuild(Subcommand):
self.parser.set_defaults(func=self._run_stack_build_command) self.parser.set_defaults(func=self._run_stack_build_command)
def _add_arguments(self): def _add_arguments(self):
from llama_toolchain.core.distribution_registry import available_distribution_specs from llama_toolchain.core.distribution_registry import (
from llama_toolchain.core.package import ( available_distribution_specs,
BuildType,
) )
from llama_toolchain.core.package import BuildType
allowed_ids = [d.distribution_type for d in available_distribution_specs()] allowed_ids = [d.distribution_type for d in available_distribution_specs()]
self.parser.add_argument( self.parser.add_argument(
"distribution", "distribution",
type=str, type=str,
help="Distribution to build (either \"adhoc\" OR one of: {})".format(allowed_ids), help='Distribution to build (either "adhoc" OR one of: {})'.format(
allowed_ids
),
) )
self.parser.add_argument( self.parser.add_argument(
"api_providers", "api_providers",
nargs='?', nargs="?",
help="Comma separated list of (api=provider) tuples", help="Comma separated list of (api=provider) tuples",
) )
@ -71,31 +74,38 @@ class StackBuild(Subcommand):
required=True, required=True,
) )
self.parser.add_argument( self.parser.add_argument(
"--type", "--package-type",
type=str, type=str,
default="conda_env", default="conda_env",
choices=[v.value for v in BuildType], choices=[v.value for v in BuildType],
) )
self.parser.add_argument(
def _run_stack_build_command(self, args: argparse.Namespace) -> None: "--config-file",
from llama_toolchain.core.distribution_registry import resolve_distribution_spec type=str,
from llama_toolchain.core.package import ( help="Path to a config file to use for the build",
ApiInput,
BuildType,
build_package,
) )
def _run_stack_build_command_from_build_config(
self, build_config: BuildConfig
) -> None:
from llama_toolchain.core.distribution_registry import resolve_distribution_spec
from llama_toolchain.core.package import ApiInput, build_package, BuildType
api_inputs = [] api_inputs = []
if args.distribution == "adhoc": if build_config.distribution == "adhoc":
if not args.api_providers: if not build_config.api_providers:
self.parser.error("You must specify API providers with (api=provider,...) for building an adhoc distribution") self.parser.error(
"You must specify API providers with (api=provider,...) for building an adhoc distribution"
)
return return
parsed = parse_api_provider_tuples(args.api_providers, self.parser) parsed = parse_api_provider_tuples(build_config.api_providers, self.parser)
for api, provider_spec in parsed.items(): for api, provider_spec in parsed.items():
for dep in provider_spec.api_dependencies: for dep in provider_spec.api_dependencies:
if dep not in parsed: if dep not in parsed:
self.parser.error(f"API {api} needs dependency {dep} provided also") self.parser.error(
f"API {api} needs dependency {dep} provided also"
)
return return
api_inputs.append( api_inputs.append(
@ -106,13 +116,17 @@ class StackBuild(Subcommand):
) )
docker_image = None docker_image = None
else: else:
if args.api_providers: if build_config.api_providers:
self.parser.error("You cannot specify API providers for pre-registered distributions") self.parser.error(
"You cannot specify API providers for pre-registered distributions"
)
return return
dist = resolve_distribution_spec(args.distribution) dist = resolve_distribution_spec(build_config.distribution)
if dist is None: if dist is None:
self.parser.error(f"Could not find distribution {args.distribution}") self.parser.error(
f"Could not find distribution {build_config.distribution}"
)
return return
for api, provider_type in dist.providers.items(): for api, provider_type in dist.providers.items():
@ -126,8 +140,47 @@ class StackBuild(Subcommand):
build_package( build_package(
api_inputs, api_inputs,
build_type=BuildType(args.type), build_type=BuildType(build_config.package_type),
name=args.name, name=build_config.name,
distribution_type=args.distribution, distribution_type=build_config.distribution,
docker_image=docker_image, docker_image=docker_image,
) )
# save build.yaml spec for building same distribution again
build_dir = (
DISTRIBS_BASE_DIR
/ build_config.distribution
/ BuildType(build_config.package_type).descriptor()
)
os.makedirs(build_dir, exist_ok=True)
build_file_path = build_dir / f"{build_config.name}-build.yaml"
with open(build_file_path, "w") as f:
to_write = json.loads(json.dumps(build_config.dict(), cls=EnumEncoder))
f.write(yaml.dump(to_write, sort_keys=False))
cprint(
f"Build spec configuration saved at {str(build_file_path)}",
color="green",
)
def _run_stack_build_command(self, args: argparse.Namespace) -> None:
if args.config_file:
with open(args.config_file, "r") as f:
try:
build_config = BuildConfig(**yaml.safe_load(f))
except Exception as e:
self.parser.error(
f"Could not parse config file {args.config_file}: {e}"
)
return
self._run_stack_build_command_from_build_config(build_config)
return
build_config = BuildConfig(
name=args.name,
distribution_type=args.distribution,
package_type=args.package_type,
api_providers=args.api_providers,
)
self._run_stack_build_command_from_build_config(build_config)

View file

@ -188,3 +188,19 @@ Provider configurations for each of the APIs provided by this package. This incl
the dependencies of these providers as well. the dependencies of these providers as well.
""", """,
) )
@json_schema_type
class BuildConfig(BaseModel):
name: str
distribution: str = Field(
default="local", description="Type of distribution to build (adhoc | {})"
)
api_providers: Optional[str] = Field(
default_factory=list,
description="List of API provider names to build",
)
package_type: str = Field(
default="conda_env",
description="Type of package to build (conda_env | container)",
)