forked from LeiGuo622/RetroPatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_api_usage.py
More file actions
79 lines (67 loc) · 2.65 KB
/
check_api_usage.py
File metadata and controls
79 lines (67 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
context window = input + output, gpt-4-turbo: 128k
In terms of characters, 3-4 characters of English are about one token. This can drop all the way to 0.4 characters per token in Chinese.
Reference
https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4
https://community.openai.com/t/character-limit-response-for-the-gpt-3-5-api/426713/2
"""
import datetime
import os
import requests
from dotenv import load_dotenv
load_dotenv()
# per 1000 tokens
price = {
"gpt-4-turbo": (0.01, 0.03),
"gpt-4-turbo-2024-04-09": (0.01, 0.03),
"gpt-4-0125-preview": (0.01, 0.03),
"gpt-4o-2024-05-13": (0.005, 0.015),
}
def get_usage(api_key):
headers = {
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
"Authorization": "Bearer " + api_key,
"Content-Type": "application/json",
}
# Get recent usage info
start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime(
"%Y-%m-%d"
)
end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime(
"%Y-%m-%d"
)
data = (datetime.datetime.now()).strftime("%Y-%m-%d")
resp_billing = requests.get(
f"https://api.openai.com/v1/usage?date={data}", headers=headers
)
if not resp_billing.ok:
return resp_billing.text
billing_data = resp_billing.json()
total_price = 0.0
total_consume_input = 0
total_consume_output = 0
for item in billing_data["data"]:
if item["snapshot_id"] in price:
input_p, output_p = price[item["snapshot_id"]]
total_consume_input += item["n_context_tokens_total"]
total_price += item["n_context_tokens_total"] * input_p / 1000
total_consume_output += item["n_generated_tokens_total"]
total_price += item["n_generated_tokens_total"] * output_p / 1000
else:
print(f"Unknown model: {item['snapshot_id']}")
result = {
"current_time": datetime.datetime.now(),
"total_cost": total_price,
"total_consume_input": total_consume_input,
"total_consume_output": total_consume_output,
"total_consume_tokens": total_consume_input + total_consume_output,
}
return result
if __name__ == "__main__":
usage = get_usage(os.getenv("OPENAI_API_KEY"))
print(f"\nCurrent time: {usage['current_time']}")
print(f"Total cost: ${usage['total_cost']:.2f}")
print(
f"Total consume input: {usage['total_consume_input']/1000}(k), output: {usage['total_consume_output']/1000}(k)"
)
print(f"Total consume tokens: {usage['total_consume_tokens']/1000}(k)\n")