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

support module.classname config specification for plugins #1108

Merged
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
11 changes: 4 additions & 7 deletions garak/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _load_config(self, config_root=_config):
spec_type = namespace_parts[-2]
namespace = namespace_parts[-1]
classname = self.__class__.__name__
namespaced_klass = f"{namespace}.{classname}"
plugins_config = {}
if isinstance(local_root, dict) and spec_type in local_root:
plugins_config = local_root[spec_type]
Expand All @@ -43,16 +44,12 @@ def _load_config(self, config_root=_config):
# generators: `nim`/`openai`/`huggingface`
# probes: `dan`/`gcg`/`xss`/`tap`/`promptinject`
attributes = plugins_config[namespace]
namespaced_klass = f"{namespace}.{classname}"
self._apply_config(attributes)
if classname in attributes:
self._apply_config(attributes[classname])
elif namespaced_klass in plugins_config:
# for compatibility remove after
logging.warning(
f"Deprecated configuration key found: {namespaced_klass}"
)
self._apply_config(plugins_config[namespaced_klass])
elif namespaced_klass in plugins_config:
# maintain support for this as consistent with cli options at this time
self._apply_config(plugins_config[namespaced_klass])
self._apply_missing_instance_defaults()
if hasattr(self, "ENV_VAR"):
if not hasattr(self, "key_env_var"):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def test_config_root_only(generator_sub_config):
assert getattr(m, k) == v


# when a parameter is provided in config_root by `module.classname` set on the resulting object
def test_config_root_module_classname(generator_sub_config):
module_config = generator_sub_config.generators.pop("mock")
generator_sub_config.generators["mock.mockConfigurable"] = module_config
m = mockConfigurable(config_root=generator_sub_config)
for k, v in generator_sub_config.generators["mock.mockConfigurable"].items():
assert getattr(m, k) == v


# when a parameter is provided in config_root as a dict set on the resulting object
def test_config_root_as_dict(generator_sub_config):
config = {"generators": generator_sub_config.generators}
Expand Down