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

move REST generator exception into garak.exceptions #779

Merged
merged 1 commit into from
Jul 5, 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
6 changes: 6 additions & 0 deletions garak/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ class BadGeneratorException(GarakException):
"""Generator config/description is not usable"""

pass


class RateLimitHit(Exception):
"""Raised when a rate limiting response is returned"""

pass
14 changes: 3 additions & 11 deletions garak/generators/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@
from jsonpath_ng.exceptions import JsonPathParserError

from garak import _config
from garak.exception import APIKeyMissingError
from garak.exception import APIKeyMissingError, RateLimitHit
from garak.generators.base import Generator


class RESTRateLimitError(Exception):
"""Raised when a rate limiting response is returned"""

pass


class RestGenerator(Generator):
"""Generic API interface for REST models

Expand Down Expand Up @@ -247,7 +241,7 @@ def _populate_template(
return output.replace("$INPUT", self.escape_function(text))

# we'll overload IOError as the rate limit exception
@backoff.on_exception(backoff.fibo, RESTRateLimitError, max_value=70)
@backoff.on_exception(backoff.fibo, RateLimitHit, max_value=70)
def _call_model(
self, prompt: str, generations_this_call: int = 1
) -> List[Union[str, None]]:
Expand All @@ -274,9 +268,7 @@ def _call_model(
}
resp = self.http_function(self.uri, **req_kArgs)
if resp.status_code in self.ratelimit_codes:
raise RESTRateLimitError(
f"Rate limited: {resp.status_code} - {resp.reason}"
)
raise RateLimitHit(f"Rate limited: {resp.status_code} - {resp.reason}")

elif str(resp.status_code)[0] == "3":
raise NotImplementedError(
Expand Down
Loading