kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | f788015f94124a53fe5eaa17bec2a05eb0ecaf99e1b24bf14816ddfa02d9e3ec |
|---|---|
| Author | Xi Yan <xiyan@meta.com> · 2024-09-16 11:02 -07:00 |
| Committer | GitHub <noreply@github.com> · 2024-09-16 11:02 -07:00 |
| Parents | 147493617926 |
27 files changed
modified docs/cli_reference.md +132 −130
| @@ -236,151 +236,156 @@ | ||
| 236 | 236 | **NOTE**: Outputs in terminal are color printed to show special tokens. |
| 237 | 237 | |
| 238 | 238 | |
| 239 | -## Step 3: Listing, Building, and Configuring Llama Stack Distributions | |
| 240 | - | |
| 241 | - | |
| 242 | -### Step 3.1: List available distributions | |
| 243 | - | |
| 244 | -Let’s start with listing available distributions: | |
| 245 | - | |
| 246 | -``` | |
| 247 | -llama stack list-distributions | |
| 248 | -``` | |
| 249 | - | |
| 250 | -<pre style="font-family: monospace;"> | |
| 251 | -i+-------------------------------+---------------------------------------+----------------------------------------------------------------------+ | |
| 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 | - | |
| 298 | -As 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 | - | |
| 302 | -Let'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 | -``` | |
| 305 | -llama stack build | |
| 306 | -``` | |
| 307 | - | |
| 308 | -Once 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 | |
| 312 | -Enter value for name (required): 8b-instruct | |
| 313 | -Enter value for distribution (default: local) (required): local | |
| 314 | -Enter value for api_providers (optional): | |
| 315 | -Enter value for image_type (default: conda) (required): | |
| 316 | - | |
| 317 | -.... | |
| 318 | -.... | |
| 319 | -Successfully 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 | - | |
| 321 | -Successfully setup conda environment. Configuring build... | |
| 239 | +## Step 3: Building, and Configuring Llama Stack Distributions | |
| 322 | 240 | |
| 323 | -... | |
| 324 | -... | |
| 241 | +- Please see our [Getting Started](getting_started.md) guide for details. | |
| 325 | 242 | |
| 326 | -YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 327 | -Target `8b-test` built with configuration at /home/xiyan/.llama/builds/local/conda/8b-test.yaml | |
| 328 | -Build spec configuration saved at /home/xiyan/.llama/distributions/local/conda/8b-test-build.yaml | |
| 243 | +### Step 3.1. Build | |
| 244 | +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: | |
| 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 | |
| 253 | +The following command and specifications allows you to get started with building. | |
| 329 | 254 | ``` |
| 255 | +llama 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. | |
| 330 | 258 | |
| 331 | -You can re-build package based on build config | |
| 259 | +The file will be of the contents | |
| 332 | 260 | ``` |
| 333 | -$ cat ~/.llama/distributions/local/conda/8b-instruct-build.yaml | |
| 261 | +$ cat ./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml | |
| 262 | + | |
| 334 | 263 | name: 8b-instruct |
| 335 | -distribution: local | |
| 336 | -api_providers: null | |
| 264 | +distribution_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 | |
| 337 | 274 | image_type: conda |
| 275 | +``` | |
| 338 | 276 | |
| 339 | -$ llama stack build --config ~/.llama/distributions/local/conda/8b-instruct-build.yaml | |
| 340 | - | |
| 341 | -Successfully setup conda environment. Configuring build... | |
| 342 | - | |
| 277 | +You 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 | |
| 343 | 280 | ... |
| 344 | 281 | ... |
| 282 | +Build spec configuration saved at ~/.llama/distributions/conda/8b-instruct-build.yaml | |
| 283 | +``` | |
| 284 | + | |
| 285 | +After 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 | |
| 289 | +To 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 | + | |
| 294 | +name: local-tgi-conda-example | |
| 295 | +distribution_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 | |
| 304 | +image_type: conda | |
| 305 | +``` | |
| 306 | + | |
| 307 | +The following command allows you to build a distribution with TGI as the inference API provider, with the name `tgi`. | |
| 308 | +``` | |
| 309 | +llama stack build --config ./llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml --name tgi | |
| 310 | +``` | |
| 311 | + | |
| 312 | +We 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 | |
| 315 | +To 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`. | |
| 345 | 316 | |
| 346 | -YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 347 | -Target `8b-instruct` built with configuration at ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 348 | -Build spec configuration saved at ~/.llama/distributions/local/conda/8b-instruct-build.yaml | |
| 349 | 317 | ``` |
| 318 | +$ cat ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml | |
| 350 | 319 | |
| 351 | -### Step 3.3: Configure a distribution | |
| 320 | +name: local-docker-example | |
| 321 | +distribution_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 | |
| 330 | +image_type: docker | |
| 331 | +``` | |
| 352 | 332 | |
| 353 | -You can re-configure this distribution by running: | |
| 333 | +The following command allows you to build a Docker image with the name `docker-local` | |
| 354 | 334 | ``` |
| 355 | -llama stack configure ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 335 | +llama stack build --config ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml --name docker-local | |
| 336 | + | |
| 337 | +Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim | |
| 338 | +WORKDIR /app | |
| 339 | +... | |
| 340 | +... | |
| 341 | +You can run it with: podman run -p 8000:8000 llamastack-docker-local | |
| 342 | +Build spec configuration saved at /home/xiyan/.llama/distributions/docker/docker-local-build.yaml | |
| 356 | 343 | ``` |
| 357 | 344 | |
| 358 | -Here is an example run of how the CLI will guide you to fill the configuration | |
| 345 | +### Step 3.2. Configure | |
| 346 | +After our distribution is built (either in form of docker or conda environment), we will run the following command to | |
| 347 | +``` | |
| 348 | +llama 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. | |
| 359 | 353 | |
| 360 | 354 | ``` |
| 361 | -$ llama stack configure ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 355 | +$ llama stack configure ~/.llama/distributions/conda/8b-instruct-build.yaml | |
| 362 | 356 | |
| 363 | 357 | Configuring API: inference (meta-reference) |
| 364 | -Enter value for model (required): Meta-Llama3.1-8B-Instruct | |
| 358 | +Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required): | |
| 365 | 359 | Enter value for quantization (optional): |
| 366 | 360 | Enter value for torch_seed (optional): |
| 367 | -Enter value for max_seq_len (required): 4096 | |
| 368 | -Enter value for max_batch_size (default: 1): 1 | |
| 361 | +Enter value for max_seq_len (existing: 4096) (required): | |
| 362 | +Enter value for max_batch_size (existing: 1) (required): | |
| 363 | + | |
| 364 | +Configuring API: memory (meta-reference-faiss) | |
| 365 | + | |
| 369 | 366 | Configuring API: safety (meta-reference) |
| 370 | 367 | Do you want to configure llama_guard_shield? (y/n): y |
| 371 | 368 | Entering sub-configuration for llama_guard_shield: |
| 372 | -Enter value for model (required): Llama-Guard-3-8B | |
| 373 | -Enter value for excluded_categories (required): [] | |
| 374 | -Enter value for disable_input_check (default: False): | |
| 375 | -Enter value for disable_output_check (default: False): | |
| 369 | +Enter value for model (default: Llama-Guard-3-8B) (required): | |
| 370 | +Enter value for excluded_categories (default: []) (required): | |
| 371 | +Enter value for disable_input_check (default: False) (required): | |
| 372 | +Enter value for disable_output_check (default: False) (required): | |
| 376 | 373 | Do you want to configure prompt_guard_shield? (y/n): y |
| 377 | 374 | Entering sub-configuration for prompt_guard_shield: |
| 378 | -Enter value for model (required): Prompt-Guard-86M | |
| 379 | -... | |
| 380 | -... | |
| 381 | -YAML configuration has been written to ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 375 | +Enter value for model (default: Prompt-Guard-86M) (required): | |
| 376 | + | |
| 377 | +Configuring API: agentic_system (meta-reference) | |
| 378 | +Enter value for brave_search_api_key (optional): | |
| 379 | +Enter value for bing_search_api_key (optional): | |
| 380 | +Enter value for wolfram_api_key (optional): | |
| 381 | + | |
| 382 | +Configuring API: telemetry (console) | |
| 383 | + | |
| 384 | +YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml | |
| 382 | 385 | ``` |
| 383 | 386 | |
| 387 | +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. | |
| 388 | + | |
| 384 | 389 | As you can see, we did basic configuration above and configured: |
| 385 | 390 | - inference to run on model `Meta-Llama3.1-8B-Instruct` (obtained from `llama model list`) |
| 386 | 391 | - Llama Guard safety shield with model `Llama-Guard-3-8B` |
| @@ -390,21 +395,18 @@ | ||
| 390 | 395 | |
| 391 | 396 | Note that all configurations as well as models are stored in `~/.llama` |
| 392 | 397 | |
| 393 | -## Step 4: Starting a Llama Stack Distribution and Testing it | |
| 394 | - | |
| 395 | -### Step 4.1: Starting a distribution | |
| 396 | - | |
| 397 | -Now let’s start Llama Stack Distribution Server. | |
| 398 | 398 | |
| 399 | -You need the YAML configuration file which was written out at the end by the `llama stack build` step. | |
| 399 | +### Step 3.3. Run | |
| 400 | +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. | |
| 400 | 401 | |
| 401 | 402 | ``` |
| 402 | -llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml --port 5000 | |
| 403 | +llama stack run ~/.llama/builds/conda/8b-instruct-run.yaml | |
| 403 | 404 | ``` |
| 404 | -You should see the Stack server start and print the APIs that it is supporting, | |
| 405 | + | |
| 406 | +You should see the Llama Stack server start and print the APIs that it is supporting | |
| 405 | 407 | |
| 406 | 408 | ``` |
| 407 | -$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml --port 5000 | |
| 409 | +$ llama stack run ~/.llama/builds/local/conda/8b-instruct.yaml | |
| 408 | 410 | |
| 409 | 411 | > initializing model parallel with size 1 |
| 410 | 412 | > initializing ddp with size 1 |
| @@ -434,7 +436,6 @@ | ||
| 434 | 436 | INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) |
| 435 | 437 | ``` |
| 436 | 438 | |
| 437 | - | |
| 438 | 439 | > [!NOTE] |
| 439 | 440 | > Configuration is in `~/.llama/builds/local/conda/8b-instruct.yaml`. Feel free to increase `max_seq_len`. |
| 440 | 441 | |
| @@ -443,9 +444,8 @@ | ||
| 443 | 444 | |
| 444 | 445 | This server is running a Llama model locally. |
| 445 | 446 | |
| 446 | -### Step 4.2: Test the distribution | |
| 447 | - | |
| 448 | -Lets test with a client. | |
| 447 | +### Step 3.4 Test with Client | |
| 448 | +Once the server is setup, we can test it with a client to see the example outputs. | |
| 449 | 449 | ``` |
| 450 | 450 | cd /path/to/llama-stack |
| 451 | 451 | conda activate <env> # any environment containing the llama-toolchain pip package will work |
| @@ -456,17 +456,19 @@ | ||
| 456 | 456 | This will run the chat completion client and query the distribution’s /inference/chat_completion API. |
| 457 | 457 | |
| 458 | 458 | Here is an example output: |
| 459 | -<pre style="font-family: monospace;"> | |
| 459 | +``` | |
| 460 | 460 | Initializing client for http://localhost:5000 |
| 461 | 461 | User>hello world, troll me in two-paragraphs about 42 |
| 462 | 462 | |
| 463 | 463 | Assistant> 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 | 464 | |
| 465 | 465 | You 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> | |
| 466 | +``` | |
| 467 | 467 | |
| 468 | 468 | Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by: |
| 469 | 469 | |
| 470 | 470 | ``` |
| 471 | 471 | python -m llama_toolchain.safety.client localhost 5000 |
| 472 | 472 | ``` |
| 473 | + | |
| 474 | +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/sdk_examples) repo. |
added docs/getting_started.md +317 −0
| @@ -0,0 +1,317 @@ | ||
| 1 | +# Getting Started | |
| 2 | + | |
| 3 | +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-toolchain` package. | |
| 4 | + | |
| 5 | +This 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 | +``` | |
| 12 | +llama stack build --config ./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml --name my-local-llama-stack | |
| 13 | +... | |
| 14 | +... | |
| 15 | +Build spec configuration saved at ~/.llama/distributions/conda/my-local-llama-stack-build.yaml | |
| 16 | +``` | |
| 17 | + | |
| 18 | +**`llama stack configure`** | |
| 19 | +``` | |
| 20 | +llama stack configure ~/.llama/distributions/conda/my-local-llama-stack-build.yaml | |
| 21 | + | |
| 22 | +Configuring API: inference (meta-reference) | |
| 23 | +Enter value for model (default: Meta-Llama3.1-8B-Instruct) (required): | |
| 24 | +Enter value for quantization (optional): | |
| 25 | +Enter value for torch_seed (optional): | |
| 26 | +Enter value for max_seq_len (required): 4096 | |
| 27 | +Enter value for max_batch_size (default: 1) (required): | |
| 28 | + | |
| 29 | +Configuring API: memory (meta-reference-faiss) | |
| 30 | + | |
| 31 | +Configuring API: safety (meta-reference) | |
| 32 | +Do you want to configure llama_guard_shield? (y/n): n | |
| 33 | +Do you want to configure prompt_guard_shield? (y/n): n | |
| 34 | + | |
| 35 | +Configuring API: agentic_system (meta-reference) | |
| 36 | +Enter value for brave_search_api_key (optional): | |
| 37 | +Enter value for bing_search_api_key (optional): | |
| 38 | +Enter value for wolfram_api_key (optional): | |
| 39 | + | |
| 40 | +Configuring API: telemetry (console) | |
| 41 | + | |
| 42 | +YAML configuration has been written to ~/.llama/builds/conda/my-local-llama-stack-run.yaml | |
| 43 | +``` | |
| 44 | + | |
| 45 | +**`llama stack run`** | |
| 46 | +``` | |
| 47 | +llama 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 | +... | |
| 54 | +Finished model load YES READY | |
| 55 | +Serving POST /inference/chat_completion | |
| 56 | +Serving POST /inference/completion | |
| 57 | +Serving POST /inference/embeddings | |
| 58 | +Serving POST /memory_banks/create | |
| 59 | +Serving DELETE /memory_bank/documents/delete | |
| 60 | +Serving DELETE /memory_banks/drop | |
| 61 | +Serving GET /memory_bank/documents/get | |
| 62 | +Serving GET /memory_banks/get | |
| 63 | +Serving POST /memory_bank/insert | |
| 64 | +Serving GET /memory_banks/list | |
| 65 | +Serving POST /memory_bank/query | |
| 66 | +Serving POST /memory_bank/update | |
| 67 | +Serving POST /safety/run_shields | |
| 68 | +Serving POST /agentic_system/create | |
| 69 | +Serving POST /agentic_system/session/create | |
| 70 | +Serving POST /agentic_system/turn/create | |
| 71 | +Serving POST /agentic_system/delete | |
| 72 | +Serving POST /agentic_system/session/delete | |
| 73 | +Serving POST /agentic_system/session/get | |
| 74 | +Serving POST /agentic_system/step/get | |
| 75 | +Serving POST /agentic_system/turn/get | |
| 76 | +Serving GET /telemetry/get_trace | |
| 77 | +Serving POST /telemetry/log_event | |
| 78 | +Listening on :::5000 | |
| 79 | +INFO: Started server process [587053] | |
| 80 | +INFO: Waiting for application startup. | |
| 81 | +INFO: Application startup complete. | |
| 82 | +INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) | |
| 83 | +``` | |
| 84 | + | |
| 85 | + | |
| 86 | +## Step 1. Build | |
| 87 | +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: | |
| 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 | |
| 96 | +The following command and specifications allows you to get started with building. | |
| 97 | +``` | |
| 98 | +llama 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 | + | |
| 102 | +The file will be of the contents | |
| 103 | +``` | |
| 104 | +$ cat ./llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml | |
| 105 | + | |
| 106 | +name: 8b-instruct | |
| 107 | +distribution_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 | |
| 117 | +image_type: conda | |
| 118 | +``` | |
| 119 | + | |
| 120 | +You 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 | +... | |
| 125 | +Build spec configuration saved at ~/.llama/distributions/conda/8b-instruct-build.yaml | |
| 126 | +``` | |
| 127 | + | |
| 128 | +After 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 | |
| 132 | +To 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 | + | |
| 137 | +name: local-tgi-conda-example | |
| 138 | +distribution_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 | |
| 147 | +image_type: conda | |
| 148 | +``` | |
| 149 | + | |
| 150 | +The following command allows you to build a distribution with TGI as the inference API provider, with the name `tgi`. | |
| 151 | +``` | |
| 152 | +llama stack build --config ./llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml --name tgi | |
| 153 | +``` | |
| 154 | + | |
| 155 | +We 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 | |
| 158 | +To 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 | + | |
| 163 | +name: local-docker-example | |
| 164 | +distribution_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 | |
| 173 | +image_type: docker | |
| 174 | +``` | |
| 175 | + | |
| 176 | +The following command allows you to build a Docker image with the name `docker-local` | |
| 177 | +``` | |
| 178 | +llama stack build --config ./llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml --name docker-local | |
| 179 | + | |
| 180 | +Dockerfile created successfully in /tmp/tmp.I0ifS2c46A/DockerfileFROM python:3.10-slim | |
| 181 | +WORKDIR /app | |
| 182 | +... | |
| 183 | +... | |
| 184 | +You can run it with: podman run -p 8000:8000 llamastack-docker-local | |
| 185 | +Build spec configuration saved at /home/xiyan/.llama/distributions/docker/docker-local-build.yaml | |
| 186 | +``` | |
| 187 | + | |
| 188 | +## Step 2. Configure | |
| 189 | +After our distribution is built (either in form of docker or conda environment), we will run the following command to | |
| 190 | +``` | |
| 191 | +llama 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 | + | |
| 200 | +Configuring API: inference (meta-reference) | |
| 201 | +Enter value for model (existing: Meta-Llama3.1-8B-Instruct) (required): | |
| 202 | +Enter value for quantization (optional): | |
| 203 | +Enter value for torch_seed (optional): | |
| 204 | +Enter value for max_seq_len (existing: 4096) (required): | |
| 205 | +Enter value for max_batch_size (existing: 1) (required): | |
| 206 | + | |
| 207 | +Configuring API: memory (meta-reference-faiss) | |
| 208 | + | |
| 209 | +Configuring API: safety (meta-reference) | |
| 210 | +Do you want to configure llama_guard_shield? (y/n): y | |
| 211 | +Entering sub-configuration for llama_guard_shield: | |
| 212 | +Enter value for model (default: Llama-Guard-3-8B) (required): | |
| 213 | +Enter value for excluded_categories (default: []) (required): | |
| 214 | +Enter value for disable_input_check (default: False) (required): | |
| 215 | +Enter value for disable_output_check (default: False) (required): | |
| 216 | +Do you want to configure prompt_guard_shield? (y/n): y | |
| 217 | +Entering sub-configuration for prompt_guard_shield: | |
| 218 | +Enter value for model (default: Prompt-Guard-86M) (required): | |
| 219 | + | |
| 220 | +Configuring API: agentic_system (meta-reference) | |
| 221 | +Enter value for brave_search_api_key (optional): | |
| 222 | +Enter value for bing_search_api_key (optional): | |
| 223 | +Enter value for wolfram_api_key (optional): | |
| 224 | + | |
| 225 | +Configuring API: telemetry (console) | |
| 226 | + | |
| 227 | +YAML configuration has been written to ~/.llama/builds/conda/8b-instruct-run.yaml | |
| 228 | +``` | |
| 229 | + | |
| 230 | +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. | |
| 231 | + | |
| 232 | +As 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 | + | |
| 237 | +For how these configurations are stored as yaml, checkout the file printed at the end of the configuration. | |
| 238 | + | |
| 239 | +Note that all configurations as well as models are stored in `~/.llama` | |
| 240 | + | |
| 241 | + | |
| 242 | +## Step 3. Run | |
| 243 | +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. | |
| 244 | + | |
| 245 | +``` | |
| 246 | +llama stack run ~/.llama/builds/conda/8b-instruct-run.yaml | |
| 247 | +``` | |
| 248 | + | |
| 249 | +You 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 | |
| 257 | +Loaded in 19.28 seconds | |
| 258 | +NCCL version 2.20.5+cuda12.4 | |
| 259 | +Finished model load YES READY | |
| 260 | +Serving POST /inference/batch_chat_completion | |
| 261 | +Serving POST /inference/batch_completion | |
| 262 | +Serving POST /inference/chat_completion | |
| 263 | +Serving POST /inference/completion | |
| 264 | +Serving POST /safety/run_shields | |
| 265 | +Serving POST /agentic_system/memory_bank/attach | |
| 266 | +Serving POST /agentic_system/create | |
| 267 | +Serving POST /agentic_system/session/create | |
| 268 | +Serving POST /agentic_system/turn/create | |
| 269 | +Serving POST /agentic_system/delete | |
| 270 | +Serving POST /agentic_system/session/delete | |
| 271 | +Serving POST /agentic_system/memory_bank/detach | |
| 272 | +Serving POST /agentic_system/session/get | |
| 273 | +Serving POST /agentic_system/step/get | |
| 274 | +Serving POST /agentic_system/turn/get | |
| 275 | +Listening on :::5000 | |
| 276 | +INFO: Started server process [453333] | |
| 277 | +INFO: Waiting for application startup. | |
| 278 | +INFO: Application startup complete. | |
| 279 | +INFO: 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 | +This server is running a Llama model locally. | |
| 289 | + | |
| 290 | +## Step 4. Test with Client | |
| 291 | +Once the server is setup, we can test it with a client to see the example outputs. | |
| 292 | +``` | |
| 293 | +cd /path/to/llama-stack | |
| 294 | +conda activate <env> # any environment containing the llama-toolchain pip package will work | |
| 295 | + | |
| 296 | +python -m llama_toolchain.inference.client localhost 5000 | |
| 297 | +``` | |
| 298 | + | |
| 299 | +This will run the chat completion client and query the distribution’s /inference/chat_completion API. | |
| 300 | + | |
| 301 | +Here is an example output: | |
| 302 | +``` | |
| 303 | +Initializing client for http://localhost:5000 | |
| 304 | +User>hello world, troll me in two-paragraphs about 42 | |
| 305 | + | |
| 306 | +Assistant> 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. | |
| 307 | + | |
| 308 | +You 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! | |
| 309 | +``` | |
| 310 | + | |
| 311 | +Similarly you can test safety (if you configured llama-guard and/or prompt-guard shields) by: | |
| 312 | + | |
| 313 | +``` | |
| 314 | +python -m llama_toolchain.safety.client localhost 5000 | |
| 315 | +``` | |
| 316 | + | |
| 317 | +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/sdk_examples) repo. |
modified llama_toolchain/cli/stack/build.py +31 −98
| @@ -8,31 +8,9 @@ | ||
| 8 | 8 | |
| 9 | 9 | from llama_toolchain.cli.subcommand import Subcommand |
| 10 | 10 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 11 | -import yaml | |
| 12 | - | |
| 13 | - | |
| 14 | -def parse_api_provider_tuples( | |
| 15 | - tuples: str, parser: argparse.ArgumentParser | |
| 16 | -) -> Dict[str, ProviderSpec]: | |
| 17 | - from llama_toolchain.core.distribution import api_providers | |
| 18 | - | |
| 19 | - all_providers = api_providers() | |
| 20 | - | |
| 21 | - deps = {} | |
| 22 | - for dep in tuples.split(","): | |
| 23 | - dep = dep.strip() | |
| 24 | - if not dep: | |
| 25 | - continue | |
| 26 | - api_str, provider = dep.split("=") | |
| 27 | - api = Api(api_str) | |
| 28 | - | |
| 29 | - provider = provider.strip() | |
| 30 | - if provider not in all_providers[api]: | |
| 31 | - parser.error(f"Provider `{provider}` is not available for API `{api}`") | |
| 32 | - return | |
| 33 | - deps[api] = all_providers[api][provider] | |
| 11 | +from pathlib import Path | |
| 34 | 12 | |
| 35 | - return deps | |
| 13 | +import yaml | |
| 36 | 14 | |
| 37 | 15 | |
| 38 | 16 | class StackBuild(Subcommand): |
| @@ -48,16 +26,16 @@ | ||
| 48 | 26 | self.parser.set_defaults(func=self._run_stack_build_command) |
| 49 | 27 | |
| 50 | 28 | def _add_arguments(self): |
| 51 | - from llama_toolchain.core.distribution_registry import ( | |
| 52 | - available_distribution_specs, | |
| 29 | + self.parser.add_argument( | |
| 30 | + "config", | |
| 31 | + type=str, | |
| 32 | + help="Path to a config file to use for the build. You may find example configs in llama_toolchain/configs/distributions", | |
| 53 | 33 | ) |
| 54 | - from llama_toolchain.core.package import ImageType | |
| 55 | 34 | |
| 56 | - allowed_ids = [d.distribution_type for d in available_distribution_specs()] | |
| 57 | 35 | self.parser.add_argument( |
| 58 | - "--config", | |
| 36 | + "--name", | |
| 59 | 37 | type=str, |
| 60 | - help="Path to a config file to use for the build", | |
| 38 | + help="Name of the llama stack build to override from template config", | |
| 61 | 39 | ) |
| 62 | 40 | |
| 63 | 41 | def _run_stack_build_command_from_build_config( |
| @@ -68,69 +46,19 @@ | ||
| 68 | 46 | |
| 69 | 47 | from llama_toolchain.common.config_dirs import DISTRIBS_BASE_DIR |
| 70 | 48 | from llama_toolchain.common.serialize import EnumEncoder |
| 71 | - from llama_toolchain.core.distribution_registry import resolve_distribution_spec | |
| 72 | 49 | from llama_toolchain.core.package import ApiInput, build_package, ImageType |
| 73 | 50 | from termcolor import cprint |
| 74 | 51 | |
| 75 | - api_inputs = [] | |
| 76 | - if build_config.distribution == "adhoc": | |
| 77 | - if not build_config.api_providers: | |
| 78 | - self.parser.error( | |
| 79 | - "You must specify API providers with (api=provider,...) for building an adhoc distribution" | |
| 80 | - ) | |
| 81 | - return | |
| 82 | - | |
| 83 | - parsed = parse_api_provider_tuples(build_config.api_providers, self.parser) | |
| 84 | - for api, provider_spec in parsed.items(): | |
| 85 | - for dep in provider_spec.api_dependencies: | |
| 86 | - if dep not in parsed: | |
| 87 | - self.parser.error( | |
| 88 | - f"API {api} needs dependency {dep} provided also" | |
| 89 | - ) | |
| 90 | - return | |
| 91 | - | |
| 92 | - api_inputs.append( | |
| 93 | - ApiInput( | |
| 94 | - api=api, | |
| 95 | - provider=provider_spec.provider_type, | |
| 96 | - ) | |
| 97 | - ) | |
| 98 | - docker_image = None | |
| 52 | + # save build.yaml spec for building same distribution again | |
| 53 | + if build_config.image_type == ImageType.docker.value: | |
| 54 | + # docker needs build file to be in the llama-stack repo dir to be able to copy over to the image | |
| 55 | + llama_toolchain_path = Path(os.path.relpath(__file__)).parent.parent.parent | |
| 56 | + build_dir = ( | |
| 57 | + llama_toolchain_path / "configs/distributions" / build_config.image_type | |
| 58 | + ) | |
| 99 | 59 | else: |
| 100 | - if build_config.api_providers: | |
| 101 | - self.parser.error( | |
| 102 | - "You cannot specify API providers for pre-registered distributions" | |
| 103 | - ) | |
| 104 | - return | |
| 105 | - | |
| 106 | - dist = resolve_distribution_spec(build_config.distribution) | |
| 107 | - if dist is None: | |
| 108 | - self.parser.error( | |
| 109 | - f"Could not find distribution {build_config.distribution}" | |
| 110 | - ) | |
| 111 | - return | |
| 112 | - | |
| 113 | - for api, provider_type in dist.providers.items(): | |
| 114 | - api_inputs.append( | |
| 115 | - ApiInput( | |
| 116 | - api=api, | |
| 117 | - provider=provider_type, | |
| 118 | - ) | |
| 119 | - ) | |
| 120 | - docker_image = dist.docker_image | |
| 60 | + build_dir = DISTRIBS_BASE_DIR / build_config.image_type | |
| 121 | 61 | |
| 122 | - build_package( | |
| 123 | - api_inputs, | |
| 124 | - image_type=ImageType(build_config.image_type), | |
| 125 | - name=build_config.name, | |
| 126 | - distribution_type=build_config.distribution, | |
| 127 | - docker_image=docker_image, | |
| 128 | - ) | |
| 129 | - | |
| 130 | - # save build.yaml spec for building same distribution again | |
| 131 | - build_dir = ( | |
| 132 | - DISTRIBS_BASE_DIR / build_config.distribution / build_config.image_type | |
| 133 | - ) | |
| 134 | 62 | os.makedirs(build_dir, exist_ok=True) |
| 135 | 63 | build_file_path = build_dir / f"{build_config.name}-build.yaml" |
| 136 | 64 | |
| @@ -138,6 +66,8 @@ | ||
| 138 | 66 | to_write = json.loads(json.dumps(build_config.dict(), cls=EnumEncoder)) |
| 139 | 67 | f.write(yaml.dump(to_write, sort_keys=False)) |
| 140 | 68 | |
| 69 | + build_package(build_config, build_file_path) | |
| 70 | + | |
| 141 | 71 | cprint( |
| 142 | 72 | f"Build spec configuration saved at {str(build_file_path)}", |
| 143 | 73 | color="green", |
| @@ -147,15 +77,18 @@ | ||
| 147 | 77 | from llama_toolchain.common.prompt_for_config import prompt_for_config |
| 148 | 78 | from llama_toolchain.core.dynamic import instantiate_class_type |
| 149 | 79 | |
| 150 | - if args.config: | |
| 151 | - with open(args.config, "r") as f: | |
| 152 | - try: | |
| 153 | - build_config = BuildConfig(**yaml.safe_load(f)) | |
| 154 | - except Exception as e: | |
| 155 | - self.parser.error(f"Could not parse config file {args.config}: {e}") | |
| 156 | - return | |
| 157 | - self._run_stack_build_command_from_build_config(build_config) | |
| 80 | + if not args.config: | |
| 81 | + self.parser.error( | |
| 82 | + "No config file specified. Please use `llama stack build /path/to/*-build.yaml`. Example config files can be found in llama_toolchain/configs/distributions" | |
| 83 | + ) | |
| 158 | 84 | return |
| 159 | 85 | |
| 160 | - build_config = prompt_for_config(BuildConfig, None) | |
| 161 | - self._run_stack_build_command_from_build_config(build_config) | |
| 86 | + with open(args.config, "r") as f: | |
| 87 | + try: | |
| 88 | + build_config = BuildConfig(**yaml.safe_load(f)) | |
| 89 | + except Exception as e: | |
| 90 | + self.parser.error(f"Could not parse config file {args.config}: {e}") | |
| 91 | + return | |
| 92 | + if args.name: | |
| 93 | + build_config.name = args.name | |
| 94 | + self._run_stack_build_command_from_build_config(build_config) |
modified llama_toolchain/cli/stack/configure.py +94 −30
| @@ -8,12 +8,18 @@ | ||
| 8 | 8 | import json |
| 9 | 9 | from pathlib import Path |
| 10 | 10 | |
| 11 | -import yaml | |
| 11 | +import pkg_resources | |
| 12 | 12 | |
| 13 | +import yaml | |
| 13 | 14 | from llama_toolchain.cli.subcommand import Subcommand |
| 14 | 15 | from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR |
| 16 | + | |
| 17 | +from llama_toolchain.common.exec import run_with_pty | |
| 15 | 18 | from termcolor import cprint |
| 16 | 19 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 20 | +import os | |
| 21 | + | |
| 22 | +from termcolor import cprint | |
| 17 | 23 | |
| 18 | 24 | |
| 19 | 25 | class StackConfigure(Subcommand): |
| @@ -31,49 +37,107 @@ | ||
| 31 | 37 | self.parser.set_defaults(func=self._run_stack_configure_cmd) |
| 32 | 38 | |
| 33 | 39 | def _add_arguments(self): |
| 34 | - from llama_toolchain.core.distribution_registry import ( | |
| 35 | - available_distribution_specs, | |
| 40 | + self.parser.add_argument( | |
| 41 | + "config", | |
| 42 | + type=str, | |
| 43 | + help="Path to the build config file (e.g. ~/.llama/builds/<image_type>/<name>-build.yaml). For docker, this could also be the name of the docker image. ", | |
| 36 | 44 | ) |
| 37 | - from llama_toolchain.core.package import ImageType | |
| 38 | 45 | |
| 39 | - allowed_ids = [d.distribution_type for d in available_distribution_specs()] | |
| 40 | 46 | self.parser.add_argument( |
| 41 | - "config", | |
| 47 | + "--output-dir", | |
| 42 | 48 | type=str, |
| 43 | - help="Path to the package config file (e.g. ~/.llama/builds/<distribution>/<image_type>/<name>.yaml)", | |
| 49 | + help="Path to the output directory to store generated run.yaml config file. If not specified, will use ~/.llama/build/<image_type>/<name>-run.yaml", | |
| 44 | 50 | ) |
| 45 | 51 | |
| 46 | 52 | def _run_stack_configure_cmd(self, args: argparse.Namespace) -> None: |
| 47 | 53 | from llama_toolchain.core.package import ImageType |
| 48 | 54 | |
| 49 | - config_file = Path(args.config) | |
| 50 | - if not config_file.exists(): | |
| 51 | - self.parser.error( | |
| 52 | - f"Could not find {config_file}. Please run `llama stack build` first" | |
| 55 | + docker_image = None | |
| 56 | + build_config_file = Path(args.config) | |
| 57 | + if not build_config_file.exists(): | |
| 58 | + cprint( | |
| 59 | + f"Could not find {build_config_file}. Trying docker image name instead...", | |
| 60 | + color="green", | |
| 61 | + ) | |
| 62 | + docker_image = args.config | |
| 63 | + | |
| 64 | + builds_dir = BUILDS_BASE_DIR / ImageType.docker.value | |
| 65 | + if args.output_dir: | |
| 66 | + builds_dir = Path(output_dir) | |
| 67 | + os.makedirs(builds_dir, exist_ok=True) | |
| 68 | + | |
| 69 | + script = pkg_resources.resource_filename( | |
| 70 | + "llama_toolchain", "core/configure_container.sh" | |
| 71 | + ) | |
| 72 | + script_args = [script, docker_image, str(builds_dir)] | |
| 73 | + | |
| 74 | + return_code = run_with_pty(script_args) | |
| 75 | + | |
| 76 | + # we have regenerated the build config file with script, now check if it exists | |
| 77 | + if return_code != 0: | |
| 78 | + self.parser.error( | |
| 79 | + f"Can not find {build_config_file}. Please run llama stack build first or check if docker image exists" | |
| 80 | + ) | |
| 81 | + | |
| 82 | + build_name = docker_image.removeprefix("llamastack-") | |
| 83 | + cprint( | |
| 84 | + f"YAML configuration has been written to {builds_dir / f'{build_name}-run.yaml'}", | |
| 85 | + color="green", | |
| 53 | 86 | ) |
| 54 | 87 | return |
| 55 | 88 | |
| 56 | - configure_llama_distribution(config_file) | |
| 57 | - | |
| 89 | + with open(build_config_file, "r") as f: | |
| 90 | + build_config = BuildConfig(**yaml.safe_load(f)) | |
| 91 | + | |
| 92 | + self._configure_llama_distribution(build_config, args.output_dir) | |
| 93 | + | |
| 94 | + def _configure_llama_distribution( | |
| 95 | + self, | |
| 96 | + build_config: BuildConfig, | |
| 97 | + output_dir: Optional[str] = None, | |
| 98 | + ): | |
| 99 | + from llama_toolchain.common.serialize import EnumEncoder | |
| 100 | + from llama_toolchain.core.configure import configure_api_providers | |
| 101 | + | |
| 102 | + builds_dir = BUILDS_BASE_DIR / build_config.image_type | |
| 103 | + if output_dir: | |
| 104 | + builds_dir = Path(output_dir) | |
| 105 | + os.makedirs(builds_dir, exist_ok=True) | |
| 106 | + package_name = build_config.name.replace("::", "-") | |
| 107 | + package_file = builds_dir / f"{package_name}-run.yaml" | |
| 108 | + | |
| 109 | + api2providers = build_config.distribution_spec.providers | |
| 110 | + | |
| 111 | + stub_config = { | |
| 112 | + api_str: {"provider_type": provider} | |
| 113 | + for api_str, provider in api2providers.items() | |
| 114 | + } | |
| 115 | + | |
| 116 | + if package_file.exists(): | |
| 117 | + cprint( | |
| 118 | + f"Configuration already exists for {build_config.name}. Will overwrite...", | |
| 119 | + "yellow", | |
| 120 | + attrs=["bold"], | |
| 121 | + ) | |
| 122 | + config = PackageConfig(**yaml.safe_load(package_file.read_text())) | |
| 123 | + else: | |
| 124 | + config = PackageConfig( | |
| 125 | + built_at=datetime.now(), | |
| 126 | + package_name=package_name, | |
| 127 | + providers=stub_config, | |
| 128 | + ) | |
| 58 | 129 | |
| 59 | -def configure_llama_distribution(config_file: Path) -> None: | |
| 60 | - from llama_toolchain.common.serialize import EnumEncoder | |
| 61 | - from llama_toolchain.core.configure import configure_api_providers | |
| 130 | + config.providers = configure_api_providers(config.providers) | |
| 131 | + config.docker_image = ( | |
| 132 | + package_name if build_config.image_type == "docker" else None | |
| 133 | + ) | |
| 134 | + config.conda_env = package_name if build_config.image_type == "conda" else None | |
| 62 | 135 | |
| 63 | - with open(config_file, "r") as f: | |
| 64 | - config = PackageConfig(**yaml.safe_load(f)) | |
| 136 | + with open(package_file, "w") as f: | |
| 137 | + to_write = json.loads(json.dumps(config.dict(), cls=EnumEncoder)) | |
| 138 | + f.write(yaml.dump(to_write, sort_keys=False)) | |
| 65 | 139 | |
| 66 | - if config.providers: | |
| 67 | 140 | cprint( |
| 68 | - f"Configuration already exists for {config.distribution_type}. Will overwrite...", | |
| 69 | - "yellow", | |
| 70 | - attrs=["bold"], | |
| 141 | + f"> YAML configuration has been written to {package_file}", | |
| 142 | + color="blue", | |
| 71 | 143 | ) |
| 72 | - | |
| 73 | - config.providers = configure_api_providers(config.providers) | |
| 74 | - | |
| 75 | - with open(config_file, "w") as fp: | |
| 76 | - to_write = json.loads(json.dumps(config.dict(), cls=EnumEncoder)) | |
| 77 | - fp.write(yaml.dump(to_write, sort_keys=False)) | |
| 78 | - | |
| 79 | - print(f"YAML configuration has been written to {config_file}") |
deleted llama_toolchain/cli/stack/list_distributions.py +0 −55
| @@ -1,55 +0,0 @@ | ||
| 1 | -# Copyright (c) Meta Platforms, Inc. and affiliates. | |
| 2 | -# All rights reserved. | |
| 3 | -# | |
| 4 | -# This source code is licensed under the terms described in the LICENSE file in | |
| 5 | -# the root directory of this source tree. | |
| 6 | - | |
| 7 | -import argparse | |
| 8 | -import json | |
| 9 | - | |
| 10 | -from llama_toolchain.cli.subcommand import Subcommand | |
| 11 | - | |
| 12 | - | |
| 13 | -class StackListDistributions(Subcommand): | |
| 14 | - def __init__(self, subparsers: argparse._SubParsersAction): | |
| 15 | - super().__init__() | |
| 16 | - self.parser = subparsers.add_parser( | |
| 17 | - "list-distributions", | |
| 18 | - prog="llama stack list-distributions", | |
| 19 | - description="Show available Llama Stack Distributions", | |
| 20 | - formatter_class=argparse.RawTextHelpFormatter, | |
| 21 | - ) | |
| 22 | - self._add_arguments() | |
| 23 | - self.parser.set_defaults(func=self._run_distribution_list_cmd) | |
| 24 | - | |
| 25 | - def _add_arguments(self): | |
| 26 | - pass | |
| 27 | - | |
| 28 | - def _run_distribution_list_cmd(self, args: argparse.Namespace) -> None: | |
| 29 | - from llama_toolchain.cli.table import print_table | |
| 30 | - from llama_toolchain.core.distribution_registry import ( | |
| 31 | - available_distribution_specs, | |
| 32 | - ) | |
| 33 | - | |
| 34 | - # eventually, this should query a registry at llama.meta.com/llamastack/distributions | |
| 35 | - headers = [ | |
| 36 | - "Distribution Type", | |
| 37 | - "Providers", | |
| 38 | - "Description", | |
| 39 | - ] | |
| 40 | - | |
| 41 | - rows = [] | |
| 42 | - for spec in available_distribution_specs(): | |
| 43 | - providers = {k.value: v for k, v in spec.providers.items()} | |
| 44 | - rows.append( | |
| 45 | - [ | |
| 46 | - spec.distribution_type, | |
| 47 | - json.dumps(providers, indent=2), | |
| 48 | - spec.description, | |
| 49 | - ] | |
| 50 | - ) | |
| 51 | - print_table( | |
| 52 | - rows, | |
| 53 | - headers, | |
| 54 | - separate_rows=True, | |
| 55 | - ) |
modified llama_toolchain/cli/stack/run.py +0 −3
| @@ -69,9 +69,6 @@ | ||
| 69 | 69 | with open(config_file, "r") as f: |
| 70 | 70 | config = PackageConfig(**yaml.safe_load(f)) |
| 71 | 71 | |
| 72 | - if not config.distribution_type: | |
| 73 | - raise ValueError("Build config appears to be corrupt.") | |
| 74 | - | |
| 75 | 72 | if config.docker_image: |
| 76 | 73 | script = pkg_resources.resource_filename( |
| 77 | 74 | "llama_toolchain", |
modified llama_toolchain/cli/stack/stack.py +0 −2
| @@ -11,7 +11,6 @@ | ||
| 11 | 11 | from .build import StackBuild |
| 12 | 12 | from .configure import StackConfigure |
| 13 | 13 | from .list_apis import StackListApis |
| 14 | -from .list_distributions import StackListDistributions | |
| 15 | 14 | from .list_providers import StackListProviders |
| 16 | 15 | from .run import StackRun |
| 17 | 16 | |
| @@ -31,6 +30,5 @@ | ||
| 31 | 30 | StackBuild.create(subparsers) |
| 32 | 31 | StackConfigure.create(subparsers) |
| 33 | 32 | StackListApis.create(subparsers) |
| 34 | - StackListDistributions.create(subparsers) | |
| 35 | 33 | StackListProviders.create(subparsers) |
| 36 | 34 | StackRun.create(subparsers) |
added llama_toolchain/configs/distributions/conda/local-conda-example-build.yaml +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +name: local-conda-example | |
| 2 | +distribution_spec: | |
| 3 | + description: Use code from `llama_toolchain` itself to serve all llama stack APIs | |
| 4 | + providers: | |
| 5 | + inference: meta-reference | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + safety: meta-reference | |
| 8 | + agentic_system: meta-reference | |
| 9 | + telemetry: console | |
| 10 | +image_type: conda |
added llama_toolchain/configs/distributions/conda/local-fireworks-conda-example-build.yaml +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +name: local-fireworks-conda-example | |
| 2 | +distribution_spec: | |
| 3 | + description: Use Fireworks.ai for running LLM inference | |
| 4 | + providers: | |
| 5 | + inference: remote::fireworks | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + safety: meta-reference | |
| 8 | + agentic_system: meta-reference | |
| 9 | + telemetry: console | |
| 10 | +image_type: conda |
added llama_toolchain/configs/distributions/conda/local-ollama-conda-example-build.yaml +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +name: local-ollama-conda-example | |
| 2 | +distribution_spec: | |
| 3 | + description: Like local, but use ollama for running LLM inference | |
| 4 | + providers: | |
| 5 | + inference: remote::ollama | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + safety: meta-reference | |
| 8 | + agentic_system: meta-reference | |
| 9 | + telemetry: console | |
| 10 | +image_type: conda |
added llama_toolchain/configs/distributions/conda/local-tgi-conda-example-build.yaml +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +name: local-tgi-conda-example | |
| 2 | +distribution_spec: | |
| 3 | + 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). | |
| 4 | + providers: | |
| 5 | + inference: remote::tgi | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + safety: meta-reference | |
| 8 | + agentic_system: meta-reference | |
| 9 | + telemetry: console | |
| 10 | +image_type: conda |
added llama_toolchain/configs/distributions/conda/local-together-conda-example-build.yaml +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +name: local-tgi-conda-example | |
| 2 | +distribution_spec: | |
| 3 | + description: Use Together.ai for running LLM inference | |
| 4 | + providers: | |
| 5 | + inference: remote::together | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + safety: meta-reference | |
| 8 | + agentic_system: meta-reference | |
| 9 | + telemetry: console | |
| 10 | +image_type: conda |
added llama_toolchain/configs/distributions/distribution_registry/local-ollama.yaml +7 −0
| @@ -0,0 +1,7 @@ | ||
| 1 | +description: Like local, but use ollama for running LLM inference | |
| 2 | +providers: | |
| 3 | + inference: remote::ollama | |
| 4 | + safety: meta-reference | |
| 5 | + agentic_system: meta-reference | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + telemetry: console |
added llama_toolchain/configs/distributions/distribution_registry/local-plus-fireworks-inference.yaml +7 −0
| @@ -0,0 +1,7 @@ | ||
| 1 | +description: Use Fireworks.ai for running LLM inference | |
| 2 | +providers: | |
| 3 | + inference: remote::fireworks | |
| 4 | + safety: meta-reference | |
| 5 | + agentic_system: meta-reference | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + telemetry: console |
added llama_toolchain/configs/distributions/distribution_registry/local-plus-tgi-inference.yaml +6 −0
| @@ -0,0 +1,6 @@ | ||
| 1 | +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). | |
| 2 | +providers: | |
| 3 | + inference: remote::tgi | |
| 4 | + safety: meta-reference | |
| 5 | + agentic_system: meta-reference | |
| 6 | + memory: meta-reference-faiss |
added llama_toolchain/configs/distributions/distribution_registry/local-plus-together-inference.yaml +7 −0
| @@ -0,0 +1,7 @@ | ||
| 1 | +description: Use Together.ai for running LLM inference | |
| 2 | +providers: | |
| 3 | + inference: remote::together | |
| 4 | + safety: meta-reference | |
| 5 | + agentic_system: meta-reference | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + telemetry: console |
added llama_toolchain/configs/distributions/distribution_registry/local.yaml +7 −0
| @@ -0,0 +1,7 @@ | ||
| 1 | +description: Use code from `llama_toolchain` itself to serve all llama stack APIs | |
| 2 | +providers: | |
| 3 | + inference: meta-reference | |
| 4 | + memory: meta-reference-faiss | |
| 5 | + safety: meta-reference | |
| 6 | + agentic_system: meta-reference | |
| 7 | + telemetry: console |
added llama_toolchain/configs/distributions/docker/local-docker-example-build.yaml +10 −0
| @@ -0,0 +1,10 @@ | ||
| 1 | +name: local-docker-example | |
| 2 | +distribution_spec: | |
| 3 | + description: Use code from `llama_toolchain` itself to serve all llama stack APIs | |
| 4 | + providers: | |
| 5 | + inference: meta-reference | |
| 6 | + memory: meta-reference-faiss | |
| 7 | + safety: meta-reference | |
| 8 | + agentic_system: meta-reference | |
| 9 | + telemetry: console | |
| 10 | +image_type: docker |
modified llama_toolchain/core/build_conda_env.sh +3 −9
| @@ -19,17 +19,15 @@ | ||
| 19 | 19 | |
| 20 | 20 | set -euo pipefail |
| 21 | 21 | |
| 22 | -if [ "$#" -ne 4 ]; then | |
| 22 | +if [ "$#" -ne 2 ]; then | |
| 23 | 23 | echo "Usage: $0 <distribution_type> <build_name> <pip_dependencies>" >&2 |
| 24 | 24 | echo "Example: $0 <distribution_type> mybuild 'numpy pandas scipy'" >&2 |
| 25 | 25 | exit 1 |
| 26 | 26 | fi |
| 27 | 27 | |
| 28 | -distribution_type="$1" | |
| 29 | -build_name="$2" | |
| 28 | +build_name="$1" | |
| 30 | 29 | env_name="llamastack-$build_name" |
| 31 | -config_file="$3" | |
| 32 | -pip_dependencies="$4" | |
| 30 | +pip_dependencies="$2" | |
| 33 | 31 | |
| 34 | 32 | # Define color codes |
| 35 | 33 | RED='\033[0;31m' |
| @@ -115,7 +113,3 @@ | ||
| 115 | 113 | } |
| 116 | 114 | |
| 117 | 115 | ensure_conda_env_python310 "$env_name" "$pip_dependencies" |
| 118 | - | |
| 119 | -printf "${GREEN}Successfully setup conda environment. Configuring build...${NC}\n" | |
| 120 | - | |
| 121 | -$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $config_file |
modified llama_toolchain/core/build_container.sh +14 −11
| @@ -4,18 +4,17 @@ | ||
| 4 | 4 | LLAMA_TOOLCHAIN_DIR=${LLAMA_TOOLCHAIN_DIR:-} |
| 5 | 5 | TEST_PYPI_VERSION=${TEST_PYPI_VERSION:-} |
| 6 | 6 | |
| 7 | -if [ "$#" -ne 5 ]; then | |
| 8 | - echo "Usage: $0 <distribution_type> <build_name> <docker_base> <pip_dependencies> | |
| 9 | - echo "Example: $0 distribution_type my-fastapi-app python:3.9-slim 'fastapi uvicorn' | |
| 7 | +if [ "$#" -ne 4 ]; then | |
| 8 | + echo "Usage: $0 <build_name> <docker_base> <pip_dependencies> | |
| 9 | + echo "Example: $0 my-fastapi-app python:3.9-slim 'fastapi uvicorn' | |
| 10 | 10 | exit 1 |
| 11 | 11 | fi |
| 12 | 12 | |
| 13 | -distribution_type=$1 | |
| 14 | -build_name="$2" | |
| 13 | +build_name="$1" | |
| 15 | 14 | image_name="llamastack-$build_name" |
| 16 | -docker_base=$3 | |
| 17 | -config_file=$4 | |
| 18 | -pip_dependencies=$5 | |
| 15 | +docker_base=$2 | |
| 16 | +build_file_path=$3 | |
| 17 | +pip_dependencies=$4 | |
| 19 | 18 | |
| 20 | 19 | # Define color codes |
| 21 | 20 | RED='\033[0;31m' |
| @@ -26,6 +25,8 @@ | ||
| 26 | 25 | |
| 27 | 26 | SCRIPT_DIR=$(dirname "$(readlink -f "$0")") |
| 28 | 27 | REPO_DIR=$(dirname $(dirname "$SCRIPT_DIR")) |
| 28 | +DOCKER_BINARY=${DOCKER_BINARY:-docker} | |
| 29 | +DOCKER_OPTS=${DOCKER_OPTS:-} | |
| 29 | 30 | |
| 30 | 31 | TEMP_DIR=$(mktemp -d) |
| 31 | 32 | |
| @@ -93,6 +94,8 @@ | ||
| 93 | 94 | |
| 94 | 95 | EOF |
| 95 | 96 | |
| 97 | +add_to_docker "ADD $build_file_path ./llamastack-build.yaml" | |
| 98 | + | |
| 96 | 99 | printf "Dockerfile created successfully in $TEMP_DIR/Dockerfile" |
| 97 | 100 | cat $TEMP_DIR/Dockerfile |
| 98 | 101 | printf "\n" |
| @@ -105,10 +108,10 @@ | ||
| 105 | 108 | mounts="$mounts -v $(readlink -f $LLAMA_MODELS_DIR):$models_mount" |
| 106 | 109 | fi |
| 107 | 110 | set -x |
| 108 | -podman build -t $image_name -f "$TEMP_DIR/Dockerfile" "$REPO_DIR" $mounts | |
| 111 | +$DOCKER_BINARY build $DOCKER_OPTS -t $image_name -f "$TEMP_DIR/Dockerfile" "$REPO_DIR" $mounts | |
| 109 | 112 | set +x |
| 110 | 113 | |
| 111 | -printf "${GREEN}Succesfully setup Podman image. Configuring build...${NC}" | |
| 112 | 114 | echo "You can run it with: podman run -p 8000:8000 $image_name" |
| 113 | 115 | |
| 114 | -$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $config_file | |
| 116 | +echo "Checking image builds..." | |
| 117 | +podman run -it $image_name cat llamastack-build.yaml |
added llama_toolchain/core/configure_container.sh +31 −0
| @@ -0,0 +1,31 @@ | ||
| 1 | +#!/bin/bash | |
| 2 | + | |
| 3 | +# Copyright (c) Meta Platforms, Inc. and affiliates. | |
| 4 | +# All rights reserved. | |
| 5 | +# | |
| 6 | +# This source code is licensed under the terms described in the LICENSE file in | |
| 7 | +# the root directory of this source tree. | |
| 8 | + | |
| 9 | +set -euo pipefail | |
| 10 | + | |
| 11 | +error_handler() { | |
| 12 | + echo "Error occurred in script at line: ${1}" >&2 | |
| 13 | + exit 1 | |
| 14 | +} | |
| 15 | + | |
| 16 | +trap 'error_handler ${LINENO}' ERR | |
| 17 | + | |
| 18 | +if [ $# -lt 2 ]; then | |
| 19 | + echo "Usage: $0 <container name> <build file path>" | |
| 20 | + exit 1 | |
| 21 | +fi | |
| 22 | + | |
| 23 | +docker_image="$1" | |
| 24 | +host_build_dir="$2" | |
| 25 | +container_build_dir="/app/builds" | |
| 26 | + | |
| 27 | +set -x | |
| 28 | +podman run -it \ | |
| 29 | + -v $host_build_dir:$container_build_dir \ | |
| 30 | + $docker_image \ | |
| 31 | + llama stack configure ./llamastack-build.yaml --output-dir $container_build_dir |
modified llama_toolchain/core/datatypes.py +7 −12
| @@ -151,11 +151,12 @@ | ||
| 151 | 151 | |
| 152 | 152 | @json_schema_type |
| 153 | 153 | class DistributionSpec(BaseModel): |
| 154 | - distribution_type: str | |
| 155 | - description: str | |
| 156 | - | |
| 154 | + description: Optional[str] = Field( | |
| 155 | + default="", | |
| 156 | + description="Description of the distribution", | |
| 157 | + ) | |
| 157 | 158 | docker_image: Optional[str] = None |
| 158 | - providers: Dict[Api, str] = Field( | |
| 159 | + providers: Dict[str, str] = Field( | |
| 159 | 160 | default_factory=dict, |
| 160 | 161 | description="Provider Types for each of the APIs provided by this distribution", |
| 161 | 162 | ) |
| @@ -172,8 +173,6 @@ | ||
| 172 | 173 | this could be just a hash |
| 173 | 174 | """, |
| 174 | 175 | ) |
| 175 | - distribution_type: Optional[str] = None | |
| 176 | - | |
| 177 | 176 | docker_image: Optional[str] = Field( |
| 178 | 177 | default=None, |
| 179 | 178 | description="Reference to the docker image if this package refers to a container", |
| @@ -194,12 +193,8 @@ | ||
| 194 | 193 | @json_schema_type |
| 195 | 194 | class BuildConfig(BaseModel): |
| 196 | 195 | name: str |
| 197 | - distribution: str = Field( | |
| 198 | - default="local", description="Type of distribution to build (adhoc | {})" | |
| 199 | - ) | |
| 200 | - api_providers: Optional[str] = Field( | |
| 201 | - default_factory=list, | |
| 202 | - description="List of API provider names to build", | |
| 196 | + distribution_spec: DistributionSpec = Field( | |
| 197 | + description="The distribution spec to build including API providers. " | |
| 203 | 198 | ) |
| 204 | 199 | image_type: str = Field( |
| 205 | 200 | default="conda", |
modified llama_toolchain/core/distribution.py +0 −10
| @@ -31,16 +31,6 @@ | ||
| 31 | 31 | ] |
| 32 | 32 | |
| 33 | 33 | |
| 34 | -def distribution_dependencies(distribution: DistributionSpec) -> List[str]: | |
| 35 | - # only consider InlineProviderSpecs when calculating dependencies | |
| 36 | - return [ | |
| 37 | - dep | |
| 38 | - for provider_spec in distribution.provider_specs.values() | |
| 39 | - if isinstance(provider_spec, InlineProviderSpec) | |
| 40 | - for dep in provider_spec.pip_packages | |
| 41 | - ] + SERVER_DEPENDENCIES | |
| 42 | - | |
| 43 | - | |
| 44 | 34 | def stack_apis() -> List[Api]: |
| 45 | 35 | return [v for v in Api] |
| 46 | 36 |
modified llama_toolchain/core/distribution_registry.py +10 −75
| @@ -5,84 +5,19 @@ | ||
| 5 | 5 | # the root directory of this source tree. |
| 6 | 6 | |
| 7 | 7 | from functools import lru_cache |
| 8 | +from pathlib import Path | |
| 8 | 9 | from typing import List, Optional |
| 9 | - | |
| 10 | 10 | from .datatypes import * # noqa: F403 |
| 11 | +import yaml | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | @lru_cache() |
| 14 | 15 | def available_distribution_specs() -> List[DistributionSpec]: |
| 15 | - return [ | |
| 16 | - DistributionSpec( | |
| 17 | - distribution_type="local", | |
| 18 | - description="Use code from `llama_toolchain` itself to serve all llama stack APIs", | |
| 19 | - providers={ | |
| 20 | - Api.inference: "meta-reference", | |
| 21 | - Api.memory: "meta-reference-faiss", | |
| 22 | - Api.safety: "meta-reference", | |
| 23 | - Api.agentic_system: "meta-reference", | |
| 24 | - Api.telemetry: "console", | |
| 25 | - }, | |
| 26 | - ), | |
| 27 | - DistributionSpec( | |
| 28 | - distribution_type="remote", | |
| 29 | - description="Point to remote services for all llama stack APIs", | |
| 30 | - providers={ | |
| 31 | - **{x: "remote" for x in Api}, | |
| 32 | - Api.telemetry: "console", | |
| 33 | - }, | |
| 34 | - ), | |
| 35 | - DistributionSpec( | |
| 36 | - distribution_type="local-ollama", | |
| 37 | - description="Like local, but use ollama for running LLM inference", | |
| 38 | - providers={ | |
| 39 | - Api.inference: remote_provider_type("ollama"), | |
| 40 | - Api.safety: "meta-reference", | |
| 41 | - Api.agentic_system: "meta-reference", | |
| 42 | - Api.memory: "meta-reference-faiss", | |
| 43 | - Api.telemetry: "console", | |
| 44 | - }, | |
| 45 | - ), | |
| 46 | - DistributionSpec( | |
| 47 | - distribution_type="local-plus-fireworks-inference", | |
| 48 | - description="Use Fireworks.ai for running LLM inference", | |
| 49 | - providers={ | |
| 50 | - Api.inference: remote_provider_type("fireworks"), | |
| 51 | - Api.safety: "meta-reference", | |
| 52 | - Api.agentic_system: "meta-reference", | |
| 53 | - Api.memory: "meta-reference-faiss", | |
| 54 | - Api.telemetry: "console", | |
| 55 | - }, | |
| 56 | - ), | |
| 57 | - DistributionSpec( | |
| 58 | - distribution_type="local-plus-together-inference", | |
| 59 | - description="Use Together.ai for running LLM inference", | |
| 60 | - providers={ | |
| 61 | - Api.inference: remote_provider_type("together"), | |
| 62 | - Api.safety: "meta-reference", | |
| 63 | - Api.agentic_system: "meta-reference", | |
| 64 | - Api.memory: "meta-reference-faiss", | |
| 65 | - Api.telemetry: "console", | |
| 66 | - }, | |
| 67 | - ), | |
| 68 | - DistributionSpec( | |
| 69 | - distribution_type="local-plus-tgi-inference", | |
| 70 | - description="Use TGI for running LLM inference", | |
| 71 | - providers={ | |
| 72 | - Api.inference: remote_provider_type("tgi"), | |
| 73 | - Api.safety: "meta-reference", | |
| 74 | - Api.agentic_system: "meta-reference", | |
| 75 | - Api.memory: "meta-reference-faiss", | |
| 76 | - }, | |
| 77 | - ), | |
| 78 | - ] | |
| 79 | - | |
| 80 | - | |
| 81 | -@lru_cache() | |
| 82 | -def resolve_distribution_spec( | |
| 83 | - distribution_type: str, | |
| 84 | -) -> Optional[DistributionSpec]: | |
| 85 | - for spec in available_distribution_specs(): | |
| 86 | - if spec.distribution_type == distribution_type: | |
| 87 | - return spec | |
| 88 | - return None | |
| 16 | + distribution_specs = [] | |
| 17 | + for p in Path("llama_toolchain/configs/distributions/distribution_registry").rglob( | |
| 18 | + "*.yaml" | |
| 19 | + ): | |
| 20 | + with open(p, "r") as f: | |
| 21 | + distribution_specs.append(DistributionSpec(**yaml.safe_load(f))) | |
| 22 | + | |
| 23 | + return distribution_specs |
modified llama_toolchain/core/package.py +18 −75
| @@ -21,6 +21,8 @@ | ||
| 21 | 21 | from termcolor import cprint |
| 22 | 22 | |
| 23 | 23 | from llama_toolchain.core.datatypes import * # noqa: F403 |
| 24 | +from pathlib import Path | |
| 25 | + | |
| 24 | 26 | from llama_toolchain.core.distribution import api_providers, SERVER_DEPENDENCIES |
| 25 | 27 | |
| 26 | 28 | |
| @@ -39,87 +41,35 @@ | ||
| 39 | 41 | provider: str |
| 40 | 42 | |
| 41 | 43 | |
| 42 | -def build_package( | |
| 43 | - api_inputs: List[ApiInput], | |
| 44 | - image_type: ImageType, | |
| 45 | - name: str, | |
| 46 | - distribution_type: Optional[str] = None, | |
| 47 | - docker_image: Optional[str] = None, | |
| 48 | -): | |
| 49 | - if not distribution_type: | |
| 50 | - distribution_type = "adhoc" | |
| 51 | - | |
| 52 | - build_dir = BUILDS_BASE_DIR / distribution_type / image_type.value | |
| 53 | - os.makedirs(build_dir, exist_ok=True) | |
| 54 | - | |
| 55 | - package_name = name.replace("::", "-") | |
| 56 | - package_file = build_dir / f"{package_name}.yaml" | |
| 57 | - | |
| 58 | - all_providers = api_providers() | |
| 59 | - | |
| 44 | +def build_package(build_config: BuildConfig, build_file_path: Path): | |
| 60 | 45 | package_deps = Dependencies( |
| 61 | - docker_image=docker_image or "python:3.10-slim", | |
| 46 | + docker_image=build_config.distribution_spec.docker_image or "python:3.10-slim", | |
| 62 | 47 | pip_packages=SERVER_DEPENDENCIES, |
| 63 | 48 | ) |
| 64 | 49 | |
| 65 | - stub_config = {} | |
| 66 | - for api_input in api_inputs: | |
| 67 | - api = api_input.api | |
| 68 | - providers_for_api = all_providers[api] | |
| 69 | - if api_input.provider not in providers_for_api: | |
| 50 | + # extend package dependencies based on providers spec | |
| 51 | + all_providers = api_providers() | |
| 52 | + for api_str, provider in build_config.distribution_spec.providers.items(): | |
| 53 | + providers_for_api = all_providers[Api(api_str)] | |
| 54 | + if provider not in providers_for_api: | |
| 70 | 55 | raise ValueError( |
| 71 | - f"Provider `{api_input.provider}` is not available for API `{api}`" | |
| 56 | + f"Provider `{provider}` is not available for API `{api_str}`" | |
| 72 | 57 | ) |
| 73 | 58 | |
| 74 | - provider = providers_for_api[api_input.provider] | |
| 75 | - package_deps.pip_packages.extend(provider.pip_packages) | |
| 76 | - if provider.docker_image: | |
| 59 | + provider_spec = providers_for_api[provider] | |
| 60 | + package_deps.pip_packages.extend(provider_spec.pip_packages) | |
| 61 | + if provider_spec.docker_image: | |
| 77 | 62 | raise ValueError("A stack's dependencies cannot have a docker image") |
| 78 | 63 | |
| 79 | - stub_config[api.value] = {"provider_type": api_input.provider} | |
| 80 | - | |
| 81 | - if package_file.exists(): | |
| 82 | - cprint( | |
| 83 | - f"Build `{package_name}` exists; will reconfigure", | |
| 84 | - color="yellow", | |
| 85 | - ) | |
| 86 | - c = PackageConfig(**yaml.safe_load(package_file.read_text())) | |
| 87 | - for api_str, new_config in stub_config.items(): | |
| 88 | - if api_str not in c.providers: | |
| 89 | - c.providers[api_str] = new_config | |
| 90 | - else: | |
| 91 | - existing_config = c.providers[api_str] | |
| 92 | - if existing_config["provider_type"] != new_config["provider_type"]: | |
| 93 | - cprint( | |
| 94 | - f"Provider `{api_str}` has changed from `{existing_config}` to `{new_config}`", | |
| 95 | - color="yellow", | |
| 96 | - ) | |
| 97 | - c.providers[api_str] = new_config | |
| 98 | - else: | |
| 99 | - c = PackageConfig( | |
| 100 | - built_at=datetime.now(), | |
| 101 | - package_name=package_name, | |
| 102 | - providers=stub_config, | |
| 103 | - ) | |
| 104 | - | |
| 105 | - c.distribution_type = distribution_type | |
| 106 | - c.docker_image = package_name if image_type == ImageType.docker else None | |
| 107 | - c.conda_env = package_name if image_type == ImageType.conda else None | |
| 108 | - | |
| 109 | - with open(package_file, "w") as f: | |
| 110 | - to_write = json.loads(json.dumps(c.dict(), cls=EnumEncoder)) | |
| 111 | - f.write(yaml.dump(to_write, sort_keys=False)) | |
| 112 | - | |
| 113 | - if image_type == ImageType.docker: | |
| 64 | + if build_config.image_type == ImageType.docker.value: | |
| 114 | 65 | script = pkg_resources.resource_filename( |
| 115 | 66 | "llama_toolchain", "core/build_container.sh" |
| 116 | 67 | ) |
| 117 | 68 | args = [ |
| 118 | 69 | script, |
| 119 | - distribution_type, | |
| 120 | - package_name, | |
| 70 | + build_config.name, | |
| 121 | 71 | package_deps.docker_image, |
| 122 | - str(package_file), | |
| 72 | + str(build_file_path), | |
| 123 | 73 | " ".join(package_deps.pip_packages), |
| 124 | 74 | ] |
| 125 | 75 | else: |
| @@ -128,21 +78,14 @@ | ||
| 128 | 78 | ) |
| 129 | 79 | args = [ |
| 130 | 80 | script, |
| 131 | - distribution_type, | |
| 132 | - package_name, | |
| 133 | - str(package_file), | |
| 81 | + build_config.name, | |
| 134 | 82 | " ".join(package_deps.pip_packages), |
| 135 | 83 | ] |
| 136 | 84 | |
| 137 | 85 | return_code = run_with_pty(args) |
| 138 | 86 | if return_code != 0: |
| 139 | 87 | cprint( |
| 140 | - f"Failed to build target {package_name} with return code {return_code}", | |
| 88 | + f"Failed to build target {build_config.name} with return code {return_code}", | |
| 141 | 89 | color="red", |
| 142 | 90 | ) |
| 143 | 91 | return |
| 144 | - | |
| 145 | - cprint( | |
| 146 | - f"Target `{package_name}` built with configuration at {str(package_file)}", | |
| 147 | - color="green", | |
| 148 | - ) |
modified llama_toolchain/inference/meta_reference/generation.py +2 −1
| @@ -28,10 +28,10 @@ | ||
| 28 | 28 | from llama_models.llama3.api.tokenizer import Tokenizer |
| 29 | 29 | from llama_models.llama3.reference_impl.model import Transformer |
| 30 | 30 | from llama_models.sku_list import resolve_model |
| 31 | -from termcolor import cprint | |
| 32 | 31 | |
| 33 | 32 | from llama_toolchain.common.model_utils import model_local_dir |
| 34 | 33 | from llama_toolchain.inference.api import QuantizationType |
| 34 | +from termcolor import cprint | |
| 35 | 35 | |
| 36 | 36 | from .config import MetaReferenceImplConfig |
| 37 | 37 | |
| @@ -80,6 +80,7 @@ | ||
| 80 | 80 | torch.distributed.init_process_group("nccl") |
| 81 | 81 | |
| 82 | 82 | model_parallel_size = config.model_parallel_size |
| 83 | + | |
| 83 | 84 | if not model_parallel_is_initialized(): |
| 84 | 85 | initialize_model_parallel(model_parallel_size) |
| 85 | 86 |
added llama_toolchain/memory/common/__init__.py +5 −0
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. | |
| 2 | +# All rights reserved. | |
| 3 | +# | |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in | |
| 5 | +# the root directory of this source tree. |