Elffuss BraidHub

kikocis/llama-stack public

CLI - add back build wizard, configure with name instead of build.yaml (#74)

* add back wizard for build

* conda build path move

* polish message

* run with name only

* prompt for build

* improve comments

* update msgs

* add new lines

* move build.yaml

* address comments

* validator for providers

* move imports

* Please enter -> enter

* comments, get started guide

* nits

* fix cprint import

* fix imports
Changeset7b7b9b1ba2432a488b2742db71a646dc6780d208978b06adfe9f58d2f12deef1
AuthorXi Yan <xiyan@meta.com> · 2024-09-18 11:41 -07:00
CommitterGitHub <noreply@github.com> · 2024-09-18 11:41 -07:00
Parents8cad8e6fa124

Browse the files at this changeset

4 files changed

modified docs/getting_started.md +35 −17
@@ -8,43 +8,61 @@
88 - Quick 3 line command to build and start a LlamaStack server using our Meta Reference implementation for all API endpoints with `conda` as build type.
99
1010 **`llama stack build`**
11+- You'll be prompted to enter build information interactively.
1112 ```
12-llama stack build ./llama_stack/distribution/example_configs/conda/local-conda-example-build.yaml --name my-local-llama-stack
13-...
14-...
15-Build spec configuration saved at ~/.llama/distributions/conda/my-local-llama-stack-build.yaml
13+llama stack build
14+
15+> Enter an unique name for identifying your Llama Stack build distribution (e.g. my-local-stack): my-local-llama-stack
16+> Enter the image type you want your distribution to be built with (docker or conda): conda
17+
18+ Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs.
19+> Enter the API provider for the inference API: (default=meta-reference): meta-reference
20+> Enter the API provider for the safety API: (default=meta-reference): meta-reference
21+> Enter the API provider for the agents API: (default=meta-reference): meta-reference
22+> Enter the API provider for the memory API: (default=meta-reference): meta-reference
23+> Enter the API provider for the telemetry API: (default=meta-reference): meta-reference
24+
25+ > (Optional) Enter a short description for your Llama Stack distribution:
26+
27+Build spec configuration saved at ~/.conda/envs/llamastack-my-local-llama-stack/my-local-llama-stack-build.yaml
1628 ```
1729
1830 **`llama stack configure`**
1931 ```
20-llama stack configure ~/.llama/distributions/conda/my-local-llama-stack-build.yaml
32+llama stack configure my-local-llama-stack
33+
34+Configuring APIs to serve...
35+Enter comma-separated list of APIs to serve:
2136
22-Configuring API: inference (meta-reference)
37+Configuring API `inference`...
38+
39+Configuring provider `meta-reference`...
2340 Enter value for model (default: Meta-Llama3.1-8B-Instruct) (required):
24-Enter value for quantization (optional):
41+Do you want to configure quantization? (y/n): n
2542 Enter value for torch_seed (optional):
2643 Enter value for max_seq_len (required): 4096
2744 Enter value for max_batch_size (default: 1) (required):
45+Configuring API `safety`...
2846
29-Configuring API: memory (meta-reference-faiss)
30-
31-Configuring API: safety (meta-reference)
47+Configuring provider `meta-reference`...
3248 Do you want to configure llama_guard_shield? (y/n): n
3349 Do you want to configure prompt_guard_shield? (y/n): n
50+Configuring API `agents`...
3451
35-Configuring API: agentic_system (meta-reference)
36-Enter value for brave_search_api_key (optional):
37-Enter value for bing_search_api_key (optional):
38-Enter value for wolfram_api_key (optional):
52+Configuring provider `meta-reference`...
53+Configuring API `memory`...
3954
40-Configuring API: telemetry (console)
55+Configuring provider `meta-reference`...
56+Configuring API `telemetry`...
4157
42-YAML configuration has been written to ~/.llama/builds/conda/my-local-llama-stack-run.yaml
58+Configuring provider `meta-reference`...
59+> YAML configuration has been written to /home/xiyan/.llama/builds/conda/my-local-llama-stack-run.yaml.
60+You can now run `llama stack run my-local-llama-stack --port PORT` or `llama stack run /home/xiyan/.llama/builds/conda/my-local-llama-stack-run.yaml --port PORT
4361 ```
4462
4563 **`llama stack run`**
4664 ```
47-llama stack run ~/.llama/builds/conda/my-local-llama-stack-run.yaml
65+llama stack run my-local-llama-stack
4866
4967 ...
5068 > initializing model parallel with size 1
modified llama_stack/cli/stack/build.py +71 −6
@@ -29,7 +29,9 @@
2929 self.parser.add_argument(
3030 "config",
3131 type=str,
32- help="Path to a config file to use for the build. You may find example configs in llama_stack/distribution/example_configs",
32+ default=None,
33+ nargs="*",
34+ help="Path to a config file to use for the build. You can find example configs in llama_stack/distribution/example_configs. If this argument is not provided, you will be prompted to enter information interactively",
3335 )
3436
3537 self.parser.add_argument(
@@ -44,9 +46,10 @@
4446 import json
4547 import os
4648
49+ from llama_stack.distribution.build import ApiInput, build_image, ImageType
50+
4751 from llama_stack.distribution.utils.config_dirs import DISTRIBS_BASE_DIR
4852 from llama_stack.distribution.utils.serialize import EnumEncoder
49- from llama_stack.distribution.build import ApiInput, build_image, ImageType
5053 from termcolor import cprint
5154
5255 # save build.yaml spec for building same distribution again
@@ -57,7 +60,10 @@
5760 llama_stack_path / "configs/distributions" / build_config.image_type
5861 )
5962 else:
60- build_dir = DISTRIBS_BASE_DIR / build_config.image_type
63+ build_dir = (
64+ Path(os.getenv("CONDA_PREFIX")).parent
65+ / f"llamastack-{build_config.name}"
66+ )
6167
6268 os.makedirs(build_dir, exist_ok=True)
6369 build_file_path = build_dir / f"{build_config.name}-build.yaml"
@@ -70,17 +76,76 @@
7076
7177 cprint(
7278 f"Build spec configuration saved at {str(build_file_path)}",
79+ color="blue",
80+ )
81+ cprint(
82+ f"You may now run `llama stack configure {build_config.name}` or `llama stack configure {str(build_file_path)}`",
7383 color="green",
7484 )
7585
7686 def _run_stack_build_command(self, args: argparse.Namespace) -> None:
77- from llama_stack.distribution.utils.prompt_for_config import prompt_for_config
87+ from llama_stack.distribution.distribution import Api, api_providers
7888 from llama_stack.distribution.utils.dynamic import instantiate_class_type
89+ from prompt_toolkit import prompt
90+ from prompt_toolkit.validation import Validator
91+ from termcolor import cprint
7992
8093 if not args.config:
81- self.parser.error(
82- "No config file specified. Please use `llama stack build /path/to/*-build.yaml`. Example config files can be found in llama_stack/distribution/example_configs"
94+ name = prompt(
95+ "> Enter a unique name for identifying your Llama Stack build (e.g. my-local-stack): "
96+ )
97+ image_type = prompt(
98+ "> Enter the image type you want your Llama Stack to be built as (docker or conda): ",
99+ validator=Validator.from_callable(
100+ lambda x: x in ["docker", "conda"],
101+ error_message="Invalid image type, please enter conda or docker",
102+ ),
103+ default="conda",
104+ )
105+
106+ cprint(
107+ f"\n Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs.",
108+ color="green",
109+ )
110+
111+ providers = dict()
112+ for api in Api:
113+ all_providers = api_providers()
114+ providers_for_api = all_providers[api]
115+
116+ api_provider = prompt(
117+ "> Enter the API provider for the {} API: (default=meta-reference): ".format(
118+ api.value
119+ ),
120+ validator=Validator.from_callable(
121+ lambda x: x in providers_for_api,
122+ error_message="Invalid provider, please enter one of the following: {}".format(
123+ providers_for_api.keys()
124+ ),
125+ ),
126+ default=(
127+ "meta-reference"
128+ if "meta-reference" in providers_for_api
129+ else list(providers_for_api.keys())[0]
130+ ),
131+ )
132+
133+ providers[api.value] = api_provider
134+
135+ description = prompt(
136+ "\n > (Optional) Enter a short description for your Llama Stack: ",
137+ default="",
138+ )
139+
140+ distribution_spec = DistributionSpec(
141+ providers=providers,
142+ description=description,
143+ )
144+
145+ build_config = BuildConfig(
146+ name=name, image_type=image_type, distribution_spec=distribution_spec
83147 )
148+ self._run_stack_build_command_from_build_config(build_config)
84149 return
85150
86151 with open(args.config, "r") as f:
modified llama_stack/cli/stack/configure.py +13 −4
@@ -9,7 +9,6 @@
99 from pathlib import Path
1010
1111 import yaml
12-from termcolor import cprint
1312
1413 from llama_stack.cli.subcommand import Subcommand
1514 from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR
@@ -50,9 +49,12 @@
5049 import pkg_resources
5150
5251 from llama_stack.distribution.build import ImageType
52+ from termcolor import cprint
5353
5454 docker_image = None
55- build_config_file = Path(args.config)
55+ conda_dir = Path(os.getenv("CONDA_PREFIX")).parent / f"llamastack-{args.config}"
56+ build_config_file = Path(conda_dir) / f"{args.config}-build.yaml"
57+
5658 if not build_config_file.exists():
5759 cprint(
5860 f"Could not find {build_config_file}. Trying docker image name instead...",
@@ -79,8 +81,9 @@
7981 )
8082
8183 build_name = docker_image.removeprefix("llamastack-")
84+ saved_file = str(builds_dir / f"{build_name}-run.yaml")
8285 cprint(
83- f"YAML configuration has been written to {builds_dir / f'{build_name}-run.yaml'}",
86+ f"YAML configuration has been written to {saved_file}. You can now run `llama stack run {saved_file}`",
8487 color="green",
8588 )
8689 return
@@ -97,6 +100,7 @@
97100 ):
98101 from llama_stack.distribution.configure import configure_api_providers
99102 from llama_stack.distribution.utils.serialize import EnumEncoder
103+ from termcolor import cprint
100104
101105 builds_dir = BUILDS_BASE_DIR / build_config.image_type
102106 if output_dir:
@@ -132,6 +136,11 @@
132136 f.write(yaml.dump(to_write, sort_keys=False))
133137
134138 cprint(
135- f"> YAML configuration has been written to {run_config_file}",
139+ f"> YAML configuration has been written to {run_config_file}.",
136140 color="blue",
137141 )
142+
143+ cprint(
144+ f"You can now run `llama stack run {image_name} --port PORT` or `llama stack run {run_config_file} --port PORT`",
145+ color="green",
146+ )
modified llama_stack/cli/stack/run.py +15 −3
@@ -47,6 +47,8 @@
4747
4848 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
4949 import pkg_resources
50+ from llama_stack.distribution.build import ImageType
51+ from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR
5052
5153 from llama_stack.distribution.utils.exec import run_with_pty
5254
@@ -54,12 +56,22 @@
5456 self.parser.error("Must specify a config file to run")
5557 return
5658
57- path = args.config
58- config_file = Path(path)
59+ config_file = Path(args.config)
60+ if not config_file.exists() and not args.config.endswith(".yaml"):
61+ # check if it's a build config saved to conda dir
62+ config_file = Path(
63+ BUILDS_BASE_DIR / ImageType.conda.value / f"{args.config}-run.yaml"
64+ )
65+
66+ if not config_file.exists() and not args.config.endswith(".yaml"):
67+ # check if it's a build config saved to docker dir
68+ config_file = Path(
69+ BUILDS_BASE_DIR / ImageType.docker.value / f"{args.config}-run.yaml"
70+ )
5971
6072 if not config_file.exists():
6173 self.parser.error(
62- f"File {str(config_file)} does not exist. Did you run `llama stack build`?"
74+ f"File {str(config_file)} does not exist. Please run `llama stack build` and `llama stack configure <name>` to generate a run.yaml file"
6375 )
6476 return
6577