From 7225ebec018109b651ceb0fd6711e6624eb92668 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Fri, 7 Nov 2025 12:15:08 +0800 Subject: [PATCH] perf: model reload (#5879) * Version doc * perf: model reload --- document/content/docs/upgrading/4-14/4141.mdx | 2 + document/data/doc-last-modified.json | 2 +- packages/service/core/ai/config/utils.ts | 183 ++++++++++-------- .../service/core/app/provider/controller.ts | 2 +- 4 files changed, 108 insertions(+), 81 deletions(-) diff --git a/document/content/docs/upgrading/4-14/4141.mdx b/document/content/docs/upgrading/4-14/4141.mdx index 7fd690db3..dce288413 100644 --- a/document/content/docs/upgrading/4-14/4141.mdx +++ b/document/content/docs/upgrading/4-14/4141.mdx @@ -11,9 +11,11 @@ description: 'FastGPT V4.14.1 更新说明' ## ⚙️ 优化 1. 在同一轮对话中,MCP Client 会持久化实例,不会销毁。 +2. 模型重载时候,不会把全局模型配置清空再添加,从而导致重载阶段模型调用错误。 ## 🐛 修复 1. Debug 模式下,交互节点无法正常使用。 2. 富文本编辑器 tab 空格未对齐。 3. 嵌套运行 Agent 时候,跳过节点队列未初始化,导致无法正常运行。 +4. 判断器右侧是 number 引用时,会出现报错。 diff --git a/document/data/doc-last-modified.json b/document/data/doc-last-modified.json index 86cb8283f..d87252c43 100644 --- a/document/data/doc-last-modified.json +++ b/document/data/doc-last-modified.json @@ -115,7 +115,7 @@ "document/content/docs/upgrading/4-13/4131.mdx": "2025-09-30T15:47:06+08:00", "document/content/docs/upgrading/4-13/4132.mdx": "2025-10-21T11:46:53+08:00", "document/content/docs/upgrading/4-14/4140.mdx": "2025-11-06T15:43:00+08:00", - "document/content/docs/upgrading/4-14/4141.mdx": "2025-11-06T15:43:00+08:00", + "document/content/docs/upgrading/4-14/4141.mdx": "2025-11-07T10:56:20+08:00", "document/content/docs/upgrading/4-8/40.mdx": "2025-08-02T19:38:37+08:00", "document/content/docs/upgrading/4-8/41.mdx": "2025-08-02T19:38:37+08:00", "document/content/docs/upgrading/4-8/42.mdx": "2025-08-02T19:38:37+08:00", diff --git a/packages/service/core/ai/config/utils.ts b/packages/service/core/ai/config/utils.ts index e94aaa042..b353cfab8 100644 --- a/packages/service/core/ai/config/utils.ts +++ b/packages/service/core/ai/config/utils.ts @@ -1,4 +1,4 @@ -import { type SystemModelItemType } from '../type'; +import type { SystemDefaultModelType, SystemModelItemType } from '../type'; import { ModelTypeEnum } from '@fastgpt/global/core/ai/model'; import { MongoSystemModel } from './schema'; import { @@ -23,8 +23,38 @@ import { refreshVersionKey } from '../../../common/cache'; import { SystemCacheKeyEnum } from '../../../common/cache/type'; export const loadSystemModels = async (init = false, language = 'en') => { + if (!init && global.systemModelList) return; + + try { + await preloadModelProviders(); + } catch (error) { + console.log('Load systen model error, please check fastgpt-plugin', error); + return Promise.reject(error); + } + + let _systemModelList: SystemModelItemType[] = []; + let _systemActiveModelList: SystemModelItemType[] = []; + let _llmModelMap = new Map(); + let _embeddingModelMap = new Map(); + let _ttsModelMap = new Map(); + let _sttModelMap = new Map(); + let _reRankModelMap = new Map(); + let _systemDefaultModel: SystemDefaultModelType = {}; + + if (!global.systemModelList) { + global.systemModelList = []; + global.systemActiveModelList = []; + global.llmModelMap = new Map(); + global.embeddingModelMap = new Map(); + global.ttsModelMap = new Map(); + global.sttModelMap = new Map(); + global.reRankModelMap = new Map(); + global.systemDefaultModel = {}; + global.systemActiveDesensitizedModels = []; + } + const pushModel = (model: SystemModelItemType) => { - global.systemModelList.push(model); + _systemModelList.push(model); // Add default value if (model.type === ModelTypeEnum.llm) { @@ -36,67 +66,48 @@ export const loadSystemModels = async (init = false, language = 'en') => { } if (model.isActive) { - global.systemActiveModelList.push(model); + _systemActiveModelList.push(model); if (model.type === ModelTypeEnum.llm) { - global.llmModelMap.set(model.model, model); - global.llmModelMap.set(model.name, model); + _llmModelMap.set(model.model, model); + _llmModelMap.set(model.name, model); if (model.isDefault) { - global.systemDefaultModel.llm = model; + _systemDefaultModel.llm = model; } if (model.isDefaultDatasetTextModel) { - global.systemDefaultModel.datasetTextLLM = model; + _systemDefaultModel.datasetTextLLM = model; } if (model.isDefaultDatasetImageModel) { - global.systemDefaultModel.datasetImageLLM = model; + _systemDefaultModel.datasetImageLLM = model; } } else if (model.type === ModelTypeEnum.embedding) { - global.embeddingModelMap.set(model.model, model); - global.embeddingModelMap.set(model.name, model); + _embeddingModelMap.set(model.model, model); + _embeddingModelMap.set(model.name, model); if (model.isDefault) { - global.systemDefaultModel.embedding = model; + _systemDefaultModel.embedding = model; } } else if (model.type === ModelTypeEnum.tts) { - global.ttsModelMap.set(model.model, model); - global.ttsModelMap.set(model.name, model); + _ttsModelMap.set(model.model, model); + _ttsModelMap.set(model.name, model); if (model.isDefault) { - global.systemDefaultModel.tts = model; + _systemDefaultModel.tts = model; } } else if (model.type === ModelTypeEnum.stt) { - global.sttModelMap.set(model.model, model); - global.sttModelMap.set(model.name, model); + _sttModelMap.set(model.model, model); + _sttModelMap.set(model.name, model); if (model.isDefault) { - global.systemDefaultModel.stt = model; + _systemDefaultModel.stt = model; } } else if (model.type === ModelTypeEnum.rerank) { - global.reRankModelMap.set(model.model, model); - global.reRankModelMap.set(model.name, model); + _reRankModelMap.set(model.model, model); + _reRankModelMap.set(model.name, model); if (model.isDefault) { - global.systemDefaultModel.rerank = model; + _systemDefaultModel.rerank = model; } } } }; - if (!init && global.systemModelList) return; - - try { - await preloadModelProviders(); - } catch (error) { - console.log('Load systen model error, please check fastgpt-plugin', error); - return Promise.reject(error); - } - - global.systemModelList = []; - global.systemActiveModelList = []; - global.llmModelMap = new Map(); - global.embeddingModelMap = new Map(); - global.ttsModelMap = new Map(); - global.sttModelMap = new Map(); - global.reRankModelMap = new Map(); - // @ts-ignore - global.systemDefaultModel = {}; - try { // Get model from db and plugin const [dbModels, systemModels] = await Promise.all([ @@ -144,7 +155,7 @@ export const loadSystemModels = async (init = false, language = 'en') => { // Custom model(Not in system config) dbModels.forEach((dbModel) => { - if (global.systemModelList.find((item) => item.model === dbModel.model)) return; + if (_systemModelList.find((item) => item.model === dbModel.model)) return; pushModel({ ...dbModel.metadata, @@ -153,61 +164,75 @@ export const loadSystemModels = async (init = false, language = 'en') => { }); // Default model check - if (!global.systemDefaultModel.llm) { - global.systemDefaultModel.llm = Array.from(global.llmModelMap.values())[0]; - } - if (!global.systemDefaultModel.datasetTextLLM) { - global.systemDefaultModel.datasetTextLLM = Array.from(global.llmModelMap.values()).find( - (item) => item.datasetProcess - ); - } - if (!global.systemDefaultModel.datasetImageLLM) { - global.systemDefaultModel.datasetImageLLM = Array.from(global.llmModelMap.values()).find( - (item) => item.vision - ); - } - if (!global.systemDefaultModel.embedding) { - global.systemDefaultModel.embedding = Array.from(global.embeddingModelMap.values())[0]; - } - if (!global.systemDefaultModel.tts) { - global.systemDefaultModel.tts = Array.from(global.ttsModelMap.values())[0]; - } - if (!global.systemDefaultModel.stt) { - global.systemDefaultModel.stt = Array.from(global.sttModelMap.values())[0]; - } - if (!global.systemDefaultModel.rerank) { - global.systemDefaultModel.rerank = Array.from(global.reRankModelMap.values())[0]; + { + if (!_systemDefaultModel.llm) { + _systemDefaultModel.llm = Array.from(_llmModelMap.values())[0]; + } + if (!_systemDefaultModel.datasetTextLLM) { + _systemDefaultModel.datasetTextLLM = Array.from(_llmModelMap.values()).find( + (item) => item.datasetProcess + ); + } + if (!_systemDefaultModel.datasetImageLLM) { + _systemDefaultModel.datasetImageLLM = Array.from(_llmModelMap.values()).find( + (item) => item.vision + ); + } + if (!_systemDefaultModel.embedding) { + _systemDefaultModel.embedding = Array.from(_embeddingModelMap.values())[0]; + } + if (!_systemDefaultModel.tts) { + _systemDefaultModel.tts = Array.from(_ttsModelMap.values())[0]; + } + if (!_systemDefaultModel.stt) { + _systemDefaultModel.stt = Array.from(_sttModelMap.values())[0]; + } + if (!_systemDefaultModel.rerank) { + _systemDefaultModel.rerank = Array.from(_reRankModelMap.values())[0]; + } } // Sort model list - global.systemActiveModelList.sort((a, b) => { + _systemActiveModelList.sort((a, b) => { const providerA = getModelProvider(a.provider, language); const providerB = getModelProvider(b.provider, language); return providerA.order - providerB.order; }); - global.systemActiveDesensitizedModels = global.systemActiveModelList.map((model) => ({ - ...model, - defaultSystemChatPrompt: undefined, - fieldMap: undefined, - defaultConfig: undefined, - weight: undefined, - dbConfig: undefined, - queryConfig: undefined, - requestUrl: undefined, - requestAuth: undefined - })) as SystemModelItemType[]; + + // Set global value + { + global.systemModelList = _systemModelList; + global.systemActiveModelList = _systemActiveModelList; + global.llmModelMap = _llmModelMap; + global.embeddingModelMap = _embeddingModelMap; + global.ttsModelMap = _ttsModelMap; + global.sttModelMap = _sttModelMap; + global.reRankModelMap = _reRankModelMap; + global.systemDefaultModel = _systemDefaultModel; + global.systemActiveDesensitizedModels = _systemActiveModelList.map((model) => ({ + ...model, + defaultSystemChatPrompt: undefined, + fieldMap: undefined, + defaultConfig: undefined, + weight: undefined, + dbConfig: undefined, + queryConfig: undefined, + requestUrl: undefined, + requestAuth: undefined + })) as SystemModelItemType[]; + } console.log( - `Load models success, total: ${global.systemModelList.length}, active: ${global.systemActiveModelList.length}`, JSON.stringify( - global.systemActiveModelList.map((item) => ({ + _systemActiveModelList.map((item) => ({ provider: item.provider, model: item.model, name: item.name })), null, 2 - ) + ), + `Load models success, total: ${_systemModelList.length}, active: ${_systemActiveModelList.length}` ); } catch (error) { console.error('Load models error', error); diff --git a/packages/service/core/app/provider/controller.ts b/packages/service/core/app/provider/controller.ts index 452c6bbfa..f19f46b57 100644 --- a/packages/service/core/app/provider/controller.ts +++ b/packages/service/core/app/provider/controller.ts @@ -25,5 +25,5 @@ export const getModelProvider = (provider?: string, language = 'en') => { return defaultProvider; } - return ModelProviderMapCache[language as langType][provider] ?? defaultProvider; + return global.ModelProviderMapCache[language as langType][provider] ?? defaultProvider; };