Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: llama stack build use UV_SYSTEM_PYTHON to install dependencies to system environment #1163

Merged
merged 8 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llama_stack/cli/stack/_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def run_stack_build_command(args: argparse.Namespace) -> None:
image_name=image_name,
config_path=args.config,
template_name=args.template,
system_install=args.system_install,
)


Expand Down Expand Up @@ -246,6 +247,7 @@ def _run_stack_build_command_from_build_config(
image_name: Optional[str] = None,
template_name: Optional[str] = None,
config_path: Optional[str] = None,
system_install: bool = False,
) -> None:
if build_config.image_type == ImageType.container.value:
if template_name:
Expand Down Expand Up @@ -274,6 +276,7 @@ def _run_stack_build_command_from_build_config(
build_file_path,
image_name,
template_or_config=template_name or config_path,
system_install=system_install,
)
if return_code != 0:
return
Expand Down
6 changes: 6 additions & 0 deletions llama_stack/cli/stack/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ def _add_arguments(self):
action="store_true",
help="Print the dependencies for the stack only, without building the stack",
)
self.parser.add_argument(
"--system-install",
default=False,
action="store_true",
help="Install the dependencies in the same system Python environment. Used when image-type is venv. By default, a new virtual environment will be created.",
)

def _run_stack_build_command(self, args: argparse.Namespace) -> None:
# always keep implementation completely silo-ed away from CLI so CLI
Expand Down
2 changes: 2 additions & 0 deletions llama_stack/distribution/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def build_image(
build_file_path: Path,
image_name: str,
template_or_config: str,
system_install: bool = False,
):
container_base = build_config.distribution_spec.container_image or "python:3.10-slim"

Expand Down Expand Up @@ -127,6 +128,7 @@ def build_image(
script,
str(image_name),
" ".join(normal_deps),
str(system_install),
]

if special_deps:
Expand Down
41 changes: 26 additions & 15 deletions llama_stack/distribution/build_venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ if [ "$#" -lt 3 ]; then
exit 1
fi

special_pip_deps="$3"
special_pip_deps="$4"

set -euo pipefail

build_name="$1"
env_name="llamastack-$build_name"
pip_dependencies="$2"

# whether we want to install dependencies in current system python environment
system_install="$3"

# Define color codes
RED='\033[0;31m'
NC='\033[0m' # No Color
Expand Down Expand Up @@ -72,18 +75,26 @@ pre_run_checks() {
run() {
local env_name="$1"
local pip_dependencies="$2"
local special_pip_deps="$3"
local system_install="$3"
local special_pip_deps="$4"

if [ "$system_install" = "True" ]; then
echo "Installing dependencies in system Python environment"
UV_FLAGS="--system"
else
echo "Using virtual environment $env_name"
uv venv "$env_name"
# shellcheck source=/dev/null
source "$env_name/bin/activate"
UV_FLAGS=""
fi

echo "Using virtual environment $env_name"
uv venv "$env_name"
# shellcheck source=/dev/null
source "$env_name/bin/activate"
if [ -n "$TEST_PYPI_VERSION" ]; then
# these packages are damaged in test-pypi, so install them first
uv pip install fastapi libcst
uv pip install $UV_FLAGS fastapi libcst
# shellcheck disable=SC2086
# we are building a command line so word splitting is expected
uv pip install --extra-index-url https://test.pypi.org/simple/ \
uv pip install $UV_FLAGS --extra-index-url https://test.pypi.org/simple/ \
llama-models=="$TEST_PYPI_VERSION" llama-stack=="$TEST_PYPI_VERSION" \
$pip_dependencies
if [ -n "$special_pip_deps" ]; then
Expand All @@ -92,7 +103,7 @@ run() {
echo "$part"
# shellcheck disable=SC2086
# we are building a command line so word splitting is expected
uv pip install $part
uv pip install $UV_FLAGS $part
done
fi
else
Expand All @@ -104,9 +115,9 @@ run() {
fi

printf "Installing from LLAMA_STACK_DIR: %s\n" "$LLAMA_STACK_DIR"
uv pip install --no-cache-dir -e "$LLAMA_STACK_DIR"
uv pip install $UV_FLAGS --no-cache-dir -e "$LLAMA_STACK_DIR"
else
uv pip install --no-cache-dir llama-stack
uv pip install $UV_FLAGS --no-cache-dir llama-stack
fi

if [ -n "$LLAMA_MODELS_DIR" ]; then
Expand All @@ -117,25 +128,25 @@ run() {

printf "Installing from LLAMA_MODELS_DIR: %s\n" "$LLAMA_MODELS_DIR"
uv pip uninstall llama-models
uv pip install --no-cache-dir -e "$LLAMA_MODELS_DIR"
uv pip install $UV_FLAGS --no-cache-dir -e "$LLAMA_MODELS_DIR"
fi

# Install pip dependencies
printf "Installing pip dependencies\n"
# shellcheck disable=SC2086
# we are building a command line so word splitting is expected
uv pip install $pip_dependencies
uv pip install $UV_FLAGS $pip_dependencies
if [ -n "$special_pip_deps" ]; then
IFS='#' read -ra parts <<<"$special_pip_deps"
for part in "${parts[@]}"; do
echo "$part"
# shellcheck disable=SC2086
# we are building a command line so word splitting is expected
uv pip install $part
uv pip install $UV_FLAGS $part
done
fi
fi
}

pre_run_checks "$env_name"
run "$env_name" "$pip_dependencies" "$special_pip_deps"
run "$env_name" "$pip_dependencies" "$system_install" "$special_pip_deps"