| 1 | # llama-stack |
| 2 | |
| 3 | [](https://pypi.org/project/llama-stack/) |
| 4 | [](https://discord.gg/TZAAYNVtrU) |
| 5 | |
| 6 | This repository contains the specifications and implementations of the APIs which are part of the Llama Stack. |
| 7 | |
| 8 | The Llama Stack defines and standardizes the building blocks needed to bring generative AI applications to market. These blocks span the entire development lifecycle: from model training and fine-tuning, through product evaluation, to invoking AI agents in production. Beyond definition, we're developing open-source versions and partnering with cloud providers, ensuring developers can assemble AI solutions using consistent, interlocking pieces across platforms. The ultimate goal is to accelerate innovation in the AI space. |
| 9 | |
| 10 | The Stack APIs are rapidly improving, but still very much work in progress and we invite feedback as well as direct contributions. |
| 11 | |
| 12 | |
| 13 | ## APIs |
| 14 | |
| 15 | The Llama Stack consists of the following set of APIs: |
| 16 | |
| 17 | - Inference |
| 18 | - Safety |
| 19 | - Memory |
| 20 | - Agentic System |
| 21 | - Evaluation |
| 22 | - Post Training |
| 23 | - Synthetic Data Generation |
| 24 | - Reward Scoring |
| 25 | |
| 26 | Each of the APIs themselves is a collection of REST endpoints. |
| 27 | |
| 28 | ## API Providers |
| 29 | |
| 30 | A Provider is what makes the API real -- they provide the actual implementation backing the API. |
| 31 | |
| 32 | As an example, for Inference, we could have the implementation be backed by open source libraries like `[ torch | vLLM | TensorRT ]` as possible options. |
| 33 | |
| 34 | A provider can also be just a pointer to a remote REST service -- for example, cloud providers or dedicated inference providers could serve these APIs. |
| 35 | |
| 36 | |
| 37 | ## Llama Stack Distribution |
| 38 | |
| 39 | A Distribution is where APIs and Providers are assembled together to provide a consistent whole to the end application developer. You can mix-and-match providers -- some could be backed by local code and some could be remote. As a hobbyist, you can serve a small model locally, but can choose a cloud provider for a large model. Regardless, the higher level APIs your app needs to work with don't need to change at all. You can even imagine moving across the server / mobile-device boundary as well always using the same uniform set of APIs for developing Generative AI applications. |
| 40 | |
| 41 | |
| 42 | ## Installation |
| 43 | |
| 44 | You can install this repository as a [package](https://pypi.org/project/llama-stack/) with `pip install llama-stack` |
| 45 | |
| 46 | If you want to install from source: |
| 47 | |
| 48 | ```bash |
| 49 | mkdir -p ~/local |
| 50 | cd ~/local |
| 51 | git clone git@github.com:meta-llama/llama-stack.git |
| 52 | |
| 53 | conda create -n stack python=3.10 |
| 54 | conda activate stack |
| 55 | |
| 56 | cd llama-stack |
| 57 | $CONDA_PREFIX/bin/pip install -e . |
| 58 | ``` |
| 59 | |
| 60 | # Getting Started |
| 61 | |
| 62 | The `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. |
| 63 | |
| 64 | This guides allows you to quickly get started with building and running a Llama Stack server in < 5 minutes! |
| 65 | |
| 66 | You may also checkout this [notebook](https://github.com/meta-llama/llama-stack/blob/main/docs/getting_started.ipynb) for trying out out demo scripts. |
| 67 | |
| 68 | ## Quick Cheatsheet |
| 69 | - 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. |
| 70 | |
| 71 | **`llama stack build`** |
| 72 | - You'll be prompted to enter build information interactively. |
| 73 | ``` |
| 74 | llama stack build |
| 75 | |
| 76 | > Enter an unique name for identifying your Llama Stack build distribution (e.g. my-local-stack): my-local-stack |
| 77 | > Enter the image type you want your distribution to be built with (docker or conda): conda |
| 78 | |
| 79 | Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs. |
| 80 | > Enter the API provider for the inference API: (default=meta-reference): meta-reference |
| 81 | > Enter the API provider for the safety API: (default=meta-reference): meta-reference |
| 82 | > Enter the API provider for the agents API: (default=meta-reference): meta-reference |
| 83 | > Enter the API provider for the memory API: (default=meta-reference): meta-reference |
| 84 | > Enter the API provider for the telemetry API: (default=meta-reference): meta-reference |
| 85 | |
| 86 | > (Optional) Enter a short description for your Llama Stack distribution: |
| 87 | |
| 88 | Build spec configuration saved at ~/.conda/envs/llamastack-my-local-stack/my-local-stack-build.yaml |
| 89 | You can now run `llama stack configure my-local-stack` |
| 90 | ``` |
| 91 | |
| 92 | **`llama stack configure`** |
| 93 | - Run `llama stack configure <name>` with the name you have previously defined in `build` step. |
| 94 | ``` |
| 95 | llama stack configure <name> |
| 96 | ``` |
| 97 | - You will be prompted to enter configurations for your Llama Stack |
| 98 | |
| 99 | ``` |
| 100 | $ llama stack configure my-local-stack |
| 101 | |
| 102 | Could not find my-local-stack. Trying conda build name instead... |
| 103 | Configuring API `inference`... |
| 104 | === Configuring provider `meta-reference` for API inference... |
| 105 | Enter value for model (default: Llama3.1-8B-Instruct) (required): |
| 106 | Do you want to configure quantization? (y/n): n |
| 107 | Enter value for torch_seed (optional): |
| 108 | Enter value for max_seq_len (default: 4096) (required): |
| 109 | Enter value for max_batch_size (default: 1) (required): |
| 110 | |
| 111 | Configuring API `safety`... |
| 112 | === Configuring provider `meta-reference` for API safety... |
| 113 | Do you want to configure llama_guard_shield? (y/n): n |
| 114 | Do you want to configure prompt_guard_shield? (y/n): n |
| 115 | |
| 116 | Configuring API `agents`... |
| 117 | === Configuring provider `meta-reference` for API agents... |
| 118 | Enter `type` for persistence_store (options: redis, sqlite, postgres) (default: sqlite): |
| 119 | |
| 120 | Configuring SqliteKVStoreConfig: |
| 121 | Enter value for namespace (optional): |
| 122 | Enter value for db_path (default: /home/xiyan/.llama/runtime/kvstore.db) (required): |
| 123 | |
| 124 | Configuring API `memory`... |
| 125 | === Configuring provider `meta-reference` for API memory... |
| 126 | > Please enter the supported memory bank type your provider has for memory: vector |
| 127 | |
| 128 | Configuring API `telemetry`... |
| 129 | === Configuring provider `meta-reference` for API telemetry... |
| 130 | |
| 131 | > YAML configuration has been written to ~/.llama/builds/conda/my-local-stack-run.yaml. |
| 132 | You can now run `llama stack run my-local-stack --port PORT` |
| 133 | ``` |
| 134 | |
| 135 | **`llama stack run`** |
| 136 | - Run `llama stack run <name>` with the name you have previously defined. |
| 137 | ``` |
| 138 | llama stack run my-local-stack |
| 139 | |
| 140 | ... |
| 141 | > initializing model parallel with size 1 |
| 142 | > initializing ddp with size 1 |
| 143 | > initializing pipeline with size 1 |
| 144 | ... |
| 145 | Finished model load YES READY |
| 146 | Serving POST /inference/chat_completion |
| 147 | Serving POST /inference/completion |
| 148 | Serving POST /inference/embeddings |
| 149 | Serving POST /memory_banks/create |
| 150 | Serving DELETE /memory_bank/documents/delete |
| 151 | Serving DELETE /memory_banks/drop |
| 152 | Serving GET /memory_bank/documents/get |
| 153 | Serving GET /memory_banks/get |
| 154 | Serving POST /memory_bank/insert |
| 155 | Serving GET /memory_banks/list |
| 156 | Serving POST /memory_bank/query |
| 157 | Serving POST /memory_bank/update |
| 158 | Serving POST /safety/run_shield |
| 159 | Serving POST /agentic_system/create |
| 160 | Serving POST /agentic_system/session/create |
| 161 | Serving POST /agentic_system/turn/create |
| 162 | Serving POST /agentic_system/delete |
| 163 | Serving POST /agentic_system/session/delete |
| 164 | Serving POST /agentic_system/session/get |
| 165 | Serving POST /agentic_system/step/get |
| 166 | Serving POST /agentic_system/turn/get |
| 167 | Serving GET /telemetry/get_trace |
| 168 | Serving POST /telemetry/log_event |
| 169 | Listening on :::5000 |
| 170 | INFO: Started server process [587053] |
| 171 | INFO: Waiting for application startup. |
| 172 | INFO: Application startup complete. |
| 173 | INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) |
| 174 | ``` |
| 175 | |
| 176 | |
| 177 | ## Step 1. Build |
| 178 | In 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: |
| 179 | - `name`: the name for our distribution (e.g. `8b-instruct`) |
| 180 | - `image_type`: our build image type (`conda | docker`) |
| 181 | - `distribution_spec`: our distribution specs for specifying API providers |
| 182 | - `description`: a short description of the configurations for the distribution |
| 183 | - `providers`: specifies the underlying implementation for serving each API endpoint |
| 184 | - `image_type`: `conda` | `docker` to specify whether to build the distribution in the form of Docker image or Conda environment. |
| 185 | |
| 186 | |
| 187 | At the end of build command, we will generate `<name>-build.yaml` file storing the build configurations. |
| 188 | |
| 189 | After 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. |
| 190 | |
| 191 | #### Building from scratch |
| 192 | - 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. |
| 193 | ``` |
| 194 | llama stack build |
| 195 | ``` |
| 196 | |
| 197 | Running the command above will allow you to fill in the configuration to build your Llama Stack distribution, you will see the following outputs. |
| 198 | |
| 199 | ``` |
| 200 | > Enter an unique name for identifying your Llama Stack build distribution (e.g. my-local-stack): 8b-instruct |
| 201 | > Enter the image type you want your distribution to be built with (docker or conda): conda |
| 202 | |
| 203 | Llama Stack is composed of several APIs working together. Let's configure the providers (implementations) you want to use for these APIs. |
| 204 | > Enter the API provider for the inference API: (default=meta-reference): meta-reference |
| 205 | > Enter the API provider for the safety API: (default=meta-reference): meta-reference |
| 206 | > Enter the API provider for the agents API: (default=meta-reference): meta-reference |
| 207 | > Enter the API provider for the memory API: (default=meta-reference): meta-reference |
| 208 | > Enter the API provider for the telemetry API: (default=meta-reference): meta-reference |
| 209 | |
| 210 | > (Optional) Enter a short description for your Llama Stack distribution: |
| 211 | |
| 212 | Build spec configuration saved at ~/.conda/envs/llamastack-my-local-llama-stack/8b-instruct-build.yaml |
| 213 | ``` |
| 214 | |
| 215 | **Ollama (optional)** |
| 216 | |
| 217 | If you plan to use Ollama for inference, you'll need to install the server [via these instructions](https://ollama.com/download). |
| 218 | |
| 219 | |
| 220 | #### Building from templates |
| 221 | - To build from alternative API providers, we provide distribution templates for users to get started building a distribution backed by different providers. |
| 222 | |
| 223 | The following command will allow you to see the available templates and their corresponding providers. |
| 224 | ``` |
| 225 | llama stack build --list-templates |
| 226 | ``` |
| 227 | |
| 228 |  |
| 229 | |
| 230 | You may then pick a template to build your distribution with providers fitted to your liking. |
| 231 | |
| 232 | ``` |
| 233 | llama stack build --template local-tgi --name my-tgi-stack |
| 234 | ``` |
| 235 | |
| 236 | ``` |
| 237 | $ llama stack build --template local-tgi --name my-tgi-stack |
| 238 | ... |
| 239 | ... |
| 240 | Build spec configuration saved at ~/.conda/envs/llamastack-my-tgi-stack/my-tgi-stack-build.yaml |
| 241 | You may now run `llama stack configure my-tgi-stack` or `llama stack configure ~/.conda/envs/llamastack-my-tgi-stack/my-tgi-stack-build.yaml` |
| 242 | ``` |
| 243 | |
| 244 | #### Building from config file |
| 245 | - 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. |
| 246 | |
| 247 | - The config file will be of contents like the ones in `llama_stack/distributions/templates/`. |
| 248 | |
| 249 | ``` |
| 250 | $ cat llama_stack/distribution/templates/local-ollama-build.yaml |
| 251 | |
| 252 | name: local-ollama |
| 253 | distribution_spec: |
| 254 | description: Like local, but use ollama for running LLM inference |
| 255 | providers: |
| 256 | inference: remote::ollama |
| 257 | memory: meta-reference |
| 258 | safety: meta-reference |
| 259 | agents: meta-reference |
| 260 | telemetry: meta-reference |
| 261 | image_type: conda |
| 262 | ``` |
| 263 | |
| 264 | ``` |
| 265 | llama stack build --config llama_stack/distribution/templates/local-ollama-build.yaml |
| 266 | ``` |
| 267 | |
| 268 | #### How to build distribution with Docker image |
| 269 | |
| 270 | To 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. |
| 271 | |
| 272 | ``` |
| 273 | llama stack build --template local --image-type docker --name docker-0 |
| 274 | ``` |
| 275 | |
| 276 | Alternatively, 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: |
| 277 | |
| 278 | ``` |
| 279 | name: local-docker-example |
| 280 | distribution_spec: |
| 281 | description: Use code from `llama_stack` itself to serve all llama stack APIs |
| 282 | docker_image: null |
| 283 | providers: |
| 284 | inference: meta-reference |
| 285 | memory: meta-reference-faiss |
| 286 | safety: meta-reference |
| 287 | agentic_system: meta-reference |
| 288 | telemetry: console |
| 289 | image_type: docker |
| 290 | ``` |
| 291 | |
| 292 | The following command allows you to build a Docker image with the name `<name>` |
| 293 | ``` |
| 294 | llama stack build --config <name>-build.yaml |
| 295 | |
| 296 | Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim |
| 297 | WORKDIR /app |
| 298 | ... |
| 299 | ... |
| 300 | You can run it with: podman run -p 8000:8000 llamastack-docker-local |
| 301 | Build spec configuration saved at ~/.llama/distributions/docker/docker-local-build.yaml |
| 302 | ``` |
| 303 | |
| 304 | |
| 305 | ## Step 2. Configure |
| 306 | After our distribution is built (either in form of docker or conda environment), we will run the following command to |
| 307 | ``` |
| 308 | llama stack configure [ <name> | <docker-image-name> | <path/to/name.build.yaml>] |
| 309 | ``` |
| 310 | - For `conda` environments: <path/to/name.build.yaml> would be the generated build spec saved from Step 1. |
| 311 | - For `docker` images downloaded from Dockerhub, you could also use <docker-image-name> as the argument. |
| 312 | - Run `docker images` to check list of available images on your machine. |
| 313 | |
| 314 | ``` |
| 315 | $ llama stack configure 8b-instruct |
| 316 | |
| 317 | Configuring API: inference (meta-reference) |
| 318 | Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required): |
| 319 | Enter value for quantization (optional): |
| 320 | Enter value for torch_seed (optional): |
| 321 | Enter value for max_seq_len (existing: 4096) (required): |
| 322 | Enter value for max_batch_size (existing: 1) (required): |
| 323 | |
| 324 | Configuring API: memory (meta-reference-faiss) |
| 325 | |
| 326 | Configuring API: safety (meta-reference) |
| 327 | Do you want to configure llama_guard_shield? (y/n): y |
| 328 | Entering sub-configuration for llama_guard_shield: |
| 329 | Enter value for model (default: Llama-Guard-3-8B) (required): |
| 330 | Enter value for excluded_categories (default: []) (required): |
| 331 | Enter value for disable_input_check (default: False) (required): |
| 332 | Enter value for disable_output_check (default: False) (required): |
| 333 | Do you want to configure prompt_guard_shield? (y/n): y |
| 334 | Entering sub-configuration for prompt_guard_shield: |
| 335 | Enter value for model (default: Prompt-Guard-86M) (required): |
| 336 | |
| 337 | Configuring API: agentic_system (meta-reference) |
| 338 | Enter value for brave_search_api_key (optional): |
| 339 | Enter value for bing_search_api_key (optional): |
| 340 | Enter value for wolfram_api_key (optional): |
| 341 | |
| 342 | Configuring API: telemetry (console) |
| 343 | |
| 344 | YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml |
| 345 | ``` |
| 346 | |
| 347 | After 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. |
| 348 | |
| 349 | As you can see, we did basic configuration above and configured: |
| 350 | - inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`) |
| 351 | - Llama Guard safety shield with model `Llama-Guard-3-8B` |
| 352 | - Prompt Guard safety shield with model `Prompt-Guard-86M` |
| 353 | |
| 354 | For how these configurations are stored as yaml, checkout the file printed at the end of the configuration. |
| 355 | |
| 356 | Note that all configurations as well as models are stored in `~/.llama` |
| 357 | |
| 358 | |
| 359 | ## Step 3. Run |
| 360 | Now, 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. |
| 361 | |
| 362 | ``` |
| 363 | llama stack run 8b-instruct |
| 364 | ``` |
| 365 | |
| 366 | You should see the Llama Stack server start and print the APIs that it is supporting |
| 367 | |
| 368 | ``` |
| 369 | $ llama stack run 8b-instruct |
| 370 | |
| 371 | > initializing model parallel with size 1 |
| 372 | > initializing ddp with size 1 |
| 373 | > initializing pipeline with size 1 |
| 374 | Loaded in 19.28 seconds |
| 375 | NCCL version 2.20.5+cuda12.4 |
| 376 | Finished model load YES READY |
| 377 | Serving POST /inference/batch_chat_completion |
| 378 | Serving POST /inference/batch_completion |
| 379 | Serving POST /inference/chat_completion |
| 380 | Serving POST /inference/completion |
| 381 | Serving POST /safety/run_shield |
| 382 | Serving POST /agentic_system/memory_bank/attach |
| 383 | Serving POST /agentic_system/create |
| 384 | Serving POST /agentic_system/session/create |
| 385 | Serving POST /agentic_system/turn/create |
| 386 | Serving POST /agentic_system/delete |
| 387 | Serving POST /agentic_system/session/delete |
| 388 | Serving POST /agentic_system/memory_bank/detach |
| 389 | Serving POST /agentic_system/session/get |
| 390 | Serving POST /agentic_system/step/get |
| 391 | Serving POST /agentic_system/turn/get |
| 392 | Listening on :::5000 |
| 393 | INFO: Started server process [453333] |
| 394 | INFO: Waiting for application startup. |
| 395 | INFO: Application startup complete. |
| 396 | INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) |
| 397 | ``` |
| 398 | |
| 399 | > [!NOTE] |
| 400 | > Configuration is in `~/.llama/builds/local/conda/8b-instruct-run.yaml`. Feel free to increase `max_seq_len`. |
| 401 | |
| 402 | > [!IMPORTANT] |
| 403 | > The "local" distribution inference server currently only supports CUDA. It will not work on Apple Silicon machines. |
| 404 | |
| 405 | > [!TIP] |
| 406 | > You might need to use the flag `--disable-ipv6` to Disable IPv6 support |
| 407 | |
| 408 | This server is running a Llama model locally. |
| 409 | |
| 410 | ## Step 4. Test with Client |
| 411 | Once the server is setup, we can test it with a client to see the example outputs. |
| 412 | ``` |
| 413 | cd /path/to/llama-stack |
| 414 | conda activate <env> # any environment containing the llama-stack pip package will work |
| 415 | |
| 416 | python -m llama_stack.apis.inference.client localhost 5000 |
| 417 | ``` |
| 418 | |
| 419 | This will run the chat completion client and query the distribution’s /inference/chat_completion API. |
| 420 | |
| 421 | Here is an example output: |
| 422 | ``` |
| 423 | User>hello world, write me a 2 sentence poem about the moon |
| 424 | Assistant> Here's a 2-sentence poem about the moon: |
| 425 | |
| 426 | The moon glows softly in the midnight sky, |
| 427 | A beacon of wonder, as it passes by. |
| 428 | ``` |
| 429 | |
| 430 | Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by: |
| 431 | |
| 432 | ``` |
| 433 | python -m llama_stack.apis.safety.client localhost 5000 |
| 434 | ``` |
| 435 | |
| 436 | You 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/examples) repo. |