Elffuss BraidHub

kikocis/llama-stack public

2e30ba2d48a0 · 23.8 KiBRenderedRawPermalink
1# Llama CLI Reference
2
3The `llama` CLI tool helps you setup and use the Llama toolchain & agentic systems. It should be available on your path after installing the `llama-stack` package.
4
5### Subcommands
61. `download`: `llama` cli tools supports downloading the model from Meta or HuggingFace.
72. `model`: Lists available models and their properties.
83. `stack`: Allows you to build and run a Llama Stack server. You can read more about this [here](/docs/cli_reference.md#step-3-building-configuring-and-running-llama-stack-servers).
9
10### Sample Usage
11
12```
13llama --help
14```
15<pre style="font-family: monospace;">
16usage: llama [-h] {download,model,stack} ...
17
18Welcome to the Llama CLI
19
20options:
21 -h, --help show this help message and exit
22
23subcommands:
24 {download,model,stack}
25</pre>
26
27## Step 1. Get the models
28
29You first need to have models downloaded locally.
30
31To download any model you need the **Model Descriptor**.
32This can be obtained by running the command
33```
34llama model list
35```
36
37You should see a table like this:
38
39<pre style="font-family: monospace;">
40+---------------------------------------+---------------------------------------------+----------------+----------------------------+
41| Model Descriptor | HuggingFace Repo | Context Length | Hardware Requirements |
42+---------------------------------------+---------------------------------------------+----------------+----------------------------+
43| Meta-Llama3.1-8B | meta-llama/Meta-Llama-3.1-8B | 128K | 1 GPU, each >= 20GB VRAM |
44+---------------------------------------+---------------------------------------------+----------------+----------------------------+
45| Meta-Llama3.1-70B | meta-llama/Meta-Llama-3.1-70B | 128K | 8 GPUs, each >= 20GB VRAM |
46+---------------------------------------+---------------------------------------------+----------------+----------------------------+
47| Meta-Llama3.1-405B:bf16-mp8 | | 128K | 8 GPUs, each >= 120GB VRAM |
48+---------------------------------------+---------------------------------------------+----------------+----------------------------+
49| Meta-Llama3.1-405B | meta-llama/Meta-Llama-3.1-405B-FP8 | 128K | 8 GPUs, each >= 70GB VRAM |
50+---------------------------------------+---------------------------------------------+----------------+----------------------------+
51| Meta-Llama3.1-405B:bf16-mp16 | meta-llama/Meta-Llama-3.1-405B | 128K | 16 GPUs, each >= 70GB VRAM |
52+---------------------------------------+---------------------------------------------+----------------+----------------------------+
53| Meta-Llama3.1-8B-Instruct | meta-llama/Meta-Llama-3.1-8B-Instruct | 128K | 1 GPU, each >= 20GB VRAM |
54+---------------------------------------+---------------------------------------------+----------------+----------------------------+
55| Meta-Llama3.1-70B-Instruct | meta-llama/Meta-Llama-3.1-70B-Instruct | 128K | 8 GPUs, each >= 20GB VRAM |
56+---------------------------------------+---------------------------------------------+----------------+----------------------------+
57| Meta-Llama3.1-405B-Instruct:bf16-mp8 | | 128K | 8 GPUs, each >= 120GB VRAM |
58+---------------------------------------+---------------------------------------------+----------------+----------------------------+
59| Meta-Llama3.1-405B-Instruct | meta-llama/Meta-Llama-3.1-405B-Instruct-FP8 | 128K | 8 GPUs, each >= 70GB VRAM |
60+---------------------------------------+---------------------------------------------+----------------+----------------------------+
61| Meta-Llama3.1-405B-Instruct:bf16-mp16 | meta-llama/Meta-Llama-3.1-405B-Instruct | 128K | 16 GPUs, each >= 70GB VRAM |
62+---------------------------------------+---------------------------------------------+----------------+----------------------------+
63| Llama-Guard-3-8B | meta-llama/Llama-Guard-3-8B | 128K | 1 GPU, each >= 20GB VRAM |
64+---------------------------------------+---------------------------------------------+----------------+----------------------------+
65| Llama-Guard-3-8B:int8-mp1 | meta-llama/Llama-Guard-3-8B-INT8 | 128K | 1 GPU, each >= 10GB VRAM |
66+---------------------------------------+---------------------------------------------+----------------+----------------------------+
67| Prompt-Guard-86M | meta-llama/Prompt-Guard-86M | 128K | 1 GPU, each >= 1GB VRAM |
68+---------------------------------------+---------------------------------------------+----------------+----------------------------+
69</pre>
70
71To download models, you can use the llama download command.
72
73#### Downloading from [Meta](https://llama.meta.com/llama-downloads/)
74
75Here is an example download command to get the 8B/70B Instruct model. You will need META_URL which can be obtained from [here](https://llama.meta.com/docs/getting_the_models/meta/)
76
77Download the required checkpoints using the following commands:
78```bash
79# download the 8B model, this can be run on a single GPU
80llama download --source meta --model-id Meta-Llama3.1-8B-Instruct --meta-url META_URL
81
82# you can also get the 70B model, this will require 8 GPUs however
83llama download --source meta --model-id Meta-Llama3.1-70B-Instruct --meta-url META_URL
84
85# llama-agents have safety enabled by default. For this, you will need
86# safety models -- Llama-Guard and Prompt-Guard
87llama download --source meta --model-id Prompt-Guard-86M --meta-url META_URL
88llama download --source meta --model-id Llama-Guard-3-8B --meta-url META_URL
89```
90
91#### Downloading from [Huggingface](https://huggingface.co/meta-llama)
92
93Essentially, the same commands above work, just replace `--source meta` with `--source huggingface`.
94
95```bash
96llama download --source huggingface --model-id Meta-Llama3.1-8B-Instruct --hf-token <HF_TOKEN>
97
98llama download --source huggingface --model-id Meta-Llama3.1-70B-Instruct --hf-token <HF_TOKEN>
99
100llama download --source huggingface --model-id Llama-Guard-3-8B --ignore-patterns *original*
101llama download --source huggingface --model-id Prompt-Guard-86M --ignore-patterns *original*
102```
103
104**Important:** Set your environment variable `HF_TOKEN` or pass in `--hf-token` to the command to validate your access. You can find your token at [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens).
105
106> **Tip:** Default for `llama download` is to run with `--ignore-patterns *.safetensors` since we use the `.pth` files in the `original` folder. For Llama Guard and Prompt Guard, however, we need safetensors. Hence, please run with `--ignore-patterns original` so that safetensors are downloaded and `.pth` files are ignored.
107
108#### Downloading via Ollama
109
110If you're already using ollama, we also have a supported Llama Stack distribution `local-ollama` and you can continue to use ollama for managing model downloads.
111
112```
113ollama pull llama3.1:8b-instruct-fp16
114ollama pull llama3.1:70b-instruct-fp16
115```
116
117> [!NOTE]
118> Only the above two models are currently supported by Ollama.
119
120
121## Step 2: Understand the models
122The `llama model` command helps you explore the model’s interface.
123
124### 2.1 Subcommands
1251. `download`: Download the model from different sources. (meta, huggingface)
1262. `list`: Lists all the models available for download with hardware requirements to deploy the models.
1273. `template`: <TODO: What is a template?>
1284. `describe`: Describes all the properties of the model.
129
130### 2.2 Sample Usage
131
132`llama model <subcommand> <options>`
133
134```
135llama model --help
136```
137<pre style="font-family: monospace;">
138usage: llama model [-h] {download,list,template,describe} ...
139
140Work with llama models
141
142options:
143 -h, --help show this help message and exit
144
145model_subcommands:
146 {download,list,template,describe}
147</pre>
148
149You can use the describe command to know more about a model:
150```
151llama model describe -m Meta-Llama3.1-8B-Instruct
152```
153### 2.3 Describe
154
155<pre style="font-family: monospace;">
156+-----------------------------+---------------------------------------+
157| Model | Meta- |
158| | Llama3.1-8B-Instruct |
159+-----------------------------+---------------------------------------+
160| HuggingFace ID | meta-llama/Meta-Llama-3.1-8B-Instruct |
161+-----------------------------+---------------------------------------+
162| Description | Llama 3.1 8b instruct model |
163+-----------------------------+---------------------------------------+
164| Context Length | 128K tokens |
165+-----------------------------+---------------------------------------+
166| Weights format | bf16 |
167+-----------------------------+---------------------------------------+
168| Model params.json | { |
169| | "dim": 4096, |
170| | "n_layers": 32, |
171| | "n_heads": 32, |
172| | "n_kv_heads": 8, |
173| | "vocab_size": 128256, |
174| | "ffn_dim_multiplier": 1.3, |
175| | "multiple_of": 1024, |
176| | "norm_eps": 1e-05, |
177| | "rope_theta": 500000.0, |
178| | "use_scaled_rope": true |
179| | } |
180+-----------------------------+---------------------------------------+
181| Recommended sampling params | { |
182| | "strategy": "top_p", |
183| | "temperature": 1.0, |
184| | "top_p": 0.9, |
185| | "top_k": 0 |
186| | } |
187+-----------------------------+---------------------------------------+
188</pre>
189### 2.4 Template
190You can even run `llama model template` see all of the templates and their tokens:
191
192```
193llama model template
194```
195
196<pre style="font-family: monospace;">
197+-----------+---------------------------------+
198| Role | Template Name |
199+-----------+---------------------------------+
200| user | user-default |
201| assistant | assistant-builtin-tool-call |
202| assistant | assistant-custom-tool-call |
203| assistant | assistant-default |
204| system | system-builtin-and-custom-tools |
205| system | system-builtin-tools-only |
206| system | system-custom-tools-only |
207| system | system-default |
208| tool | tool-success |
209| tool | tool-failure |
210+-----------+---------------------------------+
211</pre>
212
213And fetch an example by passing it to `--name`:
214```
215llama model template --name tool-success
216```
217
218<pre style="font-family: monospace;">
219+----------+----------------------------------------------------------------+
220| Name | tool-success |
221+----------+----------------------------------------------------------------+
222| Template | <|start_header_id|>ipython<|end_header_id|> |
223| | |
224| | completed |
225| | [stdout]{"results":["something |
226| | something"]}[/stdout]<|eot_id|> |
227| | |
228+----------+----------------------------------------------------------------+
229| Notes | Note ipython header and [stdout] |
230+----------+----------------------------------------------------------------+
231</pre>
232
233Or:
234```
235llama model template --name system-builtin-tools-only
236```
237
238<pre style="font-family: monospace;">
239+----------+--------------------------------------------+
240| Name | system-builtin-tools-only |
241+----------+--------------------------------------------+
242| Template | <|start_header_id|>system<|end_header_id|> |
243| | |
244| | Environment: ipython |
245| | Tools: brave_search, wolfram_alpha |
246| | |
247| | Cutting Knowledge Date: December 2023 |
248| | Today Date: 21 August 2024 |
249| | <|eot_id|> |
250| | |
251+----------+--------------------------------------------+
252| Notes | |
253+----------+--------------------------------------------+
254</pre>
255
256These commands can help understand the model interface and how prompts / messages are formatted for various scenarios.
257
258**NOTE**: Outputs in terminal are color printed to show special tokens.
259
260
261## Step 3: Building, and Configuring Llama Stack Distributions
262
263- Please see our [Getting Started](getting_started.md) guide for details.
264
265### Step 3.1 Build
266In the following steps, imagine we'll be working with a `Meta-Llama3.1-8B-Instruct` model. We will name our build `8b-instruct` to help us remember the config. We will start build our distribution (in the form of a Conda environment, or Docker image). In this step, we will specify:
267- `name`: the name for our distribution (e.g. `8b-instruct`)
268- `image_type`: our build image type (`conda | docker`)
269- `distribution_spec`: our distribution specs for specifying API providers
270 - `description`: a short description of the configurations for the distribution
271 - `providers`: specifies the underlying implementation for serving each API endpoint
272 - `image_type`: `conda` | `docker` to specify whether to build the distribution in the form of Docker image or Conda environment.
273
274
275At the end of build command, we will generate `<name>-build.yaml` file storing the build configurations.
276
277After this step is complete, a file named `<name>-build.yaml` will be generated and saved at the output file path specified at the end of the command.
278
279#### Building from scratch
280- For a new user, we could start off with running `llama stack build` which will allow you to a interactively enter wizard where you will be prompted to enter build configurations.
281```
282llama stack build
283```
284
285Running the command above will allow you to fill in the configuration to build your Llama Stack distribution, you will see the following outputs.
286
287```
288> Enter an unique name for identifying your Llama Stack build distribution (e.g. my-local-stack): my-local-llama-stack
289> Enter the image type you want your distribution to be built with (docker or conda): conda
290
291 Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs.
292> Enter the API provider for the inference API: (default=meta-reference): meta-reference
293> Enter the API provider for the safety API: (default=meta-reference): meta-reference
294> Enter the API provider for the agents API: (default=meta-reference): meta-reference
295> Enter the API provider for the memory API: (default=meta-reference): meta-reference
296> Enter the API provider for the telemetry API: (default=meta-reference): meta-reference
297
298 > (Optional) Enter a short description for your Llama Stack distribution:
299
300Build spec configuration saved at ~/.conda/envs/llamastack-my-local-llama-stack/my-local-llama-stack-build.yaml
301```
302
303#### Building from templates
304- To build from alternative API providers, we provide distribution templates for users to get started building a distribution backed by different providers.
305
306The following command will allow you to see the available templates and their corresponding providers.
307```
308llama stack build --list-templates
309```
310
311![alt text](resources/list-templates.png)
312
313You may then pick a template to build your distribution with providers fitted to your liking.
314
315```
316llama stack build --template local-tgi --name my-tgi-stack
317```
318
319```
320$ llama stack build --template local-tgi --name my-tgi-stack
321...
322...
323Build spec configuration saved at ~/.conda/envs/llamastack-my-tgi-stack/my-tgi-stack-build.yaml
324You may now run `llama stack configure my-tgi-stack` or `llama stack configure ~/.conda/envs/llamastack-my-tgi-stack/my-tgi-stack-build.yaml`
325```
326
327#### Building from config file
328- In addition to templates, you may customize the build to your liking through editing config files and build from config files with the following command.
329
330- The config file will be of contents like the ones in `llama_stack/distributions/templates/`.
331
332```
333$ cat llama_stack/distribution/templates/local-ollama-build.yaml
334
335name: local-ollama
336distribution_spec:
337 description: Like local, but use ollama for running LLM inference
338 providers:
339 inference: remote::ollama
340 memory: meta-reference
341 safety: meta-reference
342 agents: meta-reference
343 telemetry: meta-reference
344image_type: conda
345```
346
347```
348llama stack build --config llama_stack/distribution/templates/local-ollama-build.yaml
349```
350
351#### How to build distribution with Docker image
352
353To build a docker image, you may start off from a template and use the `--image-type docker` flag to specify `docker` as the build image type.
354
355```
356llama stack build --template local --image-type docker --name docker-0
357```
358
359Alternatively, you may use a config file and set `image_type` to `docker` in our `<name>-build.yaml` file, and run `llama stack build <name>-build.yaml`. The `<name>-build.yaml` will be of contents like:
360
361```
362name: local-docker-example
363distribution_spec:
364 description: Use code from `llama_stack` itself to serve all llama stack APIs
365 docker_image: null
366 providers:
367 inference: meta-reference
368 memory: meta-reference-faiss
369 safety: meta-reference
370 agentic_system: meta-reference
371 telemetry: console
372image_type: docker
373```
374
375The following command allows you to build a Docker image with the name `<name>`
376```
377llama stack build --config <name>-build.yaml
378
379Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim
380WORKDIR /app
381...
382...
383You can run it with: podman run -p 8000:8000 llamastack-docker-local
384Build spec configuration saved at ~/.llama/distributions/docker/docker-local-build.yaml
385```
386
387
388### Step 3.2 Configure
389After our distribution is built (either in form of docker or conda environment), we will run the following command to
390```
391llama stack configure [ <name> | <docker-image-name> | <path/to/name.build.yaml>]
392```
393- For `conda` environments: <path/to/name.build.yaml> would be the generated build spec saved from Step 1.
394- For `docker` images downloaded from Dockerhub, you could also use <docker-image-name> as the argument.
395 - Run `docker images` to check list of available images on your machine.
396
397```
398$ llama stack configure ~/.llama/distributions/conda/8b-instruct-build.yaml
399
400Configuring API: inference (meta-reference)
401Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required):
402Enter value for quantization (optional):
403Enter value for torch_seed (optional):
404Enter value for max_seq_len (existing: 4096) (required):
405Enter value for max_batch_size (existing: 1) (required):
406
407Configuring API: memory (meta-reference-faiss)
408
409Configuring API: safety (meta-reference)
410Do you want to configure llama_guard_shield? (y/n): y
411Entering sub-configuration for llama_guard_shield:
412Enter value for model (default: Llama-Guard-3-8B) (required):
413Enter value for excluded_categories (default: []) (required):
414Enter value for disable_input_check (default: False) (required):
415Enter value for disable_output_check (default: False) (required):
416Do you want to configure prompt_guard_shield? (y/n): y
417Entering sub-configuration for prompt_guard_shield:
418Enter value for model (default: Prompt-Guard-86M) (required):
419
420Configuring API: agentic_system (meta-reference)
421Enter value for brave_search_api_key (optional):
422Enter value for bing_search_api_key (optional):
423Enter value for wolfram_api_key (optional):
424
425Configuring API: telemetry (console)
426
427YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml
428```
429
430After this step is successful, you should be able to find a run configuration spec in `~/.llama/builds/conda/8b-instruct-run.yaml` with the following contents. You may edit this file to change the settings.
431
432As you can see, we did basic configuration above and configured:
433- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
434- Llama Guard safety shield with model `Llama-Guard-3-8B`
435- Prompt Guard safety shield with model `Prompt-Guard-86M`
436
437For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
438
439Note that all configurations as well as models are stored in `~/.llama`
440
441
442### Step 3.3 Run
443Now, let's start the Llama Stack Distribution Server. You will need the YAML configuration file which was written out at the end by the `llama stack configure` step.
444
445```
446llama stack run ~/.llama/builds/conda/8b-instruct-run.yaml
447```
448
449You should see the Llama Stack server start and print the APIs that it is supporting
450
451```
452$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml
453
454> initializing model parallel with size 1
455> initializing ddp with size 1
456> initializing pipeline with size 1
457Loaded in 19.28 seconds
458NCCL version 2.20.5+cuda12.4
459Finished model load YES READY
460Serving POST /inference/batch_chat_completion
461Serving POST /inference/batch_completion
462Serving POST /inference/chat_completion
463Serving POST /inference/completion
464Serving POST /safety/run_shields
465Serving POST /agentic_system/memory_bank/attach
466Serving POST /agentic_system/create
467Serving POST /agentic_system/session/create
468Serving POST /agentic_system/turn/create
469Serving POST /agentic_system/delete
470Serving POST /agentic_system/session/delete
471Serving POST /agentic_system/memory_bank/detach
472Serving POST /agentic_system/session/get
473Serving POST /agentic_system/step/get
474Serving POST /agentic_system/turn/get
475Listening on :::5000
476INFO: Started server process [453333]
477INFO: Waiting for application startup.
478INFO: Application startup complete.
479INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
480```
481
482> [!NOTE]
483> Configuration is in `~/.llama/builds/local/conda/8b-instruct-run.yaml`. Feel free to increase `max_seq_len`.
484
485> [!IMPORTANT]
486> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
487
488> [!TIP]
489> You might need to use the flag `--disable-ipv6` to Disable IPv6 support
490
491This server is running a Llama model locally.
492
493### Step 3.4 Test with Client
494Once the server is setup, we can test it with a client to see the example outputs.
495```
496cd /path/to/llama-stack
497conda activate <env> # any environment containing the llama-stack pip package will work
498
499python -m llama_stack.apis.inference.client localhost 5000
500```
501
502This will run the chat completion client and query the distribution’s /inference/chat_completion API.
503
504Here is an example output:
505```
506User>hello world, write me a 2 sentence poem about the moon
507Assistant> Here's a 2-sentence poem about the moon:
508
509The moon glows softly in the midnight sky,
510A beacon of wonder, as it passes by.
511```
512
513Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by:
514
515```
516python -m llama_stack.apis.safety.client localhost 5000
517```
518
519You can find more example scripts with client SDKs to talk with the Llama Stack server in our [llama-stack-apps](https://github.com/meta-llama/llama-stack-apps/tree/main/sdk_examples) repo.