From cca7cf8b75aa479223678d99a159dcc347882cfb Mon Sep 17 00:00:00 2001 From: TBXark Date: Sun, 8 Oct 2023 16:37:18 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96ENV=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/buildinfo.json | 2 +- dist/index.js | 61 ++++++++++++++++++++++++++----------------- dist/timestamp | 2 +- src/chat.js | 26 +++++++++++++----- src/context.js | 25 ++++++++++++------ src/env.js | 33 +++++++++++++++-------- src/message.js | 6 ----- src/openai.js | 3 +-- src/type.d.ts | 11 ++++---- wrangler-example.toml | 4 ++- 10 files changed, 107 insertions(+), 66 deletions(-) diff --git a/dist/buildinfo.json b/dist/buildinfo.json index a4459d61..3bf6cdb4 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha": "12fa595", "timestamp": 1696745907} +{"sha": "01eb8d9", "timestamp": 1696754208} diff --git a/dist/index.js b/dist/index.js index e9bea65e..ec00feb6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -41,9 +41,9 @@ var ENV = { // 检查更新的分支 UPDATE_BRANCH: "master", // 当前版本 - BUILD_TIMESTAMP: 1696745907, + BUILD_TIMESTAMP: 1696754208, // 当前版本 commit id - BUILD_VERSION: "12fa595", + BUILD_VERSION: "01eb8d9", I18N: null, LANGUAGE: "zh-cn", // 使用流模式 @@ -74,7 +74,7 @@ var DATABASE = null; var API_GUARD = null; var AI = null; var ENV_VALUE_TYPE = { - API_KEY: [], + API_KEY: "array", AZURE_API_KEY: "string", AZURE_COMPLETIONS_API: "string" }; @@ -82,9 +82,10 @@ function initEnv(env, i18n2) { DATABASE = env.DATABASE; API_GUARD = env.API_GUARD; AI = env.AI; - for (const key in ENV) { + for (const key of Object.keys(ENV)) { + const t = ENV_VALUE_TYPE[key] ? ENV_VALUE_TYPE[key] : typeof ENV[key]; if (env[key]) { - switch (ENV_VALUE_TYPE[key] ? typeof ENV_VALUE_TYPE[key] : typeof ENV[key]) { + switch (t) { case "number": ENV[key] = parseInt(env[key]) || ENV[key]; break; @@ -94,11 +95,12 @@ function initEnv(env, i18n2) { case "string": ENV[key] = env[key]; break; + case "array": + ENV[key] = env[key].split(","); + break; case "object": if (Array.isArray(ENV[key])) { ENV[key] = env[key].split(","); - } else if (ENV_VALUE_TYPE[key] && Array.isArray(ENV_VALUE_TYPE[key])) { - ENV[key] = env[key].split(","); } else { try { ENV[key] = JSON.parse(env[key]); @@ -292,7 +294,6 @@ var Context = class { console.log(this.USER_CONFIG); } /** - * * @return {string|null} */ openAIKeyFromContext() { @@ -302,14 +303,22 @@ var Context = class { 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; + if (ENV.API_KEY.length === 0) { + return null; + } + return ENV.API_KEY[Math.floor(Math.random() * ENV.API_KEY.length)]; + } + /** + * @return {boolean} + */ + hasValidOpenAIKey() { + if (ENV.AZURE_COMPLETIONS_API) { + return ENV.AZURE_API_KEY !== null; } + if (this.USER_CONFIG.OPENAI_API_KEY) { + return true; + } + return ENV.API_KEY.length > 0; } }; @@ -719,8 +728,7 @@ function readableStreamAsyncIterable(stream) { // src/openai.js function isOpenAIEnable(context) { - const key = context.openAIKeyFromContext(); - return key && key.length > 0; + return context.hasValidOpenAIKey(); } async function requestCompletionsFromOpenAI(message, history, context, onStream) { const key = context.openAIKeyFromContext(); @@ -1588,6 +1596,15 @@ async function loadHistory(key, context) { } return { real: history, original }; } +function loadLLM(context) { + if (isOpenAIEnable(context)) { + return requestCompletionsFromOpenAI; + } + if (isWorkersAIEnable(context)) { + return requestCompletionsFromWorkersAI; + } + return null; +} async function requestCompletionsFromLLM(text, context, llm, modifier, onStream) { const historyDisable = ENV.AUTO_TRIM_HISTORY && ENV.MAX_HISTORY_LENGTH <= 0; const historyKey = context.SHARE_CONTEXT.chatHistoryKey; @@ -1631,9 +1648,9 @@ async function chatWithLLM(text, context, modifier) { } }; } - let llm = requestCompletionsFromOpenAI; - if (isWorkersAIEnable(context)) { - llm = requestCompletionsFromWorkersAI; + const llm = loadLLM(context); + if (llm === null) { + return sendMessageToTelegramWithContext(context)("LLM is not enable"); } const answer = await requestCompletionsFromLLM(text, context, llm, modifier, onStream); context.CURRENT_CHAT_CONTEXT.parse_mode = parseMode; @@ -2107,10 +2124,6 @@ async function msgIgnoreOldMessage(message, context) { return null; } async function msgCheckEnvIsReady(message, context) { - const llmEnable = isOpenAIEnable(context) || isWorkersAIEnable(context); - if (!llmEnable) { - return sendMessageToTelegramWithContext(context)("LLM Not Set"); - } if (!DATABASE) { return sendMessageToTelegramWithContext(context)("DATABASE Not Set"); } diff --git a/dist/timestamp b/dist/timestamp index 119faa50..a4a9148a 100644 --- a/dist/timestamp +++ b/dist/timestamp @@ -1 +1 @@ -1696745907 +1696754208 diff --git a/src/chat.js b/src/chat.js index 00d26d34..89de5aae 100644 --- a/src/chat.js +++ b/src/chat.js @@ -6,7 +6,7 @@ import { import {DATABASE, ENV} from './env.js'; // eslint-disable-next-line no-unused-vars import {Context} from './context.js'; -import {requestCompletionsFromOpenAI} from './openai.js'; +import {isOpenAIEnable, requestCompletionsFromOpenAI} from './openai.js'; import {tokensCounter} from './utils.js'; import {isWorkersAIEnable, requestCompletionsFromWorkersAI} from './workers-ai.js'; @@ -105,6 +105,21 @@ async function loadHistory(key, context) { } +/** + * + * @param {Context} context + * @return {function} + */ +function loadLLM(context) { + if (isOpenAIEnable(context)) { + return requestCompletionsFromOpenAI; + } + if (isWorkersAIEnable(context)) { + return requestCompletionsFromWorkersAI; + } + return null; +} + /** * * @param {string} text @@ -114,7 +129,7 @@ async function loadHistory(key, context) { * @param {function} onStream * @return {Promise} */ -export async function requestCompletionsFromLLM(text, context, llm, modifier, onStream) { +async function requestCompletionsFromLLM(text, context, llm, modifier, onStream) { const historyDisable = ENV.AUTO_TRIM_HISTORY && ENV.MAX_HISTORY_LENGTH <= 0; const historyKey = context.SHARE_CONTEXT.chatHistoryKey; let history = await loadHistory(historyKey, context); @@ -167,11 +182,10 @@ export async function chatWithLLM(text, context, modifier) { }; } - let llm = requestCompletionsFromOpenAI; - if (isWorkersAIEnable(context)) { - llm = requestCompletionsFromWorkersAI; + const llm = loadLLM(context); + if (llm === null) { + return sendMessageToTelegramWithContext(context)('LLM is not enable'); } - const answer = await requestCompletionsFromLLM(text, context, llm, modifier, onStream); context.CURRENT_CHAT_CONTEXT.parse_mode = parseMode; if (ENV.SHOW_REPLY_BUTTON && context.CURRENT_CHAT_CONTEXT.message_id) { diff --git a/src/context.js b/src/context.js index 17170152..d2796464 100644 --- a/src/context.js +++ b/src/context.js @@ -1,4 +1,5 @@ import {CONST, DATABASE, ENV} from './env.js'; +// import {TelegramMessage} from './type.d.ts'; /** * 上下文信息 @@ -191,7 +192,6 @@ export class Context { } /** - * * @return {string|null} */ openAIKeyFromContext() { @@ -201,13 +201,22 @@ export class Context { 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; + if (ENV.API_KEY.length === 0) { + return null; + } + return ENV.API_KEY[Math.floor(Math.random() * ENV.API_KEY.length)]; + } + + /** + * @return {boolean} + */ + hasValidOpenAIKey() { + if (ENV.AZURE_COMPLETIONS_API) { + return ENV.AZURE_API_KEY !== null; + } + if (this.USER_CONFIG.OPENAI_API_KEY) { + return true; } + return ENV.API_KEY.length > 0; } } diff --git a/src/env.js b/src/env.js index 133bfb2d..11cfd3ac 100644 --- a/src/env.js +++ b/src/env.js @@ -1,13 +1,17 @@ /** * @typedef {Object} Environment - * @property {null | string} API_KEY + * + * @property {null | string[]} API_KEY * @property {string[]} TELEGRAM_AVAILABLE_TOKENS + * * @property {boolean} I_AM_A_GENEROUS_PERSON * @property {string[]} CHAT_WHITE_LIST + * * @property {string[]} TELEGRAM_BOT_NAME * @property {string[]} CHAT_GROUP_WHITE_LIST * @property {boolean} GROUP_CHAT_BOT_ENABLE * @property {boolean} GROUP_CHAT_BOT_SHARE_MODE + * * @property {string} CHAT_MODEL * @property {boolean} AUTO_TRIM_HISTORY * @property {number} MAX_HISTORY_LENGTH @@ -16,27 +20,31 @@ * @property {string} GPT3_TOKENS_COUNT_REPO * @property {string} SYSTEM_INIT_MESSAGE * @property {string} SYSTEM_INIT_MESSAGE_ROLE + * * @property {boolean} ENABLE_USAGE_STATISTICS * @property {string[]} HIDE_COMMAND_BUTTONS * @property {boolean} SHOW_REPLY_BUTTON + * * @property {string} UPDATE_BRANCH * @property {number} BUILD_TIMESTAMP * @property {string} BUILD_VERSION + * * @property {null | I18n} I18N * @property {string} LANGUAGE + * * @property {boolean} STREAM_MODE * @property {boolean} SAFE_MODE * @property {boolean} DEBUG_MODE * @property {boolean} DEV_MODE + * * @property {string} TELEGRAM_API_DOMAIN * @property {string} OPENAI_API_DOMAIN - * @property {string} AZURE_API_KEY - * @property {string} AZURE_COMPLETIONS_API + * + * @property {null | string} AZURE_API_KEY + * @property {null | string} AZURE_COMPLETIONS_API + * * @property {string} WORKERS_AI_MODEL */ -/** - * @type {Environment} - */ export const ENV = { // OpenAI API Key @@ -128,7 +136,7 @@ export let API_GUARD = null; export let AI = null; const ENV_VALUE_TYPE = { - API_KEY: [], + API_KEY: 'array', AZURE_API_KEY: 'string', AZURE_COMPLETIONS_API: 'string', }; @@ -147,9 +155,11 @@ export function initEnv(env, i18n) { DATABASE = env.DATABASE; API_GUARD = env.API_GUARD; AI = env.AI; - for (const key in ENV) { + + for (const key of Object.keys(ENV)) { + const t = ENV_VALUE_TYPE[key]?ENV_VALUE_TYPE[key]:(typeof ENV[key]); if (env[key]) { - switch (ENV_VALUE_TYPE[key]?typeof ENV_VALUE_TYPE[key]:(typeof ENV[key])) { + switch (t) { case 'number': ENV[key] = parseInt(env[key]) || ENV[key]; break; @@ -159,11 +169,12 @@ export function initEnv(env, i18n) { case 'string': ENV[key] = env[key]; break; + case 'array': + ENV[key] = env[key].split(','); + break; case 'object': if (Array.isArray(ENV[key])) { ENV[key] = env[key].split(','); - } else if (ENV_VALUE_TYPE[key] && Array.isArray(ENV_VALUE_TYPE[key])) { - ENV[key] = env[key].split(','); } else { try { ENV[key] = JSON.parse(env[key]); diff --git a/src/message.js b/src/message.js index 1305ca6b..23cdd949 100644 --- a/src/message.js +++ b/src/message.js @@ -4,8 +4,6 @@ import {sendMessageToTelegramWithContext} from './telegram.js'; import {handleCommandMessage} from './command.js'; import {errorToString} from './utils.js'; import {chatWithLLM} from './chat.js'; -import {isOpenAIEnable} from './openai.js'; -import {isWorkersAIEnable} from './workers-ai.js'; // import {TelegramMessage, TelegramWebhookRequest} from './type.d.ts'; @@ -79,10 +77,6 @@ async function msgIgnoreOldMessage(message, context) { * @return {Promise} */ async function msgCheckEnvIsReady(message, context) { - const llmEnable = isOpenAIEnable(context) || isWorkersAIEnable(context); - if (!llmEnable) { - return sendMessageToTelegramWithContext(context)('LLM Not Set'); - } if (!DATABASE) { return sendMessageToTelegramWithContext(context)('DATABASE Not Set'); } diff --git a/src/openai.js b/src/openai.js index 886ac18a..d1ffbc43 100644 --- a/src/openai.js +++ b/src/openai.js @@ -9,8 +9,7 @@ import {Stream} from './vendors/stream.js'; * @param {Context} context */ export function isOpenAIEnable(context) { - const key = context.openAIKeyFromContext(); - return key && key.length > 0; + return context.hasValidOpenAIKey(); } diff --git a/src/type.d.ts b/src/type.d.ts index eee174a6..b30a3114 100644 --- a/src/type.d.ts +++ b/src/type.d.ts @@ -1,13 +1,12 @@ /* eslint-disable */ - -interface TelegramWebhookRequest { +type TelegramWebhookRequest = { update_id: number; message?: TelegramMessage; edited_message?: TelegramMessage; } -interface TelegramMessage { +type TelegramMessage = { message_id: number; from: TelegramUser; chat: TelegramChat; @@ -17,7 +16,7 @@ interface TelegramMessage { reply_to_message?: TelegramMessage; } -interface TelegramUser { +type TelegramUser = { id: number; is_bot: boolean; first_name: string; @@ -26,12 +25,12 @@ interface TelegramUser { language_code?: string; } -interface TelegramChat { +type TelegramChat = { id: number; type: string; } -interface TelegramMessageEntity { +type TelegramMessageEntity = { type: string; offset: number; length: number; diff --git a/wrangler-example.toml b/wrangler-example.toml index f9493879..68865ff6 100644 --- a/wrangler-example.toml +++ b/wrangler-example.toml @@ -13,7 +13,9 @@ kv_namespaces = [ # 如果使用openai则注释这一段 #[ai] -#binding = "AI" +#ai = [ +# { binding = "AI" } +#] [vars]