-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate_limit_tenacity.py
57 lines (44 loc) · 1.5 KB
/
rate_limit_tenacity.py
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
import openai
from dotenv import load_dotenv
from openai.error import RateLimitError
import os
import tenacity
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
# trigger rate limit error
# for _ in range(1,100):
# openai.ChatCompletion.create(
# model = "gpt-3.5-turbo",
# messages= [{"role":"user","content":"hello"}],
# max_tokens = 1
# )
# using tenacity library
from tenacity import retry, stop_after_attempt, wait_random_exponential
def after_attempt_callback(details):
# <RetryCallState 4408074320: attempt #2;
tries = str(details).split(";")[0].split(":")[1].strip()
print(tries)
@retry(
wait=wait_random_exponential(1, 3),
stop=stop_after_attempt(3),
after=after_attempt_callback,
)
def completion_with_backoff(**kwargs):
response = openai.ChatCompletion.create(**kwargs)
res_content = response["choices"][0]["message"]["content"]
res_total_token = response["usage"]["total_tokens"]
return res_content, res_total_token
for i in range(0, 100):
try:
print("calling chat completion for i =", i)
result = completion_with_backoff(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Coco"}],
max_tokens=1,
)
text, token_count = result
print(f"""content : {text} and total_tokens : {token_count}""")
except tenacity.RetryError as e:
print("Exception caught after 3 retries", e)
break
print("Rate limit using tenacity library tested")