From e8948e6c152193d298b2e37784033605e53b090a Mon Sep 17 00:00:00 2001 From: Zhijie He Date: Tue, 26 Nov 2024 11:37:34 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20refactor=20`ge?= =?UTF-8?q?tLlmOptionsFromPayload`=20from=20AgentRuntime=20(#4790)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ refactor: refactor `getLlmOptionsFromPayload` from AgentRuntime * 🔨 chore: format coding * 🔨 chore: cleanup * 🐛 fix: fix typo * 🔨 chore: rollback changes * 🐛 fix: fix CI error --- src/server/modules/AgentRuntime/index.ts | 240 ++--------------------- 1 file changed, 21 insertions(+), 219 deletions(-) diff --git a/src/server/modules/AgentRuntime/index.ts b/src/server/modules/AgentRuntime/index.ts index 86e54f38ba2b..a643b78e6019 100644 --- a/src/server/modules/AgentRuntime/index.ts +++ b/src/server/modules/AgentRuntime/index.ts @@ -27,54 +27,32 @@ export interface AgentChatOptions { * @returns The options object. */ const getLlmOptionsFromPayload = (provider: string, payload: JWTPayload) => { + const llmConfig = getLLMConfig() as Record; + switch (provider) { - default: // Use Openai options as default - case ModelProvider.OpenAI: { - const { OPENAI_API_KEY } = getLLMConfig(); + default: { + let upperProvider = provider.toUpperCase(); + + if (!llmConfig[`${upperProvider}_API_KEY`]) { + upperProvider = ModelProvider.OpenAI.toUpperCase(); // Use OpenAI options as default + } - const openaiApiKey = payload?.apiKey || OPENAI_API_KEY; - const baseURL = payload?.endpoint || process.env.OPENAI_PROXY_URL; - const apiKey = apiKeyManager.pick(openaiApiKey); + const apiKey = apiKeyManager.pick(payload?.apiKey || llmConfig[`${upperProvider}_API_KEY`]); + const baseURL = payload?.endpoint || process.env[`${upperProvider}_PROXY_URL`]; return { apiKey, baseURL }; } + case ModelProvider.Azure: { - const { AZURE_API_KEY, AZURE_API_VERSION, AZURE_ENDPOINT } = getLLMConfig(); - const apiKey = apiKeyManager.pick(payload?.apiKey || AZURE_API_KEY); + const { AZURE_API_KEY, AZURE_API_VERSION, AZURE_ENDPOINT } = llmConfig; + const apikey = apiKeyManager.pick(payload?.apiKey || AZURE_API_KEY); const endpoint = payload?.endpoint || AZURE_ENDPOINT; const apiVersion = payload?.azureApiVersion || AZURE_API_VERSION; - return { - apiVersion, - apikey: apiKey, - endpoint, - }; - } - case ModelProvider.ZhiPu: { - const { ZHIPU_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || ZHIPU_API_KEY); - - return { apiKey }; + return { apiVersion, apikey, endpoint }; } - case ModelProvider.Google: { - const { GOOGLE_API_KEY } = getLLMConfig(); - const apiKey = apiKeyManager.pick(payload?.apiKey || GOOGLE_API_KEY); - const baseURL = payload?.endpoint || process.env.GOOGLE_PROXY_URL; - - return { apiKey, baseURL }; - } - case ModelProvider.Moonshot: { - const { MOONSHOT_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || MOONSHOT_API_KEY); - const baseURL = payload?.endpoint || process.env.MOONSHOT_PROXY_URL; - - return { apiKey, baseURL }; - } case ModelProvider.Bedrock: { - const { AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, AWS_SESSION_TOKEN } = - getLLMConfig(); + const { AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, AWS_SESSION_TOKEN } = llmConfig; let accessKeyId: string | undefined = AWS_ACCESS_KEY_ID; let accessKeySecret: string | undefined = AWS_SECRET_ACCESS_KEY; let region = AWS_REGION; @@ -88,127 +66,9 @@ const getLlmOptionsFromPayload = (provider: string, payload: JWTPayload) => { } return { accessKeyId, accessKeySecret, region, sessionToken }; } - case ModelProvider.Ollama: { - const baseURL = payload?.endpoint || process.env.OLLAMA_PROXY_URL; - return { baseURL }; - } - case ModelProvider.Perplexity: { - const { PERPLEXITY_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || PERPLEXITY_API_KEY); - const baseURL = payload?.endpoint || process.env.PERPLEXITY_PROXY_URL; - - return { apiKey, baseURL }; - } - case ModelProvider.Anthropic: { - const { ANTHROPIC_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || ANTHROPIC_API_KEY); - const baseURL = payload?.endpoint || process.env.ANTHROPIC_PROXY_URL; - - return { apiKey, baseURL }; - } - case ModelProvider.Minimax: { - const { MINIMAX_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || MINIMAX_API_KEY); - - return { apiKey }; - } - case ModelProvider.Mistral: { - const { MISTRAL_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || MISTRAL_API_KEY); - - return { apiKey }; - } - case ModelProvider.Groq: { - const { GROQ_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || GROQ_API_KEY); - const baseURL = payload?.endpoint || process.env.GROQ_PROXY_URL; - - return { apiKey, baseURL }; - } - case ModelProvider.Github: { - const { GITHUB_TOKEN } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || GITHUB_TOKEN); - - return { apiKey }; - } - case ModelProvider.OpenRouter: { - const { OPENROUTER_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || OPENROUTER_API_KEY); - - return { apiKey }; - } - case ModelProvider.DeepSeek: { - const { DEEPSEEK_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || DEEPSEEK_API_KEY); - - return { apiKey }; - } - case ModelProvider.TogetherAI: { - const { TOGETHERAI_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || TOGETHERAI_API_KEY); - - return { apiKey }; - } - case ModelProvider.FireworksAI: { - const { FIREWORKSAI_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || FIREWORKSAI_API_KEY); - - return { apiKey }; - } - case ModelProvider.ZeroOne: { - const { ZEROONE_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || ZEROONE_API_KEY); - - return { apiKey }; - } - case ModelProvider.Qwen: { - const { QWEN_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || QWEN_API_KEY); - return { apiKey }; - } - case ModelProvider.Stepfun: { - const { STEPFUN_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || STEPFUN_API_KEY); - - return { apiKey }; - } - case ModelProvider.Novita: { - const { NOVITA_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || NOVITA_API_KEY); - - return { apiKey }; - } - case ModelProvider.Baichuan: { - const { BAICHUAN_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || BAICHUAN_API_KEY); - - return { apiKey }; - } - case ModelProvider.Taichu: { - const { TAICHU_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || TAICHU_API_KEY); - - return { apiKey }; - } case ModelProvider.Cloudflare: { - const { CLOUDFLARE_API_KEY, CLOUDFLARE_BASE_URL_OR_ACCOUNT_ID } = getLLMConfig(); + const { CLOUDFLARE_API_KEY, CLOUDFLARE_BASE_URL_OR_ACCOUNT_ID } = llmConfig; const apiKey = apiKeyManager.pick(payload?.apiKey || CLOUDFLARE_API_KEY); const baseURLOrAccountID = @@ -218,68 +78,25 @@ const getLlmOptionsFromPayload = (provider: string, payload: JWTPayload) => { return { apiKey, baseURLOrAccountID }; } - case ModelProvider.Ai360: { - const { AI360_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || AI360_API_KEY); - - return { apiKey }; - } - case ModelProvider.SiliconCloud: { - const { SILICONCLOUD_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || SILICONCLOUD_API_KEY); - const baseURL = payload?.endpoint || process.env.SILICONCLOUD_PROXY_URL; - return { apiKey, baseURL }; - } case ModelProvider.GiteeAI: { - const { GITEE_AI_API_KEY } = getLLMConfig(); + const { GITEE_AI_API_KEY } = llmConfig; const apiKey = apiKeyManager.pick(payload?.apiKey || GITEE_AI_API_KEY); return { apiKey }; } - case ModelProvider.HuggingFace: { - const { HUGGINGFACE_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || HUGGINGFACE_API_KEY); - const baseURL = payload?.endpoint || process.env.HUGGINGFACE_PROXY_URL; - - return { apiKey, baseURL }; - } - - case ModelProvider.Upstage: { - const { UPSTAGE_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || UPSTAGE_API_KEY); - - return { apiKey }; - } - case ModelProvider.Spark: { - const { SPARK_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || SPARK_API_KEY); - - return { apiKey }; - } - case ModelProvider.Ai21: { - const { AI21_API_KEY } = getLLMConfig(); + case ModelProvider.Github: { + const { GITHUB_TOKEN } = llmConfig; - const apiKey = apiKeyManager.pick(payload?.apiKey || AI21_API_KEY); + const apiKey = apiKeyManager.pick(payload?.apiKey || GITHUB_TOKEN); return { apiKey }; } - case ModelProvider.Hunyuan: { - const { HUNYUAN_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || HUNYUAN_API_KEY); - return { apiKey }; - } case ModelProvider.SenseNova: { - const { SENSENOVA_ACCESS_KEY_ID, SENSENOVA_ACCESS_KEY_SECRET } = getLLMConfig(); + const { SENSENOVA_ACCESS_KEY_ID, SENSENOVA_ACCESS_KEY_SECRET } = llmConfig; const sensenovaAccessKeyID = apiKeyManager.pick( payload?.sensenovaAccessKeyID || SENSENOVA_ACCESS_KEY_ID, @@ -290,21 +107,6 @@ const getLlmOptionsFromPayload = (provider: string, payload: JWTPayload) => { const apiKey = sensenovaAccessKeyID + ':' + sensenovaAccessKeySecret; - return { apiKey }; - } - case ModelProvider.XAI: { - const { XAI_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || XAI_API_KEY); - const baseURL = payload?.endpoint || process.env.XAI_PROXY_URL; - - return { apiKey, baseURL }; - } - case ModelProvider.InternLM: { - const { INTERNLM_API_KEY } = getLLMConfig(); - - const apiKey = apiKeyManager.pick(payload?.apiKey || INTERNLM_API_KEY); - return { apiKey }; } }