From ed791ccaae2898f5d894ec108ea0544e6e4f1479 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Mon, 9 Sep 2024 22:11:03 -0700 Subject: [PATCH 1/6] install multiple validators --- guardrails/cli/hub/install.py | 17 ++++++++--------- guardrails/hub/install.py | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/guardrails/cli/hub/install.py b/guardrails/cli/hub/install.py index 3ad9a07d1..3062e4fff 100644 --- a/guardrails/cli/hub/install.py +++ b/guardrails/cli/hub/install.py @@ -1,5 +1,5 @@ import sys -from typing import Optional +from typing import Optional, List import typer @@ -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/spell_check", ), local_models: Optional[bool] = typer.Option( None, @@ -28,17 +28,16 @@ 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( "This validator has a Guardrails AI inference endpoint available. " - "Would you still like to install the" - " local models for local inference?", + "Would you still like to install the local models for local inference?", ) - install( - package_uri, + install_multiple( + package_uris, install_local_models=local_models, quiet=quiet, install_local_models_confirm=confirm, diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index 6ce9e16cf..e2002e78e 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -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, @@ -164,3 +164,37 @@ 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: + try: + 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) + except Exception as e: + cli_logger.error(f"Failed to install {package_uri}: {str(e)}") + + return installed_modules \ No newline at end of file From 5d4ca9446eda840b5436b74ce3f6a924d64a7181 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Mon, 9 Sep 2024 22:17:52 -0700 Subject: [PATCH 2/6] small fixes --- guardrails/cli/hub/install.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/guardrails/cli/hub/install.py b/guardrails/cli/hub/install.py index 3062e4fff..047f5dae9 100644 --- a/guardrails/cli/hub/install.py +++ b/guardrails/cli/hub/install.py @@ -12,7 +12,7 @@ def install( package_uris: List[str] = typer.Argument( ..., - help="URIs to the packages to install. Example: hub://guardrails/regex_match hub://guardrails/spell_check", + help="URIs to the packages to install. Example: hub://guardrails/regex_match hub://guardrails/toxic_language", ), local_models: Optional[bool] = typer.Option( None, @@ -33,7 +33,8 @@ def install( def confirm(): return typer.confirm( "This validator has a Guardrails AI inference endpoint available. " - "Would you still like to install the local models for local inference?", + "Would you still like to install the" + " local models for local inference?", ) install_multiple( From 4b797ce995ae692977a4cdc16aee14f77e20223b Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Mon, 9 Sep 2024 22:27:44 -0700 Subject: [PATCH 3/6] tests --- guardrails/hub/install.py | 2 +- tests/unit_tests/cli/hub/test_install.py | 47 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index e2002e78e..f35946d73 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -197,4 +197,4 @@ def install_multiple( except Exception as e: cli_logger.error(f"Failed to install {package_uri}: {str(e)}") - return installed_modules \ No newline at end of file + return installed_modules diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 21eb5abe9..481f8c97d 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -82,6 +82,49 @@ 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): @@ -208,7 +251,7 @@ 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 @@ -216,4 +259,4 @@ def test_get_site_packages_location(mocker): mock_pip_process.assert_called_once_with("show", "pip", format="json") - assert response == "/site-pacakges" + assert response == "/site-packages" From 1c68c0fc8179af6de344c67c852c3715c875d333 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Mon, 9 Sep 2024 23:09:34 -0700 Subject: [PATCH 4/6] lint --- guardrails/hub/install.py | 3 ++- tests/unit_tests/cli/hub/test_install.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) mode change 100644 => 100755 tests/unit_tests/cli/hub/test_install.py diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index f35946d73..745a996f4 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -165,6 +165,7 @@ def install( return installed_module + def install_multiple( package_uris: List[str], install_local_models=None, @@ -196,5 +197,5 @@ def install_multiple( installed_modules.append(installed_module) except Exception as e: cli_logger.error(f"Failed to install {package_uri}: {str(e)}") - + return installed_modules diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py old mode 100644 new mode 100755 index 481f8c97d..cd6263ca0 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -126,6 +126,7 @@ def test_install_multiple_validators_with_quiet(self, mocker): assert result.exit_code == 0 + class TestPipProcess: def test_no_package_string_format(self, mocker): mocker.patch("guardrails.cli.hub.utils.os.environ", return_value={}) From fcb446f056ac5d845a32bc079508363a8638b69c Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 07:51:09 -0700 Subject: [PATCH 5/6] fix install multiple --- guardrails/hub/install.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index 745a996f4..95f343ec7 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -187,15 +187,13 @@ def install_multiple( installed_modules = [] for package_uri in package_uris: - try: - 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) - except Exception as e: - cli_logger.error(f"Failed to install {package_uri}: {str(e)}") + 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 + From 241f4ad0feb2b37db2245c949e934527d4d43c75 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 07:56:37 -0700 Subject: [PATCH 6/6] lint --- guardrails/hub/install.py | 1 - 1 file changed, 1 deletion(-) diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index 95f343ec7..ce7d96ab1 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -196,4 +196,3 @@ def install_multiple( installed_modules.append(installed_module) return installed_modules -