|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | """garak global config"""
|
3 | 3 |
|
| 4 | +# SPDX-FileCopyrightText: Portions Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +# plugin code < base config < site config < run config < cli params |
| 8 | + |
| 9 | +# logging should be set up before config is loaded |
| 10 | + |
| 11 | +from dataclasses import dataclass |
| 12 | +import logging |
| 13 | +import os |
4 | 14 | import pathlib
|
| 15 | +from typing import List |
| 16 | +import yaml |
| 17 | + |
| 18 | +version = -1 # eh why this is here? hm. who references it |
| 19 | + |
| 20 | +system_params = ( |
| 21 | + "verbose report_prefix narrow_output parallel_requests parallel_attempts".split() |
| 22 | +) |
| 23 | +run_params = "seed deprefix eval_threshold generations".split() |
| 24 | +plugins_params = "model_type model_name extended_detectors".split() |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class GarakSubConfig: |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class TransientConfig(GarakSubConfig): |
| 34 | + """Object to hold transient global config items not set externally""" |
| 35 | + |
| 36 | + report_filename = None |
| 37 | + reportfile = None |
| 38 | + hitlogfile = None |
| 39 | + args = None # only access this when determining what was passed on CLI |
| 40 | + run_id = None |
| 41 | + basedir = pathlib.Path(__file__).parents[0] |
| 42 | + starttime = None |
| 43 | + starttime_iso = None |
| 44 | + |
| 45 | + |
| 46 | +transient = TransientConfig() |
| 47 | + |
| 48 | +system = GarakSubConfig() |
| 49 | +run = GarakSubConfig() |
| 50 | +plugins = GarakSubConfig() |
| 51 | +plugins.probes = {} |
| 52 | +plugins.generators = {} |
| 53 | +plugins.detectors = {} |
| 54 | +plugins.buffs = {} |
| 55 | + |
| 56 | +# this is so popular, let's set a default. what other defaults are worth setting? what's the policy? |
| 57 | +run.seed = None |
| 58 | + |
| 59 | +# placeholder |
| 60 | +# generator, probe, detector, buff = {}, {}, {}, {} |
| 61 | + |
| 62 | + |
| 63 | +def _set_settings(config_obj, settings_obj: dict): |
| 64 | + for k, v in settings_obj.items(): |
| 65 | + setattr(config_obj, k, v) |
| 66 | + return config_obj |
| 67 | + |
| 68 | + |
| 69 | +def _combine_into(d: dict, combined: dict) -> None: |
| 70 | + for k, v in d.items(): |
| 71 | + if isinstance(v, dict): |
| 72 | + _combine_into(v, combined.setdefault(k, {})) |
| 73 | + else: |
| 74 | + combined[k] = v |
| 75 | + return combined |
| 76 | + |
| 77 | + |
| 78 | +def _load_yaml_config(settings_filenames) -> dict: |
| 79 | + config = {} |
| 80 | + for settings_filename in settings_filenames: |
| 81 | + settings = yaml.safe_load(open(settings_filename, encoding="utf-8")) |
| 82 | + if settings is not None: |
| 83 | + config = _combine_into(settings, config) |
| 84 | + return config |
| 85 | + |
| 86 | + |
| 87 | +def _store_config(settings_files) -> None: |
| 88 | + global system, run, plugins |
| 89 | + settings = _load_yaml_config(settings_files) |
| 90 | + system = _set_settings(system, settings["system"]) |
| 91 | + run = _set_settings(run, settings["run"]) |
| 92 | + plugins = _set_settings(plugins, settings["plugins"]) |
| 93 | + |
| 94 | + |
| 95 | +def load_base_config() -> None: |
| 96 | + global system |
| 97 | + settings_files = [str(transient.basedir / "resources/garak.core.yaml")] |
| 98 | + logging.debug("Loading configs from: %s", ",".join(settings_files)) |
| 99 | + _store_config(settings_files=settings_files) |
| 100 | + |
| 101 | + |
| 102 | +def load_config( |
| 103 | + site_config_filename="garak.site.yaml", run_config_filename=None |
| 104 | +) -> None: |
| 105 | + # would be good to bubble up things from run_config, e.g. generator, probe(s), detector(s) |
| 106 | + # and then not have cli be upset when these are not given as cli params |
| 107 | + global system, run, plugins |
| 108 | + |
| 109 | + settings_files = [str(transient.basedir / "resources/garak.core.yaml")] |
| 110 | + |
| 111 | + fq_site_config_filename = str(transient.basedir / site_config_filename) |
| 112 | + if os.path.isfile(fq_site_config_filename): |
| 113 | + settings_files.append(fq_site_config_filename) |
| 114 | + else: |
| 115 | + # warning, not error, because this one has a default value |
| 116 | + logging.warning("site config not found: %s", fq_site_config_filename) |
| 117 | + |
| 118 | + if run_config_filename is not None: |
| 119 | + fq_run_config_filename = str(transient.basedir / run_config_filename) |
| 120 | + if os.path.isfile(fq_run_config_filename): |
| 121 | + settings_files.append(fq_run_config_filename) |
| 122 | + else: |
| 123 | + logging.error("run config not found: %s", fq_run_config_filename) |
| 124 | + |
| 125 | + logging.debug("Loading configs from: %s", ",".join(settings_files)) |
| 126 | + _store_config(settings_files=settings_files) |
| 127 | + |
| 128 | + |
| 129 | +def parse_plugin_spec(spec: str, category: str) -> List[str]: |
| 130 | + from garak._plugins import enumerate_plugins |
| 131 | + |
| 132 | + if spec is None or spec.lower() in ("", "auto", "none"): |
| 133 | + return [] |
| 134 | + if spec.lower() == "all": |
| 135 | + plugin_names = [ |
| 136 | + name |
| 137 | + for name, active in enumerate_plugins(category=category) |
| 138 | + if active is True |
| 139 | + ] |
| 140 | + else: |
| 141 | + plugin_names = [] |
| 142 | + for clause in spec.split(","): |
| 143 | + if clause.count(".") < 1: |
| 144 | + plugin_names += [ |
| 145 | + p |
| 146 | + for p, a in enumerate_plugins(category=category) |
| 147 | + if p.startswith(f"{category}.{clause}.") and a is True |
| 148 | + ] |
| 149 | + else: |
| 150 | + plugin_names += [f"{category}.{clause}"] # spec parsing |
5 | 151 |
|
6 |
| -basedir = pathlib.Path(__file__).parents[0] |
7 |
| - |
8 |
| -args = None |
9 |
| -reportfile = None |
10 |
| -hitlogfile = None |
11 |
| -seed = None |
12 |
| -run_id = None |
13 |
| -generator_model = None |
14 |
| -generator_name = None |
15 |
| -probe_options = {} |
16 |
| -generator_options = {} |
| 152 | + return plugin_names |
0 commit comments