mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-07-01 20:18:50 +00:00
CLI Update: build -> configure -> run (#69)
* remove configure from build * remove config from build * configure to regenerate file * update memory providers * remove comments * udpate build script * add reedme * update doc * rename getting started * update build cli * update docker build script * configure update * clean up configure * [tmp fix] hardware requirement tmp fix * clean up build * fix configure * add example build files for conda & docker * remove resolve_distribution_spec * remove available_distribution_specs * example build files * update example build files * more clean up on build * add name args to override name * move distribution to yaml files * generate distribution specs * getting started guide * getting started * add build yaml to Dockerfile * cleanup distribution_dependencies * configure from docker image name * build relative paths * minor comment * getting started * Update getting_started.md * Update getting_started.md * address comments, configure within docker file * remove distribution types! * update getting started * update documentation * remove listing distribution * minor heading * address nits, remove docker_image=null * gitignore
This commit is contained in:
parent
73b71d9689
commit
d9147f3184
27 changed files with 759 additions and 512 deletions
|
@ -8,33 +8,11 @@ import argparse
|
|||
|
||||
from llama_toolchain.cli.subcommand import Subcommand
|
||||
from llama_toolchain.core.datatypes import * # noqa: F403
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def parse_api_provider_tuples(
|
||||
tuples: str, parser: argparse.ArgumentParser
|
||||
) -> Dict[str, ProviderSpec]:
|
||||
from llama_toolchain.core.distribution import api_providers
|
||||
|
||||
all_providers = api_providers()
|
||||
|
||||
deps = {}
|
||||
for dep in tuples.split(","):
|
||||
dep = dep.strip()
|
||||
if not dep:
|
||||
continue
|
||||
api_str, provider = dep.split("=")
|
||||
api = Api(api_str)
|
||||
|
||||
provider = provider.strip()
|
||||
if provider not in all_providers[api]:
|
||||
parser.error(f"Provider `{provider}` is not available for API `{api}`")
|
||||
return
|
||||
deps[api] = all_providers[api][provider]
|
||||
|
||||
return deps
|
||||
|
||||
|
||||
class StackBuild(Subcommand):
|
||||
def __init__(self, subparsers: argparse._SubParsersAction):
|
||||
super().__init__()
|
||||
|
@ -48,16 +26,16 @@ class StackBuild(Subcommand):
|
|||
self.parser.set_defaults(func=self._run_stack_build_command)
|
||||
|
||||
def _add_arguments(self):
|
||||
from llama_toolchain.core.distribution_registry import (
|
||||
available_distribution_specs,
|
||||
)
|
||||
from llama_toolchain.core.package import ImageType
|
||||
|
||||
allowed_ids = [d.distribution_type for d in available_distribution_specs()]
|
||||
self.parser.add_argument(
|
||||
"--config",
|
||||
"config",
|
||||
type=str,
|
||||
help="Path to a config file to use for the build",
|
||||
help="Path to a config file to use for the build. You may find example configs in llama_toolchain/configs/distributions",
|
||||
)
|
||||
|
||||
self.parser.add_argument(
|
||||
"--name",
|
||||
type=str,
|
||||
help="Name of the llama stack build to override from template config",
|
||||
)
|
||||
|
||||
def _run_stack_build_command_from_build_config(
|
||||
|
@ -68,69 +46,19 @@ class StackBuild(Subcommand):
|
|||
|
||||
from llama_toolchain.common.config_dirs import DISTRIBS_BASE_DIR
|
||||
from llama_toolchain.common.serialize import EnumEncoder
|
||||
from llama_toolchain.core.distribution_registry import resolve_distribution_spec
|
||||
from llama_toolchain.core.package import ApiInput, build_package, ImageType
|
||||
from termcolor import cprint
|
||||
|
||||
api_inputs = []
|
||||
if build_config.distribution == "adhoc":
|
||||
if not build_config.api_providers:
|
||||
self.parser.error(
|
||||
"You must specify API providers with (api=provider,...) for building an adhoc distribution"
|
||||
)
|
||||
return
|
||||
|
||||
parsed = parse_api_provider_tuples(build_config.api_providers, self.parser)
|
||||
for api, provider_spec in parsed.items():
|
||||
for dep in provider_spec.api_dependencies:
|
||||
if dep not in parsed:
|
||||
self.parser.error(
|
||||
f"API {api} needs dependency {dep} provided also"
|
||||
)
|
||||
return
|
||||
|
||||
api_inputs.append(
|
||||
ApiInput(
|
||||
api=api,
|
||||
provider=provider_spec.provider_type,
|
||||
)
|
||||
)
|
||||
docker_image = None
|
||||
else:
|
||||
if build_config.api_providers:
|
||||
self.parser.error(
|
||||
"You cannot specify API providers for pre-registered distributions"
|
||||
)
|
||||
return
|
||||
|
||||
dist = resolve_distribution_spec(build_config.distribution)
|
||||
if dist is None:
|
||||
self.parser.error(
|
||||
f"Could not find distribution {build_config.distribution}"
|
||||
)
|
||||
return
|
||||
|
||||
for api, provider_type in dist.providers.items():
|
||||
api_inputs.append(
|
||||
ApiInput(
|
||||
api=api,
|
||||
provider=provider_type,
|
||||
)
|
||||
)
|
||||
docker_image = dist.docker_image
|
||||
|
||||
build_package(
|
||||
api_inputs,
|
||||
image_type=ImageType(build_config.image_type),
|
||||
name=build_config.name,
|
||||
distribution_type=build_config.distribution,
|
||||
docker_image=docker_image,
|
||||
)
|
||||
|
||||
# save build.yaml spec for building same distribution again
|
||||
build_dir = (
|
||||
DISTRIBS_BASE_DIR / build_config.distribution / build_config.image_type
|
||||
)
|
||||
if build_config.image_type == ImageType.docker.value:
|
||||
# docker needs build file to be in the llama-stack repo dir to be able to copy over to the image
|
||||
llama_toolchain_path = Path(os.path.relpath(__file__)).parent.parent.parent
|
||||
build_dir = (
|
||||
llama_toolchain_path / "configs/distributions" / build_config.image_type
|
||||
)
|
||||
else:
|
||||
build_dir = DISTRIBS_BASE_DIR / build_config.image_type
|
||||
|
||||
os.makedirs(build_dir, exist_ok=True)
|
||||
build_file_path = build_dir / f"{build_config.name}-build.yaml"
|
||||
|
||||
|
@ -138,6 +66,8 @@ class StackBuild(Subcommand):
|
|||
to_write = json.loads(json.dumps(build_config.dict(), cls=EnumEncoder))
|
||||
f.write(yaml.dump(to_write, sort_keys=False))
|
||||
|
||||
build_package(build_config, build_file_path)
|
||||
|
||||
cprint(
|
||||
f"Build spec configuration saved at {str(build_file_path)}",
|
||||
color="green",
|
||||
|
@ -147,15 +77,18 @@ class StackBuild(Subcommand):
|
|||
from llama_toolchain.common.prompt_for_config import prompt_for_config
|
||||
from llama_toolchain.core.dynamic import instantiate_class_type
|
||||
|
||||
if args.config:
|
||||
with open(args.config, "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}: {e}")
|
||||
return
|
||||
self._run_stack_build_command_from_build_config(build_config)
|
||||
if not args.config:
|
||||
self.parser.error(
|
||||
"No config file specified. Please use `llama stack build /path/to/*-build.yaml`. Example config files can be found in llama_toolchain/configs/distributions"
|
||||
)
|
||||
return
|
||||
|
||||
build_config = prompt_for_config(BuildConfig, None)
|
||||
self._run_stack_build_command_from_build_config(build_config)
|
||||
with open(args.config, "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}: {e}")
|
||||
return
|
||||
if args.name:
|
||||
build_config.name = args.name
|
||||
self._run_stack_build_command_from_build_config(build_config)
|
||||
|
|
|
@ -8,12 +8,18 @@ import argparse
|
|||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
import pkg_resources
|
||||
|
||||
import yaml
|
||||
from llama_toolchain.cli.subcommand import Subcommand
|
||||
from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
|
||||
|
||||
from llama_toolchain.common.exec import run_with_pty
|
||||
from termcolor import cprint
|
||||
from llama_toolchain.core.datatypes import * # noqa: F403
|
||||
import os
|
||||
|
||||
from termcolor import cprint
|
||||
|
||||
|
||||
class StackConfigure(Subcommand):
|
||||
|
@ -31,49 +37,107 @@ class StackConfigure(Subcommand):
|
|||
self.parser.set_defaults(func=self._run_stack_configure_cmd)
|
||||
|
||||
def _add_arguments(self):
|
||||
from llama_toolchain.core.distribution_registry import (
|
||||
available_distribution_specs,
|
||||
)
|
||||
from llama_toolchain.core.package import ImageType
|
||||
|
||||
allowed_ids = [d.distribution_type for d in available_distribution_specs()]
|
||||
self.parser.add_argument(
|
||||
"config",
|
||||
type=str,
|
||||
help="Path to the package config file (e.g. ~/.llama/builds/<distribution>/<image_type>/<name>.yaml)",
|
||||
help="Path to the build config file (e.g. ~/.llama/builds/<image_type>/<name>-build.yaml). For docker, this could also be the name of the docker image. ",
|
||||
)
|
||||
|
||||
self.parser.add_argument(
|
||||
"--output-dir",
|
||||
type=str,
|
||||
help="Path to the output directory to store generated run.yaml config file. If not specified, will use ~/.llama/build/<image_type>/<name>-run.yaml",
|
||||
)
|
||||
|
||||
def _run_stack_configure_cmd(self, args: argparse.Namespace) -> None:
|
||||
from llama_toolchain.core.package import ImageType
|
||||
|
||||
config_file = Path(args.config)
|
||||
if not config_file.exists():
|
||||
self.parser.error(
|
||||
f"Could not find {config_file}. Please run `llama stack build` first"
|
||||
docker_image = None
|
||||
build_config_file = Path(args.config)
|
||||
if not build_config_file.exists():
|
||||
cprint(
|
||||
f"Could not find {build_config_file}. Trying docker image name instead...",
|
||||
color="green",
|
||||
)
|
||||
docker_image = args.config
|
||||
|
||||
builds_dir = BUILDS_BASE_DIR / ImageType.docker.value
|
||||
if args.output_dir:
|
||||
builds_dir = Path(output_dir)
|
||||
os.makedirs(builds_dir, exist_ok=True)
|
||||
|
||||
script = pkg_resources.resource_filename(
|
||||
"llama_toolchain", "core/configure_container.sh"
|
||||
)
|
||||
script_args = [script, docker_image, str(builds_dir)]
|
||||
|
||||
return_code = run_with_pty(script_args)
|
||||
|
||||
# we have regenerated the build config file with script, now check if it exists
|
||||
if return_code != 0:
|
||||
self.parser.error(
|
||||
f"Can not find {build_config_file}. Please run llama stack build first or check if docker image exists"
|
||||
)
|
||||
|
||||
build_name = docker_image.removeprefix("llamastack-")
|
||||
cprint(
|
||||
f"YAML configuration has been written to {builds_dir / f'{build_name}-run.yaml'}",
|
||||
color="green",
|
||||
)
|
||||
return
|
||||
|
||||
configure_llama_distribution(config_file)
|
||||
with open(build_config_file, "r") as f:
|
||||
build_config = BuildConfig(**yaml.safe_load(f))
|
||||
|
||||
self._configure_llama_distribution(build_config, args.output_dir)
|
||||
|
||||
def configure_llama_distribution(config_file: Path) -> None:
|
||||
from llama_toolchain.common.serialize import EnumEncoder
|
||||
from llama_toolchain.core.configure import configure_api_providers
|
||||
def _configure_llama_distribution(
|
||||
self,
|
||||
build_config: BuildConfig,
|
||||
output_dir: Optional[str] = None,
|
||||
):
|
||||
from llama_toolchain.common.serialize import EnumEncoder
|
||||
from llama_toolchain.core.configure import configure_api_providers
|
||||
|
||||
with open(config_file, "r") as f:
|
||||
config = PackageConfig(**yaml.safe_load(f))
|
||||
builds_dir = BUILDS_BASE_DIR / build_config.image_type
|
||||
if output_dir:
|
||||
builds_dir = Path(output_dir)
|
||||
os.makedirs(builds_dir, exist_ok=True)
|
||||
package_name = build_config.name.replace("::", "-")
|
||||
package_file = builds_dir / f"{package_name}-run.yaml"
|
||||
|
||||
if config.providers:
|
||||
cprint(
|
||||
f"Configuration already exists for {config.distribution_type}. Will overwrite...",
|
||||
"yellow",
|
||||
attrs=["bold"],
|
||||
api2providers = build_config.distribution_spec.providers
|
||||
|
||||
stub_config = {
|
||||
api_str: {"provider_type": provider}
|
||||
for api_str, provider in api2providers.items()
|
||||
}
|
||||
|
||||
if package_file.exists():
|
||||
cprint(
|
||||
f"Configuration already exists for {build_config.name}. Will overwrite...",
|
||||
"yellow",
|
||||
attrs=["bold"],
|
||||
)
|
||||
config = PackageConfig(**yaml.safe_load(package_file.read_text()))
|
||||
else:
|
||||
config = PackageConfig(
|
||||
built_at=datetime.now(),
|
||||
package_name=package_name,
|
||||
providers=stub_config,
|
||||
)
|
||||
|
||||
config.providers = configure_api_providers(config.providers)
|
||||
config.docker_image = (
|
||||
package_name if build_config.image_type == "docker" else None
|
||||
)
|
||||
config.conda_env = package_name if build_config.image_type == "conda" else None
|
||||
|
||||
config.providers = configure_api_providers(config.providers)
|
||||
with open(package_file, "w") as f:
|
||||
to_write = json.loads(json.dumps(config.dict(), cls=EnumEncoder))
|
||||
f.write(yaml.dump(to_write, sort_keys=False))
|
||||
|
||||
with open(config_file, "w") as fp:
|
||||
to_write = json.loads(json.dumps(config.dict(), cls=EnumEncoder))
|
||||
fp.write(yaml.dump(to_write, sort_keys=False))
|
||||
|
||||
print(f"YAML configuration has been written to {config_file}")
|
||||
cprint(
|
||||
f"> YAML configuration has been written to {package_file}",
|
||||
color="blue",
|
||||
)
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
# All rights reserved.
|
||||
#
|
||||
# This source code is licensed under the terms described in the LICENSE file in
|
||||
# the root directory of this source tree.
|
||||
|
||||
import argparse
|
||||
import json
|
||||
|
||||
from llama_toolchain.cli.subcommand import Subcommand
|
||||
|
||||
|
||||
class StackListDistributions(Subcommand):
|
||||
def __init__(self, subparsers: argparse._SubParsersAction):
|
||||
super().__init__()
|
||||
self.parser = subparsers.add_parser(
|
||||
"list-distributions",
|
||||
prog="llama stack list-distributions",
|
||||
description="Show available Llama Stack Distributions",
|
||||
formatter_class=argparse.RawTextHelpFormatter,
|
||||
)
|
||||
self._add_arguments()
|
||||
self.parser.set_defaults(func=self._run_distribution_list_cmd)
|
||||
|
||||
def _add_arguments(self):
|
||||
pass
|
||||
|
||||
def _run_distribution_list_cmd(self, args: argparse.Namespace) -> None:
|
||||
from llama_toolchain.cli.table import print_table
|
||||
from llama_toolchain.core.distribution_registry import (
|
||||
available_distribution_specs,
|
||||
)
|
||||
|
||||
# eventually, this should query a registry at llama.meta.com/llamastack/distributions
|
||||
headers = [
|
||||
"Distribution Type",
|
||||
"Providers",
|
||||
"Description",
|
||||
]
|
||||
|
||||
rows = []
|
||||
for spec in available_distribution_specs():
|
||||
providers = {k.value: v for k, v in spec.providers.items()}
|
||||
rows.append(
|
||||
[
|
||||
spec.distribution_type,
|
||||
json.dumps(providers, indent=2),
|
||||
spec.description,
|
||||
]
|
||||
)
|
||||
print_table(
|
||||
rows,
|
||||
headers,
|
||||
separate_rows=True,
|
||||
)
|
|
@ -69,9 +69,6 @@ class StackRun(Subcommand):
|
|||
with open(config_file, "r") as f:
|
||||
config = PackageConfig(**yaml.safe_load(f))
|
||||
|
||||
if not config.distribution_type:
|
||||
raise ValueError("Build config appears to be corrupt.")
|
||||
|
||||
if config.docker_image:
|
||||
script = pkg_resources.resource_filename(
|
||||
"llama_toolchain",
|
||||
|
|
|
@ -11,7 +11,6 @@ from llama_toolchain.cli.subcommand import Subcommand
|
|||
from .build import StackBuild
|
||||
from .configure import StackConfigure
|
||||
from .list_apis import StackListApis
|
||||
from .list_distributions import StackListDistributions
|
||||
from .list_providers import StackListProviders
|
||||
from .run import StackRun
|
||||
|
||||
|
@ -31,6 +30,5 @@ class StackParser(Subcommand):
|
|||
StackBuild.create(subparsers)
|
||||
StackConfigure.create(subparsers)
|
||||
StackListApis.create(subparsers)
|
||||
StackListDistributions.create(subparsers)
|
||||
StackListProviders.create(subparsers)
|
||||
StackRun.create(subparsers)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue