Elffuss BraidHub

kikocis/llama-stack public

Add config file based CLI (#60)

* config file for build

* fix build command

* configure script with config

* fix configure script to work with config file

* update build.sh

* update readme

* distribution_type -> distribution

* fix run-config/config-file to config

* move import to inline

* only consume config as argument

* update configure to only consume config

* update readme

* update readme
Changesetc69662d961074b8339b3653622fd86f32704beb01ec67455fa1453628cc1efd0
AuthorXi Yan <xiyan@meta.com> · 2024-09-11 11:39 -07:00
CommitterGitHub <noreply@github.com> · 2024-09-11 11:39 -07:00
Parents32ac8e99b31d

Browse the files at this changeset

8 files changed

modified docs/cli_reference.md +36 −7
@@ -295,13 +295,18 @@
295295 Let's imagine you are working with a 8B-Instruct model. The following command will build a package (in the form of a Conda environment) _and_ configure it. As part of the configuration, you will be asked for some inputs (model_id, max_seq_len, etc.) Since we are working with a 8B model, we will name our build `8b-instruct` to help us remember the config.
296296
297297 ```
298-llama stack build local --name 8b-instruct
298+llama stack build
299299 ```
300300
301-Once it runs successfully , you should see some outputs in the form:
301+Once it runs, you will be prompted to enter build name and optional arguments, and should see some outputs in the form:
302302
303303 ```
304-$ llama stack build local --name 8b-instruct
304+$ llama stack build
305+Enter value for name (required): 8b-instruct
306+Enter value for distribution (default: local) (required): local
307+Enter value for api_providers (optional):
308+Enter value for image_type (default: conda) (required):
309+
305310 ....
306311 ....
307312 Successfully installed cfgv-3.4.0 distlib-0.3.8 identify-2.6.0 libcst-1.4.0 llama_toolchain-0.0.2 moreorless-0.4.0 nodeenv-1.9.1 pre-commit-3.8.0 stdlibs-2024.5.15 toml-0.10.2 tomlkit-0.13.0 trailrunner-1.4.0 ufmt-2.7.0 usort-1.0.8 virtualenv-20.26.3
@@ -312,17 +317,41 @@
312317 ...
313318
314319 YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
320+Target `8b-test` built with configuration at /home/xiyan/.llama/builds/local/conda/8b-test.yaml
321+Build spec configuration saved at /home/xiyan/.llama/distributions/local/conda/8b-test-build.yaml
322+```
323+
324+You can re-build package based on build config
325+```
326+$ cat ~/.llama/distributions/local/conda/8b-instruct-build.yaml
327+name: 8b-instruct
328+distribution: local
329+api_providers: null
330+image_type: conda
331+
332+$ llama stack build --config ~/.llama/distributions/local/conda/8b-instruct-build.yaml
333+
334+Successfully setup conda environment. Configuring build...
335+
336+...
337+...
338+
339+YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
340+Target `8b-instruct` built with configuration at ~/.llama/builds/local/conda/8b-instruct.yaml
341+Build spec configuration saved at ~/.llama/distributions/local/conda/8b-instruct-build.yaml
315342 ```
343+
316344 ### Step 3.3: Configure a distribution
317345
318346 You can re-configure this distribution by running:
319347 ```
320-llama stack configure local --name 8b-instruct
348+llama stack configure ~/.llama/builds/local/conda/8b-instruct.yaml
321349 ```
322350
323351 Here is an example run of how the CLI will guide you to fill the configuration
352+
324353 ```
325-$ llama stack configure local --name 8b-instruct
354+$ llama stack configure ~/.llama/builds/local/conda/8b-instruct.yaml
326355
327356 Configuring API: inference (meta-reference)
328357 Enter value for model (required): Meta-Llama3.1-8B-Instruct
@@ -363,12 +392,12 @@
363392 You need the YAML configuration file which was written out at the end by the `llama stack build` step.
364393
365394 ```
366-llama stack run local --name 8b-instruct --port 5000
395+llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml --port 5000
367396 ```
368397 You should see the Stack server start and print the APIs that it is supporting,
369398
370399 ```
371-$ llama stack run local --name 8b-instruct --port 5000
400+$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml --port 5000
372401
373402 > initializing model parallel with size 1
374403 > initializing ddp with size 1
modified llama_toolchain/cli/stack/build.py +69 −41
@@ -8,6 +8,7 @@
88
99 from llama_toolchain.cli.subcommand import Subcommand
1010 from llama_toolchain.core.datatypes import * # noqa: F403
11+import yaml
1112
1213
1314 def parse_api_provider_tuples(
@@ -47,55 +48,45 @@
4748 self.parser.set_defaults(func=self._run_stack_build_command)
4849
4950 def _add_arguments(self):
50- from llama_toolchain.core.distribution_registry import available_distribution_specs
51- from llama_toolchain.core.package import (
52- BuildType,
51+ from llama_toolchain.core.distribution_registry import (
52+ available_distribution_specs,
5353 )
54+ from llama_toolchain.core.package import ImageType
5455
5556 allowed_ids = [d.distribution_type for d in available_distribution_specs()]
5657 self.parser.add_argument(
57- "distribution",
58+ "--config",
5859 type=str,
59- help="Distribution to build (either \"adhoc\" OR one of: {})".format(allowed_ids),
60- )
61- self.parser.add_argument(
62- "api_providers",
63- nargs='?',
64- help="Comma separated list of (api=provider) tuples",
65- )
66-
67- self.parser.add_argument(
68- "--name",
69- type=str,
70- help="Name of the build target (image, conda env)",
71- required=True,
72- )
73- self.parser.add_argument(
74- "--type",
75- type=str,
76- default="conda_env",
77- choices=[v.value for v in BuildType],
60+ help="Path to a config file to use for the build",
7861 )
7962
80- def _run_stack_build_command(self, args: argparse.Namespace) -> None:
63+ def _run_stack_build_command_from_build_config(
64+ self, build_config: BuildConfig
65+ ) -> None:
66+ import json
67+ import os
68+
69+ from llama_toolchain.common.config_dirs import DISTRIBS_BASE_DIR
70+ from llama_toolchain.common.serialize import EnumEncoder
8171 from llama_toolchain.core.distribution_registry import resolve_distribution_spec
82- from llama_toolchain.core.package import (
83- ApiInput,
84- BuildType,
85- build_package,
86- )
72+ from llama_toolchain.core.package import ApiInput, build_package, ImageType
73+ from termcolor import cprint
8774
8875 api_inputs = []
89- if args.distribution == "adhoc":
90- if not args.api_providers:
91- self.parser.error("You must specify API providers with (api=provider,...) for building an adhoc distribution")
76+ if build_config.distribution == "adhoc":
77+ if not build_config.api_providers:
78+ self.parser.error(
79+ "You must specify API providers with (api=provider,...) for building an adhoc distribution"
80+ )
9281 return
9382
94- parsed = parse_api_provider_tuples(args.api_providers, self.parser)
83+ parsed = parse_api_provider_tuples(build_config.api_providers, self.parser)
9584 for api, provider_spec in parsed.items():
9685 for dep in provider_spec.api_dependencies:
9786 if dep not in parsed:
98- self.parser.error(f"API {api} needs dependency {dep} provided also")
87+ self.parser.error(
88+ f"API {api} needs dependency {dep} provided also"
89+ )
9990 return
10091
10192 api_inputs.append(
@@ -106,13 +97,17 @@
10697 )
10798 docker_image = None
10899 else:
109- if args.api_providers:
110- self.parser.error("You cannot specify API providers for pre-registered distributions")
100+ if build_config.api_providers:
101+ self.parser.error(
102+ "You cannot specify API providers for pre-registered distributions"
103+ )
111104 return
112105
113- dist = resolve_distribution_spec(args.distribution)
106+ dist = resolve_distribution_spec(build_config.distribution)
114107 if dist is None:
115- self.parser.error(f"Could not find distribution {args.distribution}")
108+ self.parser.error(
109+ f"Could not find distribution {build_config.distribution}"
110+ )
116111 return
117112
118113 for api, provider_type in dist.providers.items():
@@ -126,8 +121,41 @@
126121
127122 build_package(
128123 api_inputs,
129- build_type=BuildType(args.type),
130- name=args.name,
131- distribution_type=args.distribution,
124+ image_type=ImageType(build_config.image_type),
125+ name=build_config.name,
126+ distribution_type=build_config.distribution,
132127 docker_image=docker_image,
133128 )
129+
130+ # save build.yaml spec for building same distribution again
131+ build_dir = (
132+ DISTRIBS_BASE_DIR / build_config.distribution / build_config.image_type
133+ )
134+ os.makedirs(build_dir, exist_ok=True)
135+ build_file_path = build_dir / f"{build_config.name}-build.yaml"
136+
137+ with open(build_file_path, "w") as f:
138+ to_write = json.loads(json.dumps(build_config.dict(), cls=EnumEncoder))
139+ f.write(yaml.dump(to_write, sort_keys=False))
140+
141+ cprint(
142+ f"Build spec configuration saved at {str(build_file_path)}",
143+ color="green",
144+ )
145+
146+ def _run_stack_build_command(self, args: argparse.Namespace) -> None:
147+ from llama_toolchain.common.prompt_for_config import prompt_for_config
148+ from llama_toolchain.core.dynamic import instantiate_class_type
149+
150+ if args.config:
151+ with open(args.config, "r") as f:
152+ try:
153+ build_config = BuildConfig(**yaml.safe_load(f))
154+ except Exception as e:
155+ self.parser.error(f"Could not parse config file {args.config}: {e}")
156+ return
157+ self._run_stack_build_command_from_build_config(build_config)
158+ return
159+
160+ build_config = prompt_for_config(BuildConfig, None)
161+ self._run_stack_build_command_from_build_config(build_config)
modified llama_toolchain/cli/stack/configure.py +7 −26
@@ -9,10 +9,10 @@
99 from pathlib import Path
1010
1111 import yaml
12-from termcolor import cprint
1312
1413 from llama_toolchain.cli.subcommand import Subcommand
1514 from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
15+from termcolor import cprint
1616 from llama_toolchain.core.datatypes import * # noqa: F403
1717
1818
@@ -34,38 +34,19 @@
3434 from llama_toolchain.core.distribution_registry import (
3535 available_distribution_specs,
3636 )
37- from llama_toolchain.core.package import BuildType
37+ from llama_toolchain.core.package import ImageType
3838
3939 allowed_ids = [d.distribution_type for d in available_distribution_specs()]
4040 self.parser.add_argument(
41- "distribution",
42- type=str,
43- help='Distribution ("adhoc" or one of: {})'.format(allowed_ids),
44- )
45- self.parser.add_argument(
46- "--name",
41+ "config",
4742 type=str,
48- help="Name of the build",
49- required=True,
50- )
51- self.parser.add_argument(
52- "--type",
53- type=str,
54- default="conda_env",
55- choices=[v.value for v in BuildType],
43+ help="Path to the package config file (e.g. ~/.llama/builds/<distribution>/<image_type>/<name>.yaml)",
5644 )
5745
5846 def _run_stack_configure_cmd(self, args: argparse.Namespace) -> None:
59- from llama_toolchain.core.package import BuildType
60-
61- build_type = BuildType(args.type)
62- name = args.name
63- config_file = (
64- BUILDS_BASE_DIR
65- / args.distribution
66- / build_type.descriptor()
67- / f"{name}.yaml"
68- )
47+ from llama_toolchain.core.package import ImageType
48+
49+ config_file = Path(args.config)
6950 if not config_file.exists():
7051 self.parser.error(
7152 f"Could not find {config_file}. Please run `llama stack build` first"
modified llama_toolchain/cli/stack/run.py +8 −19
@@ -29,24 +29,12 @@
2929 self.parser.set_defaults(func=self._run_stack_run_cmd)
3030
3131 def _add_arguments(self):
32- from llama_toolchain.core.package import BuildType
32+ from llama_toolchain.core.package import ImageType
3333
3434 self.parser.add_argument(
35- "distribution",
35+ "config",
3636 type=str,
37- help="Distribution whose build you want to start",
38- )
39- self.parser.add_argument(
40- "--name",
41- type=str,
42- help="Name of the build you want to start",
43- required=True,
44- )
45- self.parser.add_argument(
46- "--type",
47- type=str,
48- default="conda_env",
49- choices=[v.value for v in BuildType],
37+ help="Path to config file to use for the run",
5038 )
5139 self.parser.add_argument(
5240 "--port",
@@ -63,12 +51,13 @@
6351
6452 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
6553 from llama_toolchain.common.exec import run_with_pty
66- from llama_toolchain.core.package import BuildType
54+ from llama_toolchain.core.package import ImageType
6755
68- build_type = BuildType(args.type)
69- build_dir = BUILDS_BASE_DIR / args.distribution / build_type.descriptor()
70- path = build_dir / f"{args.name}.yaml"
56+ if not args.config:
57+ self.parser.error("Must specify a config file to run")
58+ return
7159
60+ path = args.config
7261 config_file = Path(path)
7362
7463 if not config_file.exists():
modified llama_toolchain/core/build_conda_env.sh +4 −3
@@ -19,7 +19,7 @@
1919
2020 set -euo pipefail
2121
22-if [ "$#" -ne 3 ]; then
22+if [ "$#" -ne 4 ]; then
2323 echo "Usage: $0 <distribution_type> <build_name> <pip_dependencies>" >&2
2424 echo "Example: $0 <distribution_type> mybuild 'numpy pandas scipy'" >&2
2525 exit 1
@@ -28,7 +28,8 @@
2828 distribution_type="$1"
2929 build_name="$2"
3030 env_name="llamastack-$build_name"
31-pip_dependencies="$3"
31+config_file="$3"
32+pip_dependencies="$4"
3233
3334 # Define color codes
3435 RED='\033[0;31m'
@@ -117,4 +118,4 @@
117118
118119 printf "${GREEN}Successfully setup conda environment. Configuring build...${NC}\n"
119120
120-$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $distribution_type --name "$build_name" --type conda_env
121+$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $config_file
modified llama_toolchain/core/build_container.sh +4 −3
@@ -4,7 +4,7 @@
44 LLAMA_TOOLCHAIN_DIR=${LLAMA_TOOLCHAIN_DIR:-}
55 TEST_PYPI_VERSION=${TEST_PYPI_VERSION:-}
66
7-if [ "$#" -ne 4 ]; then
7+if [ "$#" -ne 5 ]; then
88 echo "Usage: $0 <distribution_type> <build_name> <docker_base> <pip_dependencies>
99 echo "Example: $0 distribution_type my-fastapi-app python:3.9-slim 'fastapi uvicorn'
1010 exit 1
@@ -14,7 +14,8 @@
1414 build_name="$2"
1515 image_name="llamastack-$build_name"
1616 docker_base=$3
17-pip_dependencies=$4
17+config_file=$4
18+pip_dependencies=$5
1819
1920 # Define color codes
2021 RED='\033[0;31m'
@@ -110,4 +111,4 @@
110111 printf "${GREEN}Succesfully setup Podman image. Configuring build...${NC}"
111112 echo "You can run it with: podman run -p 8000:8000 $image_name"
112113
113-$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $distribution_type --name "$build_name" --type container
114+$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $config_file
modified llama_toolchain/core/datatypes.py +16 −0
@@ -188,3 +188,19 @@
188188 the dependencies of these providers as well.
189189 """,
190190 )
191+
192+
193+@json_schema_type
194+class BuildConfig(BaseModel):
195+ name: str
196+ distribution: str = Field(
197+ default="local", description="Type of distribution to build (adhoc | {})"
198+ )
199+ api_providers: Optional[str] = Field(
200+ default_factory=list,
201+ description="List of API provider names to build",
202+ )
203+ image_type: str = Field(
204+ default="conda",
205+ description="Type of package to build (conda | container)",
206+ )
modified llama_toolchain/core/package.py +13 −14
@@ -12,24 +12,21 @@
1212
1313 import pkg_resources
1414 import yaml
15-from pydantic import BaseModel
16-
17-from termcolor import cprint
1815
1916 from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
2017 from llama_toolchain.common.exec import run_with_pty
2118 from llama_toolchain.common.serialize import EnumEncoder
19+from pydantic import BaseModel
20+
21+from termcolor import cprint
2222
2323 from llama_toolchain.core.datatypes import * # noqa: F403
2424 from llama_toolchain.core.distribution import api_providers, SERVER_DEPENDENCIES
2525
2626
27-class BuildType(Enum):
28- container = "container"
29- conda_env = "conda_env"
30-
31- def descriptor(self) -> str:
32- return "docker" if self == self.container else "conda"
27+class ImageType(Enum):
28+ docker = "docker"
29+ conda = "conda"
3330
3431
3532 class Dependencies(BaseModel):
@@ -44,7 +41,7 @@
4441
4542 def build_package(
4643 api_inputs: List[ApiInput],
47- build_type: BuildType,
44+ image_type: ImageType,
4845 name: str,
4946 distribution_type: Optional[str] = None,
5047 docker_image: Optional[str] = None,
@@ -52,7 +49,7 @@
5249 if not distribution_type:
5350 distribution_type = "adhoc"
5451
55- build_dir = BUILDS_BASE_DIR / distribution_type / build_type.descriptor()
52+ build_dir = BUILDS_BASE_DIR / distribution_type / image_type.value
5653 os.makedirs(build_dir, exist_ok=True)
5754
5855 package_name = name.replace("::", "-")
@@ -106,14 +103,14 @@
106103 )
107104
108105 c.distribution_type = distribution_type
109- c.docker_image = package_name if build_type == BuildType.container else None
110- c.conda_env = package_name if build_type == BuildType.conda_env else None
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
111108
112109 with open(package_file, "w") as f:
113110 to_write = json.loads(json.dumps(c.dict(), cls=EnumEncoder))
114111 f.write(yaml.dump(to_write, sort_keys=False))
115112
116- if build_type == BuildType.container:
113+ if image_type == ImageType.docker:
117114 script = pkg_resources.resource_filename(
118115 "llama_toolchain", "core/build_container.sh"
119116 )
@@ -122,6 +119,7 @@
122119 distribution_type,
123120 package_name,
124121 package_deps.docker_image,
122+ str(package_file),
125123 " ".join(package_deps.pip_packages),
126124 ]
127125 else:
@@ -132,6 +130,7 @@
132130 script,
133131 distribution_type,
134132 package_name,
133+ str(package_file),
135134 " ".join(package_deps.pip_packages),
136135 ]
137136