diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7e05c683a..5e19e73b7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,6 +12,11 @@ We actively welcome your pull requests. 5. Make sure your code lints. 6. If you haven't already, complete the Contributor License Agreement ("CLA"). + +### Updating Provider Configurations + +If you have made changes to a provider's configuration in any form (introducing a new config key, or changing models, etc.), you should run `python llama_stack/scripts/distro_codegen.py` to re-generate various YAML files as well as the documentation. You should not change `docs/source/.../distributions/` files manually as they are auto-generated. + ### Building the Documentation If you are making changes to the documentation at [https://llama-stack.readthedocs.io/en/latest/](https://llama-stack.readthedocs.io/en/latest/), you can use the following command to build the documentation and preview your changes. You will need [Sphinx](https://www.sphinx-doc.org/en/master/) and the readthedocs theme. diff --git a/distributions/bedrock/run.yaml b/distributions/bedrock/run.yaml index 45e8aa7b5..2f7cb36ef 100644 --- a/distributions/bedrock/run.yaml +++ b/distributions/bedrock/run.yaml @@ -1,5 +1,4 @@ version: '2' -built_at: '2024-11-01T17:40:45.325529' image_name: local name: bedrock docker_image: null diff --git a/distributions/dell-tgi/run.yaml b/distributions/dell-tgi/run.yaml index 4b7b331fe..3f8a98779 100644 --- a/distributions/dell-tgi/run.yaml +++ b/distributions/dell-tgi/run.yaml @@ -1,5 +1,4 @@ version: '2' -built_at: '2024-10-08T17:40:45.325529' image_name: local docker_image: null conda_env: local diff --git a/distributions/fireworks/run.yaml b/distributions/fireworks/run.yaml deleted file mode 100644 index d2903aabb..000000000 --- a/distributions/fireworks/run.yaml +++ /dev/null @@ -1,51 +0,0 @@ -version: '2' -built_at: '2024-10-08T17:40:45.325529' -image_name: local -docker_image: null -conda_env: local -apis: -- shields -- agents -- models -- memory -- memory_banks -- inference -- safety -providers: - inference: - - provider_id: fireworks0 - provider_type: remote::fireworks - config: - url: https://api.fireworks.ai/inference - # api_key: - safety: - safety: - - provider_id: meta0 - provider_type: inline::llama-guard - config: - model: Llama-Guard-3-1B - excluded_categories: [] - - provider_id: meta1 - provider_type: inline::prompt-guard - config: - model: Prompt-Guard-86M - memory: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} - # Uncomment to use weaviate memory provider - # - provider_id: weaviate0 - # provider_type: remote::weaviate - # config: {} - agents: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - persistence_store: - namespace: null - type: sqlite - db_path: ~/.llama/runtime/kvstore.db - telemetry: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} diff --git a/distributions/fireworks/run.yaml b/distributions/fireworks/run.yaml new file mode 120000 index 000000000..532e0e2a8 --- /dev/null +++ b/distributions/fireworks/run.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/fireworks/run.yaml \ No newline at end of file diff --git a/distributions/inline-vllm/run.yaml b/distributions/inline-vllm/run.yaml index b998727c0..f42c942a3 100644 --- a/distributions/inline-vllm/run.yaml +++ b/distributions/inline-vllm/run.yaml @@ -1,5 +1,4 @@ version: '2' -built_at: '2024-10-08T17:40:45.325529' image_name: local docker_image: null conda_env: local diff --git a/distributions/meta-reference-gpu/run-with-safety.yaml b/distributions/meta-reference-gpu/run-with-safety.yaml new file mode 120000 index 000000000..4c5483425 --- /dev/null +++ b/distributions/meta-reference-gpu/run-with-safety.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/meta-reference-gpu/run-with-safety.yaml \ No newline at end of file diff --git a/distributions/meta-reference-gpu/run.yaml b/distributions/meta-reference-gpu/run.yaml deleted file mode 100644 index 13d3787e1..000000000 --- a/distributions/meta-reference-gpu/run.yaml +++ /dev/null @@ -1,69 +0,0 @@ -version: '2' -built_at: '2024-10-08T17:40:45.325529' -image_name: local -docker_image: null -conda_env: local -apis: -- shields -- agents -- models -- memory -- memory_banks -- inference -- safety -providers: - inference: - - provider_id: inference0 - provider_type: inline::meta-reference - config: - model: Llama3.2-3B-Instruct - quantization: null - torch_seed: null - max_seq_len: 4096 - max_batch_size: 1 - - provider_id: inference1 - provider_type: inline::meta-reference - config: - model: Llama-Guard-3-1B - quantization: null - torch_seed: null - max_seq_len: 2048 - max_batch_size: 1 - safety: - - provider_id: meta0 - provider_type: inline::llama-guard - config: - model: Llama-Guard-3-1B - excluded_categories: [] - - provider_id: meta1 - provider_type: inline::prompt-guard - config: - model: Prompt-Guard-86M -# Uncomment to use prompt guard -# prompt_guard_shield: -# model: Prompt-Guard-86M - memory: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} - # Uncomment to use pgvector - # - provider_id: pgvector - # provider_type: remote::pgvector - # config: - # host: 127.0.0.1 - # port: 5432 - # db: postgres - # user: postgres - # password: mysecretpassword - agents: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - persistence_store: - namespace: null - type: sqlite - db_path: ~/.llama/runtime/agents_store.db - telemetry: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} diff --git a/distributions/meta-reference-gpu/run.yaml b/distributions/meta-reference-gpu/run.yaml new file mode 120000 index 000000000..d680186ab --- /dev/null +++ b/distributions/meta-reference-gpu/run.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/meta-reference-gpu/run.yaml \ No newline at end of file diff --git a/distributions/meta-reference-quantized-gpu/run.yaml b/distributions/meta-reference-quantized-gpu/run.yaml index d5012852d..19c726b09 100644 --- a/distributions/meta-reference-quantized-gpu/run.yaml +++ b/distributions/meta-reference-quantized-gpu/run.yaml @@ -1,5 +1,4 @@ version: '2' -built_at: '2024-10-08T17:40:45.325529' image_name: local docker_image: null conda_env: local diff --git a/distributions/ollama-gpu/run.yaml b/distributions/ollama-gpu/run.yaml index c702b878e..25471c69f 100644 --- a/distributions/ollama-gpu/run.yaml +++ b/distributions/ollama-gpu/run.yaml @@ -1,5 +1,4 @@ version: '2' -built_at: '2024-10-08T17:40:45.325529' image_name: local docker_image: null conda_env: local @@ -13,20 +12,15 @@ apis: - safety providers: inference: - - provider_id: ollama0 + - provider_id: ollama provider_type: remote::ollama config: - url: http://127.0.0.1:14343 + url: ${env.OLLAMA_URL:http://127.0.0.1:11434} safety: - provider_id: meta0 provider_type: inline::llama-guard config: - model: Llama-Guard-3-1B excluded_categories: [] - - provider_id: meta1 - provider_type: inline::prompt-guard - config: - model: Prompt-Guard-86M memory: - provider_id: meta0 provider_type: inline::meta-reference @@ -43,3 +37,10 @@ providers: - provider_id: meta0 provider_type: inline::meta-reference config: {} +models: + - model_id: ${env.INFERENCE_MODEL:Llama3.2-3B-Instruct} + provider_id: ollama + - model_id: ${env.SAFETY_MODEL:Llama-Guard-3-1B} + provider_id: ollama +shields: + - shield_id: ${env.SAFETY_MODEL:Llama-Guard-3-1B} diff --git a/distributions/ollama/compose.yaml b/distributions/ollama/compose.yaml index dc51d4759..176f19d6b 100644 --- a/distributions/ollama/compose.yaml +++ b/distributions/ollama/compose.yaml @@ -1,30 +1,71 @@ services: ollama: image: ollama/ollama:latest - network_mode: "host" + network_mode: ${NETWORK_MODE:-bridge} volumes: - - ollama:/root/.ollama # this solution synchronizes with the docker volume and loads the model rocket fast + - ~/.ollama:/root/.ollama ports: - "11434:11434" + environment: + OLLAMA_DEBUG: 1 command: [] + deploy: + resources: + limits: + memory: 8G # Set maximum memory + reservations: + memory: 8G # Set minimum memory reservation + # healthcheck: + # # ugh, no CURL in ollama image + # test: ["CMD", "curl", "-f", "http://ollama:11434"] + # interval: 10s + # timeout: 5s + # retries: 5 + + ollama-init: + image: ollama/ollama:latest + depends_on: + - ollama + # condition: service_healthy + network_mode: ${NETWORK_MODE:-bridge} + environment: + - OLLAMA_HOST=ollama + - INFERENCE_MODEL=${INFERENCE_MODEL} + - SAFETY_MODEL=${SAFETY_MODEL:-} + volumes: + - ~/.ollama:/root/.ollama + - ./pull-models.sh:/pull-models.sh + entrypoint: ["/pull-models.sh"] + llamastack: depends_on: - - ollama - image: llamastack/distribution-ollama - network_mode: "host" + ollama: + condition: service_started + ollama-init: + condition: service_started + image: ${LLAMA_STACK_IMAGE:-llamastack/distribution-ollama} + network_mode: ${NETWORK_MODE:-bridge} volumes: - ~/.llama:/root/.llama # Link to ollama run.yaml file - - ./run.yaml:/root/my-run.yaml + - ~/local/llama-stack/:/app/llama-stack-source + - ./run${SAFETY_MODEL:+-with-safety}.yaml:/root/my-run.yaml ports: - - "5000:5000" - # Hack: wait for ollama server to start before starting docker - entrypoint: bash -c "sleep 60; python -m llama_stack.distribution.server.server --yaml_config /root/my-run.yaml" + - "${LLAMA_STACK_PORT:-5001}:${LLAMA_STACK_PORT:-5001}" + environment: + - INFERENCE_MODEL=${INFERENCE_MODEL} + - SAFETY_MODEL=${SAFETY_MODEL:-} + - OLLAMA_URL=http://ollama:11434 + entrypoint: > + python -m llama_stack.distribution.server.server /root/my-run.yaml \ + --port ${LLAMA_STACK_PORT:-5001} deploy: restart_policy: condition: on-failure - delay: 3s - max_attempts: 5 + delay: 10s + max_attempts: 3 window: 60s volumes: ollama: + ollama-init: + llamastack: diff --git a/distributions/ollama/pull-models.sh b/distributions/ollama/pull-models.sh new file mode 100755 index 000000000..fb5bf8a4a --- /dev/null +++ b/distributions/ollama/pull-models.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +echo "Preloading (${INFERENCE_MODEL}, ${SAFETY_MODEL})..." +for model in ${INFERENCE_MODEL} ${SAFETY_MODEL}; do + echo "Preloading $model..." + if ! ollama run "$model"; then + echo "Failed to pull and run $model" + exit 1 + fi +done + +echo "All models pulled successfully" diff --git a/distributions/ollama/run-with-safety.yaml b/distributions/ollama/run-with-safety.yaml new file mode 120000 index 000000000..5695b49e7 --- /dev/null +++ b/distributions/ollama/run-with-safety.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/ollama/run-with-safety.yaml \ No newline at end of file diff --git a/distributions/ollama/run.yaml b/distributions/ollama/run.yaml deleted file mode 100644 index c702b878e..000000000 --- a/distributions/ollama/run.yaml +++ /dev/null @@ -1,45 +0,0 @@ -version: '2' -built_at: '2024-10-08T17:40:45.325529' -image_name: local -docker_image: null -conda_env: local -apis: -- shields -- agents -- models -- memory -- memory_banks -- inference -- safety -providers: - inference: - - provider_id: ollama0 - provider_type: remote::ollama - config: - url: http://127.0.0.1:14343 - safety: - - provider_id: meta0 - provider_type: inline::llama-guard - config: - model: Llama-Guard-3-1B - excluded_categories: [] - - provider_id: meta1 - provider_type: inline::prompt-guard - config: - model: Prompt-Guard-86M - memory: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} - agents: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - persistence_store: - namespace: null - type: sqlite - db_path: ~/.llama/runtime/kvstore.db - telemetry: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} diff --git a/distributions/ollama/run.yaml b/distributions/ollama/run.yaml new file mode 120000 index 000000000..b008b1bf4 --- /dev/null +++ b/distributions/ollama/run.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/ollama/run.yaml \ No newline at end of file diff --git a/distributions/remote-vllm/compose.yaml b/distributions/remote-vllm/compose.yaml index 90d58a2af..09701e099 100644 --- a/distributions/remote-vllm/compose.yaml +++ b/distributions/remote-vllm/compose.yaml @@ -1,33 +1,28 @@ -# NOTES: -# -# This Docker Compose (and the associated run.yaml) assumes you will be -# running in the default "bridged" network mode. -# -# If you need "host" network mode, please uncomment -# - network_mode: "host" -# -# Similarly change "host.docker.internal" to "localhost" in the run.yaml file -# services: - vllm-0: + vllm-inference: image: vllm/vllm-openai:latest volumes: - $HOME/.cache/huggingface:/root/.cache/huggingface - # network_mode: "host" + network_mode: ${NETWORK_MODE:-bridged} ports: - - "5100:5100" + - "${VLLM_INFERENCE_PORT:-5100}:${VLLM_INFERENCE_PORT:-5100}" devices: - nvidia.com/gpu=all environment: - - CUDA_VISIBLE_DEVICES=0 + - CUDA_VISIBLE_DEVICES=${VLLM_INFERENCE_GPU:-0} - HUGGING_FACE_HUB_TOKEN=$HF_TOKEN command: > --gpu-memory-utilization 0.75 - --model meta-llama/Llama-3.1-8B-Instruct + --model ${VLLM_INFERENCE_MODEL:-meta-llama/Llama-3.2-3B-Instruct} --enforce-eager --max-model-len 8192 --max-num-seqs 16 - --port 5100 + --port ${VLLM_INFERENCE_PORT:-5100} + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:${VLLM_INFERENCE_PORT:-5100}/v1/health"] + interval: 30s + timeout: 10s + retries: 5 deploy: resources: reservations: @@ -35,25 +30,34 @@ services: - driver: nvidia capabilities: [gpu] runtime: nvidia - vllm-1: + + # A little trick: + # if VLLM_SAFETY_MODEL is set, we will create a service for the safety model + # otherwise, the entry will end in a hyphen which gets ignored by docker compose + vllm-${VLLM_SAFETY_MODEL:+safety}: image: vllm/vllm-openai:latest volumes: - $HOME/.cache/huggingface:/root/.cache/huggingface - # network_mode: "host" + network_mode: ${NETWORK_MODE:-bridged} ports: - - "5101:5101" + - "${VLLM_SAFETY_PORT:-5101}:${VLLM_SAFETY_PORT:-5101}" devices: - nvidia.com/gpu=all environment: - - CUDA_VISIBLE_DEVICES=1 + - CUDA_VISIBLE_DEVICES=${VLLM_SAFETY_GPU:-1} - HUGGING_FACE_HUB_TOKEN=$HF_TOKEN command: > --gpu-memory-utilization 0.75 - --model meta-llama/Llama-Guard-3-1B + --model ${VLLM_SAFETY_MODEL} --enforce-eager --max-model-len 8192 --max-num-seqs 16 - --port 5101 + --port ${VLLM_SAFETY_PORT:-5101} + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:${VLLM_SAFETY_PORT:-5101}/v1/health"] + interval: 30s + timeout: 10s + retries: 5 deploy: resources: reservations: @@ -63,23 +67,25 @@ services: runtime: nvidia llamastack: depends_on: - - vllm-0 - - vllm-1 - # image: llamastack/distribution-remote-vllm + - vllm-inference: + condition: service_healthy + - vllm-${VLLM_SAFETY_MODEL:+safety}: + condition: service_healthy + # image: llamastack/distribution-remote-vllm image: llamastack/distribution-remote-vllm:test-0.0.52rc3 volumes: - ~/.llama:/root/.llama - - ~/local/llama-stack/distributions/remote-vllm/run.yaml:/root/llamastack-run-remote-vllm.yaml - # network_mode: "host" + - ./run${VLLM_SAFETY_MODEL:+-with-safety}.yaml:/root/llamastack-run-remote-vllm.yaml + network_mode: ${NETWORK_MODE:-bridged} environment: - - LLAMA_INFERENCE_VLLM_URL=${LLAMA_INFERENCE_VLLM_URL:-http://host.docker.internal:5100/v1} - - LLAMA_INFERENCE_MODEL=${LLAMA_INFERENCE_MODEL:-Llama3.1-8B-Instruct} + - VLLM_URL=http://vllm-inference:${VLLM_INFERENCE_PORT:-5100}/v1 + - VLLM_SAFETY_URL=http://vllm-safety:${VLLM_SAFETY_PORT:-5101}/v1 + - INFERENCE_MODEL=${INFERENCE_MODEL:-meta-llama/Llama-3.2-3B-Instruct} - MAX_TOKENS=${MAX_TOKENS:-4096} - SQLITE_STORE_DIR=${SQLITE_STORE_DIR:-$HOME/.llama/distributions/remote-vllm} - - LLAMA_SAFETY_VLLM_URL=${LLAMA_SAFETY_VLLM_URL:-http://host.docker.internal:5101/v1} - - LLAMA_SAFETY_MODEL=${LLAMA_SAFETY_MODEL:-Llama-Guard-3-1B} + - SAFETY_MODEL=${SAFETY_MODEL:-meta-llama/Llama-Guard-3-1B} ports: - - "5001:5001" + - "${LLAMASTACK_PORT:-5001}:${LLAMASTACK_PORT:-5001}" # Hack: wait for vLLM server to start before starting docker entrypoint: bash -c "sleep 60; python -m llama_stack.distribution.server.server --yaml_config /root/llamastack-run-remote-vllm.yaml --port 5001" deploy: @@ -89,6 +95,6 @@ services: max_attempts: 5 window: 60s volumes: - vllm-0: - vllm-1: + vllm-inference: + vllm-safety: llamastack: diff --git a/distributions/remote-vllm/run-with-safety.yaml b/distributions/remote-vllm/run-with-safety.yaml new file mode 120000 index 000000000..b2c3c36da --- /dev/null +++ b/distributions/remote-vllm/run-with-safety.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/remote-vllm/run-with-safety.yaml \ No newline at end of file diff --git a/distributions/remote-vllm/run.yaml b/distributions/remote-vllm/run.yaml deleted file mode 100644 index eae5b8a6f..000000000 --- a/distributions/remote-vllm/run.yaml +++ /dev/null @@ -1,68 +0,0 @@ -version: '2' -built_at: '2024-11-11T20:09:45.988375' -image_name: remote-vllm -docker_image: remote-vllm -conda_env: null -apis: -- inference -- memory -- safety -- agents -- telemetry -providers: - inference: - # serves main inference model - - provider_id: vllm-0 - provider_type: remote::vllm - config: - # NOTE: replace with "localhost" if you are running in "host" network mode - url: ${env.LLAMA_INFERENCE_VLLM_URL:http://host.docker.internal:5100/v1} - max_tokens: ${env.MAX_TOKENS:4096} - api_token: fake - # serves safety llama_guard model - - provider_id: vllm-1 - provider_type: remote::vllm - config: - # NOTE: replace with "localhost" if you are running in "host" network mode - url: ${env.LLAMA_SAFETY_VLLM_URL:http://host.docker.internal:5101/v1} - max_tokens: ${env.MAX_TOKENS:4096} - api_token: fake - memory: - - provider_id: faiss-0 - provider_type: inline::faiss - config: - kvstore: - namespace: null - type: sqlite - db_path: "${env.SQLITE_STORE_DIR:/home/ashwin/.llama/distributions/remote-vllm}/faiss_store.db" - safety: - - provider_id: llama-guard - provider_type: inline::llama-guard - config: {} - memory: - - provider_id: meta0 - provider_type: inline::faiss - config: {} - agents: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - persistence_store: - namespace: null - type: sqlite - db_path: "${env.SQLITE_STORE_DIR:/home/ashwin/.llama/distributions/remote-vllm}/agents_store.db" - telemetry: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} -metadata_store: - namespace: null - type: sqlite - db_path: "${env.SQLITE_STORE_DIR:/home/ashwin/.llama/distributions/remote-vllm}/registry.db" -models: - - model_id: ${env.LLAMA_INFERENCE_MODEL:Llama3.1-8B-Instruct} - provider_id: vllm-0 - - model_id: ${env.LLAMA_SAFETY_MODEL:Llama-Guard-3-1B} - provider_id: vllm-1 -shields: - - shield_id: ${env.LLAMA_SAFETY_MODEL:Llama-Guard-3-1B} diff --git a/distributions/remote-vllm/run.yaml b/distributions/remote-vllm/run.yaml new file mode 120000 index 000000000..ac70c0e6a --- /dev/null +++ b/distributions/remote-vllm/run.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/remote-vllm/run.yaml \ No newline at end of file diff --git a/distributions/tgi/compose.yaml b/distributions/tgi/compose.yaml index bea7eb907..753b7880b 100644 --- a/distributions/tgi/compose.yaml +++ b/distributions/tgi/compose.yaml @@ -1,51 +1,89 @@ services: - text-generation-inference: + tgi-inference: image: ghcr.io/huggingface/text-generation-inference:latest - network_mode: "host" volumes: - $HOME/.cache/huggingface:/data + network_mode: ${NETWORK_MODE:-bridged} ports: - - "5009:5009" + - "${TGI_INFERENCE_PORT:-8080}:${TGI_INFERENCE_PORT:-8080}" devices: - nvidia.com/gpu=all environment: - - CUDA_VISIBLE_DEVICES=0 + - CUDA_VISIBLE_DEVICES=${TGI_INFERENCE_GPU:-0} + - HF_TOKEN=$HF_TOKEN - HF_HOME=/data - HF_DATASETS_CACHE=/data - HF_MODULES_CACHE=/data - HF_HUB_CACHE=/data - command: ["--dtype", "bfloat16", "--usage-stats", "on", "--sharded", "false", "--model-id", "meta-llama/Llama-3.1-8B-Instruct", "--port", "5009", "--cuda-memory-fraction", "0.3"] + command: > + --dtype bfloat16 + --usage-stats off + --sharded false + --model-id ${TGI_INFERENCE_MODEL:-meta-llama/Llama-3.2-3B-Instruct} + --port ${TGI_INFERENCE_PORT:-8080} + --cuda-memory-fraction 0.75 + healthcheck: + test: ["CMD", "curl", "-f", "http://tgi-inference:${TGI_INFERENCE_PORT:-8080}/health"] + interval: 5s + timeout: 5s + retries: 30 deploy: resources: reservations: devices: - driver: nvidia - # that's the closest analogue to --gpus; provide - # an integer amount of devices or 'all' - count: 1 - # Devices are reserved using a list of capabilities, making - # capabilities the only required field. A device MUST - # satisfy all the requested capabilities for a successful - # reservation. capabilities: [gpu] runtime: nvidia + + tgi-${TGI_SAFETY_MODEL:+safety}: + image: ghcr.io/huggingface/text-generation-inference:latest + volumes: + - $HOME/.cache/huggingface:/data + network_mode: ${NETWORK_MODE:-bridged} + ports: + - "${TGI_SAFETY_PORT:-8081}:${TGI_SAFETY_PORT:-8081}" + devices: + - nvidia.com/gpu=all + environment: + - CUDA_VISIBLE_DEVICES=${TGI_SAFETY_GPU:-1} + - HF_TOKEN=$HF_TOKEN + - HF_HOME=/data + - HF_DATASETS_CACHE=/data + - HF_MODULES_CACHE=/data + - HF_HUB_CACHE=/data + command: > + --dtype bfloat16 + --usage-stats off + --sharded false + --model-id ${TGI_SAFETY_MODEL:-meta-llama/Llama-Guard-3-1B} + --port ${TGI_SAFETY_PORT:-8081} + --cuda-memory-fraction 0.75 healthcheck: - test: ["CMD", "curl", "-f", "http://text-generation-inference:5009/health"] + test: ["CMD", "curl", "-f", "http://tgi-safety:${TGI_SAFETY_PORT:-8081}/health"] interval: 5s timeout: 5s retries: 30 + deploy: + resources: + reservations: + devices: + - driver: nvidia + capabilities: [gpu] + runtime: nvidia + llamastack: depends_on: - text-generation-inference: + tgi-inference: condition: service_healthy - image: llamastack/distribution-tgi - network_mode: "host" + tgi-${TGI_SAFETY_MODEL:+safety}: + condition: service_healthy + image: llamastack/distribution-tgi:test-0.0.52rc3 + network_mode: ${NETWORK_MODE:-bridged} volumes: - ~/.llama:/root/.llama - # Link to TGI run.yaml file - - ./run.yaml:/root/my-run.yaml + - ./run${TGI_SAFETY_MODEL:+-with-safety}.yaml:/root/my-run.yaml ports: - - "5000:5000" + - "${LLAMA_STACK_PORT:-5001}:${LLAMA_STACK_PORT:-5001}" # Hack: wait for TGI server to start before starting docker entrypoint: bash -c "sleep 60; python -m llama_stack.distribution.server.server --yaml_config /root/my-run.yaml" restart_policy: @@ -53,3 +91,13 @@ services: delay: 3s max_attempts: 5 window: 60s + environment: + - TGI_URL=http://tgi-inference:${TGI_INFERENCE_PORT:-8080} + - SAFETY_TGI_URL=http://tgi-safety:${TGI_SAFETY_PORT:-8081} + - INFERENCE_MODEL=${INFERENCE_MODEL:-meta-llama/Llama-3.2-3B-Instruct} + - SAFETY_MODEL=${SAFETY_MODEL:-meta-llama/Llama-Guard-3-1B} + +volumes: + tgi-inference: + tgi-safety: + llamastack: diff --git a/distributions/tgi/run-with-safety.yaml b/distributions/tgi/run-with-safety.yaml new file mode 120000 index 000000000..62d26708e --- /dev/null +++ b/distributions/tgi/run-with-safety.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/tgi/run-with-safety.yaml \ No newline at end of file diff --git a/distributions/tgi/run.yaml b/distributions/tgi/run.yaml deleted file mode 100644 index 84ec536f8..000000000 --- a/distributions/tgi/run.yaml +++ /dev/null @@ -1,45 +0,0 @@ -version: '2' -built_at: '2024-10-08T17:40:45.325529' -image_name: local -docker_image: null -conda_env: local -apis: -- shields -- agents -- models -- memory -- memory_banks -- inference -- safety -providers: - inference: - - provider_id: tgi0 - provider_type: remote::tgi - config: - url: http://127.0.0.1:5009 - safety: - - provider_id: meta0 - provider_type: inline::llama-guard - config: - model: Llama-Guard-3-1B - excluded_categories: [] - - provider_id: meta1 - provider_type: inline::prompt-guard - config: - model: Prompt-Guard-86M - memory: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} - agents: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - persistence_store: - namespace: null - type: sqlite - db_path: ~/.llama/runtime/kvstore.db - telemetry: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} diff --git a/distributions/tgi/run.yaml b/distributions/tgi/run.yaml new file mode 120000 index 000000000..f3cc3a502 --- /dev/null +++ b/distributions/tgi/run.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/tgi/run.yaml \ No newline at end of file diff --git a/distributions/together/run.yaml b/distributions/together/run.yaml deleted file mode 100644 index 142316a8d..000000000 --- a/distributions/together/run.yaml +++ /dev/null @@ -1,46 +0,0 @@ -version: '2' -built_at: '2024-10-08T17:40:45.325529' -image_name: local -docker_image: null -conda_env: local -apis: -- shields -- agents -- models -- memory -- memory_banks -- inference -- safety -providers: - inference: - - provider_id: together0 - provider_type: remote::together - config: - url: https://api.together.xyz/v1 - # api_key: - safety: - - provider_id: meta0 - provider_type: inline::llama-guard - config: - model: Llama-Guard-3-1B - excluded_categories: [] - - provider_id: meta1 - provider_type: inline::prompt-guard - config: - model: Prompt-Guard-86M - memory: - - provider_id: meta0 - provider_type: remote::weaviate - config: {} - agents: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - persistence_store: - namespace: null - type: sqlite - db_path: ~/.llama/runtime/kvstore.db - telemetry: - - provider_id: meta0 - provider_type: inline::meta-reference - config: {} diff --git a/distributions/together/run.yaml b/distributions/together/run.yaml new file mode 120000 index 000000000..102d9866e --- /dev/null +++ b/distributions/together/run.yaml @@ -0,0 +1 @@ +../../llama_stack/templates/together/run.yaml \ No newline at end of file diff --git a/docs/source/getting_started/distributions/self_hosted_distro/fireworks.md b/docs/source/getting_started/distributions/self_hosted_distro/fireworks.md index ee46cd18d..03ee9e604 100644 --- a/docs/source/getting_started/distributions/self_hosted_distro/fireworks.md +++ b/docs/source/getting_started/distributions/self_hosted_distro/fireworks.md @@ -2,63 +2,67 @@ The `llamastack/distribution-fireworks` distribution consists of the following provider configurations. +| API | Provider(s) | +|-----|-------------| +| agents | `inline::meta-reference` | +| inference | `remote::fireworks` | +| memory | `inline::faiss`, `remote::chromadb`, `remote::pgvector` | +| safety | `inline::llama-guard` | +| telemetry | `inline::meta-reference` | -| **API** | **Inference** | **Agents** | **Memory** | **Safety** | **Telemetry** | -|----------------- |--------------- |---------------- |-------------------------------------------------- |---------------- |---------------- | -| **Provider(s)** | remote::fireworks | meta-reference | meta-reference | meta-reference | meta-reference | -### Step 0. Prerequisite -- Make sure you have access to a fireworks API Key. You can get one by visiting [fireworks.ai](https://fireworks.ai/) +### Environment Variables -### Step 1. Start the Distribution (Single Node CPU) +The following environment variables can be configured: -#### (Option 1) Start Distribution Via Docker -> [!NOTE] -> This assumes you have an hosted endpoint at Fireworks with API Key. +- `LLAMASTACK_PORT`: Port for the Llama Stack distribution server (default: `5001`) +- `FIREWORKS_API_KEY`: Fireworks.AI API Key (default: ``) -``` -$ cd distributions/fireworks && docker compose up +### Models + +The following models are available by default: + +- `fireworks/llama-v3p1-8b-instruct` +- `fireworks/llama-v3p1-70b-instruct` +- `fireworks/llama-v3p1-405b-instruct` +- `fireworks/llama-v3p2-1b-instruct` +- `fireworks/llama-v3p2-3b-instruct` +- `fireworks/llama-v3p2-11b-vision-instruct` +- `fireworks/llama-v3p2-90b-vision-instruct` +- `fireworks/llama-guard-3-8b` +- `fireworks/llama-guard-3-11b-vision` + + +### Prerequisite: API Keys + +Make sure you have access to a Fireworks API Key. You can get one by visiting [fireworks.ai](https://fireworks.ai/). + + +## Running Llama Stack with Fireworks + +You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-fireworks \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env FIREWORKS_API_KEY=$FIREWORKS_API_KEY ``` -Make sure in you `run.yaml` file, you inference provider is pointing to the correct Fireworks URL server endpoint. E.g. -``` -inference: - - provider_id: fireworks - provider_type: remote::fireworks - config: - url: https://api.fireworks.ai/inference - api_key: -``` - -#### (Option 2) Start Distribution Via Conda +### Via Conda ```bash llama stack build --template fireworks --image-type conda -# -- modify run.yaml to a valid Fireworks server endpoint -llama stack run ./run.yaml -``` - - -### (Optional) Model Serving - -Use `llama-stack-client models list` to check the available models served by Fireworks. -``` -$ llama-stack-client models list -+------------------------------+------------------------------+---------------+------------+ -| identifier | llama_model | provider_id | metadata | -+==============================+==============================+===============+============+ -| Llama3.1-8B-Instruct | Llama3.1-8B-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.1-70B-Instruct | Llama3.1-70B-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.1-405B-Instruct | Llama3.1-405B-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-1B-Instruct | Llama3.2-1B-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-3B-Instruct | Llama3.2-3B-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-11B-Vision-Instruct | Llama3.2-11B-Vision-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-90B-Vision-Instruct | Llama3.2-90B-Vision-Instruct | fireworks0 | {} | -+------------------------------+------------------------------+---------------+------------+ +llama stack run ./run.yaml \ + --port 5001 \ + --env FIREWORKS_API_KEY=$FIREWORKS_API_KEY ``` diff --git a/docs/source/getting_started/distributions/self_hosted_distro/meta-reference-gpu.md b/docs/source/getting_started/distributions/self_hosted_distro/meta-reference-gpu.md index 1d5842c07..a0add3858 100644 --- a/docs/source/getting_started/distributions/self_hosted_distro/meta-reference-gpu.md +++ b/docs/source/getting_started/distributions/self_hosted_distro/meta-reference-gpu.md @@ -1,15 +1,23 @@ # Meta Reference Distribution -The `llamastack/distribution-meta-reference-gpu` distribution consists of the following provider configurations. +The `llamastack/distribution-meta-reference-gpu` distribution consists of the following provider configurations: + +| API | Provider(s) | +|-----|-------------| +| agents | `inline::meta-reference` | +| inference | `inline::meta-reference` | +| memory | `inline::faiss`, `remote::chromadb`, `remote::pgvector` | +| safety | `inline::llama-guard` | +| telemetry | `inline::meta-reference` | -| **API** | **Inference** | **Agents** | **Memory** | **Safety** | **Telemetry** | -|----------------- |--------------- |---------------- |-------------------------------------------------- |---------------- |---------------- | -| **Provider(s)** | meta-reference | meta-reference | meta-reference, remote::pgvector, remote::chroma | meta-reference | meta-reference | +Note that you need access to nvidia GPUs to run this distribution. This distribution is not compatible with CPU-only machines or machines with AMD GPUs. -### Step 0. Prerequisite - Downloading Models -Please make sure you have llama model checkpoints downloaded in `~/.llama` before proceeding. See [installation guide](https://llama-stack.readthedocs.io/en/latest/cli_reference/download_models.html) here to download the models. + +## Prerequisite: Downloading Models + +Please make sure you have llama model checkpoints downloaded in `~/.llama` before proceeding. See [installation guide](https://llama-stack.readthedocs.io/en/latest/cli_reference/download_models.html) here to download the models. Run `llama model list` to see the available models to download, and `llama model download` to download the checkpoints. ``` $ ls ~/.llama/checkpoints @@ -17,55 +25,56 @@ Llama3.1-8B Llama3.2-11B-Vision-Instruct Llama3.2-1B-Instruct Llama3 Llama3.1-8B-Instruct Llama3.2-1B Llama3.2-3B-Instruct Llama-Guard-3-1B Prompt-Guard-86M ``` -### Step 1. Start the Distribution +## Running the Distribution -#### (Option 1) Start with Docker -``` -$ cd distributions/meta-reference-gpu && docker compose up +You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-meta-reference-gpu \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct ``` -> [!NOTE] -> This assumes you have access to GPU to start a local server with access to your GPU. +If you are using Llama Stack Safety / Shield APIs, use: - -> [!NOTE] -> `~/.llama` should be the path containing downloaded weights of Llama models. - - -This will download and start running a pre-built docker container. Alternatively, you may use the following commands: - -``` -docker run -it -p 5000:5000 -v ~/.llama:/root/.llama -v ./run.yaml:/root/my-run.yaml --gpus=all distribution-meta-reference-gpu --yaml_config /root/my-run.yaml +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + llamastack/distribution-meta-reference-gpu \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct \ + --env SAFETY_MODEL=meta-llama/Llama-Guard-3-1B ``` -#### (Option 2) Start with Conda +### Via Conda -1. Install the `llama` CLI. See [CLI Reference](https://llama-stack.readthedocs.io/en/latest/cli_reference/index.html) +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. -2. Build the `meta-reference-gpu` distribution - -``` -$ llama stack build --template meta-reference-gpu --image-type conda +```bash +llama stack build --template meta-reference-gpu --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct ``` -3. Start running distribution -``` -$ cd distributions/meta-reference-gpu -$ llama stack run ./run.yaml -``` +If you are using Llama Stack Safety / Shield APIs, use: -### (Optional) Serving a new model -You may change the `config.model` in `run.yaml` to update the model currently being served by the distribution. Make sure you have the model checkpoint downloaded in your `~/.llama`. +```bash +llama stack run ./run-with-safety.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct \ + --env SAFETY_MODEL=meta-llama/Llama-Guard-3-1B ``` -inference: - - provider_id: meta0 - provider_type: inline::meta-reference - config: - model: Llama3.2-11B-Vision-Instruct - quantization: null - torch_seed: null - max_seq_len: 4096 - max_batch_size: 1 -``` - -Run `llama model list` to see the available models to download, and `llama model download` to download the checkpoints. diff --git a/docs/source/getting_started/distributions/self_hosted_distro/ollama.md b/docs/source/getting_started/distributions/self_hosted_distro/ollama.md index 37bef9536..0acee3198 100644 --- a/docs/source/getting_started/distributions/self_hosted_distro/ollama.md +++ b/docs/source/getting_started/distributions/self_hosted_distro/ollama.md @@ -2,103 +2,106 @@ The `llamastack/distribution-ollama` distribution consists of the following provider configurations. -| **API** | **Inference** | **Agents** | **Memory** | **Safety** | **Telemetry** | -|----------------- |---------------- |---------------- |------------------------------------ |---------------- |---------------- | -| **Provider(s)** | remote::ollama | meta-reference | remote::pgvector, remote::chromadb | meta-reference | meta-reference | +| API | Provider(s) | +|-----|-------------| +| agents | `inline::meta-reference` | +| inference | `remote::ollama` | +| memory | `inline::faiss`, `remote::chromadb`, `remote::pgvector` | +| safety | `inline::llama-guard` | +| telemetry | `inline::meta-reference` | -## Using Docker Compose +You should use this distribution if you have a regular desktop machine without very powerful GPUs. Of course, if you have powerful GPUs, you can still continue using this distribution since Ollama supports GPU acceleration. -You can use `docker compose` to start a Ollama server and connect with Llama Stack server in a single command. +## Setting up Ollama server -### Docker: Start the Distribution (Single Node regular Desktop machine) +Please check the [Ollama Documentation](https://github.com/ollama/ollama) on how to install and run Ollama. After installing Ollama, you need to run `ollama serve` to start the server. -> [!NOTE] -> This will start an ollama server with CPU only, please see [Ollama Documentations](https://github.com/ollama/ollama) for serving models on CPU only. +In order to load models, you can run: ```bash -$ cd distributions/ollama; docker compose up +export INFERENCE_MODEL="meta-llama/Llama-3.2-3B-Instruct" + +# ollama names this model differently, and we must use the ollama name when loading the model +export OLLAMA_INFERENCE_MODEL="llama3.2:3b-instruct-fp16" +ollama run $OLLAMA_INFERENCE_MODEL --keepalive 60m ``` -### Docker: Start a Distribution (Single Node with nvidia GPUs) - -> [!NOTE] -> This assumes you have access to GPU to start a Ollama server with access to your GPU. +If you are using Llama Stack Safety / Shield APIs, you will also need to pull and run the safety model. ```bash -$ cd distributions/ollama-gpu; docker compose up +export SAFETY_MODEL="meta-llama/Llama-Guard-3-1B" + +# ollama names this model differently, and we must use the ollama name when loading the model +export OLLAMA_SAFETY_MODEL="llama-guard3:1b" +ollama run $OLLAMA_SAFETY_MODEL --keepalive 60m ``` -You will see outputs similar to following --- +## Running Llama Stack + +Now you are ready to run Llama Stack with Ollama as the inference provider. You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + ```bash -[ollama] | [GIN] 2024/10/18 - 21:19:41 | 200 | 226.841µs | ::1 | GET "/api/ps" -[ollama] | [GIN] 2024/10/18 - 21:19:42 | 200 | 60.908µs | ::1 | GET "/api/ps" -INFO: Started server process [1] -INFO: Waiting for application startup. -INFO: Application startup complete. -INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) -[llamastack] | Resolved 12 providers -[llamastack] | inner-inference => ollama0 -[llamastack] | models => __routing_table__ -[llamastack] | inference => __autorouted__ +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ~/.llama:/root/.llama \ + -v ./run.yaml:/root/my-run.yaml \ + --gpus=all \ + llamastack/distribution-ollama \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env OLLAMA_URL=http://host.docker.internal:11434 ``` -To kill the server +If you are using Llama Stack Safety / Shield APIs, use: + ```bash -docker compose down +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ~/.llama:/root/.llama \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + --gpus=all \ + llamastack/distribution-ollama \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env OLLAMA_URL=http://host.docker.internal:11434 ``` -## Starting Ollama and Llama Stack separately +### Via Conda -If you wish to separately spin up a Ollama server, and connect with Llama Stack, you should use the following commands. - -#### Start Ollama server -- Please check the [Ollama Documentation](https://github.com/ollama/ollama) for more details. - -**Via Docker** -```bash -docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama -``` - -**Via CLI** -```bash -ollama run -``` - -#### Start Llama Stack server pointing to Ollama server - -**Via Conda** +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. ```bash llama stack build --template ollama --image-type conda -llama stack run ./gpu/run.yaml +llama stack run ./run.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env OLLAMA_URL=http://127.0.0.1:11434 ``` -**Via Docker** -``` -docker run --network host -it -p 5000:5000 -v ~/.llama:/root/.llama -v ./gpu/run.yaml:/root/llamastack-run-ollama.yaml --gpus=all llamastack/distribution-ollama --yaml_config /root/llamastack-run-ollama.yaml -``` - -Make sure in your `run.yaml` file, your inference provider is pointing to the correct Ollama endpoint. E.g. -```yaml -inference: - - provider_id: ollama0 - provider_type: remote::ollama - config: - url: http://127.0.0.1:14343 -``` - -### (Optional) Update Model Serving Configuration - -#### Downloading model via Ollama - -You can use ollama for managing model downloads. +If you are using Llama Stack Safety / Shield APIs, use: ```bash -ollama pull llama3.1:8b-instruct-fp16 -ollama pull llama3.1:70b-instruct-fp16 +llama stack run ./run-with-safety.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env OLLAMA_URL=http://127.0.0.1:11434 ``` + +### (Optional) Update Model Serving Configuration + > [!NOTE] > Please check the [OLLAMA_SUPPORTED_MODELS](https://github.com/meta-llama/llama-stack/blob/main/llama_stack/providers.remote/inference/ollama/ollama.py) for the supported Ollama models. diff --git a/docs/source/getting_started/distributions/self_hosted_distro/remote-vllm.md b/docs/source/getting_started/distributions/self_hosted_distro/remote-vllm.md new file mode 100644 index 000000000..c9f8d6167 --- /dev/null +++ b/docs/source/getting_started/distributions/self_hosted_distro/remote-vllm.md @@ -0,0 +1,117 @@ +# Remote vLLM Distribution + +The `llamastack/distribution-remote-vllm` distribution consists of the following provider configurations: + +| API | Provider(s) | +|-----|-------------| +| agents | `inline::meta-reference` | +| inference | `remote::vllm` | +| memory | `inline::faiss`, `remote::chromadb`, `remote::pgvector` | +| safety | `inline::llama-guard` | +| telemetry | `inline::meta-reference` | + + +You can use this distribution if you have GPUs and want to run an independent vLLM server container for running inference. + + + +## Setting up vLLM server + +Please check the [vLLM Documentation](https://docs.vllm.ai/en/v0.5.5/serving/deploying_with_docker.html) to get a vLLM endpoint. Here is a sample script to start a vLLM server locally via Docker: + +```bash +export INFERENCE_PORT=8000 +export INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct +export CUDA_VISIBLE_DEVICES=0 + +docker run \ + --runtime nvidia \ + --gpus $CUDA_VISIBLE_DEVICES \ + -v ~/.cache/huggingface:/root/.cache/huggingface \ + --env "HUGGING_FACE_HUB_TOKEN=$HF_TOKEN" \ + -p $INFERENCE_PORT:$INFERENCE_PORT \ + --ipc=host \ + vllm/vllm-openai:latest \ + --model $INFERENCE_MODEL \ + --port $INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, then you will need to also run another instance of a vLLM with a corresponding safety model like `meta-llama/Llama-Guard-3-1B` using a script like: + +```bash +export SAFETY_PORT=8081 +export SAFETY_MODEL=meta-llama/Llama-Guard-3-1B +export CUDA_VISIBLE_DEVICES=1 + +docker run \ + --runtime nvidia \ + --gpus $CUDA_VISIBLE_DEVICES \ + -v ~/.cache/huggingface:/root/.cache/huggingface \ + --env "HUGGING_FACE_HUB_TOKEN=$HF_TOKEN" \ + -p $SAFETY_PORT:$SAFETY_PORT \ + --ipc=host \ + vllm/vllm-openai:latest \ + --model $SAFETY_MODEL \ + --port $SAFETY_PORT +``` + +## Running Llama Stack + +Now you are ready to run Llama Stack with vLLM as the inference provider. You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-remote-vllm \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://host.docker.internal:$INFERENCE_PORT \ +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + llamastack/distribution-remote-vllm \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://host.docker.internal:$INFERENCE_PORT \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env VLLM_SAFETY_URL=http://host.docker.internal:$SAFETY_PORT +``` + + +### Via Conda + +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. + +```bash +llama stack build --template remote-vllm --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://127.0.0.1:$INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +llama stack run ./run-with-safety.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://127.0.0.1:$INFERENCE_PORT \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env VLLM_SAFETY_URL=http://127.0.0.1:$SAFETY_PORT +``` diff --git a/docs/source/getting_started/distributions/self_hosted_distro/remote_vllm.md b/docs/source/getting_started/distributions/self_hosted_distro/remote_vllm.md deleted file mode 100644 index 2ab8df7b7..000000000 --- a/docs/source/getting_started/distributions/self_hosted_distro/remote_vllm.md +++ /dev/null @@ -1,83 +0,0 @@ -# Remote vLLM Distribution - -The `llamastack/distribution-remote-vllm` distribution consists of the following provider configurations. - -| **API** | **Inference** | **Agents** | **Memory** | **Safety** | **Telemetry** | -|----------------- |---------------- |---------------- |------------------------------------ |---------------- |---------------- | -| **Provider(s)** | remote::vllm | meta-reference | remote::pgvector, remote::chromadb | meta-reference | meta-reference | - -You can use this distribution if you have GPUs and want to run an independent vLLM server container for running inference. - -## Using Docker Compose - -You can use `docker compose` to start a vLLM container and Llama Stack server container together. - -> [!NOTE] -> This assumes you have access to GPU to start a vLLM server with access to your GPU. - -```bash -$ cd distributions/remote-vllm; docker compose up -``` - -You will see outputs similar to following --- -``` - -``` - -To kill the server -```bash -docker compose down -``` - -## Starting vLLM and Llama Stack separately - -You may want to start a vLLM server and connect with Llama Stack manually. There are two ways to start a vLLM server and connect with Llama Stack. - - -#### Start vLLM server. - -```bash -docker run --runtime nvidia --gpus all \ - -v ~/.cache/huggingface:/root/.cache/huggingface \ - --env "HUGGING_FACE_HUB_TOKEN=" \ - -p 8000:8000 \ - --ipc=host \ - vllm/vllm-openai:latest \ - --model meta-llama/Llama-3.1-8B-Instruct -``` - -Please check the [vLLM Documentation](https://docs.vllm.ai/en/v0.5.5/serving/deploying_with_docker.html) for more details. - - -#### Start Llama Stack server pointing to your vLLM server - - -We have provided a template `run.yaml` file in the `distributions/remote-vllm` directory. Please make sure to modify the `inference.provider_id` to point to your vLLM server endpoint. As an example, if your vLLM server is running on `http://127.0.0.1:8000`, your `run.yaml` file should look like the following: -```yaml -inference: - - provider_id: vllm0 - provider_type: remote::vllm - config: - url: http://127.0.0.1:8000 -``` - -**Via Conda** - -If you are using Conda, you can build and run the Llama Stack server with the following commands: -```bash -cd distributions/remote-vllm -llama stack build --template remote_vllm --image-type conda -llama stack run run.yaml -``` - -**Via Docker** - -You can use the Llama Stack Docker image to start the server with the following command: -```bash -docker run --network host -it -p 5000:5000 \ - -v ~/.llama:/root/.llama \ - -v ./gpu/run.yaml:/root/llamastack-run-remote-vllm.yaml \ - --gpus=all \ - llamastack/distribution-remote-vllm \ - --yaml_config /root/llamastack-run-remote-vllm.yaml -``` diff --git a/docs/source/getting_started/distributions/self_hosted_distro/tgi.md b/docs/source/getting_started/distributions/self_hosted_distro/tgi.md index 8ad9de181..7f84833f3 100644 --- a/docs/source/getting_started/distributions/self_hosted_distro/tgi.md +++ b/docs/source/getting_started/distributions/self_hosted_distro/tgi.md @@ -2,94 +2,125 @@ The `llamastack/distribution-tgi` distribution consists of the following provider configurations. - -| **API** | **Inference** | **Agents** | **Memory** | **Safety** | **Telemetry** | -|----------------- |--------------- |---------------- |-------------------------------------------------- |---------------- |---------------- | -| **Provider(s)** | remote::tgi | meta-reference | meta-reference, remote::pgvector, remote::chroma | meta-reference | meta-reference | +| API | Provider(s) | +|-----|-------------| +| agents | `inline::meta-reference` | +| inference | `remote::tgi` | +| memory | `inline::faiss`, `remote::chromadb`, `remote::pgvector` | +| safety | `inline::llama-guard` | +| telemetry | `inline::meta-reference` | -### Docker: Start the Distribution (Single Node GPU) +You can use this distribution if you have GPUs and want to run an independent TGI server container for running inference. -> [!NOTE] -> This assumes you have access to GPU to start a TGI server with access to your GPU. +### Environment Variables + +The following environment variables can be configured: + +- `LLAMASTACK_PORT`: Port for the Llama Stack distribution server (default: `5001`) +- `INFERENCE_MODEL`: Inference model loaded into the TGI server (default: `meta-llama/Llama-3.2-3B-Instruct`) +- `TGI_URL`: URL of the TGI server with the main inference model (default: `http://127.0.0.1:8080}/v1`) +- `TGI_SAFETY_URL`: URL of the TGI server with the safety model (default: `http://127.0.0.1:8081/v1`) +- `SAFETY_MODEL`: Name of the safety (Llama-Guard) model to use (default: `meta-llama/Llama-Guard-3-1B`) -``` -$ cd distributions/tgi && docker compose up +## Setting up TGI server + +Please check the [TGI Getting Started Guide](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file#get-started) to get a TGI endpoint. Here is a sample script to start a TGI server locally via Docker: + +```bash +export INFERENCE_PORT=8080 +export INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct +export CUDA_VISIBLE_DEVICES=0 + +docker run --rm -it \ + -v $HOME/.cache/huggingface:/data \ + -p $INFERENCE_PORT:$INFERENCE_PORT \ + --gpus $CUDA_VISIBLE_DEVICES \ + ghcr.io/huggingface/text-generation-inference:2.3.1 \ + --dtype bfloat16 \ + --usage-stats off \ + --sharded false \ + --cuda-memory-fraction 0.7 \ + --model-id $INFERENCE_MODEL \ + --port $INFERENCE_PORT ``` -The script will first start up TGI server, then start up Llama Stack distribution server hooking up to the remote TGI provider for inference. You should be able to see the following outputs -- -``` -[text-generation-inference] | 2024-10-15T18:56:33.810397Z INFO text_generation_router::server: router/src/server.rs:1813: Using config Some(Llama) -[text-generation-inference] | 2024-10-15T18:56:33.810448Z WARN text_generation_router::server: router/src/server.rs:1960: Invalid hostname, defaulting to 0.0.0.0 -[text-generation-inference] | 2024-10-15T18:56:33.864143Z INFO text_generation_router::server: router/src/server.rs:2353: Connected -INFO: Started server process [1] -INFO: Waiting for application startup. -INFO: Application startup complete. -INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) +If you are using Llama Stack Safety / Shield APIs, then you will need to also run another instance of a TGI with a corresponding safety model like `meta-llama/Llama-Guard-3-1B` using a script like: + +```bash +export SAFETY_PORT=8081 +export SAFETY_MODEL=meta-llama/Llama-Guard-3-1B +export CUDA_VISIBLE_DEVICES=1 + +docker run --rm -it \ + -v $HOME/.cache/huggingface:/data \ + -p $SAFETY_PORT:$SAFETY_PORT \ + --gpus $CUDA_VISIBLE_DEVICES \ + ghcr.io/huggingface/text-generation-inference:2.3.1 \ + --dtype bfloat16 \ + --usage-stats off \ + --sharded false \ + --model-id $SAFETY_MODEL \ + --port $SAFETY_PORT ``` -To kill the server -``` -docker compose down +## Running Llama Stack + +Now you are ready to run Llama Stack with TGI as the inference provider. You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-tgi \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env TGI_URL=http://host.docker.internal:$INFERENCE_PORT ``` +If you are using Llama Stack Safety / Shield APIs, use: -### Conda: TGI server + llama stack run - -If you wish to separately spin up a TGI server, and connect with Llama Stack, you may use the following commands. - -#### Start TGI server locally -- Please check the [TGI Getting Started Guide](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file#get-started) to get a TGI endpoint. - -``` -docker run --rm -it -v $HOME/.cache/huggingface:/data -p 5009:5009 --gpus all ghcr.io/huggingface/text-generation-inference:latest --dtype bfloat16 --usage-stats on --sharded false --model-id meta-llama/Llama-3.1-8B-Instruct --port 5009 +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + llamastack/distribution-tgi \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env TGI_URL=http://host.docker.internal:$INFERENCE_PORT \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env TGI_SAFETY_URL=http://host.docker.internal:$SAFETY_PORT ``` -#### Start Llama Stack server pointing to TGI server +### Via Conda -**Via Conda** +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. ```bash llama stack build --template tgi --image-type conda -# -- start a TGI server endpoint -llama stack run ./gpu/run.yaml +llama stack run ./run.yaml + --port 5001 + --env INFERENCE_MODEL=$INFERENCE_MODEL + --env TGI_URL=http://127.0.0.1:$INFERENCE_PORT ``` -**Via Docker** -``` -docker run --network host -it -p 5000:5000 -v ./run.yaml:/root/my-run.yaml --gpus=all llamastack/distribution-tgi --yaml_config /root/my-run.yaml -``` +If you are using Llama Stack Safety / Shield APIs, use: -Make sure in you `run.yaml` file, you inference provider is pointing to the correct TGI server endpoint. E.g. -``` -inference: - - provider_id: tgi0 - provider_type: remote::tgi - config: - url: http://127.0.0.1:5009 -``` - - -### (Optional) Update Model Serving Configuration -To serve a new model with `tgi`, change the docker command flag `--model-id `. - -This can be done by edit the `command` args in `compose.yaml`. E.g. Replace "Llama-3.2-1B-Instruct" with the model you want to serve. - -``` -command: ["--dtype", "bfloat16", "--usage-stats", "on", "--sharded", "false", "--model-id", "meta-llama/Llama-3.2-1B-Instruct", "--port", "5009", "--cuda-memory-fraction", "0.3"] -``` - -or by changing the docker run command's `--model-id` flag -``` -docker run --rm -it -v $HOME/.cache/huggingface:/data -p 5009:5009 --gpus all ghcr.io/huggingface/text-generation-inference:latest --dtype bfloat16 --usage-stats on --sharded false --model-id meta-llama/Llama-3.2-1B-Instruct --port 5009 -``` - -In `run.yaml`, make sure you point the correct server endpoint to the TGI server endpoint serving your model. -``` -inference: - - provider_id: tgi0 - provider_type: remote::tgi - config: - url: http://127.0.0.1:5009 +```bash +llama stack run ./run-with-safety.yaml + --port 5001 + --env INFERENCE_MODEL=$INFERENCE_MODEL + --env TGI_URL=http://127.0.0.1:$INFERENCE_PORT + --env SAFETY_MODEL=$SAFETY_MODEL + --env TGI_SAFETY_URL=http://127.0.0.1:$SAFETY_PORT ``` diff --git a/docs/source/getting_started/distributions/self_hosted_distro/together.md b/docs/source/getting_started/distributions/self_hosted_distro/together.md index b9ea9f6e6..17f109e65 100644 --- a/docs/source/getting_started/distributions/self_hosted_distro/together.md +++ b/docs/source/getting_started/distributions/self_hosted_distro/together.md @@ -1,62 +1,67 @@ -# Together Distribution - -### Connect to a Llama Stack Together Endpoint -- You may connect to a hosted endpoint `https://llama-stack.together.ai`, serving a Llama Stack distribution +# Fireworks Distribution The `llamastack/distribution-together` distribution consists of the following provider configurations. - -| **API** | **Inference** | **Agents** | **Memory** | **Safety** | **Telemetry** | -|----------------- |--------------- |---------------- |-------------------------------------------------- |---------------- |---------------- | -| **Provider(s)** | remote::together | meta-reference | meta-reference, remote::weaviate | meta-reference | meta-reference | +| API | Provider(s) | +|-----|-------------| +| agents | `inline::meta-reference` | +| inference | `remote::together` | +| memory | `inline::faiss`, `remote::chromadb`, `remote::pgvector` | +| safety | `inline::llama-guard` | +| telemetry | `inline::meta-reference` | -### Docker: Start the Distribution (Single Node CPU) +### Environment Variables -> [!NOTE] -> This assumes you have an hosted endpoint at Together with API Key. +The following environment variables can be configured: -``` -$ cd distributions/together && docker compose up +- `LLAMASTACK_PORT`: Port for the Llama Stack distribution server (default: `5001`) +- `TOGETHER_API_KEY`: Together.AI API Key (default: ``) + +### Models + +The following models are available by default: + +- `meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo` +- `meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo` +- `meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo` +- `meta-llama/Llama-3.2-3B-Instruct-Turbo` +- `meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo` +- `meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo` +- `meta-llama/Meta-Llama-Guard-3-8B` +- `meta-llama/Llama-Guard-3-11B-Vision-Turbo` + + +### Prerequisite: API Keys + +Make sure you have access to a Together API Key. You can get one by visiting [together.xyz](https://together.xyz/). + + +## Running Llama Stack with Together + +You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-together \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env TOGETHER_API_KEY=$TOGETHER_API_KEY ``` -Make sure in your `run.yaml` file, your inference provider is pointing to the correct Together URL server endpoint. E.g. -``` -inference: - - provider_id: together - provider_type: remote::together - config: - url: https://api.together.xyz/v1 - api_key: -``` - -### Conda llama stack run (Single Node CPU) +### Via Conda ```bash llama stack build --template together --image-type conda -# -- modify run.yaml to a valid Together server endpoint -llama stack run ./run.yaml -``` - -### (Optional) Update Model Serving Configuration - -Use `llama-stack-client models list` to check the available models served by together. - -``` -$ llama-stack-client models list -+------------------------------+------------------------------+---------------+------------+ -| identifier | llama_model | provider_id | metadata | -+==============================+==============================+===============+============+ -| Llama3.1-8B-Instruct | Llama3.1-8B-Instruct | together0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.1-70B-Instruct | Llama3.1-70B-Instruct | together0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.1-405B-Instruct | Llama3.1-405B-Instruct | together0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-3B-Instruct | Llama3.2-3B-Instruct | together0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-11B-Vision-Instruct | Llama3.2-11B-Vision-Instruct | together0 | {} | -+------------------------------+------------------------------+---------------+------------+ -| Llama3.2-90B-Vision-Instruct | Llama3.2-90B-Vision-Instruct | together0 | {} | -+------------------------------+------------------------------+---------------+------------+ +llama stack run ./run.yaml \ + --port 5001 \ + --env TOGETHER_API_KEY=$TOGETHER_API_KEY ``` diff --git a/llama_stack/cli/stack/build.py b/llama_stack/cli/stack/build.py index 94d41cfab..56d0151f3 100644 --- a/llama_stack/cli/stack/build.py +++ b/llama_stack/cli/stack/build.py @@ -193,7 +193,6 @@ class StackBuild(Subcommand): apis = list(build_config.distribution_spec.providers.keys()) run_config = StackRunConfig( - built_at=datetime.now(), docker_image=( build_config.name if build_config.image_type == ImageType.docker.value @@ -217,15 +216,23 @@ class StackBuild(Subcommand): provider_types = [provider_types] for i, provider_type in enumerate(provider_types): - p_spec = Provider( - provider_id=f"{provider_type}-{i}", - provider_type=provider_type, - config={}, - ) + pid = provider_type.split("::")[-1] + config_type = instantiate_class_type( provider_registry[Api(api)][provider_type].config_class ) - p_spec.config = config_type() + if hasattr(config_type, "sample_run_config"): + config = config_type.sample_run_config( + __distro_dir__=f"distributions/{build_config.name}" + ) + else: + config = {} + + p_spec = Provider( + provider_id=f"{pid}-{i}" if len(provider_types) > 1 else pid, + provider_type=provider_type, + config=config, + ) run_config.providers[api].append(p_spec) os.makedirs(build_dir, exist_ok=True) diff --git a/llama_stack/cli/stack/run.py b/llama_stack/cli/stack/run.py index 5fce8c92c..c3ea174da 100644 --- a/llama_stack/cli/stack/run.py +++ b/llama_stack/cli/stack/run.py @@ -39,6 +39,13 @@ class StackRun(Subcommand): help="Disable IPv6 support", default=False, ) + self.parser.add_argument( + "--env", + action="append", + help="Environment variables to pass to the server in KEY=VALUE format. Can be specified multiple times.", + default=[], + metavar="KEY=VALUE", + ) def _run_stack_run_cmd(self, args: argparse.Namespace) -> None: from pathlib import Path @@ -108,4 +115,16 @@ class StackRun(Subcommand): if args.disable_ipv6: run_args.append("--disable-ipv6") + for env_var in args.env: + if "=" not in env_var: + self.parser.error( + f"Environment variable '{env_var}' must be in KEY=VALUE format" + ) + return + key, value = env_var.split("=", 1) # split on first = only + if not key: + self.parser.error(f"Environment variable '{env_var}' has empty key") + return + run_args.extend(["--env", f"{key}={value}"]) + run_with_pty(run_args) diff --git a/llama_stack/distribution/build_container.sh b/llama_stack/distribution/build_container.sh index 0764fee62..139883618 100755 --- a/llama_stack/distribution/build_container.sh +++ b/llama_stack/distribution/build_container.sh @@ -146,6 +146,8 @@ fi # Set version tag based on PyPI version if [ -n "$TEST_PYPI_VERSION" ]; then version_tag="test-$TEST_PYPI_VERSION" +elif [[ -n "$LLAMA_STACK_DIR" || -n "$LLAMA_MODELS_DIR" ]]; then + version_tag="dev" else URL="https://pypi.org/pypi/llama-stack/json" version_tag=$(curl -s $URL | jq -r '.info.version') diff --git a/llama_stack/distribution/datatypes.py b/llama_stack/distribution/datatypes.py index 4aaf9c38a..c2bff4eed 100644 --- a/llama_stack/distribution/datatypes.py +++ b/llama_stack/distribution/datatypes.py @@ -4,8 +4,6 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. -from datetime import datetime - from typing import Dict, List, Optional, Union from pydantic import BaseModel, Field @@ -115,7 +113,6 @@ class Provider(BaseModel): class StackRunConfig(BaseModel): version: str = LLAMA_STACK_RUN_CONFIG_VERSION - built_at: datetime image_name: str = Field( ..., diff --git a/llama_stack/distribution/server/server.py b/llama_stack/distribution/server/server.py index 0cfd11eda..7494e9367 100644 --- a/llama_stack/distribution/server/server.py +++ b/llama_stack/distribution/server/server.py @@ -313,7 +313,8 @@ def replace_env_vars(config: Any, path: str = "") -> Any: else: value = default_val - return value + # expand "~" from the values + return os.path.expanduser(value) try: return re.sub(pattern, get_env_var, config) diff --git a/llama_stack/distribution/start_conda_env.sh b/llama_stack/distribution/start_conda_env.sh index 3d91564b8..56e921d13 100755 --- a/llama_stack/distribution/start_conda_env.sh +++ b/llama_stack/distribution/start_conda_env.sh @@ -33,10 +33,33 @@ shift port="$1" shift +# Process environment variables from --env arguments +env_vars="" +while [[ $# -gt 0 ]]; do + case "$1" in + --env) + + if [[ -n "$2" ]]; then + # collect environment variables so we can set them after activating the conda env + env_vars="$env_vars $2" + shift 2 + else + echo -e "${RED}Error: --env requires a KEY=VALUE argument${NC}" >&2 + exit 1 + fi + ;; + *) + shift + ;; + esac +done + eval "$(conda shell.bash hook)" conda deactivate && conda activate "$env_name" -$CONDA_PREFIX/bin/python \ +set -x +$env_vars \ + $CONDA_PREFIX/bin/python \ -m llama_stack.distribution.server.server \ --yaml_config "$yaml_config" \ --port "$port" "$@" diff --git a/llama_stack/distribution/start_container.sh b/llama_stack/distribution/start_container.sh index 1efb76fb9..c56606826 100755 --- a/llama_stack/distribution/start_container.sh +++ b/llama_stack/distribution/start_container.sh @@ -31,7 +31,7 @@ if [ $# -lt 3 ]; then fi build_name="$1" -docker_image="distribution-$build_name" +docker_image="localhost/distribution-$build_name" shift yaml_config="$1" @@ -40,6 +40,26 @@ shift port="$1" shift +# Process environment variables from --env arguments +env_vars="" +while [[ $# -gt 0 ]]; do + case "$1" in + --env) + echo "env = $2" + if [[ -n "$2" ]]; then + env_vars="$env_vars -e $2" + shift 2 + else + echo -e "${RED}Error: --env requires a KEY=VALUE argument${NC}" >&2 + exit 1 + fi + ;; + *) + shift + ;; + esac +done + set -x if command -v selinuxenabled &> /dev/null && selinuxenabled; then @@ -59,15 +79,18 @@ fi version_tag="latest" if [ -n "$PYPI_VERSION" ]; then version_tag="$PYPI_VERSION" +elif [ -n "$LLAMA_STACK_DIR" ]; then + version_tag="dev" elif [ -n "$TEST_PYPI_VERSION" ]; then version_tag="test-$TEST_PYPI_VERSION" fi $DOCKER_BINARY run $DOCKER_OPTS -it \ -p $port:$port \ + $env_vars \ -v "$yaml_config:/app/config.yaml" \ $mounts \ $docker_image:$version_tag \ python -m llama_stack.distribution.server.server \ --yaml_config /app/config.yaml \ - --port $port "$@" + --port "$port" diff --git a/llama_stack/providers/inline/agents/meta_reference/config.py b/llama_stack/providers/inline/agents/meta_reference/config.py index 2770ed13c..ff34e5d5f 100644 --- a/llama_stack/providers/inline/agents/meta_reference/config.py +++ b/llama_stack/providers/inline/agents/meta_reference/config.py @@ -4,11 +4,22 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. -from pydantic import BaseModel, Field +from typing import Any, Dict + +from pydantic import BaseModel from llama_stack.providers.utils.kvstore import KVStoreConfig from llama_stack.providers.utils.kvstore.config import SqliteKVStoreConfig class MetaReferenceAgentsImplConfig(BaseModel): - persistence_store: KVStoreConfig = Field(default=SqliteKVStoreConfig()) + persistence_store: KVStoreConfig + + @classmethod + def sample_run_config(cls, __distro_dir__: str) -> Dict[str, Any]: + return { + "persistence_store": SqliteKVStoreConfig.sample_run_config( + __distro_dir__=__distro_dir__, + db_name="agents_store.db", + ) + } diff --git a/llama_stack/providers/inline/inference/meta_reference/config.py b/llama_stack/providers/inline/inference/meta_reference/config.py index 48cba645b..11648b117 100644 --- a/llama_stack/providers/inline/inference/meta_reference/config.py +++ b/llama_stack/providers/inline/inference/meta_reference/config.py @@ -49,6 +49,18 @@ class MetaReferenceInferenceConfig(BaseModel): resolved = resolve_model(self.model) return resolved.pth_file_count + @classmethod + def sample_run_config( + cls, + model: str = "Llama3.2-3B-Instruct", + checkpoint_dir: str = "${env.CHECKPOINT_DIR:null}", + ) -> Dict[str, Any]: + return { + "model": model, + "max_seq_len": 4096, + "checkpoint_dir": checkpoint_dir, + } + class MetaReferenceQuantizedInferenceConfig(MetaReferenceInferenceConfig): quantization: QuantizationConfig diff --git a/llama_stack/providers/inline/inference/meta_reference/generation.py b/llama_stack/providers/inline/inference/meta_reference/generation.py index 38c982473..577f5184b 100644 --- a/llama_stack/providers/inline/inference/meta_reference/generation.py +++ b/llama_stack/providers/inline/inference/meta_reference/generation.py @@ -107,7 +107,7 @@ class Llama: sys.stdout = open(os.devnull, "w") start_time = time.time() - if config.checkpoint_dir: + if config.checkpoint_dir and config.checkpoint_dir != "null": ckpt_dir = config.checkpoint_dir else: ckpt_dir = model_checkpoint_dir(model) @@ -137,7 +137,6 @@ class Llama: ), f"model_args vocab = {model_args.vocab_size} but tokenizer vocab = {tokenizer.n_words}" if isinstance(config, MetaReferenceQuantizedInferenceConfig): - if isinstance(config.quantization, Fp8QuantizationConfig): from .quantization.loader import convert_to_fp8_quantized_model diff --git a/llama_stack/providers/inline/inference/vllm/config.py b/llama_stack/providers/inline/inference/vllm/config.py index a7469ebde..e5516673c 100644 --- a/llama_stack/providers/inline/inference/vllm/config.py +++ b/llama_stack/providers/inline/inference/vllm/config.py @@ -34,6 +34,16 @@ class VLLMConfig(BaseModel): default=0.3, ) + @classmethod + def sample_run_config(cls): + return { + "model": "${env.VLLM_INFERENCE_MODEL:Llama3.2-3B-Instruct}", + "tensor_parallel_size": "${env.VLLM_TENSOR_PARALLEL_SIZE:1}", + "max_tokens": "${env.VLLM_MAX_TOKENS:4096}", + "enforce_eager": "${env.VLLM_ENFORCE_EAGER:False}", + "gpu_memory_utilization": "${env.VLLM_GPU_MEMORY_UTILIZATION:0.3}", + } + @field_validator("model") @classmethod def validate_model(cls, model: str) -> str: diff --git a/llama_stack/providers/inline/memory/faiss/config.py b/llama_stack/providers/inline/memory/faiss/config.py index 41970b05f..d82104477 100644 --- a/llama_stack/providers/inline/memory/faiss/config.py +++ b/llama_stack/providers/inline/memory/faiss/config.py @@ -4,10 +4,11 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. +from typing import Any, Dict + from llama_models.schema_utils import json_schema_type from pydantic import BaseModel -from llama_stack.distribution.utils.config_dirs import RUNTIME_BASE_DIR from llama_stack.providers.utils.kvstore.config import ( KVStoreConfig, SqliteKVStoreConfig, @@ -16,6 +17,13 @@ from llama_stack.providers.utils.kvstore.config import ( @json_schema_type class FaissImplConfig(BaseModel): - kvstore: KVStoreConfig = SqliteKVStoreConfig( - db_path=(RUNTIME_BASE_DIR / "faiss_store.db").as_posix() - ) # Uses SQLite config specific to FAISS storage + kvstore: KVStoreConfig + + @classmethod + def sample_run_config(cls, __distro_dir__: str) -> Dict[str, Any]: + return { + "kvstore": SqliteKVStoreConfig.sample_run_config( + __distro_dir__=__distro_dir__, + db_name="faiss_store.db", + ) + } diff --git a/llama_stack/providers/inline/safety/llama_guard/llama_guard.py b/llama_stack/providers/inline/safety/llama_guard/llama_guard.py index 9950064a4..f201d550f 100644 --- a/llama_stack/providers/inline/safety/llama_guard/llama_guard.py +++ b/llama_stack/providers/inline/safety/llama_guard/llama_guard.py @@ -73,18 +73,21 @@ DEFAULT_LG_V3_SAFETY_CATEGORIES = [ CAT_ELECTIONS, ] -LLAMA_GUARD_MODEL_IDS = [ - CoreModelId.llama_guard_3_8b.value, - CoreModelId.llama_guard_3_1b.value, - CoreModelId.llama_guard_3_11b_vision.value, -] +# accept both CoreModelId and huggingface repo id +LLAMA_GUARD_MODEL_IDS = { + CoreModelId.llama_guard_3_8b.value: "meta-llama/Llama-Guard-3-8B", + "meta-llama/Llama-Guard-3-8B": "meta-llama/Llama-Guard-3-8B", + CoreModelId.llama_guard_3_1b.value: "meta-llama/Llama-Guard-3-1B", + "meta-llama/Llama-Guard-3-1B": "meta-llama/Llama-Guard-3-1B", + CoreModelId.llama_guard_3_11b_vision.value: "meta-llama/Llama-Guard-3-11B-Vision", + "meta-llama/Llama-Guard-3-11B-Vision": "meta-llama/Llama-Guard-3-11B-Vision", +} MODEL_TO_SAFETY_CATEGORIES_MAP = { - CoreModelId.llama_guard_3_8b.value: ( - DEFAULT_LG_V3_SAFETY_CATEGORIES + [CAT_CODE_INTERPRETER_ABUSE] - ), - CoreModelId.llama_guard_3_1b.value: DEFAULT_LG_V3_SAFETY_CATEGORIES, - CoreModelId.llama_guard_3_11b_vision.value: DEFAULT_LG_V3_SAFETY_CATEGORIES, + "meta-llama/Llama-Guard-3-8B": DEFAULT_LG_V3_SAFETY_CATEGORIES + + [CAT_CODE_INTERPRETER_ABUSE], + "meta-llama/Llama-Guard-3-1B": DEFAULT_LG_V3_SAFETY_CATEGORIES, + "meta-llama/Llama-Guard-3-11B-Vision": DEFAULT_LG_V3_SAFETY_CATEGORIES, } @@ -150,8 +153,9 @@ class LlamaGuardSafetyImpl(Safety, ShieldsProtocolPrivate): if len(messages) > 0 and messages[0].role != Role.user.value: messages[0] = UserMessage(content=messages[0].content) + model = LLAMA_GUARD_MODEL_IDS[shield.provider_resource_id] impl = LlamaGuardShield( - model=shield.provider_resource_id, + model=model, inference_api=self.inference_api, excluded_categories=self.config.excluded_categories, ) diff --git a/llama_stack/providers/remote/inference/fireworks/config.py b/llama_stack/providers/remote/inference/fireworks/config.py index 275ce99e7..062c1e1ea 100644 --- a/llama_stack/providers/remote/inference/fireworks/config.py +++ b/llama_stack/providers/remote/inference/fireworks/config.py @@ -4,7 +4,7 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. -from typing import Optional +from typing import Any, Dict, Optional from llama_models.schema_utils import json_schema_type from pydantic import BaseModel, Field @@ -20,3 +20,10 @@ class FireworksImplConfig(BaseModel): default=None, description="The Fireworks.ai API Key", ) + + @classmethod + def sample_run_config(cls) -> Dict[str, Any]: + return { + "url": "https://api.fireworks.ai/inference", + "api_key": "${env.FIREWORKS_API_KEY}", + } diff --git a/llama_stack/providers/remote/inference/fireworks/fireworks.py b/llama_stack/providers/remote/inference/fireworks/fireworks.py index 42075eff7..3ff50d378 100644 --- a/llama_stack/providers/remote/inference/fireworks/fireworks.py +++ b/llama_stack/providers/remote/inference/fireworks/fireworks.py @@ -35,7 +35,7 @@ from llama_stack.providers.utils.inference.prompt_adapter import ( from .config import FireworksImplConfig -model_aliases = [ +MODEL_ALIASES = [ build_model_alias( "fireworks/llama-v3p1-8b-instruct", CoreModelId.llama3_1_8b_instruct.value, @@ -79,7 +79,7 @@ class FireworksInferenceAdapter( ModelRegistryHelper, Inference, NeedsRequestProviderData ): def __init__(self, config: FireworksImplConfig) -> None: - ModelRegistryHelper.__init__(self, model_aliases) + ModelRegistryHelper.__init__(self, MODEL_ALIASES) self.config = config self.formatter = ChatFormat(Tokenizer.get_instance()) diff --git a/llama_stack/providers/remote/inference/ollama/__init__.py b/llama_stack/providers/remote/inference/ollama/__init__.py index 7763af8d1..073c31cde 100644 --- a/llama_stack/providers/remote/inference/ollama/__init__.py +++ b/llama_stack/providers/remote/inference/ollama/__init__.py @@ -4,14 +4,10 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. -from llama_stack.distribution.datatypes import RemoteProviderConfig +from .config import OllamaImplConfig -class OllamaImplConfig(RemoteProviderConfig): - port: int = 11434 - - -async def get_adapter_impl(config: RemoteProviderConfig, _deps): +async def get_adapter_impl(config: OllamaImplConfig, _deps): from .ollama import OllamaInferenceAdapter impl = OllamaInferenceAdapter(config.url) diff --git a/llama_stack/providers/remote/inference/ollama/config.py b/llama_stack/providers/remote/inference/ollama/config.py new file mode 100644 index 000000000..ad16cac62 --- /dev/null +++ b/llama_stack/providers/remote/inference/ollama/config.py @@ -0,0 +1,22 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from typing import Any, Dict + +from pydantic import BaseModel + + +DEFAULT_OLLAMA_URL = "http://localhost:11434" + + +class OllamaImplConfig(BaseModel): + url: str = DEFAULT_OLLAMA_URL + + @classmethod + def sample_run_config( + cls, url: str = "${env.OLLAMA_URL:http://localhost:11434}", **kwargs + ) -> Dict[str, Any]: + return {"url": url} diff --git a/llama_stack/providers/remote/inference/ollama/ollama.py b/llama_stack/providers/remote/inference/ollama/ollama.py index 3b3f3868b..27bf0088e 100644 --- a/llama_stack/providers/remote/inference/ollama/ollama.py +++ b/llama_stack/providers/remote/inference/ollama/ollama.py @@ -82,7 +82,7 @@ class OllamaInferenceAdapter(Inference, ModelsProtocolPrivate): return AsyncClient(host=self.url) async def initialize(self) -> None: - print("Initializing Ollama, checking connectivity to server...") + print(f"checking connectivity to Ollama at `{self.url}`...") try: await self.client.ps() except httpx.ConnectError as e: diff --git a/llama_stack/providers/remote/inference/tgi/config.py b/llama_stack/providers/remote/inference/tgi/config.py index 863f81bf7..55bda4179 100644 --- a/llama_stack/providers/remote/inference/tgi/config.py +++ b/llama_stack/providers/remote/inference/tgi/config.py @@ -12,19 +12,20 @@ from pydantic import BaseModel, Field @json_schema_type class TGIImplConfig(BaseModel): - host: str = "localhost" - port: int = 8080 - protocol: str = "http" - - @property - def url(self) -> str: - return f"{self.protocol}://{self.host}:{self.port}" - + url: str = Field( + description="The URL for the TGI serving endpoint", + ) api_token: Optional[str] = Field( default=None, description="A bearer token if your TGI endpoint is protected.", ) + @classmethod + def sample_run_config(cls, url: str = "${env.TGI_URL}", **kwargs): + return { + "url": url, + } + @json_schema_type class InferenceEndpointImplConfig(BaseModel): diff --git a/llama_stack/providers/remote/inference/together/config.py b/llama_stack/providers/remote/inference/together/config.py index e928a771d..11944c0c7 100644 --- a/llama_stack/providers/remote/inference/together/config.py +++ b/llama_stack/providers/remote/inference/together/config.py @@ -4,7 +4,7 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. -from typing import Optional +from typing import Any, Dict, Optional from llama_models.schema_utils import json_schema_type from pydantic import BaseModel, Field @@ -20,3 +20,10 @@ class TogetherImplConfig(BaseModel): default=None, description="The Together AI API Key", ) + + @classmethod + def sample_run_config(cls) -> Dict[str, Any]: + return { + "url": "https://api.together.xyz/v1", + "api_key": "${env.TOGETHER_API_KEY}", + } diff --git a/llama_stack/providers/remote/inference/together/together.py b/llama_stack/providers/remote/inference/together/together.py index aae34bb87..e7c96ce98 100644 --- a/llama_stack/providers/remote/inference/together/together.py +++ b/llama_stack/providers/remote/inference/together/together.py @@ -38,7 +38,7 @@ from llama_stack.providers.utils.inference.prompt_adapter import ( from .config import TogetherImplConfig -model_aliases = [ +MODEL_ALIASES = [ build_model_alias( "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", CoreModelId.llama3_1_8b_instruct.value, @@ -78,7 +78,7 @@ class TogetherInferenceAdapter( ModelRegistryHelper, Inference, NeedsRequestProviderData ): def __init__(self, config: TogetherImplConfig) -> None: - ModelRegistryHelper.__init__(self, model_aliases) + ModelRegistryHelper.__init__(self, MODEL_ALIASES) self.config = config self.formatter = ChatFormat(Tokenizer.get_instance()) diff --git a/llama_stack/providers/remote/inference/vllm/config.py b/llama_stack/providers/remote/inference/vllm/config.py index 50a174589..a3a4c6930 100644 --- a/llama_stack/providers/remote/inference/vllm/config.py +++ b/llama_stack/providers/remote/inference/vllm/config.py @@ -24,3 +24,15 @@ class VLLMInferenceAdapterConfig(BaseModel): default="fake", description="The API token", ) + + @classmethod + def sample_run_config( + cls, + url: str = "${env.VLLM_URL}", + **kwargs, + ): + return { + "url": url, + "max_tokens": "${env.VLLM_MAX_TOKENS:4096}", + "api_token": "${env.VLLM_API_TOKEN:fake}", + } diff --git a/llama_stack/providers/tests/resolver.py b/llama_stack/providers/tests/resolver.py index df927926e..8bbb902cd 100644 --- a/llama_stack/providers/tests/resolver.py +++ b/llama_stack/providers/tests/resolver.py @@ -6,7 +6,6 @@ import json import tempfile -from datetime import datetime from typing import Any, Dict, List, Optional from llama_stack.distribution.datatypes import * # noqa: F403 @@ -37,7 +36,6 @@ async def construct_stack_for_test( ) -> TestStack: sqlite_file = tempfile.NamedTemporaryFile(delete=False, suffix=".db") run_config = dict( - built_at=datetime.now(), image_name="test-fixture", apis=apis, providers=providers, diff --git a/llama_stack/providers/utils/kvstore/config.py b/llama_stack/providers/utils/kvstore/config.py index 0a21bf4ca..ed400efae 100644 --- a/llama_stack/providers/utils/kvstore/config.py +++ b/llama_stack/providers/utils/kvstore/config.py @@ -36,6 +36,15 @@ class RedisKVStoreConfig(CommonConfig): def url(self) -> str: return f"redis://{self.host}:{self.port}" + @classmethod + def sample_run_config(cls): + return { + "type": "redis", + "namespace": None, + "host": "${env.REDIS_HOST:localhost}", + "port": "${env.REDIS_PORT:6379}", + } + class SqliteKVStoreConfig(CommonConfig): type: Literal[KVStoreType.sqlite.value] = KVStoreType.sqlite.value @@ -44,6 +53,19 @@ class SqliteKVStoreConfig(CommonConfig): description="File path for the sqlite database", ) + @classmethod + def sample_run_config( + cls, __distro_dir__: str = "runtime", db_name: str = "kvstore.db" + ): + return { + "type": "sqlite", + "namespace": None, + "db_path": "${env.SQLITE_STORE_DIR:~/.llama/" + + __distro_dir__ + + "}/" + + db_name, + } + class PostgresKVStoreConfig(CommonConfig): type: Literal[KVStoreType.postgres.value] = KVStoreType.postgres.value @@ -54,6 +76,19 @@ class PostgresKVStoreConfig(CommonConfig): password: Optional[str] = None table_name: str = "llamastack_kvstore" + @classmethod + def sample_run_config(cls, table_name: str = "llamastack_kvstore"): + return { + "type": "postgres", + "namespace": None, + "host": "${env.POSTGRES_HOST:localhost}", + "port": "${env.POSTGRES_PORT:5432}", + "db": "${env.POSTGRES_DB}", + "user": "${env.POSTGRES_USER}", + "password": "${env.POSTGRES_PASSWORD}", + "table_name": "${env.POSTGRES_TABLE_NAME:" + table_name + "}", + } + @classmethod @field_validator("table_name") def validate_table_name(cls, v: str) -> str: diff --git a/llama_stack/scripts/distro_codegen.py b/llama_stack/scripts/distro_codegen.py new file mode 100644 index 000000000..47d2dc41c --- /dev/null +++ b/llama_stack/scripts/distro_codegen.py @@ -0,0 +1,81 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +import concurrent.futures +import importlib +from functools import partial +from pathlib import Path +from typing import Iterator + +from rich.progress import Progress, SpinnerColumn, TextColumn + + +REPO_ROOT = Path(__file__).parent.parent.parent + + +def find_template_dirs(templates_dir: Path) -> Iterator[Path]: + """Find immediate subdirectories in the templates folder.""" + if not templates_dir.exists(): + raise FileNotFoundError(f"Templates directory not found: {templates_dir}") + + return ( + d for d in templates_dir.iterdir() if d.is_dir() and d.name != "__pycache__" + ) + + +def process_template(template_dir: Path, progress) -> None: + """Process a single template directory.""" + progress.print(f"Processing {template_dir.name}") + + try: + # Import the module directly + module_name = f"llama_stack.templates.{template_dir.name}" + module = importlib.import_module(module_name) + + # Get and save the distribution template + if template_func := getattr(module, "get_distribution_template", None): + template = template_func() + + template.save_distribution( + yaml_output_dir=REPO_ROOT / "llama_stack" / "templates" / template.name, + doc_output_dir=REPO_ROOT + / "docs/source/getting_started/distributions" + / f"{template.distro_type}_distro", + ) + else: + progress.print( + f"[yellow]Warning: {template_dir.name} has no get_distribution_template function" + ) + + except Exception as e: + progress.print(f"[red]Error processing {template_dir.name}: {str(e)}") + raise e + + +def main(): + templates_dir = REPO_ROOT / "llama_stack" / "templates" + + with Progress( + SpinnerColumn(), + TextColumn("[progress.description]{task.description}"), + ) as progress: + template_dirs = list(find_template_dirs(templates_dir)) + task = progress.add_task( + "Processing distribution templates...", total=len(template_dirs) + ) + + # Create a partial function with the progress bar + process_func = partial(process_template, progress=progress) + + # Process templates in parallel + with concurrent.futures.ThreadPoolExecutor() as executor: + # Submit all tasks and wait for completion + list(executor.map(process_func, template_dirs)) + progress.update(task, advance=len(template_dirs)) + + +if __name__ == "__main__": + main() diff --git a/llama_stack/templates/__init__.py b/llama_stack/templates/__init__.py new file mode 100644 index 000000000..756f351d8 --- /dev/null +++ b/llama_stack/templates/__init__.py @@ -0,0 +1,5 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. diff --git a/llama_stack/templates/fireworks/__init__.py b/llama_stack/templates/fireworks/__init__.py new file mode 100644 index 000000000..1d85c66db --- /dev/null +++ b/llama_stack/templates/fireworks/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from .fireworks import get_distribution_template # noqa: F401 diff --git a/llama_stack/templates/fireworks/build.yaml b/llama_stack/templates/fireworks/build.yaml index ffd67738d..c16e3f5d6 100644 --- a/llama_stack/templates/fireworks/build.yaml +++ b/llama_stack/templates/fireworks/build.yaml @@ -1,11 +1,19 @@ +version: '2' name: fireworks distribution_spec: - description: Use Fireworks.ai for running LLM inference + description: Use Fireworks.AI for running LLM inference + docker_image: null providers: - inference: remote::fireworks + inference: + - remote::fireworks memory: - inline::faiss - - remote::weaviate - safety: inline::llama-guard - agents: inline::meta-reference - telemetry: inline::meta-reference + - remote::chromadb + - remote::pgvector + safety: + - inline::llama-guard + agents: + - inline::meta-reference + telemetry: + - inline::meta-reference +image_type: conda diff --git a/llama_stack/templates/fireworks/doc_template.md b/llama_stack/templates/fireworks/doc_template.md new file mode 100644 index 000000000..bd25edfc1 --- /dev/null +++ b/llama_stack/templates/fireworks/doc_template.md @@ -0,0 +1,60 @@ +# Fireworks Distribution + +The `llamastack/distribution-{{ name }}` distribution consists of the following provider configurations. + +{{ providers_table }} + +{% if run_config_env_vars %} +### Environment Variables + +The following environment variables can be configured: + +{% for var, (default_value, description) in run_config_env_vars.items() %} +- `{{ var }}`: {{ description }} (default: `{{ default_value }}`) +{% endfor %} +{% endif %} + +{% if default_models %} +### Models + +The following models are available by default: + +{% for model in default_models %} +- `{{ model.model_id }}` +{% endfor %} +{% endif %} + + +### Prerequisite: API Keys + +Make sure you have access to a Fireworks API Key. You can get one by visiting [fireworks.ai](https://fireworks.ai/). + + +## Running Llama Stack with Fireworks + +You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env FIREWORKS_API_KEY=$FIREWORKS_API_KEY +``` + +### Via Conda + +```bash +llama stack build --template fireworks --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env FIREWORKS_API_KEY=$FIREWORKS_API_KEY +``` diff --git a/llama_stack/templates/fireworks/fireworks.py b/llama_stack/templates/fireworks/fireworks.py new file mode 100644 index 000000000..c4d2fdac8 --- /dev/null +++ b/llama_stack/templates/fireworks/fireworks.py @@ -0,0 +1,60 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path + +from llama_stack.distribution.datatypes import ModelInput, Provider, ShieldInput +from llama_stack.providers.remote.inference.fireworks import FireworksImplConfig +from llama_stack.providers.remote.inference.fireworks.fireworks import MODEL_ALIASES + +from llama_stack.templates.template import DistributionTemplate, RunConfigSettings + + +def get_distribution_template() -> DistributionTemplate: + providers = { + "inference": ["remote::fireworks"], + "memory": ["inline::faiss", "remote::chromadb", "remote::pgvector"], + "safety": ["inline::llama-guard"], + "agents": ["inline::meta-reference"], + "telemetry": ["inline::meta-reference"], + } + + inference_provider = Provider( + provider_id="fireworks", + provider_type="remote::fireworks", + config=FireworksImplConfig.sample_run_config(), + ) + + default_models = [ModelInput(model_id=m.provider_model_id) for m in MODEL_ALIASES] + + return DistributionTemplate( + name="fireworks", + distro_type="self_hosted", + description="Use Fireworks.AI for running LLM inference", + docker_image=None, + template_path=Path(__file__).parent / "doc_template.md", + providers=providers, + default_models=default_models, + run_configs={ + "run.yaml": RunConfigSettings( + provider_overrides={ + "inference": [inference_provider], + }, + default_models=default_models, + default_shields=[ShieldInput(shield_id="meta-llama/Llama-Guard-3-8B")], + ), + }, + run_config_env_vars={ + "LLAMASTACK_PORT": ( + "5001", + "Port for the Llama Stack distribution server", + ), + "FIREWORKS_API_KEY": ( + "", + "Fireworks.AI API Key", + ), + }, + ) diff --git a/llama_stack/templates/fireworks/run.yaml b/llama_stack/templates/fireworks/run.yaml new file mode 100644 index 000000000..8d3316257 --- /dev/null +++ b/llama_stack/templates/fireworks/run.yaml @@ -0,0 +1,91 @@ +version: '2' +image_name: fireworks +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: fireworks + provider_type: remote::fireworks + config: + url: https://api.fireworks.ai/inference + api_key: ${env.FIREWORKS_API_KEY} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/fireworks}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/fireworks}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/fireworks}/registry.db +models: +- metadata: {} + model_id: fireworks/llama-v3p1-8b-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-v3p1-70b-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-v3p1-405b-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-v3p2-1b-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-v3p2-3b-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-v3p2-11b-vision-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-v3p2-90b-vision-instruct + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-guard-3-8b + provider_id: null + provider_model_id: null +- metadata: {} + model_id: fireworks/llama-guard-3-11b-vision + provider_id: null + provider_model_id: null +shields: +- params: null + shield_id: meta-llama/Llama-Guard-3-8B + provider_id: null + provider_shield_id: null +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/meta-reference-gpu/__init__.py b/llama_stack/templates/meta-reference-gpu/__init__.py new file mode 100644 index 000000000..1cfdb2c6a --- /dev/null +++ b/llama_stack/templates/meta-reference-gpu/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from .meta_reference import get_distribution_template # noqa: F401 diff --git a/llama_stack/templates/meta-reference-gpu/build.yaml b/llama_stack/templates/meta-reference-gpu/build.yaml index 7c468e41c..ef075d098 100644 --- a/llama_stack/templates/meta-reference-gpu/build.yaml +++ b/llama_stack/templates/meta-reference-gpu/build.yaml @@ -1,13 +1,19 @@ +version: '2' name: meta-reference-gpu distribution_spec: - docker_image: pytorch/pytorch:2.5.0-cuda12.4-cudnn9-runtime - description: Use code from `llama_stack` itself to serve all llama stack APIs + description: Use Meta Reference for running LLM inference + docker_image: null providers: - inference: meta-reference + inference: + - inline::meta-reference memory: - inline::faiss - remote::chromadb - remote::pgvector - safety: inline::llama-guard - agents: inline::meta-reference - telemetry: inline::meta-reference + safety: + - inline::llama-guard + agents: + - inline::meta-reference + telemetry: + - inline::meta-reference +image_type: conda diff --git a/llama_stack/templates/meta-reference-gpu/doc_template.md b/llama_stack/templates/meta-reference-gpu/doc_template.md new file mode 100644 index 000000000..9a61ff691 --- /dev/null +++ b/llama_stack/templates/meta-reference-gpu/doc_template.md @@ -0,0 +1,82 @@ +# Meta Reference Distribution + +The `llamastack/distribution-{{ name }}` distribution consists of the following provider configurations: + +{{ providers_table }} + +Note that you need access to nvidia GPUs to run this distribution. This distribution is not compatible with CPU-only machines or machines with AMD GPUs. + +{% if run_config_env_vars %} +### Environment Variables + +The following environment variables can be configured: + +{% for var, (default_value, description) in run_config_env_vars.items() %} +- `{{ var }}`: {{ description }} (default: `{{ default_value }}`) +{% endfor %} +{% endif %} + + +## Prerequisite: Downloading Models + +Please make sure you have llama model checkpoints downloaded in `~/.llama` before proceeding. See [installation guide](https://llama-stack.readthedocs.io/en/latest/cli_reference/download_models.html) here to download the models. Run `llama model list` to see the available models to download, and `llama model download` to download the checkpoints. + +``` +$ ls ~/.llama/checkpoints +Llama3.1-8B Llama3.2-11B-Vision-Instruct Llama3.2-1B-Instruct Llama3.2-90B-Vision-Instruct Llama-Guard-3-8B +Llama3.1-8B-Instruct Llama3.2-1B Llama3.2-3B-Instruct Llama-Guard-3-1B Prompt-Guard-86M +``` + +## Running the Distribution + +You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct \ + --env SAFETY_MODEL=meta-llama/Llama-Guard-3-1B +``` + +### Via Conda + +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. + +```bash +llama stack build --template meta-reference-gpu --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +llama stack run ./run-with-safety.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct \ + --env SAFETY_MODEL=meta-llama/Llama-Guard-3-1B +``` diff --git a/llama_stack/templates/meta-reference-gpu/meta_reference.py b/llama_stack/templates/meta-reference-gpu/meta_reference.py new file mode 100644 index 000000000..04bf889c2 --- /dev/null +++ b/llama_stack/templates/meta-reference-gpu/meta_reference.py @@ -0,0 +1,100 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path + +from llama_stack.distribution.datatypes import ModelInput, Provider, ShieldInput +from llama_stack.providers.inline.inference.meta_reference import ( + MetaReferenceInferenceConfig, +) +from llama_stack.templates.template import DistributionTemplate, RunConfigSettings + + +def get_distribution_template() -> DistributionTemplate: + providers = { + "inference": ["inline::meta-reference"], + "memory": ["inline::faiss", "remote::chromadb", "remote::pgvector"], + "safety": ["inline::llama-guard"], + "agents": ["inline::meta-reference"], + "telemetry": ["inline::meta-reference"], + } + + inference_provider = Provider( + provider_id="meta-reference-inference", + provider_type="inline::meta-reference", + config=MetaReferenceInferenceConfig.sample_run_config( + model="${env.INFERENCE_MODEL}", + checkpoint_dir="${env.INFERENCE_CHECKPOINT_DIR:null}", + ), + ) + + inference_model = ModelInput( + model_id="${env.INFERENCE_MODEL}", + provider_id="meta-reference-inference", + ) + safety_model = ModelInput( + model_id="${env.SAFETY_MODEL}", + provider_id="meta-reference-safety", + ) + + return DistributionTemplate( + name="meta-reference-gpu", + distro_type="self_hosted", + description="Use Meta Reference for running LLM inference", + template_path=Path(__file__).parent / "doc_template.md", + providers=providers, + default_models=[inference_model, safety_model], + run_configs={ + "run.yaml": RunConfigSettings( + provider_overrides={ + "inference": [inference_provider], + }, + default_models=[inference_model], + ), + "run-with-safety.yaml": RunConfigSettings( + provider_overrides={ + "inference": [ + inference_provider, + Provider( + provider_id="meta-reference-safety", + provider_type="inline::meta-reference", + config=MetaReferenceInferenceConfig.sample_run_config( + model="${env.SAFETY_MODEL}", + checkpoint_dir="${env.SAFETY_CHECKPOINT_DIR:null}", + ), + ), + ], + }, + default_models=[ + inference_model, + safety_model, + ], + default_shields=[ShieldInput(shield_id="${env.SAFETY_MODEL}")], + ), + }, + docker_compose_env_vars={ + "LLAMASTACK_PORT": ( + "5001", + "Port for the Llama Stack distribution server", + ), + "INFERENCE_MODEL": ( + "meta-llama/Llama-3.2-3B-Instruct", + "Inference model loaded into the Meta Reference server", + ), + "INFERENCE_CHECKPOINT_DIR": ( + "null", + "Directory containing the Meta Reference model checkpoint", + ), + "SAFETY_MODEL": ( + "meta-llama/Llama-Guard-3-1B", + "Name of the safety (Llama-Guard) model to use", + ), + "SAFETY_CHECKPOINT_DIR": ( + "null", + "Directory containing the Llama-Guard model checkpoint", + ), + }, + ) diff --git a/llama_stack/templates/meta-reference-gpu/run-with-safety.yaml b/llama_stack/templates/meta-reference-gpu/run-with-safety.yaml new file mode 100644 index 000000000..7d01159df --- /dev/null +++ b/llama_stack/templates/meta-reference-gpu/run-with-safety.yaml @@ -0,0 +1,70 @@ +version: '2' +image_name: meta-reference-gpu +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: meta-reference-inference + provider_type: inline::meta-reference + config: + model: ${env.INFERENCE_MODEL} + max_seq_len: 4096 + checkpoint_dir: ${env.INFERENCE_CHECKPOINT_DIR:null} + - provider_id: meta-reference-safety + provider_type: inline::meta-reference + config: + model: ${env.SAFETY_MODEL} + max_seq_len: 4096 + checkpoint_dir: ${env.SAFETY_CHECKPOINT_DIR:null} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/meta-reference-gpu}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/meta-reference-gpu}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/meta-reference-gpu}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: meta-reference-inference + provider_model_id: null +- metadata: {} + model_id: ${env.SAFETY_MODEL} + provider_id: meta-reference-safety + provider_model_id: null +shields: +- params: null + shield_id: ${env.SAFETY_MODEL} + provider_id: null + provider_shield_id: null +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/meta-reference-gpu/run.yaml b/llama_stack/templates/meta-reference-gpu/run.yaml new file mode 100644 index 000000000..c67ba60cd --- /dev/null +++ b/llama_stack/templates/meta-reference-gpu/run.yaml @@ -0,0 +1,56 @@ +version: '2' +image_name: meta-reference-gpu +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: meta-reference-inference + provider_type: inline::meta-reference + config: + model: ${env.INFERENCE_MODEL} + max_seq_len: 4096 + checkpoint_dir: ${env.INFERENCE_CHECKPOINT_DIR:null} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/meta-reference-gpu}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/meta-reference-gpu}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/meta-reference-gpu}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: meta-reference-inference + provider_model_id: null +shields: [] +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/ollama/__init__.py b/llama_stack/templates/ollama/__init__.py new file mode 100644 index 000000000..3a2c40f27 --- /dev/null +++ b/llama_stack/templates/ollama/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from .ollama import get_distribution_template # noqa: F401 diff --git a/llama_stack/templates/ollama/build.yaml b/llama_stack/templates/ollama/build.yaml index 8cab877ea..106449309 100644 --- a/llama_stack/templates/ollama/build.yaml +++ b/llama_stack/templates/ollama/build.yaml @@ -1,12 +1,19 @@ +version: '2' name: ollama distribution_spec: - description: Use ollama for running LLM inference + description: Use (an external) Ollama server for running LLM inference + docker_image: null providers: - inference: remote::ollama + inference: + - remote::ollama memory: - inline::faiss - remote::chromadb - remote::pgvector - safety: inline::llama-guard - agents: inline::meta-reference - telemetry: inline::meta-reference + safety: + - inline::llama-guard + agents: + - inline::meta-reference + telemetry: + - inline::meta-reference +image_type: conda diff --git a/llama_stack/templates/ollama/doc_template.md b/llama_stack/templates/ollama/doc_template.md new file mode 100644 index 000000000..11a15c9e9 --- /dev/null +++ b/llama_stack/templates/ollama/doc_template.md @@ -0,0 +1,134 @@ +# Ollama Distribution + +The `llamastack/distribution-{{ name }}` distribution consists of the following provider configurations. + +{{ providers_table }} + +You should use this distribution if you have a regular desktop machine without very powerful GPUs. Of course, if you have powerful GPUs, you can still continue using this distribution since Ollama supports GPU acceleration. + +{%- if run_config_env_vars %} +### Environment Variables + +The following environment variables can be configured: + +{% for var, (default_value, description) in run_config_env_vars.items() %} +- `{{ var }}`: {{ description }} (default: `{{ default_value }}`) +{% endfor %} +{% endif %} + + +## Setting up Ollama server + +Please check the [Ollama Documentation](https://github.com/ollama/ollama) on how to install and run Ollama. After installing Ollama, you need to run `ollama serve` to start the server. + +In order to load models, you can run: + +```bash +export INFERENCE_MODEL="meta-llama/Llama-3.2-3B-Instruct" + +# ollama names this model differently, and we must use the ollama name when loading the model +export OLLAMA_INFERENCE_MODEL="llama3.2:3b-instruct-fp16" +ollama run $OLLAMA_INFERENCE_MODEL --keepalive 60m +``` + +If you are using Llama Stack Safety / Shield APIs, you will also need to pull and run the safety model. + +```bash +export SAFETY_MODEL="meta-llama/Llama-Guard-3-1B" + +# ollama names this model differently, and we must use the ollama name when loading the model +export OLLAMA_SAFETY_MODEL="llama-guard3:1b" +ollama run $OLLAMA_SAFETY_MODEL --keepalive 60m +``` + +## Running Llama Stack + +Now you are ready to run Llama Stack with Ollama as the inference provider. You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ~/.llama:/root/.llama \ + -v ./run.yaml:/root/my-run.yaml \ + --gpus=all \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env OLLAMA_URL=http://host.docker.internal:11434 +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ~/.llama:/root/.llama \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + --gpus=all \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env OLLAMA_URL=http://host.docker.internal:11434 +``` + +### Via Conda + +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. + +```bash +llama stack build --template ollama --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env OLLAMA_URL=http://127.0.0.1:11434 +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +llama stack run ./run-with-safety.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env OLLAMA_URL=http://127.0.0.1:11434 +``` + + +### (Optional) Update Model Serving Configuration + +> [!NOTE] +> Please check the [OLLAMA_SUPPORTED_MODELS](https://github.com/meta-llama/llama-stack/blob/main/llama_stack/providers.remote/inference/ollama/ollama.py) for the supported Ollama models. + + +To serve a new model with `ollama` +```bash +ollama run +``` + +To make sure that the model is being served correctly, run `ollama ps` to get a list of models being served by ollama. +``` +$ ollama ps + +NAME ID SIZE PROCESSOR UNTIL +llama3.1:8b-instruct-fp16 4aacac419454 17 GB 100% GPU 4 minutes from now +``` + +To verify that the model served by ollama is correctly connected to Llama Stack server +```bash +$ llama-stack-client models list ++----------------------+----------------------+---------------+-----------------------------------------------+ +| identifier | llama_model | provider_id | metadata | ++======================+======================+===============+===============================================+ +| Llama3.1-8B-Instruct | Llama3.1-8B-Instruct | ollama0 | {'ollama_model': 'llama3.1:8b-instruct-fp16'} | ++----------------------+----------------------+---------------+-----------------------------------------------+ +``` diff --git a/llama_stack/templates/ollama/ollama.py b/llama_stack/templates/ollama/ollama.py new file mode 100644 index 000000000..6e0056a77 --- /dev/null +++ b/llama_stack/templates/ollama/ollama.py @@ -0,0 +1,84 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path + +from llama_stack.distribution.datatypes import ModelInput, Provider, ShieldInput +from llama_stack.providers.remote.inference.ollama import OllamaImplConfig +from llama_stack.templates.template import DistributionTemplate, RunConfigSettings + + +def get_distribution_template() -> DistributionTemplate: + providers = { + "inference": ["remote::ollama"], + "memory": ["inline::faiss", "remote::chromadb", "remote::pgvector"], + "safety": ["inline::llama-guard"], + "agents": ["inline::meta-reference"], + "telemetry": ["inline::meta-reference"], + } + + inference_provider = Provider( + provider_id="ollama", + provider_type="remote::ollama", + config=OllamaImplConfig.sample_run_config(), + ) + + inference_model = ModelInput( + model_id="${env.INFERENCE_MODEL}", + provider_id="ollama", + ) + safety_model = ModelInput( + model_id="${env.SAFETY_MODEL}", + provider_id="ollama", + ) + + return DistributionTemplate( + name="ollama", + distro_type="self_hosted", + description="Use (an external) Ollama server for running LLM inference", + docker_image=None, + template_path=Path(__file__).parent / "doc_template.md", + providers=providers, + default_models=[inference_model, safety_model], + run_configs={ + "run.yaml": RunConfigSettings( + provider_overrides={ + "inference": [inference_provider], + }, + default_models=[inference_model], + ), + "run-with-safety.yaml": RunConfigSettings( + provider_overrides={ + "inference": [ + inference_provider, + ] + }, + default_models=[ + inference_model, + safety_model, + ], + default_shields=[ShieldInput(shield_id="${env.SAFETY_MODEL}")], + ), + }, + docker_compose_env_vars={ + "LLAMASTACK_PORT": ( + "5001", + "Port for the Llama Stack distribution server", + ), + "OLLAMA_URL": ( + "http://127.0.0.1:11434", + "URL of the Ollama server", + ), + "INFERENCE_MODEL": ( + "meta-llama/Llama-3.2-3B-Instruct", + "Inference model loaded into the Ollama server", + ), + "SAFETY_MODEL": ( + "meta-llama/Llama-Guard-3-1B", + "Safety model loaded into the Ollama server", + ), + }, + ) diff --git a/llama_stack/templates/ollama/run-with-safety.yaml b/llama_stack/templates/ollama/run-with-safety.yaml new file mode 100644 index 000000000..d0f657377 --- /dev/null +++ b/llama_stack/templates/ollama/run-with-safety.yaml @@ -0,0 +1,62 @@ +version: '2' +image_name: ollama +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: ollama + provider_type: remote::ollama + config: + url: ${env.OLLAMA_URL:http://localhost:11434} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/ollama}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/ollama}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/ollama}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: ollama + provider_model_id: null +- metadata: {} + model_id: ${env.SAFETY_MODEL} + provider_id: ollama + provider_model_id: null +shields: +- params: null + shield_id: ${env.SAFETY_MODEL} + provider_id: null + provider_shield_id: null +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/ollama/run.yaml b/llama_stack/templates/ollama/run.yaml new file mode 100644 index 000000000..c4003006b --- /dev/null +++ b/llama_stack/templates/ollama/run.yaml @@ -0,0 +1,54 @@ +version: '2' +image_name: ollama +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: ollama + provider_type: remote::ollama + config: + url: ${env.OLLAMA_URL:http://localhost:11434} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/ollama}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/ollama}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/ollama}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: ollama + provider_model_id: null +shields: [] +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/remote-vllm/__init__.py b/llama_stack/templates/remote-vllm/__init__.py new file mode 100644 index 000000000..7b3d59a01 --- /dev/null +++ b/llama_stack/templates/remote-vllm/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from .vllm import get_distribution_template # noqa: F401 diff --git a/llama_stack/templates/remote-vllm/build.yaml b/llama_stack/templates/remote-vllm/build.yaml index 39abb10af..9f4597cb0 100644 --- a/llama_stack/templates/remote-vllm/build.yaml +++ b/llama_stack/templates/remote-vllm/build.yaml @@ -1,12 +1,19 @@ +version: '2' name: remote-vllm distribution_spec: description: Use (an external) vLLM server for running LLM inference + docker_image: null providers: - inference: remote::vllm + inference: + - remote::vllm memory: - inline::faiss - remote::chromadb - remote::pgvector - safety: inline::llama-guard - agents: inline::meta-reference - telemetry: inline::meta-reference + safety: + - inline::llama-guard + agents: + - inline::meta-reference + telemetry: + - inline::meta-reference +image_type: conda diff --git a/llama_stack/templates/remote-vllm/doc_template.md b/llama_stack/templates/remote-vllm/doc_template.md new file mode 100644 index 000000000..c6ed53246 --- /dev/null +++ b/llama_stack/templates/remote-vllm/doc_template.md @@ -0,0 +1,119 @@ +# Remote vLLM Distribution + +The `llamastack/distribution-{{ name }}` distribution consists of the following provider configurations: + +{{ providers_table }} + +You can use this distribution if you have GPUs and want to run an independent vLLM server container for running inference. + +{% if run_config_env_vars %} +### Environment Variables + +The following environment variables can be configured: + +{% for var, (default_value, description) in run_config_env_vars.items() %} +- `{{ var }}`: {{ description }} (default: `{{ default_value }}`) +{% endfor %} +{% endif %} + + +## Setting up vLLM server + +Please check the [vLLM Documentation](https://docs.vllm.ai/en/v0.5.5/serving/deploying_with_docker.html) to get a vLLM endpoint. Here is a sample script to start a vLLM server locally via Docker: + +```bash +export INFERENCE_PORT=8000 +export INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct +export CUDA_VISIBLE_DEVICES=0 + +docker run \ + --runtime nvidia \ + --gpus $CUDA_VISIBLE_DEVICES \ + -v ~/.cache/huggingface:/root/.cache/huggingface \ + --env "HUGGING_FACE_HUB_TOKEN=$HF_TOKEN" \ + -p $INFERENCE_PORT:$INFERENCE_PORT \ + --ipc=host \ + vllm/vllm-openai:latest \ + --model $INFERENCE_MODEL \ + --port $INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, then you will need to also run another instance of a vLLM with a corresponding safety model like `meta-llama/Llama-Guard-3-1B` using a script like: + +```bash +export SAFETY_PORT=8081 +export SAFETY_MODEL=meta-llama/Llama-Guard-3-1B +export CUDA_VISIBLE_DEVICES=1 + +docker run \ + --runtime nvidia \ + --gpus $CUDA_VISIBLE_DEVICES \ + -v ~/.cache/huggingface:/root/.cache/huggingface \ + --env "HUGGING_FACE_HUB_TOKEN=$HF_TOKEN" \ + -p $SAFETY_PORT:$SAFETY_PORT \ + --ipc=host \ + vllm/vllm-openai:latest \ + --model $SAFETY_MODEL \ + --port $SAFETY_PORT +``` + +## Running Llama Stack + +Now you are ready to run Llama Stack with vLLM as the inference provider. You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://host.docker.internal:$INFERENCE_PORT \ +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://host.docker.internal:$INFERENCE_PORT \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env VLLM_SAFETY_URL=http://host.docker.internal:$SAFETY_PORT +``` + + +### Via Conda + +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. + +```bash +llama stack build --template remote-vllm --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://127.0.0.1:$INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +llama stack run ./run-with-safety.yaml \ + --port 5001 \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env VLLM_URL=http://127.0.0.1:$INFERENCE_PORT \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env VLLM_SAFETY_URL=http://127.0.0.1:$SAFETY_PORT +``` diff --git a/llama_stack/templates/remote-vllm/run-with-safety.yaml b/llama_stack/templates/remote-vllm/run-with-safety.yaml new file mode 100644 index 000000000..075cd793f --- /dev/null +++ b/llama_stack/templates/remote-vllm/run-with-safety.yaml @@ -0,0 +1,70 @@ +version: '2' +image_name: remote-vllm +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: vllm-inference + provider_type: remote::vllm + config: + url: ${env.VLLM_URL} + max_tokens: ${env.VLLM_MAX_TOKENS:4096} + api_token: ${env.VLLM_API_TOKEN:fake} + - provider_id: vllm-safety + provider_type: remote::vllm + config: + url: ${env.SAFETY_VLLM_URL} + max_tokens: ${env.VLLM_MAX_TOKENS:4096} + api_token: ${env.VLLM_API_TOKEN:fake} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/remote-vllm}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/remote-vllm}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/remote-vllm}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: vllm-inference + provider_model_id: null +- metadata: {} + model_id: ${env.SAFETY_MODEL} + provider_id: vllm-safety + provider_model_id: null +shields: +- params: null + shield_id: ${env.SAFETY_MODEL} + provider_id: null + provider_shield_id: null +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/remote-vllm/run.yaml b/llama_stack/templates/remote-vllm/run.yaml new file mode 100644 index 000000000..da45acee2 --- /dev/null +++ b/llama_stack/templates/remote-vllm/run.yaml @@ -0,0 +1,56 @@ +version: '2' +image_name: remote-vllm +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: vllm-inference + provider_type: remote::vllm + config: + url: ${env.VLLM_URL} + max_tokens: ${env.VLLM_MAX_TOKENS:4096} + api_token: ${env.VLLM_API_TOKEN:fake} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/remote-vllm}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/remote-vllm}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/remote-vllm}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: vllm-inference + provider_model_id: null +shields: [] +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/remote-vllm/vllm.py b/llama_stack/templates/remote-vllm/vllm.py new file mode 100644 index 000000000..ad3c1d8e2 --- /dev/null +++ b/llama_stack/templates/remote-vllm/vllm.py @@ -0,0 +1,100 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path + +from llama_stack.distribution.datatypes import ModelInput, Provider, ShieldInput +from llama_stack.providers.remote.inference.vllm import VLLMInferenceAdapterConfig +from llama_stack.templates.template import DistributionTemplate, RunConfigSettings + + +def get_distribution_template() -> DistributionTemplate: + providers = { + "inference": ["remote::vllm"], + "memory": ["inline::faiss", "remote::chromadb", "remote::pgvector"], + "safety": ["inline::llama-guard"], + "agents": ["inline::meta-reference"], + "telemetry": ["inline::meta-reference"], + } + + inference_provider = Provider( + provider_id="vllm-inference", + provider_type="remote::vllm", + config=VLLMInferenceAdapterConfig.sample_run_config( + url="${env.VLLM_URL}", + ), + ) + + inference_model = ModelInput( + model_id="${env.INFERENCE_MODEL}", + provider_id="vllm-inference", + ) + safety_model = ModelInput( + model_id="${env.SAFETY_MODEL}", + provider_id="vllm-safety", + ) + + return DistributionTemplate( + name="remote-vllm", + distro_type="self_hosted", + description="Use (an external) vLLM server for running LLM inference", + template_path=Path(__file__).parent / "doc_template.md", + providers=providers, + default_models=[inference_model, safety_model], + run_configs={ + "run.yaml": RunConfigSettings( + provider_overrides={ + "inference": [inference_provider], + }, + default_models=[inference_model], + ), + "run-with-safety.yaml": RunConfigSettings( + provider_overrides={ + "inference": [ + inference_provider, + Provider( + provider_id="vllm-safety", + provider_type="remote::vllm", + config=VLLMInferenceAdapterConfig.sample_run_config( + url="${env.SAFETY_VLLM_URL}", + ), + ), + ], + }, + default_models=[ + inference_model, + safety_model, + ], + default_shields=[ShieldInput(shield_id="${env.SAFETY_MODEL}")], + ), + }, + docker_compose_env_vars={ + "LLAMASTACK_PORT": ( + "5001", + "Port for the Llama Stack distribution server", + ), + "INFERENCE_MODEL": ( + "meta-llama/Llama-3.2-3B-Instruct", + "Inference model loaded into the vLLM server", + ), + "VLLM_URL": ( + "http://host.docker.internal:5100}/v1", + "URL of the vLLM server with the main inference model", + ), + "MAX_TOKENS": ( + "4096", + "Maximum number of tokens for generation", + ), + "SAFETY_VLLM_URL": ( + "http://host.docker.internal:5101/v1", + "URL of the vLLM server with the safety model", + ), + "SAFETY_MODEL": ( + "meta-llama/Llama-Guard-3-1B", + "Name of the safety (Llama-Guard) model to use", + ), + }, + ) diff --git a/llama_stack/templates/template.py b/llama_stack/templates/template.py new file mode 100644 index 000000000..3048889a9 --- /dev/null +++ b/llama_stack/templates/template.py @@ -0,0 +1,163 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path +from typing import Dict, List, Literal, Optional, Tuple + +import jinja2 +import yaml +from pydantic import BaseModel, Field + +from llama_stack.distribution.datatypes import ( + Api, + BuildConfig, + DistributionSpec, + ModelInput, + Provider, + ShieldInput, + StackRunConfig, +) +from llama_stack.distribution.distribution import get_provider_registry +from llama_stack.distribution.utils.dynamic import instantiate_class_type +from llama_stack.providers.utils.kvstore.config import SqliteKVStoreConfig + + +class RunConfigSettings(BaseModel): + provider_overrides: Dict[str, List[Provider]] = Field(default_factory=dict) + default_models: List[ModelInput] + default_shields: Optional[List[ShieldInput]] = None + + def run_config( + self, + name: str, + providers: Dict[str, List[str]], + docker_image: Optional[str] = None, + ) -> StackRunConfig: + provider_registry = get_provider_registry() + + provider_configs = {} + for api_str, provider_types in providers.items(): + if api_providers := self.provider_overrides.get(api_str): + provider_configs[api_str] = api_providers + continue + + provider_type = provider_types[0] + provider_id = provider_type.split("::")[-1] + + api = Api(api_str) + if provider_type not in provider_registry[api]: + raise ValueError( + f"Unknown provider type: {provider_type} for API: {api_str}" + ) + + config_class = provider_registry[api][provider_type].config_class + assert ( + config_class is not None + ), f"No config class for provider type: {provider_type} for API: {api_str}" + + config_class = instantiate_class_type(config_class) + if hasattr(config_class, "sample_run_config"): + config = config_class.sample_run_config( + __distro_dir__=f"distributions/{name}" + ) + else: + config = {} + + provider_configs[api_str] = [ + Provider( + provider_id=provider_id, + provider_type=provider_type, + config=config, + ) + ] + + # Get unique set of APIs from providers + apis = list(sorted(providers.keys())) + + return StackRunConfig( + image_name=name, + docker_image=docker_image, + apis=apis, + providers=provider_configs, + metadata_store=SqliteKVStoreConfig.sample_run_config( + __distro_dir__=f"distributions/{name}", + db_name="registry.db", + ), + models=self.default_models, + shields=self.default_shields or [], + ) + + +class DistributionTemplate(BaseModel): + """ + Represents a Llama Stack distribution instance that can generate configuration + and documentation files. + """ + + name: str + description: str + distro_type: Literal["self_hosted", "remote_hosted", "ondevice"] + + providers: Dict[str, List[str]] + run_configs: Dict[str, RunConfigSettings] + template_path: Path + + # Optional configuration + run_config_env_vars: Optional[Dict[str, Tuple[str, str]]] = None + docker_image: Optional[str] = None + + default_models: Optional[List[ModelInput]] = None + + def build_config(self) -> BuildConfig: + return BuildConfig( + name=self.name, + distribution_spec=DistributionSpec( + description=self.description, + docker_image=self.docker_image, + providers=self.providers, + ), + image_type="conda", # default to conda, can be overridden + ) + + def generate_markdown_docs(self) -> str: + providers_table = "| API | Provider(s) |\n" + providers_table += "|-----|-------------|\n" + + for api, providers in sorted(self.providers.items()): + providers_str = ", ".join(f"`{p}`" for p in providers) + providers_table += f"| {api} | {providers_str} |\n" + + template = self.template_path.read_text() + # Render template with rich-generated table + env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True) + template = env.from_string(template) + return template.render( + name=self.name, + description=self.description, + providers=self.providers, + providers_table=providers_table, + run_config_env_vars=self.run_config_env_vars, + default_models=self.default_models, + ) + + def save_distribution(self, yaml_output_dir: Path, doc_output_dir: Path) -> None: + for output_dir in [yaml_output_dir, doc_output_dir]: + output_dir.mkdir(parents=True, exist_ok=True) + + build_config = self.build_config() + with open(yaml_output_dir / "build.yaml", "w") as f: + yaml.safe_dump(build_config.model_dump(), f, sort_keys=False) + + for yaml_pth, settings in self.run_configs.items(): + run_config = settings.run_config( + self.name, self.providers, self.docker_image + ) + with open(yaml_output_dir / yaml_pth, "w") as f: + yaml.safe_dump(run_config.model_dump(), f, sort_keys=False) + + docs = self.generate_markdown_docs() + with open(doc_output_dir / f"{self.name}.md", "w") as f: + f.write(docs) diff --git a/llama_stack/templates/tgi/__init__.py b/llama_stack/templates/tgi/__init__.py new file mode 100644 index 000000000..fa1932f6a --- /dev/null +++ b/llama_stack/templates/tgi/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from .tgi import get_distribution_template # noqa: F401 diff --git a/llama_stack/templates/tgi/build.yaml b/llama_stack/templates/tgi/build.yaml index 5500361c4..5f44c2d86 100644 --- a/llama_stack/templates/tgi/build.yaml +++ b/llama_stack/templates/tgi/build.yaml @@ -1,12 +1,19 @@ +version: '2' name: tgi distribution_spec: - description: Use TGI for running LLM inference + description: Use (an external) TGI server for running LLM inference + docker_image: llamastack/distribution-tgi:test-0.0.52rc3 providers: - inference: remote::tgi + inference: + - remote::tgi memory: - inline::faiss - remote::chromadb - remote::pgvector - safety: inline::llama-guard - agents: inline::meta-reference - telemetry: inline::meta-reference + safety: + - inline::llama-guard + agents: + - inline::meta-reference + telemetry: + - inline::meta-reference +image_type: conda diff --git a/llama_stack/templates/tgi/doc_template.md b/llama_stack/templates/tgi/doc_template.md new file mode 100644 index 000000000..d4dee7fb7 --- /dev/null +++ b/llama_stack/templates/tgi/doc_template.md @@ -0,0 +1,119 @@ +# TGI Distribution + +The `llamastack/distribution-{{ name }}` distribution consists of the following provider configurations. + +{{ providers_table }} + +You can use this distribution if you have GPUs and want to run an independent TGI server container for running inference. + +{% if run_config_env_vars %} +### Environment Variables + +The following environment variables can be configured: + +{% for var, (default_value, description) in run_config_env_vars.items() %} +- `{{ var }}`: {{ description }} (default: `{{ default_value }}`) +{% endfor %} +{% endif %} + + +## Setting up TGI server + +Please check the [TGI Getting Started Guide](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file#get-started) to get a TGI endpoint. Here is a sample script to start a TGI server locally via Docker: + +```bash +export INFERENCE_PORT=8080 +export INFERENCE_MODEL=meta-llama/Llama-3.2-3B-Instruct +export CUDA_VISIBLE_DEVICES=0 + +docker run --rm -it \ + -v $HOME/.cache/huggingface:/data \ + -p $INFERENCE_PORT:$INFERENCE_PORT \ + --gpus $CUDA_VISIBLE_DEVICES \ + ghcr.io/huggingface/text-generation-inference:2.3.1 \ + --dtype bfloat16 \ + --usage-stats off \ + --sharded false \ + --cuda-memory-fraction 0.7 \ + --model-id $INFERENCE_MODEL \ + --port $INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, then you will need to also run another instance of a TGI with a corresponding safety model like `meta-llama/Llama-Guard-3-1B` using a script like: + +```bash +export SAFETY_PORT=8081 +export SAFETY_MODEL=meta-llama/Llama-Guard-3-1B +export CUDA_VISIBLE_DEVICES=1 + +docker run --rm -it \ + -v $HOME/.cache/huggingface:/data \ + -p $SAFETY_PORT:$SAFETY_PORT \ + --gpus $CUDA_VISIBLE_DEVICES \ + ghcr.io/huggingface/text-generation-inference:2.3.1 \ + --dtype bfloat16 \ + --usage-stats off \ + --sharded false \ + --model-id $SAFETY_MODEL \ + --port $SAFETY_PORT +``` + +## Running Llama Stack + +Now you are ready to run Llama Stack with TGI as the inference provider. You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env TGI_URL=http://host.docker.internal:$INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run-with-safety.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env INFERENCE_MODEL=$INFERENCE_MODEL \ + --env TGI_URL=http://host.docker.internal:$INFERENCE_PORT \ + --env SAFETY_MODEL=$SAFETY_MODEL \ + --env TGI_SAFETY_URL=http://host.docker.internal:$SAFETY_PORT +``` + +### Via Conda + +Make sure you have done `pip install llama-stack` and have the Llama Stack CLI available. + +```bash +llama stack build --template {{ name }} --image-type conda +llama stack run ./run.yaml + --port 5001 + --env INFERENCE_MODEL=$INFERENCE_MODEL + --env TGI_URL=http://127.0.0.1:$INFERENCE_PORT +``` + +If you are using Llama Stack Safety / Shield APIs, use: + +```bash +llama stack run ./run-with-safety.yaml + --port 5001 + --env INFERENCE_MODEL=$INFERENCE_MODEL + --env TGI_URL=http://127.0.0.1:$INFERENCE_PORT + --env SAFETY_MODEL=$SAFETY_MODEL + --env TGI_SAFETY_URL=http://127.0.0.1:$SAFETY_PORT +``` diff --git a/llama_stack/templates/tgi/run-with-safety.yaml b/llama_stack/templates/tgi/run-with-safety.yaml new file mode 100644 index 000000000..b1f12cc88 --- /dev/null +++ b/llama_stack/templates/tgi/run-with-safety.yaml @@ -0,0 +1,66 @@ +version: '2' +image_name: tgi +docker_image: llamastack/distribution-tgi:test-0.0.52rc3 +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: tgi-inference + provider_type: remote::tgi + config: + url: ${env.TGI_URL} + - provider_id: tgi-safety + provider_type: remote::tgi + config: + url: ${env.TGI_SAFETY_URL} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/tgi}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/tgi}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/tgi}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: tgi-inference + provider_model_id: null +- metadata: {} + model_id: ${env.SAFETY_MODEL} + provider_id: tgi-safety + provider_model_id: null +shields: +- params: null + shield_id: ${env.SAFETY_MODEL} + provider_id: null + provider_shield_id: null +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/tgi/run.yaml b/llama_stack/templates/tgi/run.yaml new file mode 100644 index 000000000..5571beabd --- /dev/null +++ b/llama_stack/templates/tgi/run.yaml @@ -0,0 +1,54 @@ +version: '2' +image_name: tgi +docker_image: llamastack/distribution-tgi:test-0.0.52rc3 +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: tgi-inference + provider_type: remote::tgi + config: + url: ${env.TGI_URL} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/tgi}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/tgi}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/tgi}/registry.db +models: +- metadata: {} + model_id: ${env.INFERENCE_MODEL} + provider_id: tgi-inference + provider_model_id: null +shields: [] +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/tgi/tgi.py b/llama_stack/templates/tgi/tgi.py new file mode 100644 index 000000000..79f2ad395 --- /dev/null +++ b/llama_stack/templates/tgi/tgi.py @@ -0,0 +1,97 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path + +from llama_stack.distribution.datatypes import ModelInput, Provider, ShieldInput +from llama_stack.providers.remote.inference.tgi import TGIImplConfig +from llama_stack.templates.template import DistributionTemplate, RunConfigSettings + + +def get_distribution_template() -> DistributionTemplate: + providers = { + "inference": ["remote::tgi"], + "memory": ["inline::faiss", "remote::chromadb", "remote::pgvector"], + "safety": ["inline::llama-guard"], + "agents": ["inline::meta-reference"], + "telemetry": ["inline::meta-reference"], + } + + inference_provider = Provider( + provider_id="tgi-inference", + provider_type="remote::tgi", + config=TGIImplConfig.sample_run_config( + url="${env.TGI_URL}", + ), + ) + + inference_model = ModelInput( + model_id="${env.INFERENCE_MODEL}", + provider_id="tgi-inference", + ) + safety_model = ModelInput( + model_id="${env.SAFETY_MODEL}", + provider_id="tgi-safety", + ) + + return DistributionTemplate( + name="tgi", + distro_type="self_hosted", + description="Use (an external) TGI server for running LLM inference", + docker_image="llamastack/distribution-tgi:test-0.0.52rc3", + template_path=Path(__file__).parent / "doc_template.md", + providers=providers, + default_models=[inference_model, safety_model], + run_configs={ + "run.yaml": RunConfigSettings( + provider_overrides={ + "inference": [inference_provider], + }, + default_models=[inference_model], + ), + "run-with-safety.yaml": RunConfigSettings( + provider_overrides={ + "inference": [ + inference_provider, + Provider( + provider_id="tgi-safety", + provider_type="remote::tgi", + config=TGIImplConfig.sample_run_config( + url="${env.TGI_SAFETY_URL}", + ), + ), + ], + }, + default_models=[ + inference_model, + safety_model, + ], + default_shields=[ShieldInput(shield_id="${env.SAFETY_MODEL}")], + ), + }, + run_config_env_vars={ + "LLAMASTACK_PORT": ( + "5001", + "Port for the Llama Stack distribution server", + ), + "INFERENCE_MODEL": ( + "meta-llama/Llama-3.2-3B-Instruct", + "Inference model loaded into the TGI server", + ), + "TGI_URL": ( + "http://127.0.0.1:8080}/v1", + "URL of the TGI server with the main inference model", + ), + "TGI_SAFETY_URL": ( + "http://127.0.0.1:8081/v1", + "URL of the TGI server with the safety model", + ), + "SAFETY_MODEL": ( + "meta-llama/Llama-Guard-3-1B", + "Name of the safety (Llama-Guard) model to use", + ), + }, + ) diff --git a/llama_stack/templates/together/__init__.py b/llama_stack/templates/together/__init__.py new file mode 100644 index 000000000..757995b6b --- /dev/null +++ b/llama_stack/templates/together/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from .together import get_distribution_template # noqa: F401 diff --git a/llama_stack/templates/together/build.yaml b/llama_stack/templates/together/build.yaml index 5c149272d..a4402ba93 100644 --- a/llama_stack/templates/together/build.yaml +++ b/llama_stack/templates/together/build.yaml @@ -1,11 +1,19 @@ +version: '2' name: together distribution_spec: - description: Use Together.ai for running LLM inference + description: Use Together.AI for running LLM inference + docker_image: null providers: - inference: remote::together + inference: + - remote::together memory: - inline::faiss - - remote::weaviate - safety: inline::llama-guard - agents: inline::meta-reference - telemetry: inline::meta-reference + - remote::chromadb + - remote::pgvector + safety: + - inline::llama-guard + agents: + - inline::meta-reference + telemetry: + - inline::meta-reference +image_type: conda diff --git a/llama_stack/templates/together/doc_template.md b/llama_stack/templates/together/doc_template.md new file mode 100644 index 000000000..667a68713 --- /dev/null +++ b/llama_stack/templates/together/doc_template.md @@ -0,0 +1,60 @@ +# Fireworks Distribution + +The `llamastack/distribution-{{ name }}` distribution consists of the following provider configurations. + +{{ providers_table }} + +{% if run_config_env_vars %} +### Environment Variables + +The following environment variables can be configured: + +{% for var, (default_value, description) in run_config_env_vars.items() %} +- `{{ var }}`: {{ description }} (default: `{{ default_value }}`) +{% endfor %} +{% endif %} + +{% if default_models %} +### Models + +The following models are available by default: + +{% for model in default_models %} +- `{{ model.model_id }}` +{% endfor %} +{% endif %} + + +### Prerequisite: API Keys + +Make sure you have access to a Together API Key. You can get one by visiting [together.xyz](https://together.xyz/). + + +## Running Llama Stack with Together + +You can do this via Conda (build code) or Docker which has a pre-built image. + +### Via Docker + +This method allows you to get started quickly without having to build the distribution code. + +```bash +LLAMA_STACK_PORT=5001 +docker run \ + -it \ + -p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT \ + -v ./run.yaml:/root/my-run.yaml \ + llamastack/distribution-{{ name }} \ + /root/my-run.yaml \ + --port $LLAMA_STACK_PORT \ + --env TOGETHER_API_KEY=$TOGETHER_API_KEY +``` + +### Via Conda + +```bash +llama stack build --template together --image-type conda +llama stack run ./run.yaml \ + --port 5001 \ + --env TOGETHER_API_KEY=$TOGETHER_API_KEY +``` diff --git a/llama_stack/templates/together/run.yaml b/llama_stack/templates/together/run.yaml new file mode 100644 index 000000000..cc3c890f4 --- /dev/null +++ b/llama_stack/templates/together/run.yaml @@ -0,0 +1,87 @@ +version: '2' +image_name: together +docker_image: null +conda_env: null +apis: +- agents +- inference +- memory +- safety +- telemetry +providers: + inference: + - provider_id: together + provider_type: remote::together + config: + url: https://api.together.xyz/v1 + api_key: ${env.TOGETHER_API_KEY} + memory: + - provider_id: faiss + provider_type: inline::faiss + config: + kvstore: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/together}/faiss_store.db + safety: + - provider_id: llama-guard + provider_type: inline::llama-guard + config: {} + agents: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: + persistence_store: + type: sqlite + namespace: null + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/together}/agents_store.db + telemetry: + - provider_id: meta-reference + provider_type: inline::meta-reference + config: {} +metadata_store: + namespace: null + type: sqlite + db_path: ${env.SQLITE_STORE_DIR:~/.llama/distributions/together}/registry.db +models: +- metadata: {} + model_id: meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Llama-3.2-3B-Instruct-Turbo + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Meta-Llama-Guard-3-8B + provider_id: null + provider_model_id: null +- metadata: {} + model_id: meta-llama/Llama-Guard-3-11B-Vision-Turbo + provider_id: null + provider_model_id: null +shields: +- params: null + shield_id: meta-llama/Llama-Guard-3-1B + provider_id: null + provider_shield_id: null +memory_banks: [] +datasets: [] +scoring_fns: [] +eval_tasks: [] diff --git a/llama_stack/templates/together/together.py b/llama_stack/templates/together/together.py new file mode 100644 index 000000000..250ef02c3 --- /dev/null +++ b/llama_stack/templates/together/together.py @@ -0,0 +1,60 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from pathlib import Path + +from llama_stack.distribution.datatypes import ModelInput, Provider, ShieldInput +from llama_stack.providers.remote.inference.together import TogetherImplConfig +from llama_stack.providers.remote.inference.together.together import MODEL_ALIASES + +from llama_stack.templates.template import DistributionTemplate, RunConfigSettings + + +def get_distribution_template() -> DistributionTemplate: + providers = { + "inference": ["remote::together"], + "memory": ["inline::faiss", "remote::chromadb", "remote::pgvector"], + "safety": ["inline::llama-guard"], + "agents": ["inline::meta-reference"], + "telemetry": ["inline::meta-reference"], + } + + inference_provider = Provider( + provider_id="together", + provider_type="remote::together", + config=TogetherImplConfig.sample_run_config(), + ) + + default_models = [ModelInput(model_id=m.provider_model_id) for m in MODEL_ALIASES] + + return DistributionTemplate( + name="together", + distro_type="self_hosted", + description="Use Together.AI for running LLM inference", + docker_image=None, + template_path=Path(__file__).parent / "doc_template.md", + providers=providers, + default_models=default_models, + run_configs={ + "run.yaml": RunConfigSettings( + provider_overrides={ + "inference": [inference_provider], + }, + default_models=default_models, + default_shields=[ShieldInput(shield_id="meta-llama/Llama-Guard-3-1B")], + ), + }, + run_config_env_vars={ + "LLAMASTACK_PORT": ( + "5001", + "Port for the Llama Stack distribution server", + ), + "TOGETHER_API_KEY": ( + "", + "Together.AI API Key", + ), + }, + )