Skip to content

Commit

Permalink
support module.classname config specification for plugins (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin-tech committed Feb 24, 2025
2 parents f8108e9 + 96f436b commit d78d975
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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

0 comments on commit d78d975

Please sign in to comment.