forked from lonlie/openai-chatgpt-billing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_api_dotnet.cs
421 lines (365 loc) · 19.8 KB
/
backend_api_dotnet.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using Kaliko.ImageLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using RestSharp;
using System.Text.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
using RestSharp.Extensions;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
namespace MyTest
{
public class QueryBillDto
{
[Required]
public string key { get; set; }
[Required]
public string ext { get; set; }
public string publicId { get; set; }
public string api { get; set; }
public DateTime? beginDate { get; set; }
public DateTime? endDate { get; set; }
}
public class UsageChatData
{
public string organization_id { get; set; }
public int aggregation_timestamp { get; set; }
public int n_requests { get; set; }
public string operation { get; set; }
public string snapshot_id { get; set; }
public int n_context_tokens_total { get; set; }
public int n_generated_tokens_total { get; set; }
public string user_id { get; set; }
public string api_key_id { get; set; }
public string api_key_name { get; set; }
public string api_key_redacted { get; set; }
}
public class UsageDalleData
{
public int timestamp { get; set; }
public int num_images { get; set; }
public int num_requests { get; set; }
public string image_size { get; set; }
public string operation { get; set; }
public string user_id { get; set; }
public string organization_id { get; set; }
public string api_key_id { get; set; }
public string api_key_name { get; set; }
public string api_key_redacted { get; set; }
}
/// <summary>
/// 返回给前端的用量数据
/// </summary>
public class UsageData
{
public string date { get; set; }
public List<UsageDataItem> data { get; set; }
}
/// <summary>
/// 用量数据明细
/// </summary>
public class UsageDataItem
{
public string model { get; set; }
public int context_tokens_total { get; set; }
public int generated_tokens_total { get; set; }
public double amount { get; set; }
public int count { get; set; }
public bool exact { get; set; }
}
[TestClass]
public class BillTest
{
//chat和base相关模型的价格,Tuple<context,generated>
static Dictionary<string, Tuple<double, double>> chatPrices = new Dictionary<string, Tuple<double, double>>
{
{ "gpt-3.5-turbo", new Tuple<double, double>(0.0015, 0.002) },
{ "gpt-3.5-turbo-0301", new Tuple<double, double>(0.0015, 0.002) },
{ "gpt-3.5-turbo-0613", new Tuple<double, double>(0.0015, 0.002) },
{ "gpt-3.5-turbo-1106", new Tuple<double, double>(0.0015, 0.002) },
{ "gpt-3.5-turbo-0125", new Tuple<double, double>(0.0005, 0.0015) },
{ "gpt-3.5-turbo-instruct", new Tuple<double, double>(0.0015, 0.002) },
{ "gpt-3.5-turbo-instruct-0914", new Tuple<double, double>(0.0015, 0.002) },
{ "gpt-3.5-turbo-16k", new Tuple<double, double>(0.003, 0.004) },
{ "gpt-3.5-turbo-16k-0613", new Tuple<double, double>(0.003, 0.004) },
{ "gpt-4", new Tuple<double, double>(0.03, 0.06) },
{ "gpt-4-0314", new Tuple<double, double>(0.03, 0.06) },
{ "gpt-4-0613", new Tuple<double, double>(0.03, 0.06) },
{ "gpt-4-turbo", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-turbo-2024-04-09", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-turbo-preview", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-0125-preview", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-1106-preview", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-vision-preview", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-1106-vision-preview", new Tuple<double, double>(0.01, 0.03) },
{ "gpt-4-32k", new Tuple<double, double>(0.06, 0.12) },
{ "gpt-4-32k-0314", new Tuple<double, double>(0.06, 0.12) },
{ "gpt-4-32k-0613", new Tuple<double, double>(0.06, 0.12) },
{ "text-ada-001", new Tuple<double, double>(0.0004, 0.0004) },
{ "davinci", new Tuple<double, double>(0.002, 0.002) },
{ "text-babbage-001", new Tuple<double, double>(0.0005, 0.0005) },
{ "text-curie-001", new Tuple<double, double>(0.002, 0.002) },
{ "text-davinci-001", new Tuple<double, double>(0.02, 0.02) },
{ "text-davinci-002", new Tuple<double, double>(0.02, 0.02) },
{ "text-davinci-003", new Tuple<double, double>(0.02, 0.02) },
{ "text-embedding-ada-002", new Tuple<double, double>(0.0001, 0.0001) },
{ "text-embedding-ada-002-v2", new Tuple<double, double>(0.0001, 0.0001) },
{ "text-embedding-3-small", new Tuple<double, double>(0.00013, 0.00013) },
{ "text-embedding-3-large", new Tuple<double, double>(0.0004, 0.0004) }
};
//dalle相关模型的价格,image_size Tuple<price,是否为精确价格>
static Dictionary<string, Tuple<double, bool>> dallePrices = new Dictionary<string, Tuple<double, bool>>
{
{ "1792x1024", new Tuple<double, bool>(0.1, false) },
{ "1024x1792", new Tuple<double, bool>(0.1, false) },
{ "1024x1024", new Tuple<double, bool>(0.04, false) },
{ "1024x1024>2", new Tuple<double, bool>(0.04, true) },//两张以上的是dalle2
{ "512x512", new Tuple<double, bool>(0.018, true) },
{ "256x256", new Tuple<double, bool>(0.016, true) }
};
static string apiDomain = "https://api.openai.com";
[TestMethod]
public void TesQueryBase()
{
try
{
string jsonString = File.ReadAllText("chatPrices.json");
chatPrices = JsonSerializer.Deserialize<Dictionary<string, Tuple<double, double>>>(jsonString);
}
catch (Exception ex)
{
}
QueryBillDto dto = new QueryBillDto() { key = "sk-LE4XuFtT17c6kFJwLdeLUF4zOWOyjDXeNYiXo1BqYnuT3Blb", publicId = "user-nNchTlf0bOsxdsaTUFuZdPaw", ext = "1652831557593", beginDate = DateTime.Now.AddDays(-5), endDate = DateTime.Now.AddDays(-5) };
var x = TesQuery(dto).Result;
}
public async Task<ResultSingle<dynamic>> TesQuery(QueryBillDto dto)
{
var result = new ResultSingle<dynamic>();
var hasAPI = !string.IsNullOrWhiteSpace(dto.api);
if (dto.ext != "1652831557593")
{
result.Status = RStatus.S9999;
result.Desc = "非法请求";
return result;
}
//指定api
if (hasAPI)
{
dto.api = dto.api.Trim().Replace("/v1","");
if (dto.api.EndsWith("/"))
{
dto.api = dto.api.Remove(dto.api.Length - 1);
}
apiDomain = dto.api;
}
// 计算起始日期和结束日期
DateTime now = DateTime.Now;
DateTime startDate = now.AddDays(-90);
DateTime endDate = now.AddDays(1);
// 设置API请求URL和请求头
string urlSubscription = apiDomain + "/v1/dashboard/billing/subscription"; // 查是否订阅
string urlUsage = $"{apiDomain}/v1/dashboard/billing/usage?start_date={startDate.ToString("yyyy-MM-dd")}&end_date={endDate.ToString("yyyy-MM-dd")}"; // 查使用量
try
{
var useKey = dto.key.StartsWith("sk-");
HttpClient client = new HttpClient();
// 获取API限额
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", dto.key);
HttpResponseMessage response = await client.GetAsync(urlSubscription);
string subscriptionDataString = await response.Content.ReadAsStringAsync();
JObject subscriptionData = JObject.Parse(subscriptionDataString);
if (!response.IsSuccessStatusCode)
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
if ((string)subscriptionData["error"]["code"] == "invalid_api_key")
{
result.Status = RStatus.S9999;
result.Desc = "输入的key或会话密钥无效。";
return result;
}
else if ((string)subscriptionData["error"]["code"] == "expired_session_key")
{
result.Status = RStatus.S9999;
result.Desc = "会话密钥过期,请重新获取后查询。";
return result;
}
else if ((string)subscriptionData["error"]["code"] == "account_deactivated")
{
result.Status = RStatus.S9999;
result.Desc = "对应账户已被封禁。";
return result;
}
}
}
//按原逻辑走
if (!useKey || (useKey && hasAPI))
{
// 判断是否过期
long timestamp_now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long timestamp_expire = (long)subscriptionData["access_until"];
if (!hasAPI && timestamp_now > timestamp_expire)
{
result.Status = RStatus.S9999;
result.Desc = "对应账户额度已过期, 请登录OpenAI进行查看。";
return result;
}
double totalAmount = (double)subscriptionData["hard_limit_usd"];
bool is_subscribed = (bool)subscriptionData["has_payment_method"];
// 获取已使用量
response = await client.GetAsync(urlUsage);
string usageDataString = await response.Content.ReadAsStringAsync();
JObject usageData = JObject.Parse(usageDataString);
double totalUsage = (double)usageData["total_usage"] / 100;
// 如果用户绑卡,额度每月会刷新
if (is_subscribed)
{
// 获取当前月的第一天日期
int day = now.Day; // 本月过去的天数
startDate = now.AddDays(-(day - 1)); // 本月第一天
urlUsage = $"{apiDomain}/v1/dashboard/billing/usage?start_date={startDate.ToString("yyyy-MM-dd")}&end_date={endDate.ToString("yyyy-MM-dd")}"; // 查使用量
response = await client.GetAsync(urlUsage);
usageDataString = await response.Content.ReadAsStringAsync();
usageData = JObject.Parse(usageDataString);
totalUsage = (double)usageData["total_usage"] / 100;
}
// 计算剩余额度
double remaining = totalAmount - totalUsage;
result.ReturnObject = new { total = totalAmount, usage = totalUsage, remaining, bind = is_subscribed, util = timestamp_expire, useKey, detail = new List<UsageData>() };
}
//使用key按天查询
else
{
//按天查询,汇总结果
var usageData = GetUsageByKey(client, dto.key, dto.publicId, dto.beginDate, dto.endDate);
result.ReturnObject = new { total = 0, usage = usageData.Sum(m => m.data.Sum(i => i.amount)), remaining = 0, bind = true, util = 0, useKey, detail = usageData };
}
}
catch (Exception e)
{
result.Status = RStatus.S9999;
}
return result;
}
private List<UsageData> GetUsageByKey(HttpClient client, string key, string publicId, DateTime? beginDate, DateTime? endDate)
{
var result = new List<UsageData>();
int requestCount = 0;
if (beginDate == null || endDate == null)
{
DateTime now = DateTime.Now;
beginDate = new DateTime(now.Year, now.Month, 1);
endDate = now;
}
if ((endDate - beginDate).Value.Days > 31)
{
return result;
}
for (var date = beginDate.Value; date <= endDate; date = date.AddDays(1))
{
try
{
// 用于限制请求速率的计数器和时间记录
if (requestCount > 4)
{
Thread.Sleep(TimeSpan.FromMinutes(1));
requestCount = 0;
}
string urlUsage = $"{apiDomain}/v1/usage?date={date:yyyy-MM-dd}{(string.IsNullOrWhiteSpace(publicId) ? "" : ("&user_public_id=" + publicId))}";
var response = client.GetAsync(urlUsage).Result;
var usageDataString = response.Content.ReadAsStringAsync().Result;
var usageData = JObject.Parse(usageDataString);
var chatData = ((JArray)usageData["data"]).ToObject<List<UsageChatData>>();
var dalleData = ((JArray)usageData["dalle_api_data"]).ToObject<List<UsageDalleData>>();
var usageResult = chatData.
Select(m => new { m.snapshot_id, m.n_context_tokens_total, m.n_generated_tokens_total, amount = chatPrices[m.snapshot_id].Item1 * m.n_context_tokens_total + chatPrices[m.snapshot_id].Item2 * m.n_generated_tokens_total }).GroupBy(m => m.snapshot_id).
Select(m => new UsageDataItem() { model = m.Key, context_tokens_total = m.Sum(t => t.n_context_tokens_total), generated_tokens_total = m.Sum(t => t.n_generated_tokens_total), amount = Math.Round(m.Sum(t => t.amount) / 1000, 2, MidpointRounding.AwayFromZero), count = m.Count(), exact = true }).ToList();
var dalleUsage = dalleData.Select(m =>
{
var size = (m.image_size == "1024x1024" && m.num_images > 2) ? "1024x1024>2" : m.image_size;
return new { amount = dallePrices[size].Item1 * m.num_images, m.num_requests, exact = dallePrices[size].Item2 };
}).ToList();
//增加dalle相关的数据
usageResult.Add(new UsageDataItem() { model = "dalle", context_tokens_total = 0, generated_tokens_total = 0, amount = Math.Round(dalleUsage.Sum(m => m.amount), 2, MidpointRounding.AwayFromZero), count = dalleUsage.Count, exact = !dalleUsage.Any(m => !m.exact) });
result.Add(new UsageData() { date = date.ToString("yyyy-MM-dd"), data = usageResult.OrderBy(m => m.model).ToList() });
// 更新请求计数
requestCount++;
}
catch (Exception ex)
{
}
}
return result;
}
/// <summary>
/// 可以区分每个key的用量
/// </summary>
private List<UsageData> GetUsageByPerKey(HttpClient client, string keyName, string keyId, string sess, DateTime? beginDate, DateTime? endDate)
{
var result = new List<UsageData>();
if (beginDate == null || endDate == null)
{
DateTime now = DateTime.Now;
beginDate = new DateTime(now.Year, now.Month, 1);
endDate = now;
}
if ((endDate - beginDate).Value.Days > 31)
{
return result;
}
endDate = endDate.Value.AddDays(20);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sess);
try
{
string urlUsage = $"{apiDomain}/v1/dashboard/activity?end_date={endDate:yyyy-MM-dd}&start_date={beginDate:yyyy-MM-dd}";
var response = client.GetAsync(urlUsage).Result;
var usageDataString = response.Content.ReadAsStringAsync().Result;
var usageData = JObject.Parse(usageDataString);
for (var date = beginDate.Value; date <= endDate; date = date.AddDays(1))
{
var timestamp = new DateTimeOffset(date.Date.AddHours(8)).ToUnixTimeSeconds();
var chatData = ((JArray)usageData["data"]).ToObject<List<UsageChatData>>().Where(m => m.api_key_id == keyId && m.aggregation_timestamp == timestamp).ToList();
var dalleData = ((JArray)usageData["dalle_api_data"]).ToObject<List<UsageDalleData>>().Where(m => m.api_key_id == keyId && m.timestamp == timestamp).ToList();
var usageResult = chatData.
Select(m => new { m.snapshot_id, m.n_context_tokens_total, m.n_generated_tokens_total, amount = chatPrices[m.snapshot_id].Item1 * m.n_context_tokens_total + chatPrices[m.snapshot_id].Item2 * m.n_generated_tokens_total }).GroupBy(m => m.snapshot_id).
Select(m => new UsageDataItem() { model = m.Key, context_tokens_total = m.Sum(t => t.n_context_tokens_total), generated_tokens_total = m.Sum(t => t.n_generated_tokens_total), amount = Math.Round(m.Sum(t => t.amount) / 1000, 2, MidpointRounding.AwayFromZero), count = m.Count(), exact = true }).ToList();
var dalleUsage = dalleData.Select(m =>
{
var size = (m.image_size == "1024x1024" && m.num_images > 2) ? "1024x1024>2" : m.image_size;
var price = dallePrices[size].Item1;
if (keyName.Contains("guoke") && (size == "1024x1024" || size == "1024x1024>2"))
{
price = 0.04d;
}
if (keyName.Contains("guoke") && (size == "1792x1024" || size == "1024x1792"))
{
price = 0.08d;
}
return new { amount = price * m.num_images, m.num_requests, exact = dallePrices[size].Item2 };
}).ToList();
//增加dalle相关的数据
usageResult.Add(new UsageDataItem() { model = "dalle", context_tokens_total = 0, generated_tokens_total = 0, amount = Math.Round(dalleUsage.Sum(m => m.amount), 2, MidpointRounding.AwayFromZero), count = dalleUsage.Sum(m => m.num_requests), exact = !dalleUsage.Any(m => !m.exact) });
result.Add(new UsageData() { date = date.ToString("yyyy-MM-dd"), data = usageResult.OrderBy(m => m.model).ToList() });
}
}
catch (Exception ex)
{
}
return result;
}
}
}