Skip to content
Open
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
6 changes: 4 additions & 2 deletions garak/generators/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ class CohereGenerator(Generator):
generator_family_name = "Cohere"

def __init__(self, name="command", config_root=_config):
self.name = name
self._load_config(config_root)
if name != "command" or not hasattr(self, "name"): # "command" is the default
self.name = name
self.fullname = f"Cohere {self.name}"

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

logging.debug(
"Cohere generation request limit capped at %s", COHERE_GENERATION_LIMIT
Expand Down
5 changes: 3 additions & 2 deletions garak/generators/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def __init__(
):
if len(kwargs) > 0:
self.kwargs = kwargs.copy()
self.name = name # if the user's function requires `name` it would have been extracted from kwargs and will not be passed later
self._load_config(config_root)
if name or not hasattr(self, "name"): # name is required for function generators
self.name = name

gen_module_name, gen_function_name = self.name.split("#")

Expand All @@ -89,7 +90,7 @@ def __init__(
'Incompatible function signature: "name" is incompatible with this Generator'
)

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

def _call_model(
self, prompt: Conversation, generations_this_call: int = 1
Expand Down
5 changes: 3 additions & 2 deletions garak/generators/ggml.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def _command_args_list(self):
return command_list

def __init__(self, name="", config_root=_config):
self.name = name
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name

if not hasattr(self, "path_to_ggml_main") or self.path_to_ggml_main is None:
self.path_to_ggml_main = os.getenv(self.key_env_var)
Expand Down Expand Up @@ -100,7 +101,7 @@ def __init__(self, name="", config_root=_config):
if magic_num != GGUF_MAGIC:
raise RuntimeError(f"{self.name} is not in GGUF format")

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

def _validate_env_var(self):
pass # suppress default behavior for api_key
Expand Down
5 changes: 3 additions & 2 deletions garak/generators/guardrails.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ def __init__(self, name="", config_root=_config):
"You must first install NeMo Guardrails using `pip install nemoguardrails`."
) from e

self.name = name
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name
self.fullname = f"Guardrails {self.name}"

# Currently, we use the target_name as the path to the config
with redirect_stderr(io.StringIO()) as f: # quieten the tqdm
config = RailsConfig.from_path(self.name)
self.rails = LLMRails(config=config)

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

def _call_model(
self, prompt: Conversation, generations_this_call: int = 1
Expand Down
14 changes: 9 additions & 5 deletions garak/generators/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class Pipeline(Generator, HFCompatible):
parallel_capable = False

def __init__(self, name="", config_root=_config):
self.name = name
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

import torch.multiprocessing as mp

Expand Down Expand Up @@ -224,8 +226,10 @@ class InferenceAPI(Generator):
}

def __init__(self, name="", config_root=_config):
self.name = name
super().__init__(self.name, config_root=config_root)
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name
super().__init__(name, config_root=config_root)

self.uri = self.URI + self.name

Expand Down Expand Up @@ -555,7 +559,7 @@ def __init__(self, name="", config_root=_config):
raise TargetNameMissingError(
f"Invalid model name {self.name}, current support: {self.supported_models}."
)
super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration

Expand Down
5 changes: 3 additions & 2 deletions garak/generators/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ class LangChainLLMGenerator(Generator):
generator_family_name = "LangChain"

def __init__(self, name="", config_root=_config):
self.name = name
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name
self.fullname = f"LangChain LLM {self.name}"

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

try:
# this might need some special handling to allow tests
Expand Down
9 changes: 3 additions & 6 deletions garak/generators/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,18 @@ class LiteLLMGenerator(Generator):
)

def __init__(self, name: str = "", generations: int = 10, config_root=_config):
self.name = name
self.api_base = None
self.provider = None
self._load_config(config_root)

# Ensure suppressed_params is a set for efficient lookup
self.suppressed_params = set(self.suppressed_params)

Comment on lines -117 to -120
Copy link
Collaborator

Choose a reason for hiding this comment

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

Changes should not remove code not related to the issue.

if name or not hasattr(self, "name"):
self.name = name
self.fullname = f"LiteLLM {self.name}"
self.supports_multiple_generations = not any(
self.name.startswith(provider)
for provider in unsupported_multiple_gen_providers
)

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

@backoff.on_exception(backoff.fibo, litellm.exceptions.APIError, max_value=70)
def _call_model(
Expand Down
5 changes: 3 additions & 2 deletions garak/generators/nemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ class NeMoGenerator(Generator):
generator_family_name = "NeMo"

def __init__(self, name=None, config_root=_config):
self.name = name
self.org_id = None
self._load_config(config_root)
if name is not None or not hasattr(self, "name"):
self.name = name
self.fullname = f"NeMo {self.name}"

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

self.nemo = nemollm.api.NemoLLM(
api_host=self.api_uri, api_key=self.api_key, org_id=self.org_id
Expand Down
5 changes: 3 additions & 2 deletions garak/generators/nvcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class NvcfChat(Generator):
generator_family_name = "NVCF"

def __init__(self, name=None, config_root=_config):
self.name = name
self._load_config(config_root)
if name is not None or not hasattr(self, "name"):
self.name = name
self.fullname = (
f"{self.generator_family_name} {self.__class__.__name__} {self.name}"
)
Expand All @@ -54,7 +55,7 @@ def __init__(self, name=None, config_root=_config):
if self.version_id is not None:
self.invoke_uri += f"/versions/{self.version_id}"

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

self.headers = {
"Authorization": f"Bearer {self.api_key}",
Expand Down
10 changes: 6 additions & 4 deletions garak/generators/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ def _validate_config(self):
pass

def __init__(self, name="", config_root=_config):
self.name = name
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name
self.fullname = f"{self.generator_family_name} {self.name}"
self.key_env_var = self.ENV_VAR

Expand All @@ -191,7 +192,7 @@ def __init__(self, name="", config_root=_config):

self._validate_config()

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)

# clear client config to enable object to `pickle`
self._clear_client()
Expand Down Expand Up @@ -335,12 +336,13 @@ def _load_client(self):
raise garak.exception.BadGeneratorException("🛑 " + msg)

def __init__(self, name="", config_root=_config):
self.name = name
self._load_config(config_root)
if name or not hasattr(self, "name"):
self.name = name
if self.name in context_lengths:
self.context_len = context_lengths[self.name]

super().__init__(self.name, config_root=config_root)
super().__init__(name, config_root=config_root)


class OpenAIReasoningGenerator(OpenAIGenerator):
Expand Down
2 changes: 1 addition & 1 deletion garak/generators/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def __init__(self, uri=None, config_root=_config):
)
raise e

super().__init__(self.name, config_root=config_root)
super().__init__(uri, config_root=config_root)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Multiple earlier locations already validated what self.name should be. This revision would change expected behavior.


def _validate_env_var(self):
key_match = "$KEY"
Expand Down