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

Install multiple validators through hub cli #1057

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions guardrails/cli/hub/install.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Optional
from typing import Optional, List

import typer

Expand All @@ -10,9 +10,9 @@

@hub_command.command()
def install(
package_uri: str = typer.Argument(
help="URI to the package to install.\
Example: hub://guardrails/regex_match."
package_uris: List[str] = typer.Argument(
...,
help="URIs to the packages to install. Example: hub://guardrails/regex_match hub://guardrails/toxic_language",
),
local_models: Optional[bool] = typer.Option(
None,
Expand All @@ -28,7 +28,7 @@ def install(
):
try:
trace_if_enabled("hub/install")
from guardrails.hub.install import install
from guardrails.hub.install import install_multiple

def confirm():
return typer.confirm(
Expand All @@ -37,8 +37,8 @@ def confirm():
" local models for local inference?",
)

install(
package_uri,
install_multiple(
package_uris,
install_local_models=local_models,
quiet=quiet,
install_local_models_confirm=confirm,
Expand Down
34 changes: 33 additions & 1 deletion guardrails/hub/install.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from contextlib import contextmanager
from string import Template
from typing import Callable, cast
from typing import Callable, cast, List

from guardrails.hub.validator_package_service import (
ValidatorPackageService,
Expand Down Expand Up @@ -164,3 +164,35 @@ def install(
installed_module.__validator_exports__ = module_manifest.exports

return installed_module


def install_multiple(
package_uris: List[str],
install_local_models=None,
quiet: bool = True,
install_local_models_confirm: Callable = default_local_models_confirm,
) -> List[ValidatorModuleType]:
"""Install multiple validator packages from hub URIs.

Args:
package_uris (List[str]): List of URIs of the packages to install.
install_local_models (bool): Whether to install local models or not.
quiet (bool): Whether to suppress output or not.
install_local_models_confirm (Callable): A function to confirm the
installation of local models.

Returns:
List[ValidatorModuleType]: List of installed validator modules.
"""
installed_modules = []

for package_uri in package_uris:
installed_module = install(
package_uri,
install_local_models=install_local_models,
quiet=quiet,
install_local_models_confirm=install_local_models_confirm,
)
installed_modules.append(installed_module)

return installed_modules
48 changes: 46 additions & 2 deletions tests/unit_tests/cli/hub/test_install.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,50 @@ def test_install_quiet(self, mocker):

assert result.exit_code == 0

def test_install_multiple_validators(self, mocker):
mock_install_multiple = mocker.patch("guardrails.hub.install.install_multiple")
runner = CliRunner()
result = runner.invoke(
hub_command,
[
"install",
"hub://guardrails/validator1",
"hub://guardrails/validator2",
"--no-install-local-models",
],
)

mock_install_multiple.assert_called_once_with(
["hub://guardrails/validator1", "hub://guardrails/validator2"],
install_local_models=False,
quiet=False,
install_local_models_confirm=ANY,
)

assert result.exit_code == 0

def test_install_multiple_validators_with_quiet(self, mocker):
mock_install_multiple = mocker.patch("guardrails.hub.install.install_multiple")
runner = CliRunner()
result = runner.invoke(
hub_command,
[
"install",
"hub://guardrails/validator1",
"hub://guardrails/validator2",
"--quiet",
],
)

mock_install_multiple.assert_called_once_with(
["hub://guardrails/validator1", "hub://guardrails/validator2"],
install_local_models=None,
quiet=True,
install_local_models_confirm=ANY,
)

assert result.exit_code == 0


class TestPipProcess:
def test_no_package_string_format(self, mocker):
Expand Down Expand Up @@ -208,12 +252,12 @@ def test_other_exception(self, mocker):

def test_get_site_packages_location(mocker):
mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process")
mock_pip_process.return_value = {"Location": "/site-pacakges"}
mock_pip_process.return_value = {"Location": "/site-packages"}

from guardrails.cli.hub.utils import get_site_packages_location

response = get_site_packages_location()

mock_pip_process.assert_called_once_with("show", "pip", format="json")

assert response == "/site-pacakges"
assert response == "/site-packages"
Loading