From d09f4f42213fda69f852e4bfd453718027faa64f Mon Sep 17 00:00:00 2001 From: Sai Krishna Vishnumolakala Date: Mon, 27 Jan 2025 21:50:26 +0000 Subject: [PATCH] Fix AttributeError: Update OpenAI error imports (Closes #1564) --- evals/utils/api_utils.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/evals/utils/api_utils.py b/evals/utils/api_utils.py index a45476c9af..6113a852ac 100644 --- a/evals/utils/api_utils.py +++ b/evals/utils/api_utils.py @@ -1,22 +1,33 @@ import logging import os - import backoff +import requests + +from openai import APIError, APIConnectionError, APITimeoutError, RateLimitError EVALS_THREAD_TIMEOUT = float(os.environ.get("EVALS_THREAD_TIMEOUT", "40")) -logging.getLogger("httpx").setLevel(logging.WARNING) # suppress "OK" logs from openai API calls +logging.getLogger("httpx").setLevel(logging.WARNING) # Suppress "OK" logs from OpenAI API +RETRY_ERRORS = ( + APIConnectionError, + APIError, + APITimeoutError, + RateLimitError, + requests.exceptions.ConnectionError, + requests.exceptions.Timeout, +) @backoff.on_predicate( wait_gen=backoff.expo, max_value=60, factor=1.5, ) -def create_retrying(func: callable, retry_exceptions: tuple[Exception], *args, **kwargs): +def create_retrying(func: callable, retry_exceptions: tuple[Exception] = RETRY_ERRORS, *args, **kwargs): """ Retries given function if one of given exceptions is raised """ try: return func(*args, **kwargs) - except retry_exceptions: - return False + except retry_exceptions as e: + logging.warning(f"Retrying due to error: {str(e)}") + return False \ No newline at end of file