Elffuss BraidHub

kikocis/llama-stack public

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