Skip to content

Commit

Permalink
Merge pull request #172 from TBXark/dev
Browse files Browse the repository at this point in the history
perf: 优化openai key获取逻辑,优化账单
  • Loading branch information
TBXark authored Apr 23, 2023
2 parents e5b1904 + 6009c92 commit 5001ecb
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 95 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "4e15113", "timestamp": 1682166538}
{"sha": "311c627", "timestamp": 1682221130}
81 changes: 38 additions & 43 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ var ENV = {
// 检查更新的分支
UPDATE_BRANCH: "master",
// 当前版本
BUILD_TIMESTAMP: 1682166538,
BUILD_TIMESTAMP: 1682221130,
// 当前版本 commit id
BUILD_VERSION: "4e15113",
BUILD_VERSION: "311c627",
/**
* @type {I18n}
*/
Expand Down Expand Up @@ -281,6 +281,23 @@ var Context = class {
await this._initUserConfig(this.SHARE_CONTEXT.configStoreKey);
console.log(this.USER_CONFIG);
}
/**
*
* @return {string|null}
*/
openAIKeyFromContext() {
if (this.USER_CONFIG.OPENAI_API_KEY) {
return this.USER_CONFIG.OPENAI_API_KEY;
}
if (Array.isArray(ENV.API_KEY)) {
if (ENV.API_KEY.length === 0) {
return null;
}
return ENV.API_KEY[Math.floor(Math.random() * ENV.API_KEY.length)];
} else {
return ENV.API_KEY;
}
}
};

// src/telegram.js
Expand Down Expand Up @@ -774,16 +791,6 @@ function makeResponse200(resp) {
}

// src/openai.js
function openAIKeyFromContext(context) {
if (context.USER_CONFIG.OPENAI_API_KEY) {
return context.USER_CONFIG.OPENAI_API_KEY;
}
if (Array.isArray(ENV.API_KEY)) {
return ENV.API_KEY[Math.floor(("0." + Math.sin((/* @__PURE__ */ new Date()).getTime()).toString().substring(6)) * ENV.API_KEY.length)];
} else {
return ENV.API_KEY;
}
}
function extractContentFromStreamData(stream) {
const line = stream.split("\n");
let remainingStr = "";
Expand All @@ -808,7 +815,7 @@ function extractContentFromStreamData(stream) {
async function requestCompletionsFromOpenAI(message, history, context, onStream) {
console.log(`requestCompletionsFromOpenAI: ${message}`);
console.log(`history: ${JSON.stringify(history, null, 2)}`);
const key = openAIKeyFromContext(context);
const key = context.openAIKeyFromContext();
const body = {
model: ENV.CHAT_MODEL,
...context.USER_CONFIG.OPENAI_API_EXTRA_PARAMS,
Expand Down Expand Up @@ -860,7 +867,7 @@ Body: ${JSON.stringify(body)}`);
}
async function requestImageFromOpenAI(prompt, context) {
console.log(`requestImageFromOpenAI: ${prompt}`);
const key = openAIKeyFromContext(context);
const key = context.openAIKeyFromContext();
const body = {
prompt,
n: 1,
Expand All @@ -881,38 +888,26 @@ async function requestImageFromOpenAI(prompt, context) {
return resp.data[0].url;
}
async function requestBill(context) {
const now = /* @__PURE__ */ new Date();
const apiUrl = ENV.OPENAI_API_DOMAIN;
const key = openAIKeyFromContext(context);
let startDate = new Date(now - 90 * 24 * 60 * 60 * 1e3);
const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1e3);
const subDate = new Date(now);
subDate.setDate(1);
const formatDate = (date) => {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
return `${year}-${month}-${day}`;
};
const urlSubscription = `${apiUrl}/v1/dashboard/billing/subscription`;
let urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`;
const key = context.openAIKeyFromContext();
const date = /* @__PURE__ */ new Date();
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
const endDate = `${year}-${month}-${day}`;
const startDate = `${year}-${month}-01`;
const urlSub = `${apiUrl}/v1/dashboard/billing/subscription`;
const urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`;
const headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/json"
};
try {
let response = await fetch(urlSubscription, { headers });
if (!response.ok) {
return {};
}
const subscriptionData = await response.json();
const subResp = await fetch(urlSub, { headers });
const subscriptionData = await subResp.json();
const totalAmount = subscriptionData.hard_limit_usd;
if (totalAmount > 20) {
startDate = subDate;
}
urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`;
response = await fetch(urlUsage, { headers });
const usageData = await response.json();
const usageResp = await fetch(urlUsage, { headers });
const usageData = await usageResp.json();
const totalUsage = usageData.total_usage / 100;
const remaining = totalAmount - totalUsage;
return {
Expand Down Expand Up @@ -1103,7 +1098,7 @@ var zh_hans_default = {
"command_error": (e) => `\u547D\u4EE4\u6267\u884C\u9519\u8BEF: ${e.message}`
},
bill: {
"bill_detail": (totalAmount, totalUsage, remaining) => `\u{1F4CA} \u5F53\u524D\u673A\u5668\u4EBA\u7528\u91CF
"bill_detail": (totalAmount, totalUsage, remaining) => `\u{1F4CA} \u672C\u6708\u673A\u5668\u4EBA\u7528\u91CF
- \u603B\u989D\u5EA6: $${totalAmount || 0}
- \u5DF2\u4F7F\u7528: $${totalUsage || 0}
Expand Down Expand Up @@ -1186,7 +1181,7 @@ var zh_hant_default = {
"command_error": (e) => `\u547D\u4EE4\u57F7\u884C\u51FA\u932F\uFF1A${e.message}`
},
bill: {
"bill_detail": (totalAmount, totalUsage, remaining) => `\u{1F4CA} \u5F53\u524D\u673A\u5668\u4EBA\u7528\u91CF
"bill_detail": (totalAmount, totalUsage, remaining) => `\u{1F4CA} \u672C\u6708\u673A\u5668\u4EBA\u7528\u91CF
- \u603B\u989D\u5EA6: $${totalAmount || 0}
- \u5DF2\u4F7F\u7528: $${totalUsage || 0}
Expand Down Expand Up @@ -1269,7 +1264,7 @@ var en_default = {
"command_error": (e) => `Command execution error: ${e.message}`
},
bill: {
"bill_detail": (totalAmount, totalUsage, remaining) => `\u{1F4CA} Current robot usage
"bill_detail": (totalAmount, totalUsage, remaining) => `\u{1F4CA} This month usage
- Amount: $${totalAmount || 0}
- Usage: $${totalUsage || 0}
Expand Down Expand Up @@ -1782,7 +1777,7 @@ async function msgIgnoreOldMessage(message, context) {
return null;
}
async function msgCheckEnvIsReady(message, context) {
if (!ENV.API_KEY || ENV.API_KEY.length === 0) {
if (context.openAIKeyFromContext() === null) {
return sendMessageToTelegramWithContext(context)("OpenAI API Key Not Set");
}
if (!DATABASE) {
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1682166538
1682221130
18 changes: 18 additions & 0 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,22 @@ export class Context {
await this._initUserConfig(this.SHARE_CONTEXT.configStoreKey);
console.log(this.USER_CONFIG);
}

/**
*
* @return {string|null}
*/
openAIKeyFromContext() {
if (this.USER_CONFIG.OPENAI_API_KEY) {
return this.USER_CONFIG.OPENAI_API_KEY;
}
if (Array.isArray(ENV.API_KEY)) {
if (ENV.API_KEY.length === 0) {
return null;
}
return ENV.API_KEY[Math.floor(Math.random() * ENV.API_KEY.length)];
} else {
return ENV.API_KEY;
}
}
}
2 changes: 1 addition & 1 deletion src/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default {
'command_error': (e) => `Command execution error: ${e.message}`,
},
bill: {
'bill_detail': (totalAmount, totalUsage, remaining) => `📊 Current robot usage\n\n\t- Amount: $${totalAmount || 0}\n\t- Usage: $${totalUsage || 0}\n\t- Remaining: $${remaining || 0}`,
'bill_detail': (totalAmount, totalUsage, remaining) => `📊 This month usage\n\n\t- Amount: $${totalAmount || 0}\n\t- Usage: $${totalUsage || 0}\n\t- Remaining: $${remaining || 0}`,
},
},
};
2 changes: 1 addition & 1 deletion src/i18n/zh-hans.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default {
'command_error': (e) => `命令执行错误: ${e.message}`,
},
bill: {
'bill_detail': (totalAmount, totalUsage, remaining) => `📊 当前机器人用量\n\n\t- 总额度: $${totalAmount || 0}\n\t- 已使用: $${totalUsage || 0}\n\t- 剩余额度: $${remaining || 0}`,
'bill_detail': (totalAmount, totalUsage, remaining) => `📊 本月机器人用量\n\n\t- 总额度: $${totalAmount || 0}\n\t- 已使用: $${totalUsage || 0}\n\t- 剩余额度: $${remaining || 0}`,
},
},
};
2 changes: 1 addition & 1 deletion src/i18n/zh-hant.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default {
'command_error': (e) => `命令執行出錯:${e.message}`,
},
bill: {
'bill_detail': (totalAmount, totalUsage, remaining) => `📊 当前机器人用量\n\n\t- 总额度: $${totalAmount || 0}\n\t- 已使用: $${totalUsage || 0}\n\t- 剩余额度: $${remaining || 0}`,
'bill_detail': (totalAmount, totalUsage, remaining) => `📊 本月机器人用量\n\n\t- 总额度: $${totalAmount || 0}\n\t- 已使用: $${totalUsage || 0}\n\t- 剩余额度: $${remaining || 0}`,
},
},
};
2 changes: 1 addition & 1 deletion src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function msgIgnoreOldMessage(message, context) {
* @return {Promise<Response>}
*/
async function msgCheckEnvIsReady(message, context) {
if (!ENV.API_KEY || ENV.API_KEY.length === 0) {
if (context.openAIKeyFromContext() === null) {
return sendMessageToTelegramWithContext(context)('OpenAI API Key Not Set');
}
if (!DATABASE) {
Expand Down
64 changes: 18 additions & 46 deletions src/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@ import {Context} from './context.js';
import {DATABASE, ENV} from './env.js';
import {tokensCounter} from './utils.js';


/**
*
* @param {Context} context
* @return {string|null}
*/
function openAIKeyFromContext(context) {
if (context.USER_CONFIG.OPENAI_API_KEY) {
return context.USER_CONFIG.OPENAI_API_KEY;
}
if (Array.isArray(ENV.API_KEY)) {
return (ENV.API_KEY)[Math.floor(('0.'+Math.sin(new Date().getTime()).toString().substring(6)) * (ENV.API_KEY).length)];
} else {
return ENV.API_KEY;
}
}

/**
* 从流数据中提取内容
* @param {string} stream
Expand Down Expand Up @@ -59,7 +42,7 @@ function extractContentFromStreamData(stream) {
async function requestCompletionsFromOpenAI(message, history, context, onStream) {
console.log(`requestCompletionsFromOpenAI: ${message}`);
console.log(`history: ${JSON.stringify(history, null, 2)}`);
const key = openAIKeyFromContext(context);
const key = context.openAIKeyFromContext();
const body = {
model: ENV.CHAT_MODEL,
...context.USER_CONFIG.OPENAI_API_EXTRA_PARAMS,
Expand Down Expand Up @@ -118,7 +101,7 @@ async function requestCompletionsFromOpenAI(message, history, context, onStream)
*/
export async function requestImageFromOpenAI(prompt, context) {
console.log(`requestImageFromOpenAI: ${prompt}`);
const key = openAIKeyFromContext(context);
const key = context.openAIKeyFromContext();
const body = {
prompt: prompt,
n: 1,
Expand All @@ -145,44 +128,33 @@ export async function requestImageFromOpenAI(prompt, context) {
* @return {Promise<{totalAmount,totalUsage,remaining,}>}
*/
export async function requestBill(context) {
// 计算起始日期和结束日期
const now = new Date();
const apiUrl = ENV.OPENAI_API_DOMAIN;
const key = openAIKeyFromContext(context);
let startDate = new Date(now - 90 * 24 * 60 * 60 * 1000);
const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1000);
const subDate = new Date(now);
subDate.setDate(1);
const formatDate = (date) => {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');

return `${year}-${month}-${day}`;
};
const key = context.openAIKeyFromContext();

const date = new Date();
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const endDate = `${year}-${month}-${day}`;
const startDate = `${year}-${month}-01`;

const urlSubscription = `${apiUrl}/v1/dashboard/billing/subscription`;
let urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`;
const urlSub = `${apiUrl}/v1/dashboard/billing/subscription`;
const urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`;
const headers = {
'Authorization': 'Bearer ' + key,
'Content-Type': 'application/json',
};

try {
let response = await fetch(urlSubscription, {headers});
if (!response.ok) {
return {};
}
const subscriptionData = await response.json();
const subResp = await fetch(urlSub, {headers});
const subscriptionData = await subResp.json();
const totalAmount = subscriptionData.hard_limit_usd;
if (totalAmount > 20) {
startDate = subDate;
}
urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`;
response = await fetch(urlUsage, {headers});
const usageData = await response.json();

const usageResp = await fetch(urlUsage, {headers});
const usageData = await usageResp.json();
const totalUsage = usageData.total_usage / 100;
const remaining = totalAmount - totalUsage;

return {
totalAmount: totalAmount.toFixed(2),
totalUsage: totalUsage.toFixed(2),
Expand Down

0 comments on commit 5001ecb

Please sign in to comment.