superdup95 commited on
Commit
21050e2
1 Parent(s): dca23cf

Update api_usage.py

Browse files
Files changed (1) hide show
  1. api_usage.py +65 -19
api_usage.py CHANGED
@@ -4,34 +4,70 @@ from datetime import datetime
4
  from dateutil.relativedelta import relativedelta
5
  import openai
6
 
 
 
 
 
 
 
 
7
 
8
  def get_headers(key):
9
  headers = {'Authorization': f'Bearer {key}'}
10
  return headers
11
 
12
  def get_subscription(key):
13
- queryUrl = 'https://api.openai.com/dashboard/billing/subscription'
14
- headers = get_headers(key)
15
- r = requests.get(queryUrl, headers=headers)
16
- results = r.json()
 
 
17
  if check_key_availability():
18
- has_payment_method = results["has_payment_method"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # hard_limit = results["hard_limit"]
20
- soft_limit_usd = results["soft_limit_usd"]
21
- hard_limit_usd = results["hard_limit_usd"]
22
- plan_title = results["plan"]["title"]
23
- plan_id = results["plan"]["id"]
24
- account_name = results["account_name"]
25
- return {"account_name": account_name,
26
- "has_payment_method": has_payment_method,
27
- "soft_limit_usd": soft_limit_usd,
28
- "hard_limit_usd": hard_limit_usd,
29
- "plan": plan_title + ", " + plan_id}
 
 
30
  else:
31
- return {"account_name": "",
32
- "has_payment_method": False,
33
- "hard_limit_usd": "",
34
- "plan": ""}
 
 
35
 
36
  #def get_usage(key):
37
  # if check_key_availability():
@@ -54,6 +90,16 @@ def check_gpt4_availability():
54
  else:
55
  return False
56
 
 
 
 
 
 
 
 
 
 
 
57
  def check_key_availability():
58
  try:
59
  openai.Model.list()
 
4
  from dateutil.relativedelta import relativedelta
5
  import openai
6
 
7
+ GPT_TYPES = ["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"]
8
+
9
+ rate_limit_per_model = {
10
+ "gpt-3.5-turbo": 3500,
11
+ "gpt-4": 200,
12
+ "gpt-4-32k": 10 # No actual clue, rare enough
13
+ }
14
 
15
  def get_headers(key):
16
  headers = {'Authorization': f'Bearer {key}'}
17
  return headers
18
 
19
  def get_subscription(key):
20
+ queryUrl = 'https://api.openai.com/v1/chat/completions'
21
+ headers = get_headers(key)
22
+ #results = r.json()
23
+ body_turbo = {"model": "gpt-3.5-turbo", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
24
+ body_gpt4 = {"model": "gpt-4", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
25
+
26
  if check_key_availability():
27
+ rpm = ""
28
+ org = ""
29
+ quota = ""
30
+ try:
31
+ r = requests.post(queryUrl, headers=headers, json=body_gpt4 if check_gpt4_availability() else body_turbo)
32
+ rpm = r.headers['x-ratelimit-limit-requests']
33
+ org = r.headers['openai-organization']
34
+ quota = ""
35
+ except Exception as e:
36
+ error_message = str(e)
37
+ if "You exceeded your current quota" in error_message:
38
+ rpm = "";
39
+ org = "";
40
+ quota = "No quota left."
41
+ elif "Your account is not active" in error_message:
42
+ rpm = "";
43
+ org = "";
44
+ quota = "Error: Your account is not active, please check your billing details on our website."
45
+ else:
46
+ rpm = "";
47
+ org = "";
48
+ quota = f"Unexpected Error at check_key: {error_message}"
49
+
50
+ #has_payment_method = results["has_payment_method"]
51
  # hard_limit = results["hard_limit"]
52
+ #soft_limit_usd = results["soft_limit_usd"]
53
+ #hard_limit_usd = results["hard_limit_usd"]
54
+ #plan_title = results["plan"]["title"]
55
+ #plan_id = results["plan"]["id"]
56
+ #account_name = results["account_name"]
57
+ return {"organization": org,
58
+ "rpm": rpm,
59
+ "quota": quota}
60
+ #"has_payment_method": has_payment_method,
61
+ #"soft_limit_usd": soft_limit_usd,
62
+ #"hard_limit_usd": hard_limit_usd,
63
+ #"plan": plan_title + ", " + plan_id}
64
  else:
65
+ return {"organization": "",
66
+ "rpm": "",
67
+ "quota": "Invalid key or key has been revoked!"}
68
+ #"has_payment_method": False,
69
+ #"hard_limit_usd": "",
70
+ #"plan": ""}
71
 
72
  #def get_usage(key):
73
  # if check_key_availability():
 
90
  else:
91
  return False
92
 
93
+ def check_gpt4_32k_availability():
94
+ if check_key_availability():
95
+ available_models = [model["root"] for model in openai.Model.list()["data"]]
96
+ if 'gpt-4-32k' in available_models:
97
+ return True
98
+ else:
99
+ return False
100
+ else:
101
+ return False
102
+
103
  def check_key_availability():
104
  try:
105
  openai.Model.list()