Elffuss BraidHub

kikocis/llama-stack public

371459e1ee27 · 24.2 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-toolchain` 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
73Here 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/)
74```
75llama download --source meta --model-id Meta-Llama3.1-8B-Instruct --meta-url <META_URL>
76```
77```
78llama download --source meta --model-id Meta-Llama3.1-70B-Instruct --meta-url <META_URL>
79```
80
81You can download from HuggingFace using these commands
82Set your environment variable HF_TOKEN or pass in --hf-token to the command to validate your access.
83You can find your token at [here](https://huggingface.co/settings/tokens)
84```
85llama download --source huggingface --model-id Meta-Llama3.1-8B-Instruct --hf-token <HF_TOKEN>
86```
87```
88llama download --source huggingface --model-id Meta-Llama3.1-70B-Instruct --hf-token <HF_TOKEN>
89```
90
91You can also download safety models from HF
92```
93llama download --source huggingface --model-id Llama-Guard-3-8B --ignore-patterns *original*
94```
95```
96llama download --source huggingface --model-id Prompt-Guard-86M --ignore-patterns *original*
97```
98
99## Step 2: Understand the models
100The `llama model` command helps you explore the model’s interface.
101
102### 2.1 Subcommands
1031. `download`: Download the model from different sources. (meta, huggingface)
1042. `list`: Lists all the models available for download with hardware requirements to deploy the models.
1053. `template`: <TODO: What is a template?>
1064. `describe`: Describes all the properties of the model.
107
108### 2.2 Sample Usage
109
110`llama model <subcommand> <options>`
111
112```
113llama model --help
114```
115<pre style="font-family: monospace;">
116usage: llama model [-h] {download,list,template,describe} ...
117
118Work with llama models
119
120options:
121 -h, --help show this help message and exit
122
123model_subcommands:
124 {download,list,template,describe}
125</pre>
126
127You can use the describe command to know more about a model:
128```
129llama model describe -m Meta-Llama3.1-8B-Instruct
130```
131### 2.3 Describe
132
133<pre style="font-family: monospace;">
134+-----------------------------+---------------------------------------+
135| Model | Meta- |
136| | Llama3.1-8B-Instruct |
137+-----------------------------+---------------------------------------+
138| HuggingFace ID | meta-llama/Meta-Llama-3.1-8B-Instruct |
139+-----------------------------+---------------------------------------+
140| Description | Llama 3.1 8b instruct model |
141+-----------------------------+---------------------------------------+
142| Context Length | 128K tokens |
143+-----------------------------+---------------------------------------+
144| Weights format | bf16 |
145+-----------------------------+---------------------------------------+
146| Model params.json | { |
147| | "dim": 4096, |
148| | "n_layers": 32, |
149| | "n_heads": 32, |
150| | "n_kv_heads": 8, |
151| | "vocab_size": 128256, |
152| | "ffn_dim_multiplier": 1.3, |
153| | "multiple_of": 1024, |
154| | "norm_eps": 1e-05, |
155| | "rope_theta": 500000.0, |
156| | "use_scaled_rope": true |
157| | } |
158+-----------------------------+---------------------------------------+
159| Recommended sampling params | { |
160| | "strategy": "top_p", |
161| | "temperature": 1.0, |
162| | "top_p": 0.9, |
163| | "top_k": 0 |
164| | } |
165+-----------------------------+---------------------------------------+
166</pre>
167### 2.4 Template
168You can even run `llama model template` see all of the templates and their tokens:
169
170```
171llama model template
172```
173
174<pre style="font-family: monospace;">
175+-----------+---------------------------------+
176| Role | Template Name |
177+-----------+---------------------------------+
178| user | user-default |
179| assistant | assistant-builtin-tool-call |
180| assistant | assistant-custom-tool-call |
181| assistant | assistant-default |
182| system | system-builtin-and-custom-tools |
183| system | system-builtin-tools-only |
184| system | system-custom-tools-only |
185| system | system-default |
186| tool | tool-success |
187| tool | tool-failure |
188+-----------+---------------------------------+
189</pre>
190
191And fetch an example by passing it to `--name`:
192```
193llama model template --name tool-success
194```
195
196<pre style="font-family: monospace;">
197+----------+----------------------------------------------------------------+
198| Name | tool-success |
199+----------+----------------------------------------------------------------+
200| Template | <|start_header_id|>ipython<|end_header_id|> |
201| | |
202| | completed |
203| | [stdout]{"results":["something |
204| | something"]}[/stdout]<|eot_id|> |
205| | |
206+----------+----------------------------------------------------------------+
207| Notes | Note ipython header and [stdout] |
208+----------+----------------------------------------------------------------+
209</pre>
210
211Or:
212```
213llama model template --name system-builtin-tools-only
214```
215
216<pre style="font-family: monospace;">
217+----------+--------------------------------------------+
218| Name | system-builtin-tools-only |
219+----------+--------------------------------------------+
220| Template | <|start_header_id|>system<|end_header_id|> |
221| | |
222| | Environment: ipython |
223| | Tools: brave_search, wolfram_alpha |
224| | |
225| | Cutting Knowledge Date: December 2023 |
226| | Today Date: 21 August 2024 |
227| | <|eot_id|> |
228| | |
229+----------+--------------------------------------------+
230| Notes | |
231+----------+--------------------------------------------+
232</pre>
233
234These commands can help understand the model interface and how prompts / messages are formatted for various scenarios.
235
236**NOTE**: Outputs in terminal are color printed to show special tokens.
237
238
239## Step 3: Listing, Building, and Configuring Llama Stack Distributions
240
241
242### Step 3.1: List available distributions
243
244Let’s start with listing available distributions:
245
246```
247llama stack list-distributions
248```
249
250<pre style="font-family: monospace;">
251i+-------------------------------+---------------------------------------+----------------------------------------------------------------------+
252| Distribution Type | Providers | Description |
253+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
254| local | { | Use code from `llama_toolchain` itself to serve all llama stack APIs |
255| | "inference": "meta-reference", | |
256| | "memory": "meta-reference-faiss", | |
257| | "safety": "meta-reference", | |
258| | "agentic_system": "meta-reference" | |
259| | } | |
260+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
261| remote | { | Point to remote services for all llama stack APIs |
262| | "inference": "remote", | |
263| | "safety": "remote", | |
264| | "agentic_system": "remote", | |
265| | "memory": "remote" | |
266| | } | |
267+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
268| local-ollama | { | Like local, but use ollama for running LLM inference |
269| | "inference": "remote::ollama", | |
270| | "safety": "meta-reference", | |
271| | "agentic_system": "meta-reference", | |
272| | "memory": "meta-reference-faiss" | |
273| | } | |
274+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
275| local-plus-fireworks-inference | { | Use Fireworks.ai for running LLM inference |
276| | "inference": "remote::fireworks", | |
277| | "safety": "meta-reference", | |
278| | "agentic_system": "meta-reference", | |
279| | "memory": "meta-reference-faiss" | |
280| | } | |
281+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
282| local-plus-together-inference | { | Use Together.ai for running LLM inference |
283| | "inference": "remote::together", | |
284| | "safety": "meta-reference", | |
285| | "agentic_system": "meta-reference", | |
286| | "memory": "meta-reference-faiss" | |
287| | } | |
288+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
289</pre>
290
291As you can see above, each “distribution” details the “providers” it is composed of. For example, `local` uses the “meta-reference” provider for inference while local-ollama relies on a different provider (Ollama) for inference. Similarly, you can use Fireworks or Together.AI for running inference as well.
292
293### Step 3.2: Build a distribution
294
295Let'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.
296
297```
298llama stack build local --name 8b-instruct
299```
300
301Once it runs successfully , you should see some outputs in the form:
302
303```
304$ llama stack build local --name 8b-instruct
305....
306....
307Successfully 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
308
309Successfully setup conda environment. Configuring build...
310
311...
312...
313
314YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
315```
316### Step 3.3: Configure a distribution
317
318You can re-configure this distribution by running:
319```
320llama stack configure local --name 8b-instruct
321```
322
323Here is an example run of how the CLI will guide you to fill the configuration
324```
325$ llama stack configure local --name 8b-instruct
326
327Configuring API: inference (meta-reference)
328Enter value for model (required): Meta-Llama3.1-8B-Instruct
329Enter value for quantization (optional):
330Enter value for torch_seed (optional):
331Enter value for max_seq_len (required): 4096
332Enter value for max_batch_size (default: 1): 1
333Configuring API: safety (meta-reference)
334Do you want to configure llama_guard_shield? (y/n): y
335Entering sub-configuration for llama_guard_shield:
336Enter value for model (required): Llama-Guard-3-8B
337Enter value for excluded_categories (required): []
338Enter value for disable_input_check (default: False):
339Enter value for disable_output_check (default: False):
340Do you want to configure prompt_guard_shield? (y/n): y
341Entering sub-configuration for prompt_guard_shield:
342Enter value for model (required): Prompt-Guard-86M
343...
344...
345YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
346```
347
348As you can see, we did basic configuration above and configured:
349- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
350- Llama Guard safety shield with model `Llama-Guard-3-8B`
351- Prompt Guard safety shield with model `Prompt-Guard-86M`
352
353For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
354
355Note that all configurations as well as models are stored in `~/.llama`
356
357## Step 4: Starting a Llama Stack Distribution and Testing it
358
359### Step 4.1: Starting a distribution
360
361Now let’s start Llama Stack Distribution Server.
362
363You need the YAML configuration file which was written out at the end by the `llama stack build` step.
364
365```
366llama stack run local --name 8b-instruct --port 5000
367```
368You should see the Stack server start and print the APIs that it is supporting,
369
370```
371$ llama stack run local --name 8b-instruct --port 5000
372
373> initializing model parallel with size 1
374> initializing ddp with size 1
375> initializing pipeline with size 1
376Loaded in 19.28 seconds
377NCCL version 2.20.5+cuda12.4
378Finished model load YES READY
379Serving POST /inference/batch_chat_completion
380Serving POST /inference/batch_completion
381Serving POST /inference/chat_completion
382Serving POST /inference/completion
383Serving POST /safety/run_shields
384Serving POST /agentic_system/memory_bank/attach
385Serving POST /agentic_system/create
386Serving POST /agentic_system/session/create
387Serving POST /agentic_system/turn/create
388Serving POST /agentic_system/delete
389Serving POST /agentic_system/session/delete
390Serving POST /agentic_system/memory_bank/detach
391Serving POST /agentic_system/session/get
392Serving POST /agentic_system/step/get
393Serving POST /agentic_system/turn/get
394Listening on :::5000
395INFO: Started server process [453333]
396INFO: Waiting for application startup.
397INFO: Application startup complete.
398INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
399```
400
401
402> [!NOTE]
403> Configuration is in `~/.llama/builds/local/conda/8b-instruct.yaml`. Feel free to increase `max_seq_len`.
404
405> [!IMPORTANT]
406> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
407
408This server is running a Llama model locally.
409
410### Step 4.2: Test the distribution
411
412Lets test with a client.
413```
414cd /path/to/llama-stack
415conda activate <env> # any environment containing the llama-toolchain pip package will work
416
417python -m llama_toolchain.inference.client localhost 5000
418```
419
420This will run the chat completion client and query the distribution’s /inference/chat_completion API.
421
422Here is an example output:
423<pre style="font-family: monospace;">
424Initializing client for http://localhost:5000
425User>hello world, troll me in two-paragraphs about 42
426
427Assistant> 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.
428
429You 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!
430</pre>
431
432Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by:
433
434```
435python -m llama_toolchain.safety.client localhost 5000
436```