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

generator: lorem ipsum #776

Merged
merged 8 commits into from
Jul 18, 2024
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
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"
Loading