Skip to content

Commit

Permalink
Fix retry with backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
shaltielshmid committed Aug 25, 2024
1 parent 7415803 commit 80b2677
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/lighteval/models/endpoint_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ def _async_process_request(
) -> Coroutine[None, list[TextGenerationOutput], str]:
# Todo: add an option to launch with conversational instead for chat prompts
# https://huggingface.co/docs/huggingface_hub/v0.20.3/en/package_reference/inference_client#huggingface_hub.AsyncInferenceClient.conversational
generated_text = self.async_client.text_generation(
generated_text = retry_with_backoff(lambda: self.async_client.text_generation(
prompt=context,
details=True,
decoder_input_details=True,
max_new_tokens=max_tokens,
stop_sequences=stop_tokens,
# truncate=,
)
))

return retry_with_backoff(generated_text)
return generated_text

def _process_request(self, context: str, stop_tokens: list[str], max_tokens: int) -> TextGenerationOutput:
# Todo: add an option to launch with conversational instead for chat prompts
Expand Down
2 changes: 1 addition & 1 deletion src/lighteval/models/oai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def _async_process_request(
self, context: str, stop_tokens: list[str], max_tokens: int
) -> Coroutine[None, TextGenerationOutput, str]:
# Todo: add an option to launch with conversational instead for chat prompts
output = await retry_with_backoff(self.client.completions.create(
output = await retry_with_backoff(lambda: self.client.completions.create(
model="/repository",
prompt=context,
max_tokens=max_tokens,
Expand Down
4 changes: 2 additions & 2 deletions src/lighteval/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def batched(iterable, n):
import random
MAX_RETRIES = 5
INITIAL_BACKOFF = 1
async def retry_with_backoff(coro):
async def retry_with_backoff(coro_fn):
for attempt in range(MAX_RETRIES):
try:
return await coro
return await coro_fn()
except Exception as e:
if attempt < MAX_RETRIES - 1:
backoff_time = INITIAL_BACKOFF * (2 ** attempt) + random.uniform(0, 1)
Expand Down

0 comments on commit 80b2677

Please sign in to comment.