superdup95
commited on
Commit
•
7be74d6
1
Parent(s):
743687c
Update api_usage.py
Browse files- api_usage.py +21 -10
api_usage.py
CHANGED
@@ -7,13 +7,17 @@ import openai
|
|
7 |
queryUrl = 'https://api.openai.com/v1/chat/completions'
|
8 |
GPT_TYPES = ["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"]
|
9 |
rate_limit_per_model = {
|
10 |
-
"gpt-3.5-turbo-
|
11 |
-
"gpt-3.5-turbo-
|
12 |
"gpt-4": 200,
|
13 |
"gpt-4-32k": 1000 # No actual clue, rare enough
|
14 |
}
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def get_headers(key):
|
19 |
headers = {'Authorization': f'Bearer {key}'}
|
@@ -22,18 +26,25 @@ def get_headers(key):
|
|
22 |
def get_subscription(key):
|
23 |
headers = get_headers(key)
|
24 |
#results = r.json()
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
if check_key_availability():
|
28 |
rpm = ""
|
29 |
org = ""
|
30 |
quota = ""
|
31 |
-
r = requests.post(queryUrl, headers=headers, json=
|
32 |
result = r.json()
|
33 |
if "id" in result:
|
34 |
rpm = r.headers['x-ratelimit-limit-requests']
|
35 |
org = r.headers['openai-organization']
|
36 |
-
quota = check_key_type(
|
37 |
else:
|
38 |
e = result["error"]["code"]
|
39 |
quota = f"Error: {e}"
|
@@ -61,10 +72,10 @@ def get_subscription(key):
|
|
61 |
# return ""
|
62 |
|
63 |
def check_key_type(model, rpm):
|
64 |
-
if model ==
|
65 |
-
if rpm > rate_limit_per_model['gpt-3.5-turbo-
|
66 |
return "yes | pay, possibly big key"
|
67 |
-
elif rpm
|
68 |
return "yes | pay"
|
69 |
else:
|
70 |
return "yes | trial"
|
|
|
7 |
queryUrl = 'https://api.openai.com/v1/chat/completions'
|
8 |
GPT_TYPES = ["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"]
|
9 |
rate_limit_per_model = {
|
10 |
+
"gpt-3.5-turbo-trial": 2000,
|
11 |
+
"gpt-3.5-turbo-pay": 3500,
|
12 |
"gpt-4": 200,
|
13 |
"gpt-4-32k": 1000 # No actual clue, rare enough
|
14 |
}
|
15 |
+
body_gpt = {
|
16 |
+
"gpt-3.5-turbo": {"model": "gpt-3.5-turbo", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
|
17 |
+
"gpt-4": {"model": "gpt-4", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
|
18 |
+
"gpt-4-32k": {"model": "gpt-4", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
|
19 |
+
}
|
20 |
+
|
21 |
|
22 |
def get_headers(key):
|
23 |
headers = {'Authorization': f'Bearer {key}'}
|
|
|
26 |
def get_subscription(key):
|
27 |
headers = get_headers(key)
|
28 |
#results = r.json()
|
29 |
+
|
30 |
+
key_highest_model = ""
|
31 |
+
if check_gpt4_32k_availability():
|
32 |
+
key_highest_model = GPT_TYPES[2]
|
33 |
+
elif check_gpt4_availability():
|
34 |
+
key_highest_model = GPT_TYPES[1]
|
35 |
+
else:
|
36 |
+
key_highest_model = GPT_TYPES[0]
|
37 |
|
38 |
if check_key_availability():
|
39 |
rpm = ""
|
40 |
org = ""
|
41 |
quota = ""
|
42 |
+
r = requests.post(queryUrl, headers=headers, json=body_gpt[key_highest_model])
|
43 |
result = r.json()
|
44 |
if "id" in result:
|
45 |
rpm = r.headers['x-ratelimit-limit-requests']
|
46 |
org = r.headers['openai-organization']
|
47 |
+
quota = check_key_type(key_highest_model, int(rpm))
|
48 |
else:
|
49 |
e = result["error"]["code"]
|
50 |
quota = f"Error: {e}"
|
|
|
72 |
# return ""
|
73 |
|
74 |
def check_key_type(model, rpm):
|
75 |
+
if model == GPT_TYPES[0]:
|
76 |
+
if rpm > rate_limit_per_model['gpt-3.5-turbo-pay']:
|
77 |
return "yes | pay, possibly big key"
|
78 |
+
elif rpm > rate_limit_per_model['gpt-3.5-turbo-trial'] and rpm <= rate_limit_per_model['gpt-3.5-turbo-pay']:
|
79 |
return "yes | pay"
|
80 |
else:
|
81 |
return "yes | trial"
|