From cbab824fd0d78a140a4376be176e0125e5ab2500 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Tue, 28 Feb 2023 10:38:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(!usage):=20=E6=94=AF=E6=8C=81=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B=E8=B4=A6=E6=88=B7=E9=A2=9D=E5=BA=A6=E5=89=A9=E4=BD=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/command.py | 7 +++++++ pkg/utils/credit.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 pkg/utils/credit.py diff --git a/pkg/qqbot/command.py b/pkg/qqbot/command.py index 6aaceac6..7de7e5a7 100644 --- a/pkg/qqbot/command.py +++ b/pkg/qqbot/command.py @@ -11,6 +11,7 @@ import pkg.utils.reloader import pkg.utils.updater import pkg.utils.context import pkg.qqbot.message +import pkg.utils.credit as credit from mirai import Image @@ -254,6 +255,12 @@ def process_command(session_name: str, text_message: str, mgr, config, .get_image_count_of_key(api_keys[key_name]) reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), int(image_count)) + # 获取此key的额度 + try: + credit_data = credit.fetch_credit_data(api_keys[key_name]) + reply_str += " - 使用额度:{:.2f}/{:.2f}\n".format(credit_data['total_used'],credit_data['total_granted']) + except Exception as e: + logging.warning("获取额度失败:{}".format(e)) reply = [reply_str] elif cmd == 'draw': diff --git a/pkg/utils/credit.py b/pkg/utils/credit.py new file mode 100644 index 00000000..f263ed9b --- /dev/null +++ b/pkg/utils/credit.py @@ -0,0 +1,13 @@ +# OpenAI账号免费额度剩余查询 +import requests + + +def fetch_credit_data(api_key: str) -> dict: + """OpenAI账号免费额度剩余查询""" + resp = requests.get( + url="https://api.openai.com/dashboard/billing/credit_grants", + headers={ + "Authorization": "Bearer {}".format(api_key), + } + ) + return resp.json() \ No newline at end of file