Elffuss BraidHub

kikocis/llama-stack public

f788015f9412 · 22.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-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: Building, and Configuring Llama Stack Distributions
240
241- Please see our [Getting Started](getting_started.md) guide for details.
242
243### Step 3.1. Build
244In 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:
245- `name`: the name for our distribution (e.g. `8b-instruct`)
246- `image_type`: our build image type (`conda | docker`)
247- `distribution_spec`: our distribution specs for specifying API providers
248 - `description`: a short description of the configurations for the distribution
249 - `providers`: specifies the underlying implementation for serving each API endpoint
250 - `image_type`: `conda` | `docker` to specify whether to build the distribution in the form of Docker image or Conda environment.
251
252#### Build a local distribution with conda
253The following command and specifications allows you to get started with building.
254```
255llama stack build <path/to/config>
256```
257- You will be required to pass in a file path to the build.config file (e.g. `./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml`). We provide some example build config files for configuring different types of distributions in the `./llama_toolchain/configs/distributions/` folder.
258
259The file will be of the contents
260```
261$ cat ./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml
262
263name: 8b-instruct
264distribution_spec:
265 distribution_type: local
266 description: Use code from `llama_toolchain` itself to serve all llama stack APIs
267 docker_image: null
268 providers:
269 inference: meta-reference
270 memory: meta-reference-faiss
271 safety: meta-reference
272 agentic_system: meta-reference
273 telemetry: console
274image_type: conda
275```
276
277You may run the `llama stack build` command to generate your distribution with `--name` to override the name for your distribution.
278```
279$ llama stack build ~/.llama/distributions/conda/8b-instruct-build.yaml --name 8b-instruct
280...
281...
282Build spec configuration saved at ~/.llama/distributions/conda/8b-instruct-build.yaml
283```
284
285After this step is complete, a file named `8b-instruct-build.yaml` will be generated and saved at `~/.llama/distributions/conda/8b-instruct-build.yaml`.
286
287
288#### How to build distribution with different API providers using configs
289To 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.
290
291```
292$ cat ./llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml
293
294name: local-tgi-conda-example
295distribution_spec:
296 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).
297 docker_image: null
298 providers:
299 inference: remote::tgi
300 memory: meta-reference-faiss
301 safety: meta-reference
302 agentic_system: meta-reference
303 telemetry: console
304image_type: conda
305```
306
307The following command allows you to build a distribution with TGI as the inference API provider, with the name `tgi`.
308```
309llama stack build --config ./llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml --name tgi
310```
311
312We provide some example build configs to help you get started with building with different API providers.
313
314#### How to build distribution with Docker image
315To 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`.
316
317```
318$ cat ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml
319
320name: local-docker-example
321distribution_spec:
322 description: Use code from `llama_toolchain` itself to serve all llama stack APIs
323 docker_image: null
324 providers:
325 inference: meta-reference
326 memory: meta-reference-faiss
327 safety: meta-reference
328 agentic_system: meta-reference
329 telemetry: console
330image_type: docker
331```
332
333The following command allows you to build a Docker image with the name `docker-local`
334```
335llama stack build --config ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml --name docker-local
336
337Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim
338WORKDIR /app
339...
340...
341You can run it with: podman run -p 8000:8000 llamastack-docker-local
342Build spec configuration saved at /home/xiyan/.llama/distributions/docker/docker-local-build.yaml
343```
344
345### Step 3.2. Configure
346After our distribution is built (either in form of docker or conda environment), we will run the following command to
347```
348llama stack configure [<path/to/name.build.yaml> | <docker-image-name>]
349```
350- For `conda` environments: <path/to/name.build.yaml> would be the generated build spec saved from Step 1.
351- For `docker` images downloaded from Dockerhub, you could also use <docker-image-name> as the argument.
352 - Run `docker images` to check list of available images on your machine.
353
354```
355$ llama stack configure ~/.llama/distributions/conda/8b-instruct-build.yaml
356
357Configuring API: inference (meta-reference)
358Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required):
359Enter value for quantization (optional):
360Enter value for torch_seed (optional):
361Enter value for max_seq_len (existing: 4096) (required):
362Enter value for max_batch_size (existing: 1) (required):
363
364Configuring API: memory (meta-reference-faiss)
365
366Configuring API: safety (meta-reference)
367Do you want to configure llama_guard_shield? (y/n): y
368Entering sub-configuration for llama_guard_shield:
369Enter value for model (default: Llama-Guard-3-8B) (required):
370Enter value for excluded_categories (default: []) (required):
371Enter value for disable_input_check (default: False) (required):
372Enter value for disable_output_check (default: False) (required):
373Do you want to configure prompt_guard_shield? (y/n): y
374Entering sub-configuration for prompt_guard_shield:
375Enter value for model (default: Prompt-Guard-86M) (required):
376
377Configuring API: agentic_system (meta-reference)
378Enter value for brave_search_api_key (optional):
379Enter value for bing_search_api_key (optional):
380Enter value for wolfram_api_key (optional):
381
382Configuring API: telemetry (console)
383
384YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml
385```
386
387After 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.
388
389As you can see, we did basic configuration above and configured:
390- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
391- Llama Guard safety shield with model `Llama-Guard-3-8B`
392- Prompt Guard safety shield with model `Prompt-Guard-86M`
393
394For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
395
396Note that all configurations as well as models are stored in `~/.llama`
397
398
399### Step 3.3. Run
400Now, 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.
401
402```
403llama stack run ~/.llama/builds/conda/8b-instruct-run.yaml
404```
405
406You should see the Llama Stack server start and print the APIs that it is supporting
407
408```
409$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml
410
411> initializing model parallel with size 1
412> initializing ddp with size 1
413> initializing pipeline with size 1
414Loaded in 19.28 seconds
415NCCL version 2.20.5+cuda12.4
416Finished model load YES READY
417Serving POST /inference/batch_chat_completion
418Serving POST /inference/batch_completion
419Serving POST /inference/chat_completion
420Serving POST /inference/completion
421Serving POST /safety/run_shields
422Serving POST /agentic_system/memory_bank/attach
423Serving POST /agentic_system/create
424Serving POST /agentic_system/session/create
425Serving POST /agentic_system/turn/create
426Serving POST /agentic_system/delete
427Serving POST /agentic_system/session/delete
428Serving POST /agentic_system/memory_bank/detach
429Serving POST /agentic_system/session/get
430Serving POST /agentic_system/step/get
431Serving POST /agentic_system/turn/get
432Listening on :::5000
433INFO: Started server process [453333]
434INFO: Waiting for application startup.
435INFO: Application startup complete.
436INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
437```
438
439> [!NOTE]
440> Configuration is in `~/.llama/builds/local/conda/8b-instruct.yaml`. Feel free to increase `max_seq_len`.
441
442> [!IMPORTANT]
443> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
444
445This server is running a Llama model locally.
446
447### Step 3.4 Test with Client
448Once the server is setup, we can test it with a client to see the example outputs.
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```
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```
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```
473
474You 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.