AllenYkl commited on
Commit
d8e436d
·
1 Parent(s): 739500a

Create openai_func.py

Browse files
Files changed (1) hide show
  1. bin_public/app/openai_func.py +81 -0
bin_public/app/openai_func.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import logging
3
+ from bin_public.config.presets import (
4
+ timeout_all,
5
+ USAGE_API_URL,
6
+ BALANCE_API_URL,
7
+ standard_error_msg,
8
+ connection_timeout_prompt,
9
+ error_retrieve_prompt,
10
+ read_timeout_prompt
11
+ )
12
+
13
+ from bin_public.utils.utils import get_proxies
14
+ import datetime
15
+
16
+ def get_billing_data(openai_api_key, billing_url):
17
+ headers = {
18
+ "Content-Type": "application/json",
19
+ "Authorization": f"Bearer {openai_api_key}"
20
+ }
21
+
22
+ timeout = timeout_all
23
+ proxies = get_proxies()
24
+ response = requests.get(
25
+ billing_url,
26
+ headers=headers,
27
+ timeout=timeout,
28
+ proxies=proxies,
29
+ )
30
+
31
+ if response.status_code == 200:
32
+ data = response.json()
33
+ return data
34
+ else:
35
+ raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
36
+
37
+
38
+ def get_usage(openai_api_key):
39
+ try:
40
+ balance_data=get_billing_data(openai_api_key, BALANCE_API_URL)
41
+ logging.debug(balance_data)
42
+ try:
43
+ balance = balance_data["total_available"] if balance_data["total_available"] else 0
44
+ total_used = balance_data["total_used"] if balance_data["total_used"] else 0
45
+ usage_percent = round(total_used / (total_used+balance) * 100, 2)
46
+ except Exception as e:
47
+ logging.error(f"API使用情况解析失败:"+str(e))
48
+ balance = 0
49
+ total_used=0
50
+ return f"**API使用情况解析失败**"
51
+ if balance == 0:
52
+ last_day_of_month = datetime.datetime.now().strftime("%Y-%m-%d")
53
+ first_day_of_month = datetime.datetime.now().replace(day=1).strftime("%Y-%m-%d")
54
+ usage_url = f"{USAGE_API_URL}?start_date={first_day_of_month}&end_date={last_day_of_month}"
55
+ try:
56
+ usage_data = get_billing_data(openai_api_key, usage_url)
57
+ except Exception as e:
58
+ logging.error(f"获取API使用情况失败:"+str(e))
59
+ return f"**获取API使用情况失败**"
60
+ return f"**本月使用金额** \u3000 ${usage_data['total_usage'] / 100}"
61
+
62
+ # return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
63
+ return f"""\
64
+ <b>免费额度使用情况</b>
65
+ <div class="progress-bar">
66
+ <div class="progress" style="width: {usage_percent}%;">
67
+ <span class="progress-text">{usage_percent}%</span>
68
+ </div>
69
+ </div>
70
+ <div style="display: flex; justify-content: space-between;"><span>已用 ${total_used}</span><span>可用 ${balance}</span></div>
71
+ """
72
+
73
+ except requests.exceptions.ConnectTimeout:
74
+ status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
75
+ return status_text
76
+ except requests.exceptions.ReadTimeout:
77
+ status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt
78
+ return status_text
79
+ except Exception as e:
80
+ logging.error(f"获取API使用情况失败:"+str(e))
81
+ return standard_error_msg + error_retrieve_prompt