Elffuss BraidHub

kikocis/llama-stack public

efac2a2f572e · 13.5 KiBRenderedRawPermalink
1# Getting Started
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
5This guides allows you to quickly get started with building and running a Llama Stack server in < 5 minutes!
6
7## Quick Cheatsheet
8- Quick 3 line command to build and start a LlamaStack server using our Meta Reference implementation for all API endpoints with `conda` as build type.
9
10**`llama stack build`**
11```
12llama stack build --config ./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml --name my-local-llama-stack
13...
14...
15Build spec configuration saved at ~/.llama/distributions/conda/my-local-llama-stack-build.yaml
16```
17
18**`llama stack configure`**
19```
20llama stack configure ~/.llama/distributions/conda/my-local-llama-stack-build.yaml
21
22Configuring API: inference (meta-reference)
23Enter value for model (default: Meta-Llama3.1-8B-Instruct) (required):
24Enter value for quantization (optional):
25Enter value for torch_seed (optional):
26Enter value for max_seq_len (required): 4096
27Enter value for max_batch_size (default: 1) (required):
28
29Configuring API: memory (meta-reference-faiss)
30
31Configuring API: safety (meta-reference)
32Do you want to configure llama_guard_shield? (y/n): n
33Do you want to configure prompt_guard_shield? (y/n): n
34
35Configuring API: agentic_system (meta-reference)
36Enter value for brave_search_api_key (optional):
37Enter value for bing_search_api_key (optional):
38Enter value for wolfram_api_key (optional):
39
40Configuring API: telemetry (console)
41
42YAML configuration has been written to ~/.llama/builds/conda/my-local-llama-stack-run.yaml
43```
44
45**`llama stack run`**
46```
47llama stack run ~/.llama/builds/conda/my-local-llama-stack-run.yaml
48
49...
50> initializing model parallel with size 1
51> initializing ddp with size 1
52> initializing pipeline with size 1
53...
54Finished model load YES READY
55Serving POST /inference/chat_completion
56Serving POST /inference/completion
57Serving POST /inference/embeddings
58Serving POST /memory_banks/create
59Serving DELETE /memory_bank/documents/delete
60Serving DELETE /memory_banks/drop
61Serving GET /memory_bank/documents/get
62Serving GET /memory_banks/get
63Serving POST /memory_bank/insert
64Serving GET /memory_banks/list
65Serving POST /memory_bank/query
66Serving POST /memory_bank/update
67Serving POST /safety/run_shields
68Serving POST /agentic_system/create
69Serving POST /agentic_system/session/create
70Serving POST /agentic_system/turn/create
71Serving POST /agentic_system/delete
72Serving POST /agentic_system/session/delete
73Serving POST /agentic_system/session/get
74Serving POST /agentic_system/step/get
75Serving POST /agentic_system/turn/get
76Serving GET /telemetry/get_trace
77Serving POST /telemetry/log_event
78Listening on :::5000
79INFO: Started server process [587053]
80INFO: Waiting for application startup.
81INFO: Application startup complete.
82INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
83```
84
85
86## Step 1. Build
87In 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:
88- `name`: the name for our distribution (e.g. `8b-instruct`)
89- `image_type`: our build image type (`conda | docker`)
90- `distribution_spec`: our distribution specs for specifying API providers
91 - `description`: a short description of the configurations for the distribution
92 - `providers`: specifies the underlying implementation for serving each API endpoint
93 - `image_type`: `conda` | `docker` to specify whether to build the distribution in the form of Docker image or Conda environment.
94
95#### Build a local distribution with conda
96The following command and specifications allows you to get started with building.
97```
98llama stack build <path/to/config>
99```
100- 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.
101
102The file will be of the contents
103```
104$ cat ./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml
105
106name: 8b-instruct
107distribution_spec:
108 distribution_type: local
109 description: Use code from `llama_toolchain` itself to serve all llama stack APIs
110 docker_image: null
111 providers:
112 inference: meta-reference
113 memory: meta-reference-faiss
114 safety: meta-reference
115 agentic_system: meta-reference
116 telemetry: console
117image_type: conda
118```
119
120You may run the `llama stack build` command to generate your distribution with `--name` to override the name for your distribution.
121```
122$ llama stack build ~/.llama/distributions/conda/8b-instruct-build.yaml --name 8b-instruct
123...
124...
125Build spec configuration saved at ~/.llama/distributions/conda/8b-instruct-build.yaml
126```
127
128After this step is complete, a file named `8b-instruct-build.yaml` will be generated and saved at `~/.llama/distributions/conda/8b-instruct-build.yaml`.
129
130
131#### How to build distribution with different API providers using configs
132To 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.
133
134```
135$ cat ./llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml
136
137name: local-tgi-conda-example
138distribution_spec:
139 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).
140 docker_image: null
141 providers:
142 inference: remote::tgi
143 memory: meta-reference-faiss
144 safety: meta-reference
145 agentic_system: meta-reference
146 telemetry: console
147image_type: conda
148```
149
150The following command allows you to build a distribution with TGI as the inference API provider, with the name `tgi`.
151```
152llama stack build --config ./llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml --name tgi
153```
154
155We provide some example build configs to help you get started with building with different API providers.
156
157#### How to build distribution with Docker image
158To 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`.
159
160```
161$ cat ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml
162
163name: local-docker-example
164distribution_spec:
165 description: Use code from `llama_toolchain` itself to serve all llama stack APIs
166 docker_image: null
167 providers:
168 inference: meta-reference
169 memory: meta-reference-faiss
170 safety: meta-reference
171 agentic_system: meta-reference
172 telemetry: console
173image_type: docker
174```
175
176The following command allows you to build a Docker image with the name `docker-local`
177```
178llama stack build --config ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml --name docker-local
179
180Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim
181WORKDIR /app
182...
183...
184You can run it with: podman run -p 8000:8000 llamastack-docker-local
185Build spec configuration saved at /home/xiyan/.llama/distributions/docker/docker-local-build.yaml
186```
187
188## Step 2. Configure
189After our distribution is built (either in form of docker or conda environment), we will run the following command to
190```
191llama stack configure [<path/to/name.build.yaml> | <docker-image-name>]
192```
193- For `conda` environments: <path/to/name.build.yaml> would be the generated build spec saved from Step 1.
194- For `docker` images downloaded from Dockerhub, you could also use <docker-image-name> as the argument.
195 - Run `docker images` to check list of available images on your machine.
196
197```
198$ llama stack configure ~/.llama/distributions/conda/8b-instruct-build.yaml
199
200Configuring API: inference (meta-reference)
201Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required):
202Enter value for quantization (optional):
203Enter value for torch_seed (optional):
204Enter value for max_seq_len (existing: 4096) (required):
205Enter value for max_batch_size (existing: 1) (required):
206
207Configuring API: memory (meta-reference-faiss)
208
209Configuring API: safety (meta-reference)
210Do you want to configure llama_guard_shield? (y/n): y
211Entering sub-configuration for llama_guard_shield:
212Enter value for model (default: Llama-Guard-3-8B) (required):
213Enter value for excluded_categories (default: []) (required):
214Enter value for disable_input_check (default: False) (required):
215Enter value for disable_output_check (default: False) (required):
216Do you want to configure prompt_guard_shield? (y/n): y
217Entering sub-configuration for prompt_guard_shield:
218Enter value for model (default: Prompt-Guard-86M) (required):
219
220Configuring API: agentic_system (meta-reference)
221Enter value for brave_search_api_key (optional):
222Enter value for bing_search_api_key (optional):
223Enter value for wolfram_api_key (optional):
224
225Configuring API: telemetry (console)
226
227YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml
228```
229
230After 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.
231
232As you can see, we did basic configuration above and configured:
233- inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`)
234- Llama Guard safety shield with model `Llama-Guard-3-8B`
235- Prompt Guard safety shield with model `Prompt-Guard-86M`
236
237For how these configurations are stored as yaml, checkout the file printed at the end of the configuration.
238
239Note that all configurations as well as models are stored in `~/.llama`
240
241
242## Step 3. Run
243Now, 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.
244
245```
246llama stack run ~/.llama/builds/conda/8b-instruct-run.yaml
247```
248
249You should see the Llama Stack server start and print the APIs that it is supporting
250
251```
252$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml
253
254> initializing model parallel with size 1
255> initializing ddp with size 1
256> initializing pipeline with size 1
257Loaded in 19.28 seconds
258NCCL version 2.20.5+cuda12.4
259Finished model load YES READY
260Serving POST /inference/batch_chat_completion
261Serving POST /inference/batch_completion
262Serving POST /inference/chat_completion
263Serving POST /inference/completion
264Serving POST /safety/run_shields
265Serving POST /agentic_system/memory_bank/attach
266Serving POST /agentic_system/create
267Serving POST /agentic_system/session/create
268Serving POST /agentic_system/turn/create
269Serving POST /agentic_system/delete
270Serving POST /agentic_system/session/delete
271Serving POST /agentic_system/memory_bank/detach
272Serving POST /agentic_system/session/get
273Serving POST /agentic_system/step/get
274Serving POST /agentic_system/turn/get
275Listening on :::5000
276INFO: Started server process [453333]
277INFO: Waiting for application startup.
278INFO: Application startup complete.
279INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
280```
281
282> [!NOTE]
283> Configuration is in `~/.llama/builds/local/conda/8b-instruct.yaml`. Feel free to increase `max_seq_len`.
284
285> [!IMPORTANT]
286> The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines.
287
288> [!TIP]
289> You might need to use the flag `--disable-ipv6` to Disable IPv6 support
290
291This server is running a Llama model locally.
292
293## Step 4. Test with Client
294Once the server is setup, we can test it with a client to see the example outputs.
295```
296cd /path/to/llama-stack
297conda activate <env> # any environment containing the llama-toolchain pip package will work
298
299python -m llama_toolchain.inference.client localhost 5000
300```
301
302This will run the chat completion client and query the distribution’s /inference/chat_completion API.
303
304Here is an example output:
305```
306Initializing client for http://localhost:5000
307User>hello world, troll me in two-paragraphs about 42
308
309Assistant> 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.
310
311You 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!
312```
313
314Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by:
315
316```
317python -m llama_toolchain.safety.client localhost 5000
318```
319
320You 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.