Skip to content

Commit

Permalink
Merge branch 'main' into feature/atkgen_custom_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
leondz committed Jul 18, 2024
2 parents 9015a33 + 70a6113 commit f6972a4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
17 changes: 16 additions & 1 deletion garak/generators/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from typing import List

import lorem

from garak.generators.base import Generator


Expand Down Expand Up @@ -47,4 +49,17 @@ def _call_model(self, prompt: str, generations_this_call: int = 1) -> List[str]:
)


DEFAULT_CLASS = "Blank"
class Lipsum(Generator):
"""Lorem Ipsum generator, so we can get non-zero outputs that vary"""

supports_multiple_generations = False
generator_family_name = "Test"
name = "Lorem Ipsum"

def _call_model(
self, prompt: str, generations_this_call: int = 1
) -> List[str | None]:
return [lorem.sentence() for i in range(generations_this_call)]


DEFAULT_CLASS = "Lipsum"
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies = [
"fschat>=0.2.36",
"litellm>=1.33.8",
"jsonpath-ng>=1.6.1",
"lorem==0.1.1"
]

[project.optional-dependencies]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ deepl==1.17.0
fschat>=0.2.36
litellm>=1.33.8
jsonpath-ng>=1.6.1
lorem==0.1.1
# tests
pytest>=8.0
requests-mock==1.12.1
Expand Down
35 changes: 35 additions & 0 deletions tests/generators/test_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import pytest

import garak._plugins
import garak.generators.base
import garak.generators.test

TEST_GENERATORS = [
a
for a, b in garak._plugins.enumerate_plugins("generators")
if b is True and a.startswith("generators.test")
]


@pytest.mark.parametrize("klassname", TEST_GENERATORS)
def test_test_instantiate(klassname):
g = garak._plugins.load_plugin(klassname)
assert isinstance(g, garak.generators.base.Generator)


@pytest.mark.parametrize("klassname", TEST_GENERATORS)
def test_test_gen(klassname):
g = garak._plugins.load_plugin(klassname)
for generations in (1, 50):
out = g.generate("", generations_this_call=generations)
assert isinstance(out, list), ".generate() must return a list"
assert (
len(out) == generations
), ".generate() must respect generations_per_call param"
for s in out:
assert (
isinstance(s, str) or s is None
), "generate()'s returned list's items must be string or None"

0 comments on commit f6972a4

Please sign in to comment.