查询OpenAI账号APIKey可用余额、使用量和使用明细接口,用python实现

闪电发卡3年前ChatGPT4817

现在可以使用以下接口进行实现


Python


apikey = ""
    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 subscription_response.text
 
    # start_date设置为今天日期前99天
    start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime("%Y-%m-%d")
    # end_date设置为今天日期+1
    end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    billing_url = f"https://api.openai.com/v1/dashboard/billing/usage?start_date={start_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(5, 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"\t{date}\t{cost / 100} \n"
    else:
        return billing_response.text
 
    return f"\n总额:\t{total:.4f}  \n" \
                f"已用:\t{total_usage:.4f}  \n" \
                f"剩余:\t{total-total_usage:.4f}  \n" \
                f"\n"+recent

输出结果如下:


总额: 18.0000


已用: 0.4737


剩余: 17.5263


最近5天使用情况


2023-04-28 0.0


2023-04-27 0.0


2023-04-26 0.0


2023-04-25 0.0


2023-04-24 0.0


可访问 https://aichat.xingtupai.com 查看示例

余额查询方法可参考:https://www.chatgptzh.com/post/325.html


相关文章

对接ChatGPT时如何保证同一个用户会话的连惯性

要保证对接ChatGPT API时是同一个用户的连续问题,可以使用API返回的completion对象中的conversation_id属性来实现。conversation_id是一个字符串,可以唯一...

AI在交通运输行业的前景与挑战

在我们日常生活中,交通运输扮演着至关重要的角色。无论是上下班通勤,还是物流运输,交通运输系统的效率和安全性直接影响着我们的生活质量和经济发展。而随着人工智能(AI)的快速发展,它在交通运输行业的应用前...

GPT-4.0和GPT3.5大比拼,究竟谁胜?ChatGPT大模型版本对比分析

GPT-4.0和GPT3.5大比拼,究竟谁胜?ChatGPT大模型版本对比分析

作为人工智能史上里程碑事件之一的ChatGPT,自2022年11月30日发布至今,一直备受热议。在ChatGPT热潮尚未见减弱之势,2023年3月14日,OpenAI公司继续发布新一代AI语言大模型G...

ChatGPT真的可以帮你月入百万吗?

ChatGPT真的可以帮你月入百万吗?

自ChatGPT这个软件爆火以来,相信很多朋友都在网上看到过有人利用ChatGPT做到月入百万,那么到底是真的还是假的呢?我们今天来分析一下。ChatGPTChatGPT,美国OpenAI研发的聊天机...

新必应(New Bing)申请出错终极方案

新必应(New Bing)申请出错终极方案

随着ChatGPT的爆火,微软推出了AI驱动的新必应(New Bing),新必应大大提升了Bing的体验和效率,一时间吸引力众多申请和试用。然而很多想要加入新必应候补名单时,却总是报出错了,请重试。弯...

Python+ChatGPT编程5分钟快速上手,强烈推荐!!!

Python+ChatGPT编程5分钟快速上手,强烈推荐!!!

最近一段时间ChatGPT火爆出圈!无论是在互联网行业,还是其他各行业都赚足了话题。俗话说:“外行看笑话,内行看门道”,今天从ChatGPT个人体验感受以及如何用的角度来分享一下。1、chatGPT是...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。