mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-10-09 13:14:39 +00:00
All the new CLI for api + stack work
This commit is contained in:
parent
fd3b65b718
commit
197f768636
16 changed files with 459 additions and 486 deletions
|
@ -116,4 +116,12 @@ ensure_conda_env_python310 "$env_name" "$pip_dependencies"
|
|||
|
||||
printf "${GREEN}Successfully setup conda environment. Configuring build...${NC}"
|
||||
|
||||
$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama api configure "$api_or_stack" --name "$env_name"
|
||||
if [ "$api_or_stack" = "stack" ]; then
|
||||
subcommand="stack"
|
||||
target=""
|
||||
else
|
||||
subcommand="api"
|
||||
target="$api_or_stack"
|
||||
fi
|
||||
|
||||
$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama $subcommand configure $target --build-name "$env_name"
|
||||
|
|
|
@ -110,4 +110,12 @@ set +x
|
|||
printf "${GREEN}Succesfully setup Podman image. Configuring build...${NC}"
|
||||
echo "You can run it with: podman run -p 8000:8000 $image_name"
|
||||
|
||||
$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama api configure "$api_or_stack" --name "$image_name"
|
||||
if [ "$api_or_stack" = "stack" ]; then
|
||||
subcommand="stack"
|
||||
target=""
|
||||
else
|
||||
subcommand="api"
|
||||
target="$api_or_stack"
|
||||
fi
|
||||
|
||||
$CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama $subcommand configure $target --build-name "$image_name"
|
||||
|
|
50
llama_toolchain/distribution/configure.py
Normal file
50
llama_toolchain/distribution/configure.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
# 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 llama_toolchain.distribution.datatypes import * # noqa: F403
|
||||
from termcolor import cprint
|
||||
|
||||
from llama_toolchain.common.prompt_for_config import prompt_for_config
|
||||
from llama_toolchain.distribution.distribution import api_providers
|
||||
from llama_toolchain.distribution.dynamic import instantiate_class_type
|
||||
|
||||
|
||||
def configure_api_providers(existing_configs: Dict[str, Any]) -> None:
|
||||
all_providers = api_providers()
|
||||
|
||||
provider_configs = {}
|
||||
for api_str, stub_config in existing_configs.items():
|
||||
api = Api(api_str)
|
||||
providers = all_providers[api]
|
||||
provider_id = stub_config["provider_id"]
|
||||
if provider_id not in providers:
|
||||
raise ValueError(
|
||||
f"Unknown provider `{provider_id}` is not available for API `{api_str}`"
|
||||
)
|
||||
|
||||
provider_spec = providers[provider_id]
|
||||
cprint(f"Configuring API: {api_str} ({provider_id})", "white", attrs=["bold"])
|
||||
config_type = instantiate_class_type(provider_spec.config_class)
|
||||
|
||||
try:
|
||||
existing_provider_config = config_type(**stub_config)
|
||||
except Exception:
|
||||
existing_provider_config = None
|
||||
|
||||
provider_config = prompt_for_config(
|
||||
config_type,
|
||||
existing_provider_config,
|
||||
)
|
||||
print("")
|
||||
|
||||
provider_configs[api_str] = {
|
||||
"provider_id": provider_id,
|
||||
**provider_config.dict(),
|
||||
}
|
||||
|
||||
return provider_configs
|
|
@ -97,7 +97,7 @@ class RemoteProviderConfig(BaseModel):
|
|||
def validate_url(cls, url: str) -> str:
|
||||
if not url.startswith("http"):
|
||||
raise ValueError(f"URL must start with http: {url}")
|
||||
return url
|
||||
return url.rstrip("/")
|
||||
|
||||
|
||||
def remote_provider_id(adapter_id: str) -> str:
|
||||
|
@ -150,12 +150,13 @@ def remote_provider_spec(
|
|||
|
||||
@json_schema_type
|
||||
class DistributionSpec(BaseModel):
|
||||
spec_id: str
|
||||
distribution_id: str
|
||||
description: str
|
||||
|
||||
provider_specs: Dict[Api, ProviderSpec] = Field(
|
||||
docker_image: Optional[str] = None
|
||||
providers: Dict[Api, str] = Field(
|
||||
default_factory=dict,
|
||||
description="Provider specifications for each of the APIs provided by this distribution",
|
||||
description="Provider IDs for each of the APIs provided by this distribution",
|
||||
)
|
||||
|
||||
|
||||
|
@ -170,6 +171,8 @@ Reference to the distribution this package refers to. For unregistered (adhoc) p
|
|||
this could be just a hash
|
||||
""",
|
||||
)
|
||||
distribution_id: Optional[str] = None
|
||||
|
||||
docker_image: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Reference to the docker image if this package refers to a container",
|
||||
|
|
179
llama_toolchain/distribution/package.py
Normal file
179
llama_toolchain/distribution/package.py
Normal file
|
@ -0,0 +1,179 @@
|
|||
# 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 json
|
||||
import os
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
import pkg_resources
|
||||
import yaml
|
||||
from pydantic import BaseModel
|
||||
|
||||
from termcolor import cprint
|
||||
|
||||
from llama_toolchain.common.config_dirs import BUILDS_BASE_DIR
|
||||
from llama_toolchain.distribution.datatypes import * # noqa: F403
|
||||
|
||||
from llama_toolchain.common.exec import run_with_pty
|
||||
from llama_toolchain.common.serialize import EnumEncoder
|
||||
from llama_toolchain.distribution.distribution import api_providers
|
||||
|
||||
|
||||
class BuildType(Enum):
|
||||
container = "container"
|
||||
conda_env = "conda_env"
|
||||
|
||||
|
||||
class Dependencies(BaseModel):
|
||||
pip_packages: List[str]
|
||||
docker_image: Optional[str] = None
|
||||
|
||||
|
||||
def get_dependencies(
|
||||
provider: ProviderSpec, dependencies: Dict[str, ProviderSpec]
|
||||
) -> Dependencies:
|
||||
from llama_toolchain.distribution.distribution import SERVER_DEPENDENCIES
|
||||
|
||||
pip_packages = provider.pip_packages
|
||||
for dep in dependencies.values():
|
||||
if dep.docker_image:
|
||||
raise ValueError(
|
||||
"You can only have the root provider specify a docker image"
|
||||
)
|
||||
pip_packages.extend(dep.pip_packages)
|
||||
|
||||
return Dependencies(
|
||||
docker_image=provider.docker_image,
|
||||
pip_packages=pip_packages + SERVER_DEPENDENCIES,
|
||||
)
|
||||
|
||||
|
||||
class ApiInput(BaseModel):
|
||||
api: Api
|
||||
provider: str
|
||||
dependencies: Dict[str, ProviderSpec]
|
||||
|
||||
|
||||
def build_package(
|
||||
api_inputs: List[ApiInput],
|
||||
build_type: BuildType,
|
||||
name: str,
|
||||
distribution_id: Optional[str] = None,
|
||||
docker_image: Optional[str] = None,
|
||||
):
|
||||
is_stack = len(api_inputs) > 1
|
||||
if is_stack:
|
||||
if not distribution_id:
|
||||
raise ValueError(
|
||||
"You must specify a distribution name when building the Llama Stack"
|
||||
)
|
||||
|
||||
api1 = api_inputs[0]
|
||||
|
||||
provider = distribution_id if is_stack else api1.provider
|
||||
api_or_stack = "stack" if is_stack else api1.api.value
|
||||
build_desc = "image" if build_type == BuildType.container else "env"
|
||||
|
||||
build_dir = BUILDS_BASE_DIR / api_or_stack
|
||||
os.makedirs(build_dir, exist_ok=True)
|
||||
|
||||
package_name = f"{build_desc}-{provider}-{name}"
|
||||
package_name = package_name.replace("::", "-")
|
||||
package_file = build_dir / f"{package_name}.yaml"
|
||||
|
||||
all_providers = api_providers()
|
||||
|
||||
package_deps = Dependencies(
|
||||
docker_image=docker_image or "python:3.10-slim",
|
||||
pip_packages=[],
|
||||
)
|
||||
stub_config = {}
|
||||
for api_input in api_inputs:
|
||||
api = api_input.api
|
||||
providers_for_api = all_providers[api]
|
||||
if api_input.provider not in providers_for_api:
|
||||
raise ValueError(
|
||||
f"Provider `{api_input.provider}` is not available for API `{api}`"
|
||||
)
|
||||
|
||||
deps = get_dependencies(
|
||||
providers_for_api[api_input.provider],
|
||||
api_input.dependencies,
|
||||
)
|
||||
if deps.docker_image:
|
||||
raise ValueError("A stack's dependencies cannot have a docker image")
|
||||
package_deps.pip_packages.extend(deps.pip_packages)
|
||||
|
||||
stub_config[api.value] = {"provider_id": api_input.provider}
|
||||
|
||||
if package_file.exists():
|
||||
cprint(
|
||||
f"Build `{package_name}` exists; will reconfigure",
|
||||
color="yellow",
|
||||
)
|
||||
c = PackageConfig(**yaml.safe_load(package_file.read_text()))
|
||||
for api_str, new_config in stub_config.items():
|
||||
if api_str not in c.providers:
|
||||
c.providers[api_str] = new_config
|
||||
else:
|
||||
existing_config = c.providers[api_str]
|
||||
if existing_config["provider_id"] != new_config["provider_id"]:
|
||||
cprint(
|
||||
f"Provider `{api_str}` has changed from `{existing_config}` to `{new_config}`",
|
||||
color="yellow",
|
||||
)
|
||||
c.providers[api_str] = new_config
|
||||
else:
|
||||
c = PackageConfig(
|
||||
built_at=datetime.now(),
|
||||
package_name=package_name,
|
||||
providers=stub_config,
|
||||
)
|
||||
|
||||
c.distribution_id = distribution_id
|
||||
c.docker_image = package_name if build_type == BuildType.container else None
|
||||
c.conda_env = package_name if build_type == BuildType.conda_env else None
|
||||
|
||||
with open(package_file, "w") as f:
|
||||
to_write = json.loads(json.dumps(c.dict(), cls=EnumEncoder))
|
||||
f.write(yaml.dump(to_write, sort_keys=False))
|
||||
|
||||
if build_type == BuildType.container:
|
||||
script = pkg_resources.resource_filename(
|
||||
"llama_toolchain", "distribution/build_container.sh"
|
||||
)
|
||||
args = [
|
||||
script,
|
||||
api_or_stack,
|
||||
package_name,
|
||||
package_deps.docker_image,
|
||||
" ".join(package_deps.pip_packages),
|
||||
]
|
||||
else:
|
||||
script = pkg_resources.resource_filename(
|
||||
"llama_toolchain", "distribution/build_conda_env.sh"
|
||||
)
|
||||
args = [
|
||||
script,
|
||||
api_or_stack,
|
||||
package_name,
|
||||
" ".join(package_deps.pip_packages),
|
||||
]
|
||||
|
||||
return_code = run_with_pty(args)
|
||||
if return_code != 0:
|
||||
cprint(
|
||||
f"Failed to build target {package_name} with return code {return_code}",
|
||||
color="red",
|
||||
)
|
||||
return
|
||||
|
||||
cprint(
|
||||
f"Target `{package_name}` built with configuration at {str(package_file)}",
|
||||
color="green",
|
||||
)
|
|
@ -8,77 +8,42 @@ from functools import lru_cache
|
|||
from typing import List, Optional
|
||||
|
||||
from .datatypes import * # noqa: F403
|
||||
from .distribution import api_providers
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def available_distribution_specs() -> List[DistributionSpec]:
|
||||
providers = api_providers()
|
||||
return [
|
||||
DistributionSpec(
|
||||
spec_id="local",
|
||||
distribution_id="local",
|
||||
description="Use code from `llama_toolchain` itself to serve all llama stack APIs",
|
||||
provider_specs={
|
||||
Api.inference: providers[Api.inference]["meta-reference"],
|
||||
Api.memory: providers[Api.memory]["meta-reference-faiss"],
|
||||
Api.safety: providers[Api.safety]["meta-reference"],
|
||||
Api.agentic_system: providers[Api.agentic_system]["meta-reference"],
|
||||
providers={
|
||||
Api.inference: "meta-reference",
|
||||
Api.memory: "meta-reference-faiss",
|
||||
Api.safety: "meta-reference",
|
||||
Api.agentic_system: "meta-reference",
|
||||
},
|
||||
),
|
||||
DistributionSpec(
|
||||
spec_id="remote",
|
||||
distribution_id="remote",
|
||||
description="Point to remote services for all llama stack APIs",
|
||||
provider_specs={x: remote_provider_spec(x) for x in providers},
|
||||
providers={x: "remote" for x in Api},
|
||||
),
|
||||
DistributionSpec(
|
||||
spec_id="local-ollama",
|
||||
distribution_id="local-ollama",
|
||||
description="Like local, but use ollama for running LLM inference",
|
||||
provider_specs={
|
||||
# this is ODD; make this easier -- we just need a better function to retrieve registered providers
|
||||
Api.inference: providers[Api.inference][remote_provider_id("ollama")],
|
||||
Api.safety: providers[Api.safety]["meta-reference"],
|
||||
Api.agentic_system: providers[Api.agentic_system]["meta-reference"],
|
||||
Api.memory: providers[Api.memory]["meta-reference-faiss"],
|
||||
},
|
||||
),
|
||||
DistributionSpec(
|
||||
spec_id="test-agentic",
|
||||
description="Test agentic with others as remote",
|
||||
provider_specs={
|
||||
Api.agentic_system: providers[Api.agentic_system]["meta-reference"],
|
||||
Api.inference: remote_provider_spec(Api.inference),
|
||||
Api.memory: remote_provider_spec(Api.memory),
|
||||
Api.safety: remote_provider_spec(Api.safety),
|
||||
},
|
||||
),
|
||||
DistributionSpec(
|
||||
spec_id="test-inference",
|
||||
description="Test inference provider",
|
||||
provider_specs={
|
||||
Api.inference: providers[Api.inference]["meta-reference"],
|
||||
},
|
||||
),
|
||||
DistributionSpec(
|
||||
spec_id="test-memory",
|
||||
description="Test memory provider",
|
||||
provider_specs={
|
||||
Api.inference: providers[Api.inference]["meta-reference"],
|
||||
Api.memory: providers[Api.memory]["meta-reference-faiss"],
|
||||
},
|
||||
),
|
||||
DistributionSpec(
|
||||
spec_id="test-safety",
|
||||
description="Test safety provider",
|
||||
provider_specs={
|
||||
Api.safety: providers[Api.safety]["meta-reference"],
|
||||
providers={
|
||||
Api.inference: remote_provider_id("ollama"),
|
||||
Api.safety: "meta-reference",
|
||||
Api.agentic_system: "meta-reference",
|
||||
Api.memory: "meta-reference-faiss",
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def resolve_distribution_spec(spec_id: str) -> Optional[DistributionSpec]:
|
||||
def resolve_distribution_spec(distribution_id: str) -> Optional[DistributionSpec]:
|
||||
for spec in available_distribution_specs():
|
||||
if spec.spec_id == spec_id:
|
||||
if spec.distribution_id == distribution_id:
|
||||
return spec
|
||||
return None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue