mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-28 02:53:30 +00:00
Switch to use importlib instead of deprecated pkg_resources (#678)
`pkg_resources` has been deprecated. This PR switches to use `importlib.resources`. --------- Signed-off-by: Yuan Tang <terrytangyuan@gmail.com>
This commit is contained in:
parent
747683a8a2
commit
9ec54dcbe7
5 changed files with 43 additions and 45 deletions
|
@ -43,7 +43,7 @@ class ModelPromptFormat(Subcommand):
|
||||||
)
|
)
|
||||||
|
|
||||||
def _run_model_template_cmd(self, args: argparse.Namespace) -> None:
|
def _run_model_template_cmd(self, args: argparse.Namespace) -> None:
|
||||||
import pkg_resources
|
import importlib.resources
|
||||||
|
|
||||||
# Only Llama 3.1 and 3.2 are supported
|
# Only Llama 3.1 and 3.2 are supported
|
||||||
supported_model_ids = [
|
supported_model_ids = [
|
||||||
|
@ -64,25 +64,26 @@ class ModelPromptFormat(Subcommand):
|
||||||
f"{model_id} is not a valid Model. Choose one from --\n {model_str}"
|
f"{model_id} is not a valid Model. Choose one from --\n {model_str}"
|
||||||
)
|
)
|
||||||
|
|
||||||
llama_3_1_file = pkg_resources.resource_filename(
|
llama_3_1_file = (
|
||||||
"llama_models", "llama3_1/prompt_format.md"
|
importlib.resources.files("llama_models") / "llama3_1/prompt_format.md"
|
||||||
)
|
)
|
||||||
llama_3_2_text_file = pkg_resources.resource_filename(
|
llama_3_2_text_file = (
|
||||||
"llama_models", "llama3_2/text_prompt_format.md"
|
importlib.resources.files("llama_models") / "llama3_2/text_prompt_format.md"
|
||||||
)
|
)
|
||||||
llama_3_2_vision_file = pkg_resources.resource_filename(
|
llama_3_2_vision_file = (
|
||||||
"llama_models", "llama3_2/vision_prompt_format.md"
|
importlib.resources.files("llama_models")
|
||||||
|
/ "llama3_2/vision_prompt_format.md"
|
||||||
)
|
)
|
||||||
if model_family(model_id) == ModelFamily.llama3_1:
|
if model_family(model_id) == ModelFamily.llama3_1:
|
||||||
with open(llama_3_1_file, "r") as f:
|
with importlib.resources.as_file(llama_3_1_file) as f:
|
||||||
content = f.read()
|
content = f.open("r").read()
|
||||||
elif model_family(model_id) == ModelFamily.llama3_2:
|
elif model_family(model_id) == ModelFamily.llama3_2:
|
||||||
if is_multimodal(model_id):
|
if is_multimodal(model_id):
|
||||||
with open(llama_3_2_vision_file, "r") as f:
|
with importlib.resources.as_file(llama_3_2_vision_file) as f:
|
||||||
content = f.read()
|
content = f.open("r").read()
|
||||||
else:
|
else:
|
||||||
with open(llama_3_2_text_file, "r") as f:
|
with importlib.resources.as_file(llama_3_2_text_file) as f:
|
||||||
content = f.read()
|
content = f.open("r").read()
|
||||||
|
|
||||||
render_markdown_to_pager(content)
|
render_markdown_to_pager(content)
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,15 @@
|
||||||
# This source code is licensed under the terms described in the LICENSE file in
|
# This source code is licensed under the terms described in the LICENSE file in
|
||||||
# the root directory of this source tree.
|
# the root directory of this source tree.
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
import importlib.resources
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
import pkg_resources
|
|
||||||
|
|
||||||
from llama_stack.cli.subcommand import Subcommand
|
from llama_stack.cli.subcommand import Subcommand
|
||||||
|
|
||||||
from llama_stack.distribution.datatypes import (
|
from llama_stack.distribution.datatypes import (
|
||||||
|
@ -290,13 +291,12 @@ class StackBuild(Subcommand):
|
||||||
|
|
||||||
if template_name:
|
if template_name:
|
||||||
# copy run.yaml from template to build_dir instead of generating it again
|
# copy run.yaml from template to build_dir instead of generating it again
|
||||||
template_path = pkg_resources.resource_filename(
|
template_path = (
|
||||||
"llama_stack", f"templates/{template_name}/run.yaml"
|
importlib.resources.files("llama_stack")
|
||||||
|
/ f"templates/{template_name}/run.yaml"
|
||||||
)
|
)
|
||||||
os.makedirs(build_dir, exist_ok=True)
|
with importlib.resources.as_file(template_path) as path:
|
||||||
run_config_file = build_dir / f"{build_config.name}-run.yaml"
|
shutil.copy(path, run_config_file)
|
||||||
shutil.copy(template_path, run_config_file)
|
|
||||||
|
|
||||||
# Find all ${env.VARIABLE} patterns
|
# Find all ${env.VARIABLE} patterns
|
||||||
cprint("Build Successful!", color="green")
|
cprint("Build Successful!", color="green")
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -52,7 +52,8 @@ class StackRun(Subcommand):
|
||||||
)
|
)
|
||||||
|
|
||||||
def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
|
def _run_stack_run_cmd(self, args: argparse.Namespace) -> None:
|
||||||
import pkg_resources
|
import importlib.resources
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from llama_stack.distribution.build import ImageType
|
from llama_stack.distribution.build import ImageType
|
||||||
|
@ -107,15 +108,15 @@ class StackRun(Subcommand):
|
||||||
config = parse_and_maybe_upgrade_config(config_dict)
|
config = parse_and_maybe_upgrade_config(config_dict)
|
||||||
|
|
||||||
if config.docker_image:
|
if config.docker_image:
|
||||||
script = pkg_resources.resource_filename(
|
script = (
|
||||||
"llama_stack",
|
importlib.resources.files("llama_stack")
|
||||||
"distribution/start_container.sh",
|
/ "distribution/start_container.sh"
|
||||||
)
|
)
|
||||||
run_args = [script, config.docker_image]
|
run_args = [script, config.docker_image]
|
||||||
else:
|
else:
|
||||||
script = pkg_resources.resource_filename(
|
script = (
|
||||||
"llama_stack",
|
importlib.resources.files("llama_stack")
|
||||||
"distribution/start_conda_env.sh",
|
/ "distribution/start_conda_env.sh"
|
||||||
)
|
)
|
||||||
run_args = [
|
run_args = [
|
||||||
script,
|
script,
|
||||||
|
|
|
@ -4,13 +4,13 @@
|
||||||
# This source code is licensed under the terms described in the LICENSE file in
|
# This source code is licensed under the terms described in the LICENSE file in
|
||||||
# the root directory of this source tree.
|
# the root directory of this source tree.
|
||||||
|
|
||||||
|
import importlib.resources
|
||||||
import logging
|
import logging
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
import pkg_resources
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from termcolor import cprint
|
from termcolor import cprint
|
||||||
|
|
||||||
|
@ -111,8 +111,8 @@ def build_image(build_config: BuildConfig, build_file_path: Path):
|
||||||
normal_deps += SERVER_DEPENDENCIES
|
normal_deps += SERVER_DEPENDENCIES
|
||||||
|
|
||||||
if build_config.image_type == ImageType.docker.value:
|
if build_config.image_type == ImageType.docker.value:
|
||||||
script = pkg_resources.resource_filename(
|
script = (
|
||||||
"llama_stack", "distribution/build_container.sh"
|
importlib.resources.files("llama_stack") / "distribution/build_container.sh"
|
||||||
)
|
)
|
||||||
args = [
|
args = [
|
||||||
script,
|
script,
|
||||||
|
@ -123,8 +123,8 @@ def build_image(build_config: BuildConfig, build_file_path: Path):
|
||||||
" ".join(normal_deps),
|
" ".join(normal_deps),
|
||||||
]
|
]
|
||||||
elif build_config.image_type == ImageType.conda.value:
|
elif build_config.image_type == ImageType.conda.value:
|
||||||
script = pkg_resources.resource_filename(
|
script = (
|
||||||
"llama_stack", "distribution/build_conda_env.sh"
|
importlib.resources.files("llama_stack") / "distribution/build_conda_env.sh"
|
||||||
)
|
)
|
||||||
args = [
|
args = [
|
||||||
script,
|
script,
|
||||||
|
@ -133,9 +133,7 @@ def build_image(build_config: BuildConfig, build_file_path: Path):
|
||||||
" ".join(normal_deps),
|
" ".join(normal_deps),
|
||||||
]
|
]
|
||||||
elif build_config.image_type == ImageType.venv.value:
|
elif build_config.image_type == ImageType.venv.value:
|
||||||
script = pkg_resources.resource_filename(
|
script = importlib.resources.files("llama_stack") / "distribution/build_venv.sh"
|
||||||
"llama_stack", "distribution/build_venv.sh"
|
|
||||||
)
|
|
||||||
args = [
|
args = [
|
||||||
script,
|
script,
|
||||||
build_config.name,
|
build_config.name,
|
||||||
|
|
|
@ -4,13 +4,12 @@
|
||||||
# This source code is licensed under the terms described in the LICENSE file in
|
# This source code is licensed under the terms described in the LICENSE file in
|
||||||
# the root directory of this source tree.
|
# the root directory of this source tree.
|
||||||
|
|
||||||
|
import importlib.resources
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
import pkg_resources
|
|
||||||
import yaml
|
import yaml
|
||||||
from termcolor import colored
|
from termcolor import colored
|
||||||
|
|
||||||
|
@ -211,14 +210,13 @@ async def construct_stack(
|
||||||
|
|
||||||
|
|
||||||
def get_stack_run_config_from_template(template: str) -> StackRunConfig:
|
def get_stack_run_config_from_template(template: str) -> StackRunConfig:
|
||||||
template_path = pkg_resources.resource_filename(
|
template_path = (
|
||||||
"llama_stack", f"templates/{template}/run.yaml"
|
importlib.resources.files("llama_stack") / f"templates/{template}/run.yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not Path(template_path).exists():
|
with importlib.resources.as_file(template_path) as path:
|
||||||
|
if not path.exists():
|
||||||
raise ValueError(f"Template '{template}' not found at {template_path}")
|
raise ValueError(f"Template '{template}' not found at {template_path}")
|
||||||
|
run_config = yaml.safe_load(path.open())
|
||||||
with open(template_path) as f:
|
|
||||||
run_config = yaml.safe_load(f)
|
|
||||||
|
|
||||||
return StackRunConfig(**replace_env_vars(run_config))
|
return StackRunConfig(**replace_env_vars(run_config))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue