Skip to content

Commit

Permalink
Merge branch 'main' into update/config_nested_dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
leondz committed Jul 5, 2024
2 parents cec0079 + e5d2458 commit fc06af8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
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
18 changes: 13 additions & 5 deletions garak/generators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from garak import _config
from garak.configurable import Configurable
import garak.resources.theme


class Generator(Configurable):
Expand Down Expand Up @@ -107,11 +108,12 @@ def generate(
if generations_this_call == 1:
outputs = self._call_model(prompt, 1)

if self.supports_multiple_generations:
elif self.supports_multiple_generations:
outputs = self._call_model(prompt, generations_this_call)

else:
outputs = []

if (
hasattr(_config.system, "parallel_requests")
and _config.system.parallel_requests
Expand All @@ -120,19 +122,25 @@ def generate(
):
from multiprocessing import Pool

bar = tqdm.tqdm(total=generations_this_call, leave=False)
bar.set_description(self.fullname[:55])
multi_generator_bar = tqdm.tqdm(
total=generations_this_call,
leave=False,
colour=f"#{garak.resources.theme.GENERATOR_RGB}",
)
multi_generator_bar.set_description(self.fullname[:55])

with Pool(_config.system.parallel_requests) as pool:
for result in pool.imap_unordered(
self._call_model, [prompt] * generations_this_call
):
outputs.append(result)
bar.update(1)
multi_generator_bar.update(1)

else:
generation_iterator = tqdm.tqdm(
list(range(generations_this_call)), leave=False
list(range(generations_this_call)),
leave=False,
colour=f"#{garak.resources.theme.GENERATOR_RGB}",
)
generation_iterator.set_description(self.fullname[:55])
for i in generation_iterator:
Expand Down
2 changes: 1 addition & 1 deletion garak/generators/nvcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def _call_model(
payload["seed"] = self.seed

request_time = time.time()
logging.debug("nvcf : payload %s", repr(payload))
response = session.post(self.invoke_url, headers=self.headers, json=payload)

while response.status_code == 202:
Expand All @@ -126,7 +127,6 @@ def _call_model(

if 400 <= response.status_code < 600:
logging.warning("nvcf : returned error code %s", response.status_code)
logging.warning("nvcf : payload %s", repr(payload))
logging.warning("nvcf : returned error body %s", response.content)
if response.status_code == 400 and prompt == "":
# error messages for refusing a blank prompt are fragile and include multi-level wrapped JSON, so this catch is a little broad
Expand Down
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

0 comments on commit fc06af8

Please sign in to comment.