-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_utils.py
More file actions
59 lines (47 loc) · 1.69 KB
/
Copy path_utils.py
File metadata and controls
59 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# imports
import random
import time
import openai
from boteval import log
# define a retry decorator
def retry_with_exponential_backoff(
func,
initial_delay: float = 1,
exponential_base: float = 2,
jitter: bool = True,
max_retries: int = -1,
errors: tuple = (openai.error.RateLimitError,),
):
"""Retry a function with exponential backoff.
Ex.
@retry_with_exponential_backoff
def completions_with_backoff(**kwargs):
return openai.Completion.create(**kwargs)
completions_with_backoff(model="text-davinci-002", prompt="Once upon a time,")
"""
def wrapper(*args, **kwargs):
# Initialize variables
num_retries = 0
delay = initial_delay
# Loop until a successful response or max_retries is hit or an exception is raised
while True:
try:
return func(*args, **kwargs)
# Retry on specified errors
except errors as e:
# Increment retries
num_retries += 1
# Check if max retries has been reached
if num_retries > max_retries:
raise Exception(
f"Maximum number of retries ({max_retries}) exceeded."
)
# Increment the delay
delay *= exponential_base * (1 + jitter * random.random())
# Sleep for the delay
log.warning(f'OpenAI RateLimitError - sleeping for {delay} then retrying..')
time.sleep(delay)
# Raise exceptions for any errors not specified
except Exception as e:
raise e
return wrapper