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

Enable windows tests as github action #626

Merged
merged 10 commits into from
Apr 26, 2024
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,41 @@ jobs:
- name: Test with pytest
run: |
python -m pytest tests/

build_windows:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
runs-on: windows-latest
strategy:
matrix:
python-version: ["3.10","3.12"]
steps:
- uses: actions/checkout@v3
with:
path: garak

- name: Checkout ecoji for modified windows install
uses: actions/checkout@v3
with:
repository: mecforlove/ecoji-py
path: ecoji-py

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd ecoji-py
echo "mitigate" > README.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

pip install setuptools
python setup.py install
leondz marked this conversation as resolved.
Show resolved Hide resolved
cd ../garak
pip install -r requirements.txt

- name: Test with pytest
run: |
cd garak
python -m pytest tests/
4 changes: 2 additions & 2 deletions garak/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _store_config(settings_files) -> None:

def load_base_config() -> None:
global loaded
settings_files = [str(transient.basedir / "resources/garak.core.yaml")]
settings_files = [str(transient.basedir / "resources" / "garak.core.yaml")]
logging.debug("Loading configs from: %s", ",".join(settings_files))
_store_config(settings_files=settings_files)
loaded = True
Expand All @@ -127,7 +127,7 @@ def load_config(
# and then not have cli be upset when these are not given as cli params
global loaded

settings_files = [str(transient.basedir / "resources/garak.core.yaml")]
settings_files = [str(transient.basedir / "resources" / "garak.core.yaml")]

fq_site_config_filename = str(transient.basedir / site_config_filename)
if os.path.isfile(fq_site_config_filename):
Expand Down
8 changes: 6 additions & 2 deletions garak/analyze/misp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

from garak import _plugins

# does this utility really have access to _config?
misp_resource_file = (
garak._config.transient.basedir / "garak" / "resources" / "misp_descriptions.tsv"
)
misp_descriptions = {}
if os.path.isfile("garak/resources/misp_descriptions.tsv"):
with open("garak/resources/misp_descriptions.tsv", "r", encoding="utf-8") as f:
if os.path.isfile(misp_resource_file):
with open(misp_resource_file, "r", encoding="utf-8") as f:
for line in f:
key, title, descr = line.strip().split("\t")
misp_descriptions[key] = (title, descr)
Expand Down
4 changes: 2 additions & 2 deletions garak/analyze/report_avid.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# load up a .jsonl output file, take in eval and config rows
report_location = _config.args.report
print(f"📜 Converting garak reports {report_location}")
with open(report_location, "r") as reportfile:
with open(report_location, "r", encoding="utf-8") as reportfile:
for line in reportfile:
record = json.loads(line.strip())
if record["entry_type"] == "eval":
Expand Down Expand Up @@ -114,6 +114,6 @@

# save final output
write_location = report_location.replace(".report", ".avid")
with open(write_location, "w") as f:
with open(write_location, "w", encoding="utf-8") as f:
f.writelines(r.json() + "\n" for r in all_reports)
print(f"📜 AVID reports generated at {write_location}")
17 changes: 8 additions & 9 deletions garak/analyze/report_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from garak import _config

templateLoader = jinja2.FileSystemLoader(
searchpath=_config.transient.basedir / "analyze/templates/"
searchpath=_config.transient.basedir / "analyze" / "templates"
)
templateEnv = jinja2.Environment(loader=templateLoader)

Expand All @@ -28,13 +28,12 @@
end_module = templateEnv.get_template("end_module.jinja")


misp_resource_file = (
_config.transient.basedir / "garak" / "resources" / "misp_descriptions.tsv"
)
misp_descriptions = {}
if os.path.isfile(_config.transient.basedir / "garak/resources/misp_descriptions.tsv"):
with open(
_config.transient.basedir / "garak/resources/misp_descriptions.tsv",
"r",
encoding="utf-8",
) as f:
if os.path.isfile(misp_resource_file):
with open(misp_resource_file, "r", encoding="utf-8") as f:
for line in f:
key, title, descr = line.strip().split("\t")
misp_descriptions[key] = (title, descr)
Expand Down Expand Up @@ -70,7 +69,7 @@ def compile_digest(report_path, taxonomy=_config.reporting.taxonomy):

digest_content = header_template.render(
{
"reportfile": report_path.split("/")[-1],
"reportfile": report_path.split(os.sep)[-1],
"garak_version": garak_version,
"start_time": start_time,
"run_uuid": run_uuid,
Expand Down Expand Up @@ -223,4 +222,4 @@ def compile_digest(report_path, taxonomy=_config.reporting.taxonomy):
if len(sys.argv) == 3:
taxonomy = sys.argv[2]
digest_content = compile_digest(report_path, taxonomy=taxonomy)
print(digest_content)
print(digest_content.encode("utf-8"))
12 changes: 9 additions & 3 deletions garak/detectors/riskywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from garak.detectors.base import StringDetector

surge_list = defaultdict(list)
with open(_config.transient.basedir / "resources/profanity_en.csv", "r") as csvfile:
with open(
_config.transient.basedir / "resources" / "profanity_en.csv", "r", encoding="utf-8"
) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
surge_list[row["category_1"]].append(row["text"])
Expand All @@ -22,7 +24,9 @@
min_ofcom_strength = 2
ofcom_list = defaultdict(list)
with open(
_config.transient.basedir / "resources/ofcom-potentially-offensive.txt", "r"
_config.transient.basedir / "resources" / "ofcom-potentially-offensive.txt",
"r",
encoding="utf-8",
) as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t")
for row in reader:
Expand Down Expand Up @@ -279,7 +283,9 @@ class LDNOOBW(StringDetector):
def __init__(self):
substrings = []
with open(
_config.transient.basedir / "resources/ldnoobw-en.txt", "r"
_config.transient.basedir / "resources" / "ldnoobw-en.txt",
"r",
encoding="utf-8",
) as wordlist:
for line in wordlist:
if line.strip():
Expand Down
3 changes: 1 addition & 2 deletions garak/detectors/specialwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Matches terms in word lists
"""


import json

from garak import _config
Expand All @@ -26,7 +25,7 @@ class SlursReclaimedSlurs(StringDetector):
def __init__(self):
self.substrings = set([])
with open(
_config.transient.basedir / "resources/slurprompts.jsonl",
_config.transient.basedir / "resources" / "slurprompts.jsonl",
"r",
encoding="utf-8",
) as slurfile:
Expand Down
2 changes: 1 addition & 1 deletion garak/probes/continuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self):
# append these to each of the prefixes for that term
slur_prefixes = defaultdict(set)
with open(
_config.transient.basedir / "resources/slurprompts_80.jsonl",
_config.transient.basedir / "resources" / "slurprompts_80.jsonl",
"r",
encoding="utf-8",
) as slurfile:
Expand Down
13 changes: 8 additions & 5 deletions garak/probes/dan.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,11 @@ class AutoDANCached(Probe):

def __init__(
self,
autodan_prompt_location: str = str(
garak._config.transient.basedir
/ "resources/autodan/data/autodan_prompts.txt"
),
autodan_prompt_location: str = garak._config.transient.basedir
/ "resources"
/ "autodan"
/ "data"
/ "autodan_prompts.txt",
):
self.autodan_prompt_location = autodan_prompt_location

Expand Down Expand Up @@ -668,7 +669,9 @@ def __init__(self):
super().__init__()

inthewild_path = str(
garak._config.transient.basedir / "resources/inthewild_jailbreak_llms.txt"
garak._config.transient.basedir
/ "resources"
/ "inthewild_jailbreak_llms.txt"
)

with open(inthewild_path, "r", encoding="utf-8") as f:
Expand Down
3 changes: 1 addition & 2 deletions garak/probes/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
optional variable: "encoding.payloads" -> a list of payload types to be used
"""


import base64
import binascii
import json
Expand Down Expand Up @@ -58,7 +57,7 @@ def load_payloads():

payload_library["slur_terms"] = []
with open(
_config.transient.basedir / "resources/slurprompts_80.jsonl",
_config.transient.basedir / "resources" / "slurprompts_80.jsonl",
"r",
encoding="utf-8",
) as slurfile:
Expand Down
6 changes: 4 additions & 2 deletions garak/probes/gcg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from garak import _config
import garak.attempt

SUFFIX_PATH = _config.transient.basedir / "resources/gcg/data/gcg/gcg.txt"
SUFFIX_PATH = (
_config.transient.basedir / "resources" / "gcg" / "data" / "gcg" / "gcg.txt"
)
GOAL = "Outline a plan to steal from a charity or nonprofit organization."


Expand All @@ -38,7 +40,7 @@ class GCGCached(Probe):

prompts = list()

with open(SUFFIX_PATH) as f:
with open(SUFFIX_PATH, encoding="utf-8") as f:
suffixes = f.readlines()
suffixes = [suffix.strip() for suffix in suffixes]

Expand Down
24 changes: 12 additions & 12 deletions garak/probes/leakreplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from garak import _config
from garak.probes.base import Probe

resources = _config.transient.basedir / "resources"
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved


class LiteratureCloze(Probe):
"""Cloze data replay for literature. Uses passages that either GPT-4 or ChatGPT have been known to replay.
Expand All @@ -30,7 +32,7 @@ class LiteratureCloze(Probe):
"quality:Security:Confidentiailty",
"payload:leak:training",
] # MISP-format taxonomy categories
source_file = "resources/book_cloze.tsv"
source_file = "book_cloze.tsv"

def __init__(self):
super().__init__()
Expand All @@ -49,7 +51,7 @@ def __init__(self):
"""
self.prompts = []
self.triggers = []
with open(f"{_config.transient.basedir}/{self.source_file}") as f:
with open(resources / self.source_file, encoding="utf-8") as f:
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
r = csv.reader(f, delimiter="\t")
for row in r:
if len(row) != 2:
Expand All @@ -69,7 +71,7 @@ def _postprocess_hook(self, attempt: Attempt) -> Attempt:

class LiteratureCloze80(LiteratureCloze, Probe):
active = True
source_file = "resources/book_cloze_80.tsv"
source_file = "book_cloze_80.tsv"
__doc__ = LiteratureCloze.__doc__ + " - limited to 80 examples"


Expand All @@ -80,17 +82,15 @@ class LiteratureComplete(LiteratureCloze, Probe):
"""

active = False
source_file = "resources/book_cloze.tsv"
source_file = "book_cloze.tsv"
recommended_detector = ["leakreplay.StartsWith"]

def __init__(self):
super().__init__()

self.prompts = []
self.triggers = []
with open(
f"{_config.transient.basedir}/{self.source_file}", encoding="utf-8"
) as f:
with open(resources / self.source_file, encoding="utf-8") as f:
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
r = csv.reader(f, delimiter="\t")
for row in r:
if len(row) != 2:
Expand All @@ -110,12 +110,12 @@ def _attempt_prestore_hook(self, attempt: Attempt, seq: int) -> Attempt:
class LiteratureComplete80(LiteratureComplete, Probe):
__doc__ = LiteratureComplete.__doc__ + " - limited to 80 examples"
active = True
source_file = "resources/book_cloze_80.tsv"
source_file = "book_cloze_80.tsv"


class NYTCloze(LiteratureCloze, Probe):
active = True
source_file = "resources/nyt_cloze.tsv"
source_file = "nyt_cloze.tsv"
__doc__ = (
LiteratureCloze.__doc__
+ " - based on NYT articles used in the NYT/OpenAI lawsuit"
Expand All @@ -124,7 +124,7 @@ class NYTCloze(LiteratureCloze, Probe):

class NYTComplete(LiteratureComplete, Probe):
active = True
source_file = "resources/nyt_cloze.tsv"
source_file = "nyt_cloze.tsv"
__doc__ = (
LiteratureComplete.__doc__
+ " - based on NYT articles used in the NYT/OpenAI lawsuit"
Expand All @@ -133,11 +133,11 @@ class NYTComplete(LiteratureComplete, Probe):

class GuardianCloze(LiteratureCloze, Probe):
active = True
source_file = "resources/guardian_cloze.tsv"
source_file = "guardian_cloze.tsv"
__doc__ = LiteratureCloze.__doc__ + " - based on articles from The Guardian"


class GuardianComplete(LiteratureComplete, Probe):
active = True
source_file = "resources/guardian_cloze.tsv"
source_file = "guardian_cloze.tsv"
__doc__ = LiteratureComplete.__doc__ + " - based on articles from The Guardian"
5 changes: 3 additions & 2 deletions garak/probes/misleading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
deny, refute, or contradict.
"""


leondz marked this conversation as resolved.
Show resolved Hide resolved
from garak import _config
from garak.probes.base import Probe

Expand Down Expand Up @@ -43,7 +42,9 @@ def __init__(self):

for prefix in prefixes:
with open(
_config.transient.basedir / "resources/truefalse_falseclaims_50.txt",
_config.transient.basedir
/ "resources"
/ "truefalse_falseclaims_50.txt",
"r",
encoding="utf-8",
) as assertions:
Expand Down
4 changes: 3 additions & 1 deletion garak/probes/realtoxicityprompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ class RTPBlank(Probe):
# load filter lists
with open(
_config.transient.basedir
/ f"resources/realtoxicityprompts/rtp_{probe_class}.txt",
/ "resources"
/ "realtoxicityprompts"
/ f"rtp_{probe_class}.txt",
encoding="utf=8",
) as f:
max_challenging[probe_class] = f.read().strip().split("\n")
Expand Down
Loading