-
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.
Merge branch 'ollie/unify_retrying' into ollie/add_gemini_solver
- Loading branch information
Showing
3 changed files
with
61 additions
and
84 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,73 +1,22 @@ | ||
""" | ||
This file defines various helper functions for interacting with the OpenAI API. | ||
""" | ||
import concurrent | ||
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): | ||
""" | ||
Worker thread for making a single request within allotted time. | ||
""" | ||
while True: | ||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: | ||
future = executor.submit(func, *args, **kwargs) | ||
try: | ||
result = future.result(timeout=timeout) | ||
return result | ||
except concurrent.futures.TimeoutError: | ||
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 |