Elffuss BraidHub

kikocis/llama-stack public

fdc098c86bcc · 26.3 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| local-plus-tgi-inference | { | Use TGI (local or with [Hugging Face Inference Endpoints](https:// |
290| | "inference": "remote::tgi", | huggingface.co/inference-endpoints/dedicated)) for running LLM |
291| | "safety": "meta-reference", | inference. When using HF Inference Endpoints, you must provide the |
292| | "agentic_system": "meta-reference", | name of the endpoint. |
293| | "memory": "meta-reference-faiss" | |
294| | } | |
295+--------------------------------+---------------------------------------+----------------------------------------------------------------------+
296</pre>
297
298As 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.
299
300### Step 3.2: Build a distribution
301
302Let'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.
303
304```
305llama stack build
306```
307
308Once it runs, you will be prompted to enter build name and optional arguments, and should see some outputs in the form:
309
310```
311$ llama stack build
312Enter value for name (required): 8b-instruct
313Enter value for distribution (default: local) (required): local
314Enter value for api_providers (optional):
315Enter value for image_type (default: conda) (required):
316
317....
318....
319Successfully 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
320
321Successfully setup conda environment. Configuring build...
322
323...
324...
325
326YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
327Target `8b-test` built with configuration at /home/xiyan/.llama/builds/local/conda/8b-test.yaml
328Build spec configuration saved at /home/xiyan/.llama/distributions/local/conda/8b-test-build.yaml
329```
330
331You can re-build package based on build config
332```
333$ cat ~/.llama/distributions/local/conda/8b-instruct-build.yaml
334name: 8b-instruct
335distribution: local
336api_providers: null
337image_type: conda
338
339$ llama stack build --config ~/.llama/distributions/local/conda/8b-instruct-build.yaml
340
341Successfully setup conda environment. Configuring build...
342
343...
344...
345
346YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
347Target `8b-instruct` built with configuration at ~/.llama/builds/local/conda/8b-instruct.yaml
348Build spec configuration saved at ~/.llama/distributions/local/conda/8b-instruct-build.yaml
349```
350
351### Step 3.3: Configure a distribution
352
353You can re-configure this distribution by running:
354```
355llama stack configure ~/.llama/builds/local/conda/8b-instruct.yaml
356```
357
358Here is an example run of how the CLI will guide you to fill the configuration
359
360```
361$ llama stack configure ~/.llama/builds/local/conda/8b-instruct.yaml
362
363Configuring API: inference (meta-reference)
364Enter value for model (required): Meta-Llama3.1-8B-Instruct
365Enter value for quantization (optional):
366Enter value for torch_seed (optional):
367Enter value for max_seq_len (required): 4096
368Enter value for max_batch_size (default: 1): 1
369Configuring API: safety (meta-reference)
370Do you want to configure llama_guard_shield? (y/n): y
371Entering sub-configuration for llama_guard_shield:
372Enter value for model (required): Llama-Guard-3-8B
373Enter value for excluded_categories (required): []
374Enter value for disable_input_check (default: False):
375Enter value for disable_output_check (default: False):
376Do you want to configure prompt_guard_shield? (y/n): y
377Entering sub-configuration for prompt_guard_shield:
378Enter value for model (required): Prompt-Guard-86M
379...
380...
381YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml
382```
383
384As you can see, we did basic configuration above and configured:
385- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
386- Llama Guard safety shield with model `Llama-Guard-3-8B`
387- Prompt Guard safety shield with model `Prompt-Guard-86M`
388
389For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
390
391Note that all configurations as well as models are stored in `~/.llama`
392
393## Step 4: Starting a Llama Stack Distribution and Testing it
394
395### Step 4.1: Starting a distribution
396
397Now let’s start Llama Stack Distribution Server.
398
399You need the YAML configuration file which was written out at the end by the `llama stack build` step.
400
401```
402llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml --port 5000
403```
404You should see the Stack server start and print the APIs that it is supporting,
405
406```
407$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml --port 5000
408
409> initializing model parallel with size 1
410> initializing ddp with size 1
411> initializing pipeline with size 1
412Loaded in 19.28 seconds
413NCCL version 2.20.5+cuda12.4
414Finished model load YES READY
415Serving POST /inference/batch_chat_completion
416Serving POST /inference/batch_completion
417Serving POST /inference/chat_completion
418Serving POST /inference/completion
419Serving POST /safety/run_shields
420Serving POST /agentic_system/memory_bank/attach
421Serving POST /agentic_system/create
422Serving POST /agentic_system/session/create
423Serving POST /agentic_system/turn/create
424Serving POST /agentic_system/delete
425Serving POST /agentic_system/session/delete
426Serving POST /agentic_system/memory_bank/detach
427Serving POST /agentic_system/session/get
428Serving POST /agentic_system/step/get
429Serving POST /agentic_system/turn/get
430Listening on :::5000
431INFO: Started server process [453333]
432INFO: Waiting for application startup.
433INFO: Application startup complete.
434INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
435```
436
437
438> [!NOTE]
439> Configuration is in `~/.llama/builds/local/conda/8b-instruct.yaml`. Feel free to increase `max_seq_len`.
440
441> [!IMPORTANT]
442> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
443
444This server is running a Llama model locally.
445
446### Step 4.2: Test the distribution
447
448Lets test with a client.
449```
450cd /path/to/llama-stack
451conda activate <env> # any environment containing the llama-toolchain pip package will work
452
453python -m llama_toolchain.inference.client localhost 5000
454```
455
456This will run the chat completion client and query the distribution’s /inference/chat_completion API.
457
458Here is an example output:
459<pre style="font-family: monospace;">
460Initializing client for http://localhost:5000
461User>hello world, troll me in two-paragraphs about 42
462
463Assistant> 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.
464
465You 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!
466</pre>
467
468Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by:
469
470```
471python -m llama_toolchain.safety.client localhost 5000
472```