Elffuss BraidHub

kikocis/llama-stack public

5498ce93bcd1 · 24.6 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#### Build a local distribution with conda
275The following command and specifications allows you to get started with building.
276```
277llama stack build <path/to/config>
278```
279- You will be required to pass in a file path to the build.config file (e.g. `./llama_stack/distribution/example_configs/conda/local-conda-example-build.yaml`). We provide some example build config files for configuring different types of distributions in the `./llama_stack/distribution/example_configs/` folder.
280
281The file will be of the contents
282```
283$ cat ./llama_stack/distribution/example_configs/conda/local-conda-example-build.yaml
284
285name: 8b-instruct
286distribution_spec:
287 distribution_type: local
288 description: Use code from `llama_stack` itself to serve all llama stack APIs
289 docker_image: null
290 providers:
291 inference: meta-reference
292 memory: meta-reference-faiss
293 safety: meta-reference
294 agentic_system: meta-reference
295 telemetry: console
296image_type: conda
297```
298
299You may run the `llama stack build` command to generate your distribution with `--name` to override the name for your distribution.
300```
301$ llama stack build ~/.llama/distributions/conda/8b-instruct-build.yaml --name 8b-instruct
302...
303...
304Build spec configuration saved at ~/.llama/distributions/conda/8b-instruct-build.yaml
305```
306
307After this step is complete, a file named `8b-instruct-build.yaml` will be generated and saved at `~/.llama/distributions/conda/8b-instruct-build.yaml`.
308
309
310#### How to build distribution with different API providers using configs
311To specify a different API provider, we can change the `distribution_spec` in our `<name>-build.yaml` config. For example, the following build spec allows you to build a distribution using TGI as the inference API provider.
312
313```
314$ cat ./llama_stack/distribution/example_configs/conda/local-tgi-conda-example-build.yaml
315
316name: local-tgi-conda-example
317distribution_spec:
318 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).
319 docker_image: null
320 providers:
321 inference: remote::tgi
322 memory: meta-reference-faiss
323 safety: meta-reference
324 agentic_system: meta-reference
325 telemetry: console
326image_type: conda
327```
328
329The following command allows you to build a distribution with TGI as the inference API provider, with the name `tgi`.
330```
331llama stack build --config ./llama_stack/distribution/example_configs/conda/local-tgi-conda-example-build.yaml --name tgi
332```
333
334We provide some example build configs to help you get started with building with different API providers.
335
336#### How to build distribution with Docker image
337To build a docker image, simply change the `image_type` to `docker` in our `<name>-build.yaml` file, and run `llama stack build --config <name>-build.yaml`.
338
339```
340$ cat ./llama_stack/distribution/example_configs/docker/local-docker-example-build.yaml
341
342name: local-docker-example
343distribution_spec:
344 description: Use code from `llama_stack` itself to serve all llama stack APIs
345 docker_image: null
346 providers:
347 inference: meta-reference
348 memory: meta-reference-faiss
349 safety: meta-reference
350 agentic_system: meta-reference
351 telemetry: console
352image_type: docker
353```
354
355The following command allows you to build a Docker image with the name `docker-local`
356```
357llama stack build --config ./llama_stack/distribution/example_configs/docker/local-docker-example-build.yaml --name docker-local
358
359Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim
360WORKDIR /app
361...
362...
363You can run it with: podman run -p 8000:8000 llamastack-docker-local
364Build spec configuration saved at /home/xiyan/.llama/distributions/docker/docker-local-build.yaml
365```
366
367### Step 3.2. Configure
368After our distribution is built (either in form of docker or conda environment), we will run the following command to
369```
370llama stack configure [<path/to/name.build.yaml> | <docker-image-name>]
371```
372- For `conda` environments: <path/to/name.build.yaml> would be the generated build spec saved from Step 1.
373- For `docker` images downloaded from Dockerhub, you could also use <docker-image-name> as the argument.
374 - Run `docker images` to check list of available images on your machine.
375
376```
377$ llama stack configure ~/.llama/distributions/conda/8b-instruct-build.yaml
378
379Configuring API: inference (meta-reference)
380Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required):
381Enter value for quantization (optional):
382Enter value for torch_seed (optional):
383Enter value for max_seq_len (existing: 4096) (required):
384Enter value for max_batch_size (existing: 1) (required):
385
386Configuring API: memory (meta-reference-faiss)
387
388Configuring API: safety (meta-reference)
389Do you want to configure llama_guard_shield? (y/n): y
390Entering sub-configuration for llama_guard_shield:
391Enter value for model (default: Llama-Guard-3-8B) (required):
392Enter value for excluded_categories (default: []) (required):
393Enter value for disable_input_check (default: False) (required):
394Enter value for disable_output_check (default: False) (required):
395Do you want to configure prompt_guard_shield? (y/n): y
396Entering sub-configuration for prompt_guard_shield:
397Enter value for model (default: Prompt-Guard-86M) (required):
398
399Configuring API: agentic_system (meta-reference)
400Enter value for brave_search_api_key (optional):
401Enter value for bing_search_api_key (optional):
402Enter value for wolfram_api_key (optional):
403
404Configuring API: telemetry (console)
405
406YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml
407```
408
409After 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.
410
411As you can see, we did basic configuration above and configured:
412- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
413- Llama Guard safety shield with model `Llama-Guard-3-8B`
414- Prompt Guard safety shield with model `Prompt-Guard-86M`
415
416For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
417
418Note that all configurations as well as models are stored in `~/.llama`
419
420
421#### Step 3.2.1 API Keys for Tools
422
423API key configuration for the Agentic System will be asked by the `llama stack build` script when you install a Llama Stack distribution.
424
425Tools that the model supports and which need API Keys --
426- Brave for web search (https://api.search.brave.com/register)
427- Wolfram for math operations (https://developer.wolframalpha.com/)
428
429> **Tip** If you do not have API keys, you can still run the app without model having access to the tools.
430
431### Step 3.3. Run
432Now, 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.
433
434```
435llama stack run ~/.llama/builds/conda/8b-instruct-run.yaml
436```
437
438You should see the Llama Stack server start and print the APIs that it is supporting
439
440```
441$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml
442
443> initializing model parallel with size 1
444> initializing ddp with size 1
445> initializing pipeline with size 1
446Loaded in 19.28 seconds
447NCCL version 2.20.5+cuda12.4
448Finished model load YES READY
449Serving POST /inference/batch_chat_completion
450Serving POST /inference/batch_completion
451Serving POST /inference/chat_completion
452Serving POST /inference/completion
453Serving POST /safety/run_shields
454Serving POST /agentic_system/memory_bank/attach
455Serving POST /agentic_system/create
456Serving POST /agentic_system/session/create
457Serving POST /agentic_system/turn/create
458Serving POST /agentic_system/delete
459Serving POST /agentic_system/session/delete
460Serving POST /agentic_system/memory_bank/detach
461Serving POST /agentic_system/session/get
462Serving POST /agentic_system/step/get
463Serving POST /agentic_system/turn/get
464Listening on :::5000
465INFO: Started server process [453333]
466INFO: Waiting for application startup.
467INFO: Application startup complete.
468INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
469```
470
471> [!NOTE]
472> Configuration is in `~/.llama/builds/local/conda/8b-instruct.yaml`. Feel free to increase `max_seq_len`.
473
474> [!IMPORTANT]
475> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
476
477This server is running a Llama model locally.
478
479### Step 3.4 Test with Client
480Once the server is setup, we can test it with a client to see the example outputs.
481```
482cd /path/to/llama-stack
483conda activate <env> # any environment containing the llama-stack pip package will work
484
485python -m llama_stack.apis.inference.client localhost 5000
486```
487
488This will run the chat completion client and query the distribution’s /inference/chat_completion API.
489
490Here is an example output:
491```
492Initializing client for http://localhost:5000
493User>hello world, troll me in two-paragraphs about 42
494
495Assistant> You think you're so smart, don't you? You think you can just waltz in here and ask about 42, like it's some kind of trivial matter. Well, let me tell you, 42 is not just a number, it's a way of life. It's the answer to the ultimate question of life, the universe, and everything, according to Douglas Adams' magnum opus, "The Hitchhiker's Guide to the Galaxy". But do you know what's even more interesting about 42? It's that it's not actually the answer to anything, it's just a number that some guy made up to sound profound.
496
497You know what's even more hilarious? People like you who think they can just Google "42" and suddenly become experts on the subject. Newsflash: you're not a supercomputer, you're just a human being with a fragile ego and a penchant for thinking you're smarter than you actually are. 42 is just a number, a meaningless collection of digits that holds no significance whatsoever. So go ahead, keep thinking you're so clever, but deep down, you're just a pawn in the grand game of life, and 42 is just a silly little number that's been used to make you feel like you're part of something bigger than yourself. Ha!
498```
499
500Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by:
501
502```
503python -m llama_stack.apis.safety.client localhost 5000
504```
505
506You 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.