Elffuss BraidHub

kikocis/llama-stack public

CLI: add build templates support, move imports (#77)

* list templates implementation

* relative path

* finalize templates

* remove imports

* remove templates from name, name templates

* fix docker

* fix docker
Changeset111d87ec9830b5a992fec69696cd1b898dcf150b28b620950694791160ec2843
AuthorXi Yan <xiyan@meta.com> · 2024-09-18 14:25 -07:00
CommitterGitHub <noreply@github.com> · 2024-09-18 14:25 -07:00
Parents7b7b9b1ba243

Browse the files at this changeset

14 files changed

modified llama_stack/cli/stack/build.py +107 −11
@@ -8,9 +8,26 @@
88
99 from llama_stack.cli.subcommand import Subcommand
1010 from llama_stack.distribution.datatypes import * # noqa: F403
11+import os
12+from functools import lru_cache
1113 from pathlib import Path
1214
13-import yaml
15+TEMPLATES_PATH = (
16+ Path(os.path.relpath(__file__)).parent.parent.parent / "distribution" / "templates"
17+)
18+
19+
20+@lru_cache()
21+def available_templates_specs() -> List[BuildConfig]:
22+ import yaml
23+
24+ template_specs = []
25+ for p in TEMPLATES_PATH.rglob("*.yaml"):
26+ with open(p, "r") as f:
27+ build_config = BuildConfig(**yaml.safe_load(f))
28+ template_specs.append(build_config)
29+
30+ return template_specs
1431
1532
1633 class StackBuild(Subcommand):
@@ -27,17 +44,38 @@
2744
2845 def _add_arguments(self):
2946 self.parser.add_argument(
30- "config",
47+ "--config",
3148 type=str,
3249 default=None,
33- nargs="*",
3450 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",
3551 )
3652
53+ self.parser.add_argument(
54+ "--template",
55+ type=str,
56+ default=None,
57+ help="Name of the example template config to use for build. You may use `llama stack build --list-templates` to check out the available templates",
58+ )
59+
60+ self.parser.add_argument(
61+ "--list-templates",
62+ type=bool,
63+ default=False,
64+ action=argparse.BooleanOptionalAction,
65+ help="Show the available templates for building a Llama Stack distribution",
66+ )
67+
3768 self.parser.add_argument(
3869 "--name",
3970 type=str,
40- help="Name of the llama stack build to override from template config",
71+ help="Name of the Llama Stack build to override from template config. This name will be used as paths to store configuration files, build conda environments/docker images. If not specified, will use the name from the template config. ",
72+ )
73+
74+ self.parser.add_argument(
75+ "--image-type",
76+ type=str,
77+ help="Image Type to use for the build. This can be either conda or docker. If not specified, will use conda by default",
78+ default="conda",
4179 )
4280
4381 def _run_stack_build_command_from_build_config(
@@ -46,6 +84,8 @@
4684 import json
4785 import os
4886
87+ import yaml
88+
4989 from llama_stack.distribution.build import ApiInput, build_image, ImageType
5090
5191 from llama_stack.distribution.utils.config_dirs import DISTRIBS_BASE_DIR
@@ -78,22 +118,80 @@
78118 f"Build spec configuration saved at {str(build_file_path)}",
79119 color="blue",
80120 )
121+
122+ configure_name = (
123+ build_config.name
124+ if build_config.image_type == "conda"
125+ else (f"llamastack-{build_config.name}")
126+ )
81127 cprint(
82- f"You may now run `llama stack configure {build_config.name}` or `llama stack configure {str(build_file_path)}`",
128+ f"You may now run `llama stack configure {configure_name}` or `llama stack configure {str(build_file_path)}`",
83129 color="green",
84130 )
85131
132+ def _run_template_list_cmd(self, args: argparse.Namespace) -> None:
133+ import json
134+
135+ import yaml
136+
137+ from llama_stack.cli.table import print_table
138+
139+ # eventually, this should query a registry at llama.meta.com/llamastack/distributions
140+ headers = [
141+ "Template Name",
142+ "Providers",
143+ "Description",
144+ ]
145+
146+ rows = []
147+ for spec in available_templates_specs():
148+ rows.append(
149+ [
150+ spec.name,
151+ json.dumps(spec.distribution_spec.providers, indent=2),
152+ spec.distribution_spec.description,
153+ ]
154+ )
155+ print_table(
156+ rows,
157+ headers,
158+ separate_rows=True,
159+ )
160+
86161 def _run_stack_build_command(self, args: argparse.Namespace) -> None:
162+ import yaml
87163 from llama_stack.distribution.distribution import Api, api_providers
88164 from llama_stack.distribution.utils.dynamic import instantiate_class_type
89165 from prompt_toolkit import prompt
90166 from prompt_toolkit.validation import Validator
91167 from termcolor import cprint
92168
93- if not args.config:
94- name = prompt(
95- "> Enter a unique name for identifying your Llama Stack build (e.g. my-local-stack): "
96- )
169+ if args.list_templates:
170+ self._run_template_list_cmd(args)
171+ return
172+
173+ if args.template:
174+ if not args.name:
175+ self.parser.error(
176+ "You must specify a name for the build using --name when using a template"
177+ )
178+ return
179+ build_path = TEMPLATES_PATH / f"{args.template}-build.yaml"
180+ if not build_path.exists():
181+ self.parser.error(
182+ f"Could not find template {args.template}. Please run `llama stack build --list-templates` to check out the available templates"
183+ )
184+ return
185+ with open(build_path, "r") as f:
186+ build_config = BuildConfig(**yaml.safe_load(f))
187+ build_config.name = args.name
188+ build_config.image_type = args.image_type
189+ self._run_stack_build_command_from_build_config(build_config)
190+
191+ return
192+
193+ if not args.config and not args.template:
194+ name = prompt("> Enter a name for your Llama Stack (e.g. my-local-stack): ")
97195 image_type = prompt(
98196 "> Enter the image type you want your Llama Stack to be built as (docker or conda): ",
99197 validator=Validator.from_callable(
@@ -154,6 +252,4 @@
154252 except Exception as e:
155253 self.parser.error(f"Could not parse config file {args.config}: {e}")
156254 return
157- if args.name:
158- build_config.name = args.name
159255 self._run_stack_build_command_from_build_config(build_config)
modified llama_stack/cli/stack/configure.py +15 −7
@@ -5,17 +5,10 @@
55 # the root directory of this source tree.
66
77 import argparse
8-import json
9-from pathlib import Path
10-
11-import yaml
128
139 from llama_stack.cli.subcommand import Subcommand
1410 from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR
15-
16-from llama_stack.distribution.utils.exec import run_with_pty
1711 from llama_stack.distribution.datatypes import * # noqa: F403
18-import os
1912
2013
2114 class StackConfigure(Subcommand):
@@ -46,9 +39,17 @@
4639 )
4740
4841 def _run_stack_configure_cmd(self, args: argparse.Namespace) -> None:
42+ import json
43+ import os
44+ from pathlib import Path
45+
4946 import pkg_resources
5047
48+ import yaml
49+
5150 from llama_stack.distribution.build import ImageType
51+
52+ from llama_stack.distribution.utils.exec import run_with_pty
5253 from termcolor import cprint
5354
5455 docker_image = None
@@ -98,7 +99,14 @@
9899 build_config: BuildConfig,
99100 output_dir: Optional[str] = None,
100101 ):
102+ import json
103+ import os
104+ from pathlib import Path
105+
106+ import yaml
101107 from llama_stack.distribution.configure import configure_api_providers
108+
109+ from llama_stack.distribution.utils.exec import run_with_pty
102110 from llama_stack.distribution.utils.serialize import EnumEncoder
103111 from termcolor import cprint
104112
modified llama_stack/cli/stack/run.py +3 −4
@@ -6,10 +6,6 @@
66
77 import argparse
88
9-from pathlib import Path
10-
11-import yaml
12-
139 from llama_stack.cli.subcommand import Subcommand
1410 from llama_stack.distribution.datatypes import * # noqa: F403
1511
@@ -46,7 +42,10 @@
4642 )
4743
4844 def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
45+ from pathlib import Path
46+
4947 import pkg_resources
48+ import yaml
5049 from llama_stack.distribution.build import ImageType
5150 from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR
5251
deleted llama_stack/distribution/example_configs/conda/local-conda-example-build.yaml +0 −10
@@ -1,10 +0,0 @@
1-name: local-conda-example
2-distribution_spec:
3- description: Use code from `llama_stack` itself to serve all llama stack APIs
4- providers:
5- inference: meta-reference
6- memory: meta-reference
7- safety: meta-reference
8- agents: meta-reference
9- telemetry: meta-reference
10-image_type: conda
deleted llama_stack/distribution/example_configs/conda/local-fireworks-conda-example-build.yaml +0 −10
@@ -1,10 +0,0 @@
1-name: local-fireworks-conda-example
2-distribution_spec:
3- description: Use Fireworks.ai for running LLM inference
4- providers:
5- inference: remote::fireworks
6- memory: meta-reference
7- safety: meta-reference
8- agents: meta-reference
9- telemetry: meta-reference
10-image_type: conda
deleted llama_stack/distribution/example_configs/conda/local-ollama-conda-example-build.yaml +0 −10
@@ -1,10 +0,0 @@
1-name: local-ollama-conda-example
2-distribution_spec:
3- description: Like local, but use ollama for running LLM inference
4- providers:
5- inference: remote::ollama
6- memory: meta-reference
7- safety: meta-reference
8- agents: meta-reference
9- telemetry: meta-reference
10-image_type: conda
deleted llama_stack/distribution/example_configs/conda/local-tgi-conda-example-build.yaml +0 −10
@@ -1,10 +0,0 @@
1-name: local-tgi-conda-example
2-distribution_spec:
3- description: Use TGI (local or with Hugging Face Inference Endpoints for running LLM inference. When using HF Inference Endpoints, you must provide the name of the endpoint).
4- providers:
5- inference: remote::tgi
6- memory: meta-reference
7- safety: meta-reference
8- agents: meta-reference
9- telemetry: meta-reference
10-image_type: conda
deleted llama_stack/distribution/example_configs/conda/local-together-conda-example-build.yaml +0 −10
@@ -1,10 +0,0 @@
1-name: local-tgi-conda-example
2-distribution_spec:
3- description: Use Together.ai for running LLM inference
4- providers:
5- inference: remote::together
6- memory: meta-reference
7- safety: meta-reference
8- agents: meta-reference
9- telemetry: meta-reference
10-image_type: conda
deleted llama_stack/distribution/example_configs/docker/local-docker-example-build.yaml +0 −10
@@ -1,10 +0,0 @@
1-name: local-docker-example
2-distribution_spec:
3- description: Use code from `llama_stack` itself to serve all llama stack APIs
4- providers:
5- inference: meta-reference
6- memory: meta-reference
7- safety: meta-reference
8- agents: meta-reference
9- telemetry: meta-reference
10-image_type: docker
added llama_stack/distribution/templates/local-build.yaml +10 −0
@@ -0,0 +1,10 @@
1+name: local
2+distribution_spec:
3+ description: Use code from `llama_stack` itself to serve all llama stack APIs
4+ providers:
5+ inference: meta-reference
6+ memory: meta-reference
7+ safety: meta-reference
8+ agents: meta-reference
9+ telemetry: meta-reference
10+image_type: conda
added llama_stack/distribution/templates/local-fireworks-build.yaml +10 −0
@@ -0,0 +1,10 @@
1+name: local-fireworks
2+distribution_spec:
3+ description: Use Fireworks.ai for running LLM inference
4+ providers:
5+ inference: remote::fireworks
6+ memory: meta-reference
7+ safety: meta-reference
8+ agents: meta-reference
9+ telemetry: meta-reference
10+image_type: conda
added llama_stack/distribution/templates/local-ollama-build.yaml +10 −0
@@ -0,0 +1,10 @@
1+name: local-ollama
2+distribution_spec:
3+ description: Like local, but use ollama for running LLM inference
4+ providers:
5+ inference: remote::ollama
6+ memory: meta-reference
7+ safety: meta-reference
8+ agents: meta-reference
9+ telemetry: meta-reference
10+image_type: conda
added llama_stack/distribution/templates/local-tgi-build.yaml +10 −0
@@ -0,0 +1,10 @@
1+name: local-tgi
2+distribution_spec:
3+ description: Use TGI (local or with Hugging Face Inference Endpoints for running LLM inference. When using HF Inference Endpoints, you must provide the name of the endpoint).
4+ providers:
5+ inference: remote::tgi
6+ memory: meta-reference
7+ safety: meta-reference
8+ agents: meta-reference
9+ telemetry: meta-reference
10+image_type: conda
added llama_stack/distribution/templates/local-together-build.yaml +10 −0
@@ -0,0 +1,10 @@
1+name: local-together
2+distribution_spec:
3+ description: Use Together.ai for running LLM inference
4+ providers:
5+ inference: remote::together
6+ memory: meta-reference
7+ safety: meta-reference
8+ agents: meta-reference
9+ telemetry: meta-reference
10+image_type: conda