Skip to content

Commit

Permalink
feat(server): Use system packages for execution
Browse files Browse the repository at this point in the history
Users prefer to rely on the main CLI rather than invoking the server
through a Python module. Users interact with a high-level CLI rather
than needing to know internal module structures.

Now, when running llama stack run <path-to-config>, the server will
attempt to use the system package or a virtual environment if one is
active.

This also eliminates the current process dependency chain when running
from a virtual environment:

-> llama stack run
  -> start_env.sh
    -> python -m server...

Signed-off-by: Sébastien Han <[email protected]>
  • Loading branch information
leseb committed Feb 25, 2025
1 parent 30f79fa commit 95ddd2b
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions llama_stack/cli/stack/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
# the root directory of this source tree.

import argparse
import logging
import os
import sys
from pathlib import Path

from llama_stack.cli.subcommand import Subcommand

REPO_ROOT = Path(__file__).parent.parent.parent.parent

logger = logging.getLogger(__name__)


class StackRun(Subcommand):
def __init__(self, subparsers: argparse._SubParsersAction):
Expand Down Expand Up @@ -70,7 +74,6 @@ def _add_arguments(self):
type=str,
help="Image Type used during the build. This can be either conda or container or venv.",
choices=["conda", "container", "venv"],
default="conda",
)

def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
Expand Down Expand Up @@ -121,28 +124,51 @@ def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
config_dict = yaml.safe_load(config_file.read_text())
config = parse_and_maybe_upgrade_config(config_dict)

run_args = formulate_run_args(args.image_type, args.image_name, config, template_name)

run_args.extend([str(config_file), str(args.port)])
if args.disable_ipv6:
run_args.append("--disable-ipv6")

for env_var in args.env:
if "=" not in env_var:
cprint(
f"Environment variable '{env_var}' must be in KEY=VALUE format",
color="red",
)
return
key, value = env_var.split("=", 1) # split on first = only
if not key:
cprint(
f"Environment variable '{env_var}' has empty key",
color="red",
)
return
run_args.extend(["--env", f"{key}={value}"])

if args.tls_keyfile and args.tls_certfile:
run_args.extend(["--tls-keyfile", args.tls_keyfile, "--tls-certfile", args.tls_certfile])
run_with_pty(run_args)
# If the image type and name are not provided, we assume the user wants to run using system
# packages and directly run the server code.
if not args.image_type and not args.image_name:
logger.info("No image type or image name provided. Assuming system packages.")
# Import and run the server directly
try:
from llama_stack.distribution.server.server import main as server_main

# Remove the first argument which is the command name, the second argument which is
# the config file, the third argument which is the pass to the config file.
sys.argv = sys.argv[3:]

# Build final sys.argv to pass to the server and add the config file
sys.argv = ["server"] + sys.argv[1:]
sys.argv.extend(["--yaml-config", str(config_file)])

# Run the server
server_main()
except Exception as e:
self.parser.error(f"Failed to run server: {e}")
except ImportError as e:
self.parser.error(f"Failed to import server module: {e}")
else:
run_args = formulate_run_args(args.image_type, args.image_name, config, template_name)

run_args.extend([str(config_file), str(args.port)])
if args.disable_ipv6:
run_args.append("--disable-ipv6")

for env_var in args.env:
if "=" not in env_var:
cprint(
f"Environment variable '{env_var}' must be in KEY=VALUE format",
color="red",
)
return
key, value = env_var.split("=", 1) # split on first = only
if not key:
cprint(
f"Environment variable '{env_var}' has empty key",
color="red",
)
return
run_args.extend(["--env", f"{key}={value}"])

if args.tls_keyfile and args.tls_certfile:
run_args.extend(["--tls-keyfile", args.tls_keyfile, "--tls-certfile", args.tls_certfile])
run_with_pty(run_args)

0 comments on commit 95ddd2b

Please sign in to comment.