File size: 2,268 Bytes
98c28c1 ac08bfb 98c28c1 ac08bfb 98c28c1 ac08bfb 98c28c1 ac08bfb 98c28c1 ac08bfb 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 |
import requests
import os
from datetime import datetime
from dateutil.relativedelta import relativedelta
import openai
def get_headers(key):
headers = {'Authorization': f'Bearer {key}'}
return headers
def get_subscription(key):
queryUrl = 'https://api.openai.com/dashboard/billing/subscription'
headers = get_headers(key)
r = requests.get(queryUrl, headers=headers)
results = r.json()
if check_key_availability():
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 {"account_name": account_name,
"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 {"account_name": "",
"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_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}") |