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

闪电发卡3年前ChatGPT4767

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


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在金融业的应用:AI提示词案例分享

近年来,人工智能(AI)技术的发展突飞猛进,尤其是ChatGPT这样的自然语言处理模型,更是在各个行业中展示了它强大的潜力。今天,我们来聊一聊ChatGPT在金融业的应用,看看这些智能提示词是如何帮助...

如何使用ChatGPT提升人工智能科研效率

在科技飞速发展的今天,人工智能(AI)已经成为各大领域的关键技术,而ChatGPT作为一种强大的自然语言处理工具,更是为科研人员提供了新的助力。那么,如何利用ChatGPT来提升人工智能科研效率呢?今...

ChatGPT免费版、ChatGPT Plus、ChatGPT Team团队版和ChatGPT企业版的区别和开通条件

ChatGPT免费版、ChatGPT Plus、ChatGPT Team团队版和ChatGPT企业版的区别和开通条件

ChatGPT目前共有4个版本,即ChatGPT免费版、ChatGPT Plus、ChatGPT Team团队版和ChatGPT企业版,他们的使用群体,开通条件均不一样,用户可以根据自己的情况进行选择...

用ChatGPT生成热门视频脚本:自媒体创作新利器

闪电发卡ChatGPT产品推荐:ChatGPT独享账号:https://www.chatgptzh.com/post/86.htmlChatGPT Plus独享共享账号购买代充:https://www...

图文版|小白基础 ChatGPT 注册使用教程

图文版|小白基础 ChatGPT 注册使用教程

前言最近的 ChatGPT 的大火,带动了各种概念的暴涨,抖音等各大短视频平台以及各种流量号铺天盖地的宣传,一时间可谓是众说纷纭。至于这个东西到底是什么?有什么用?还是各位实际跟随教程使用后再说吧。本...

闪电发卡关于2024年五一劳动节商品打折活动的通知(仅此5天)

正逢五一佳节,闪电发卡为了回馈广大顾客的支持与厚爱,我们诚挚地推出五一节商品打折特惠活动,为您带来无与伦比的购物体验,让您真正体验到闪电发卡网的快速,安全,低价的购物方式,以下是我们的优惠详情(到点自...

发表评论    

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