mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 19:37:36 +08:00
feat(!usage): 支持查看账户额度剩余
This commit is contained in:
@@ -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':
|
||||
|
||||
13
pkg/utils/credit.py
Normal file
13
pkg/utils/credit.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user