Skip to content

Commit

Permalink
fix: query key command (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
maq917 committed Aug 24, 2023
1 parent 17804cb commit b379085
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
6 changes: 6 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ pip install -r requirements.txt
"query_key_command": "#query key", # 查询 api key 使用情况
"recent_days": 5 # 查询最近的<recent_days>天
"plugins": [{ "name": <plugin name>, other configs }]# 添加你喜爱的插件
"openai_sensitive_id": "" # 查询api key时使用
}
```

openai_sensitive_id获取:登录https://platform.openai.com/overview页面,按F12找到如下值,维护到配置中
![image](https://github.com/maq917/wechat-gptbot/assets/126306230/36b146dd-649f-4b91-9905-32875f3455b2)



### 运行

#### 1. 准备
Expand Down
3 changes: 2 additions & 1 deletion config.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"single_chat_prefix": ["bot", "@bot"],
"query_key_command": "#query key",
"recent_days": 5,
"plugins": [{ "name": "tiktok", "command": "#tiktok" }]
"plugins": [{ "name": "tiktok", "command": "#tiktok" }],
"openai_sensitive_id": ""
}
47 changes: 8 additions & 39 deletions utils/query_key.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
import requests
from config import conf

Expand All @@ -10,52 +9,22 @@ def get_key():
api_base = api_base
else:
api_base = "https://api.openai.com/v1"
subscription_url = api_base + "/dashboard/billing/subscription"
subscription_url = api_base + "/dashboard/billing/credit_grants"
headers = {
"Authorization": "Bearer " + conf().get("openai_api_key"),
"Authorization": "Bearer " + conf().get("openai_sensitive_id"),
"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")
total_granted = data.get("total_granted")
total_used = data.get("total_used")
total_available = data.get("total_available")
else:
return subscription_response.text
# Set start_date to 99 days before today’s date.
start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime(
"%Y-%m-%d"
)
# Set end_date to today’s date plus 1 day.
end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime(
"%Y-%m-%d"
)
billing_url = (
api_base
+ f"/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(conf().get("recent_days", 5), len(daily_costs))
recent = f"### Usage in the last {days} 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):.2f} \n"
else:
return billing_response.text

return (
f"## Total:\t{total:.2f}$ \n"
f"## Used:\t{total_usage:.2f}$ \n"
f"## Remaining:\t{total - total_usage:.2f}$ \n"
f"\n" + recent
f"## Total:\t{total_granted:.2f}$ \n"
f"## Used:\t{total_used:.2f}$ \n"
f"## Available:\t{total_available:.2f}$ \n"
)

0 comments on commit b379085

Please sign in to comment.