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

闪电发卡3年前ChatGPT4439

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


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 Prompt:全面教程

大家好!今天我们要来聊一聊如何打造一个完美的ChatGPT Prompt。Prompt就是我们输入给ChatGPT的那段文本,它会引导AI给出相应的回答。掌握如何撰写有效的Prompt,不仅能提高回答...

人工智障ChatGPT的奇葩操作,看完笑的肚子疼

人工智障ChatGPT的奇葩操作,看完笑的肚子疼

ChatGPT的奇葩操作最近大家都迷上了和ChatGPT进行对话,新诞生的机器人,给我们惊喜的同时,也不禁让人思考,人对比机器,还有什么优势呢?其实,ChatGPT还是有很大的局限性,毕竟不会像真人一...

我也谈谈 ChatGPT:ChatGPT用后感分析感悟

我也谈谈 ChatGPT:ChatGPT用后感分析感悟

最近 ChatGPT 从 AI 走向多个领域与大众视野,引起热议。了解了一下,作为一个科技产品,ChatGPT 的出现,其实背后有着比较清晰的演化路线,包括 GPT、GPT2、GPT3、Instruc...

当我让ChatGPT帮我写报告:ChatGPT使用案例集锦

当我让ChatGPT帮我写报告:ChatGPT使用案例集锦

导语|ChatGPT火出圈,各行各业纷纷在思考和实践如何借助ChatGPT降本增效。用户研究工作中涉及到大量知识获取、文本总结、数据分析、价值洞察等工作,无疑也是ChatGPT发挥作用的场景之一。本文...

体验了一下火爆全球的 ChatGPT,我震惊了

体验了一下火爆全球的 ChatGPT,我震惊了

这几天,要说编程圈最热的话题,莫过于OpenAI的ChatGPT,写小说,写代码,找BUG,写论文,画漫画,谱曲……简直没有它干不了的事。趁着下班时间,我也光速注册体验了一下,作为AI从业者来说,都觉...

ChatGPT的APIKey获取和提取方法教程(推荐购买3.5和4.0的ChatGPT APIKey 的靠谱渠道)

ChatGPT的APIKey获取和提取方法教程(推荐购买3.5和4.0的ChatGPT APIKey 的靠谱渠道)

一、什么是ChatGPT APIkey?APIKey 是OpenAI提供给开发者用来调用ChatGPT API的密钥,我们可以通过调用ChatGPT的API,将ChatGPT的功能和能力集成到自己的应...

发表评论    

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