File size: 4,655 Bytes
98c28c1 21050e2 98c28c1 21050e2 98c28c1 21050e2 8066e54 19ed918 85cfa8c 19ed918 21050e2 58255e2 21050e2 04764e4 21050e2 98c28c1 21050e2 98c28c1 21050e2 39d068f 21050e2 98c28c1 ac08bfb 98c28c1 21050e2 98c28c1 e4f662e 98c28c1 |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import requests
import os
from datetime import datetime
from dateutil.relativedelta import relativedelta
import openai
GPT_TYPES = ["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"]
rate_limit_per_model = {
"gpt-3.5-turbo": 3500,
"gpt-4": 200,
"gpt-4-32k": 10 # No actual clue, rare enough
}
def get_headers(key):
headers = {'Authorization': f'Bearer {key}'}
return headers
def get_subscription(key):
queryUrl = 'https://api.openai.com/v1/chat/completions'
headers = get_headers(key)
#results = r.json()
body_turbo = {"model": "gpt-3.5-turbo", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
body_gpt4 = {"model": "gpt-4", "max_tokens": 1, "messages": [{'role':'user', 'content': ''}]}
if check_key_availability():
rpm = ""
org = ""
quota = ""
try:
r = requests.post(queryUrl, headers=headers, json=body_gpt4 if check_gpt4_availability() else body_turbo)
print (r.json())
if (r.json()["error"]):
if "You exceeded your current quota" in error_message:
rpm = "";
org = "";
quota = "No quota left."
elif "Your account is not active" in error_message:
rpm = "";
org = "";
quota = "Error: Your account is not active, please check your billing details on our website."
else:
rpm = "";
org = "";
quota = f"Unexpected Error at check_key: {error_message}"
else:
rpm = r.headers['x-ratelimit-limit-requests']
org = r.headers['openai-organization']
quota = ""
except Exception as e:
error_message = str(e)
print(e)
if "You exceeded your current quota" in error_message:
rpm = "";
org = "";
quota = "No quota left."
elif "Your account is not active" in error_message:
rpm = "";
org = "";
quota = "Error: Your account is not active, please check your billing details on our website."
else:
rpm = "";
org = "";
quota = f"Unexpected Error at check_key: {error_message}"
#has_payment_method = results["has_payment_method"]
# hard_limit = results["hard_limit"]
#soft_limit_usd = results["soft_limit_usd"]
#hard_limit_usd = results["hard_limit_usd"]
#plan_title = results["plan"]["title"]
#plan_id = results["plan"]["id"]
#account_name = results["account_name"]
return {"organization": org,
"rpm": rpm,
"quota": quota}
#"has_payment_method": has_payment_method,
#"soft_limit_usd": soft_limit_usd,
#"hard_limit_usd": hard_limit_usd,
#"plan": plan_title + ", " + plan_id}
else:
return {"organization": "",
"rpm": "",
"quota": ""}
#"has_payment_method": False,
#"hard_limit_usd": "",
#"plan": ""}
#def get_usage(key):
# if check_key_availability():
# start_date = datetime.now().strftime('%Y-%m-01')
# end_date = (datetime.now() + relativedelta(months=1)).strftime('%Y-%m-01')
# queryUrl = f'https://api.openai.com/dashboard/billing/usage?start_date={start_date}&end_date={end_date}'
# headers = get_headers(key)
# r = requests.get(queryUrl, headers=headers)
# return r.json()
# else:
# return ""
def check_gpt4_availability():
if check_key_availability():
available_models = [model["root"] for model in openai.Model.list()["data"]]
if 'gpt-4' in available_models:
return True
else:
return False
else:
return False
def check_gpt4_32k_availability():
if check_key_availability():
available_models = [model["root"] for model in openai.Model.list()["data"]]
if 'gpt-4-32k' in available_models:
return True
else:
return False
else:
return False
def check_key_availability():
try:
openai.Model.list()
return True
except:
return False
if __name__ == "__main__":
key = os.getenv("OPENAI_API_KEY")
# results = get_usage(key)
# print(results)
results = get_subscription(key)
for k, v in results.items():
print(f"{k}: {v}") |