Skip to content

Commit

Permalink
fail gracefully if nvcf rejects input; compact zalgo prompts (#509)
Browse files Browse the repository at this point in the history
* skip past prompts giving input value errors on nvcf

* reduce zalgo intensity to compact prompts
  • Loading branch information
leondz authored Feb 23, 2024
1 parent 7e7c030 commit 0a2367e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
4 changes: 1 addition & 3 deletions garak/buffs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def transform(
"""attempt copying is handled elsewhere. isn't that nice"""
yield self._derive_new_attempt(attempt)

def untransform(
self, attempt: garak.attempt.Attempt
) -> garak.attempt.Attempt:
def untransform(self, attempt: garak.attempt.Attempt) -> garak.attempt.Attempt:
"""Only necessary if buff requires a post buff hook"""
pass

Expand Down
2 changes: 1 addition & 1 deletion garak/buffs/low_resource_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def untransform(self, attempt: garak.attempt.Attempt) -> garak.attempt.Attempt:
translated_output = response.text
translated_outputs.append(translated_output)
attempt.outputs = translated_outputs
return attempt
return attempt
4 changes: 3 additions & 1 deletion garak/generators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def generate(self, prompt: str) -> List[str]:
for i in generation_iterator:
outputs.append(self._call_model(prompt))

cleaned_outputs = [o for o in outputs if o is not None]
cleaned_outputs = [
o for o in outputs if o is not None
] # "None" means no good response
outputs = cleaned_outputs

return outputs
14 changes: 11 additions & 3 deletions garak/generators/nvcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

"""NVCF LLM interface"""

import backoff
import json
import logging
import os
import requests
import time

import backoff
import requests

from garak import _config
from garak.generators.base import Generator

Expand Down Expand Up @@ -97,7 +99,13 @@ def _call_model(self, prompt: str) -> str:
if 400 <= response.status_code < 600:
logging.warning("nvcf : returned error code %s", response.status_code)
logging.warning("nvcf : returned error body %s", response.content)
response.raise_for_status()
if response.status_code == 500 and json.loads(response.content)[
"detail"
].startswith("Input value error"):
logging.warning("nvcf : skipping this prompt")
return None
else:
response.raise_for_status()

else:
response_body = response.json()
Expand Down
4 changes: 3 additions & 1 deletion garak/probes/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ class InjectZalgo(BaseEncodingProbe, Probe):
def zalgo(text: bytes) -> bytes:
from zalgolib.zalgolib import enzalgofy

zalged = enzalgofy(text=text.decode(), intensity=50)
zalged = enzalgofy(
text=text.decode(), intensity=20
) # default value of 50 explodes prompt length

return bytes(zalged, "utf-8")

Expand Down

0 comments on commit 0a2367e

Please sign in to comment.