import datetime import json import requests
def get_balance(docker_port, apikey): head = "" if apikey is not None: head = "\n" else: head = "### 通用api key \n" apikey = API_KEY
subscription_url = "https://api.openai.com/v1/dashboard/billing/subscription" headers = { "Authorization": "Bearer " + apikey, "Content-Type": "application/json" } subscription_response = requests.get(subscription_url, headers=headers) if subscription_response.status_code == 200: data = subscription_response.json() total = data.get("hard_limit_usd") else: return head+subscription_response.text
# end_date设置为今天日期+1 end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d") begin_date = (datetime.datetime.now() + datetime.timedelta(days=-99)).strftime("%Y-%m-%d") billing_url = "https://api.openai.com/v1/dashboard/billing/usage?start_date=" + begin_date +"&end_date=" + end_date billing_response = requests.get(billing_url, headers=headers) if billing_response.status_code == 200: data = billing_response.json() total_usage = data.get("total_usage") / 100 daily_costs = data.get("daily_costs") days = min(30, len(daily_costs)) recent = f"最近{days}天使用情况 \n" for i in range(days): cur = daily_costs[-i-1] date = datetime.datetime.fromtimestamp(cur.get("timestamp")).strftime("%Y-%m-%d") line_items = cur.get("line_items") cost = 0 for item in line_items: cost += item.get("cost") recent += f"{date}: {cost / 100} \n" else: return head+billing_response.text
#return head+f"{total-total_usage:.4f}" return head+f"总额: {total:.4f} \n" \ f"已用: {total_usage:.4f} \n" \ f"剩余: {total-total_usage:.4f} \n" \ f"\n"+recent
您的邮箱地址不会被公开。 必填项已用 * 标注
共以下 1 个回答
import datetime
import json
import requests
def get_balance(docker_port, apikey):
head = ""
if apikey is not None:
head = "\n"
else:
head = "### 通用api key \n"
apikey = API_KEY
subscription_url = "https://api.openai.com/v1/dashboard/billing/subscription"
headers = {
"Authorization": "Bearer " + apikey,
"Content-Type": "application/json"
}
subscription_response = requests.get(subscription_url, headers=headers)
if subscription_response.status_code == 200:
data = subscription_response.json()
total = data.get("hard_limit_usd")
else:
return head+subscription_response.text
# end_date设置为今天日期+1
end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
begin_date = (datetime.datetime.now() + datetime.timedelta(days=-99)).strftime("%Y-%m-%d")
billing_url = "https://api.openai.com/v1/dashboard/billing/usage?start_date=" + begin_date +"&end_date=" + end_date
billing_response = requests.get(billing_url, headers=headers)
if billing_response.status_code == 200:
data = billing_response.json()
total_usage = data.get("total_usage") / 100
daily_costs = data.get("daily_costs")
days = min(30, len(daily_costs))
recent = f"最近{days}天使用情况 \n"
for i in range(days):
cur = daily_costs[-i-1]
date = datetime.datetime.fromtimestamp(cur.get("timestamp")).strftime("%Y-%m-%d")
line_items = cur.get("line_items")
cost = 0
for item in line_items:
cost += item.get("cost")
recent += f"{date}: {cost / 100} \n"
else:
return head+billing_response.text
#return head+f"{total-total_usage:.4f}"
return head+f"总额: {total:.4f} \n" \
f"已用: {total_usage:.4f} \n" \
f"剩余: {total-total_usage:.4f} \n" \
f"\n"+recent