-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unified create_retrying for all solvers (#1501)
We're now implementing solvers for new APIs we're calling (Anthropic, Gemini, ...). Each solver was implementing the same logic for backing off and retrying when the API query limit was hit. This PR created a generic create_retrying function, which retries when specific exceptions are raised. These exceptions are passed as arguments. This uses the changes from #1482
- Loading branch information
Showing
3 changed files
with
59 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,22 @@ | ||
""" | ||
This file defines various helper functions for interacting with the OpenAI API. | ||
""" | ||
import logging | ||
import os | ||
|
||
import backoff | ||
import openai | ||
from openai import OpenAI | ||
|
||
EVALS_THREAD_TIMEOUT = float(os.environ.get("EVALS_THREAD_TIMEOUT", "40")) | ||
logging.getLogger("httpx").setLevel(logging.WARNING) # suppress "OK" logs from openai API calls | ||
|
||
|
||
@backoff.on_exception( | ||
@backoff.on_predicate( | ||
wait_gen=backoff.expo, | ||
exception=( | ||
openai.RateLimitError, | ||
openai.APIConnectionError, | ||
openai.APITimeoutError, | ||
openai.InternalServerError, | ||
), | ||
max_value=60, | ||
factor=1.5, | ||
) | ||
def openai_completion_create_retrying(client: OpenAI, *args, **kwargs): | ||
def create_retrying(func: callable, retry_exceptions: tuple[Exception], *args, **kwargs): | ||
""" | ||
Helper function for creating a completion. | ||
`args` and `kwargs` match what is accepted by `openai.Completion.create`. | ||
Retries given function if one of given exceptions is raised | ||
""" | ||
result = client.completions.create(*args, **kwargs) | ||
if "error" in result: | ||
logging.warning(result) | ||
raise openai.error.APIError(result["error"]) | ||
return result | ||
|
||
|
||
def request_with_timeout(func, *args, timeout=EVALS_THREAD_TIMEOUT, **kwargs): | ||
""" | ||
Function for making a single request within allotted time. | ||
""" | ||
while True: | ||
try: | ||
result = func(*args, timeout=timeout, **kwargs) | ||
return result | ||
except openai.APITimeoutError as e: | ||
continue | ||
|
||
|
||
@backoff.on_exception( | ||
wait_gen=backoff.expo, | ||
exception=( | ||
openai.RateLimitError, | ||
openai.APIConnectionError, | ||
openai.APITimeoutError, | ||
openai.InternalServerError, | ||
), | ||
max_value=60, | ||
factor=1.5, | ||
) | ||
def openai_chat_completion_create_retrying(client: OpenAI, *args, **kwargs): | ||
""" | ||
Helper function for creating a chat completion. | ||
`args` and `kwargs` match what is accepted by `openai.ChatCompletion.create`. | ||
""" | ||
result = request_with_timeout(client.chat.completions.create, *args, **kwargs) | ||
if "error" in result: | ||
logging.warning(result) | ||
raise openai.error.APIError(result["error"]) | ||
return result | ||
try: | ||
return func(*args, **kwargs) | ||
except retry_exceptions: | ||
return False |