Make llama stack build not create a new conda by default (#788)

## What does this PR do?

So far `llama stack build` has always created a separate conda
environment for packaging the dependencies of a distribution. The main
reason to do so is isolation -- distributions are composed of providers
which can have a variety of potentially conflicting dependencies. That
said, this has created significant annoyance for new users since it is
not at all transparent. The fact that `llama stack run` is actually
running the code in some other conda is very surprising.

This PR tries to make things better. 

- Both `llama stack build` and `llama stack run` now accept an
`--image-name` argument which represents the (conda, docker, virtualenv)
image you want to operate upon.
- For the default (conda) mode, the script checks if a current conda
environment exists. If one exists, it uses it.
- If `--image-name` is provided, that option is used. In this case, an
environment is created if needed.
- There is no automatic `llamastack-` prefixing of the environment names
done anymore.


## Test Plan

Start in a conda environment, run `llama stack build --template
fireworks`; verify that it successfully built into the current
environment and stored the build file at
`$CONDA_PREFIX/llamastack-build.yaml`. Run `llama stack run fireworks`
which started correctly in the current environment.

Ran the same build command outside of conda. It failed asking for
`--image-name`. Ran it with `llama stack build --template fireworks
--image-name foo`. This successfully created a conda environment called
`foo` and installed deps. Ran `llama stack run fireworks` outside conda
which failed. Activated a different conda, ran again, it failed saying
it did not find the `llamastack-build.yaml` file. Then used
`--image-name foo` option and it ran successfully.
This commit is contained in:
Ashwin Bharambe 2025-01-16 13:44:53 -08:00 committed by GitHub
parent 59eeaf7f81
commit cee3816609
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 400 additions and 338 deletions

View file

@ -10,7 +10,7 @@ import sys
from enum import Enum
from pathlib import Path
from typing import Dict, List
from typing import Dict, List, Optional
from pydantic import BaseModel
from termcolor import cprint
@ -106,6 +106,8 @@ def print_pip_install_help(providers: Dict[str, List[Provider]]):
def build_image(
build_config: BuildConfig,
build_file_path: Path,
image_name: str,
template_name: Optional[str] = None,
):
docker_image = build_config.distribution_spec.docker_image or "python:3.10-slim"
@ -115,32 +117,34 @@ def build_image(
normal_deps += SERVER_DEPENDENCIES
if build_config.image_type == ImageType.docker.value:
script = (
script = str(
importlib.resources.files("llama_stack") / "distribution/build_container.sh"
)
args = [
script,
build_config.name,
image_name,
docker_image,
str(build_file_path),
str(BUILDS_BASE_DIR / ImageType.docker.value),
" ".join(normal_deps),
]
elif build_config.image_type == ImageType.conda.value:
script = (
script = str(
importlib.resources.files("llama_stack") / "distribution/build_conda_env.sh"
)
args = [
script,
build_config.name,
str(image_name),
str(build_file_path),
" ".join(normal_deps),
]
elif build_config.image_type == ImageType.venv.value:
script = importlib.resources.files("llama_stack") / "distribution/build_venv.sh"
script = str(
importlib.resources.files("llama_stack") / "distribution/build_venv.sh"
)
args = [
script,
build_config.name,
str(image_name),
str(build_file_path),
" ".join(normal_deps),
]
@ -156,7 +160,7 @@ def build_image(
if return_code != 0:
log.error(
f"Failed to build target {build_config.name} with return code {return_code}",
f"Failed to build target {image_name} with return code {return_code}",
)
return return_code

View file

@ -18,8 +18,8 @@ if [ -n "$LLAMA_MODELS_DIR" ]; then
fi
if [ "$#" -lt 3 ]; then
echo "Usage: $0 <distribution_type> <build_name> <build_file_path> <pip_dependencies> [<special_pip_deps>]" >&2
echo "Example: $0 <distribution_type> mybuild ./my-stack-build.yaml 'numpy pandas scipy'" >&2
echo "Usage: $0 <distribution_type> <conda_env_name> <build_file_path> <pip_dependencies> [<special_pip_deps>]" >&2
echo "Example: $0 <distribution_type> my-conda-env ./my-stack-build.yaml 'numpy pandas scipy'" >&2
exit 1
fi
@ -27,8 +27,7 @@ special_pip_deps="$4"
set -euo pipefail
build_name="$1"
env_name="llamastack-$build_name"
env_name="$1"
build_file_path="$2"
pip_dependencies="$3"
@ -137,8 +136,8 @@ ensure_conda_env_python310() {
fi
fi
mv $build_file_path $CONDA_PREFIX/
echo "Build spec configuration saved at $CONDA_PREFIX/$build_name-build.yaml"
mv $build_file_path $CONDA_PREFIX/llamastack-build.yaml
echo "Build spec configuration saved at $CONDA_PREFIX/llamastack-build.yaml"
}
ensure_conda_env_python310 "$env_name" "$pip_dependencies" "$special_pip_deps"

View file

@ -131,10 +131,6 @@ this could be just a hash
default=None,
description="Reference to the docker image if this package refers to a container",
)
conda_env: Optional[str] = Field(
default=None,
description="Reference to the conda environment if this package refers to a conda environment",
)
apis: List[str] = Field(
default_factory=list,
description="""
@ -166,7 +162,7 @@ a default SQLite store will be used.""",
class BuildConfig(BaseModel):
version: str = LLAMA_STACK_BUILD_CONFIG_VERSION
name: str
distribution_spec: DistributionSpec = Field(
description="The distribution spec to build including API providers. "
)

View file

@ -23,8 +23,7 @@ if [ $# -lt 3 ]; then
exit 1
fi
build_name="$1"
env_name="llamastack-$build_name"
env_name="$1"
shift
yaml_config="$1"