Commit 7225ebec by Archer Committed by GitHub

perf: model reload (#5879)

* Version doc

* perf: model reload
parent 1f9c8d32
...@@ -11,9 +11,11 @@ description: 'FastGPT V4.14.1 更新说明' ...@@ -11,9 +11,11 @@ description: 'FastGPT V4.14.1 更新说明'
## ⚙️ 优化 ## ⚙️ 优化
1. 在同一轮对话中,MCP Client 会持久化实例,不会销毁。 1. 在同一轮对话中,MCP Client 会持久化实例,不会销毁。
2. 模型重载时候,不会把全局模型配置清空再添加,从而导致重载阶段模型调用错误。
## 🐛 修复 ## 🐛 修复
1. Debug 模式下,交互节点无法正常使用。 1. Debug 模式下,交互节点无法正常使用。
2. 富文本编辑器 tab 空格未对齐。 2. 富文本编辑器 tab 空格未对齐。
3. 嵌套运行 Agent 时候,跳过节点队列未初始化,导致无法正常运行。 3. 嵌套运行 Agent 时候,跳过节点队列未初始化,导致无法正常运行。
4. 判断器右侧是 number 引用时,会出现报错。
...@@ -115,7 +115,7 @@ ...@@ -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/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-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/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/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/41.mdx": "2025-08-02T19:38:37+08:00",
"document/content/docs/upgrading/4-8/42.mdx": "2025-08-02T19:38:37+08:00", "document/content/docs/upgrading/4-8/42.mdx": "2025-08-02T19:38:37+08:00",
......
import { type SystemModelItemType } from '../type'; import type { SystemDefaultModelType, SystemModelItemType } from '../type';
import { ModelTypeEnum } from '@fastgpt/global/core/ai/model'; import { ModelTypeEnum } from '@fastgpt/global/core/ai/model';
import { MongoSystemModel } from './schema'; import { MongoSystemModel } from './schema';
import { import {
...@@ -23,8 +23,38 @@ import { refreshVersionKey } from '../../../common/cache'; ...@@ -23,8 +23,38 @@ import { refreshVersionKey } from '../../../common/cache';
import { SystemCacheKeyEnum } from '../../../common/cache/type'; import { SystemCacheKeyEnum } from '../../../common/cache/type';
export const loadSystemModels = async (init = false, language = 'en') => { 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<string, LLMModelItemType>();
let _embeddingModelMap = new Map<string, EmbeddingModelItemType>();
let _ttsModelMap = new Map<string, TTSModelType>();
let _sttModelMap = new Map<string, STTModelType>();
let _reRankModelMap = new Map<string, RerankModelItemType>();
let _systemDefaultModel: SystemDefaultModelType = {};
if (!global.systemModelList) {
global.systemModelList = [];
global.systemActiveModelList = [];
global.llmModelMap = new Map<string, LLMModelItemType>();
global.embeddingModelMap = new Map<string, EmbeddingModelItemType>();
global.ttsModelMap = new Map<string, TTSModelType>();
global.sttModelMap = new Map<string, STTModelType>();
global.reRankModelMap = new Map<string, RerankModelItemType>();
global.systemDefaultModel = {};
global.systemActiveDesensitizedModels = [];
}
const pushModel = (model: SystemModelItemType) => { const pushModel = (model: SystemModelItemType) => {
global.systemModelList.push(model); _systemModelList.push(model);
// Add default value // Add default value
if (model.type === ModelTypeEnum.llm) { if (model.type === ModelTypeEnum.llm) {
...@@ -36,67 +66,48 @@ export const loadSystemModels = async (init = false, language = 'en') => { ...@@ -36,67 +66,48 @@ export const loadSystemModels = async (init = false, language = 'en') => {
} }
if (model.isActive) { if (model.isActive) {
global.systemActiveModelList.push(model); _systemActiveModelList.push(model);
if (model.type === ModelTypeEnum.llm) { if (model.type === ModelTypeEnum.llm) {
global.llmModelMap.set(model.model, model); _llmModelMap.set(model.model, model);
global.llmModelMap.set(model.name, model); _llmModelMap.set(model.name, model);
if (model.isDefault) { if (model.isDefault) {
global.systemDefaultModel.llm = model; _systemDefaultModel.llm = model;
} }
if (model.isDefaultDatasetTextModel) { if (model.isDefaultDatasetTextModel) {
global.systemDefaultModel.datasetTextLLM = model; _systemDefaultModel.datasetTextLLM = model;
} }
if (model.isDefaultDatasetImageModel) { if (model.isDefaultDatasetImageModel) {
global.systemDefaultModel.datasetImageLLM = model; _systemDefaultModel.datasetImageLLM = model;
} }
} else if (model.type === ModelTypeEnum.embedding) { } else if (model.type === ModelTypeEnum.embedding) {
global.embeddingModelMap.set(model.model, model); _embeddingModelMap.set(model.model, model);
global.embeddingModelMap.set(model.name, model); _embeddingModelMap.set(model.name, model);
if (model.isDefault) { if (model.isDefault) {
global.systemDefaultModel.embedding = model; _systemDefaultModel.embedding = model;
} }
} else if (model.type === ModelTypeEnum.tts) { } else if (model.type === ModelTypeEnum.tts) {
global.ttsModelMap.set(model.model, model); _ttsModelMap.set(model.model, model);
global.ttsModelMap.set(model.name, model); _ttsModelMap.set(model.name, model);
if (model.isDefault) { if (model.isDefault) {
global.systemDefaultModel.tts = model; _systemDefaultModel.tts = model;
} }
} else if (model.type === ModelTypeEnum.stt) { } else if (model.type === ModelTypeEnum.stt) {
global.sttModelMap.set(model.model, model); _sttModelMap.set(model.model, model);
global.sttModelMap.set(model.name, model); _sttModelMap.set(model.name, model);
if (model.isDefault) { if (model.isDefault) {
global.systemDefaultModel.stt = model; _systemDefaultModel.stt = model;
} }
} else if (model.type === ModelTypeEnum.rerank) { } else if (model.type === ModelTypeEnum.rerank) {
global.reRankModelMap.set(model.model, model); _reRankModelMap.set(model.model, model);
global.reRankModelMap.set(model.name, model); _reRankModelMap.set(model.name, model);
if (model.isDefault) { 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<string, LLMModelItemType>();
global.embeddingModelMap = new Map<string, EmbeddingModelItemType>();
global.ttsModelMap = new Map<string, TTSModelType>();
global.sttModelMap = new Map<string, STTModelType>();
global.reRankModelMap = new Map<string, RerankModelItemType>();
// @ts-ignore
global.systemDefaultModel = {};
try { try {
// Get model from db and plugin // Get model from db and plugin
const [dbModels, systemModels] = await Promise.all([ const [dbModels, systemModels] = await Promise.all([
...@@ -144,7 +155,7 @@ export const loadSystemModels = async (init = false, language = 'en') => { ...@@ -144,7 +155,7 @@ export const loadSystemModels = async (init = false, language = 'en') => {
// Custom model(Not in system config) // Custom model(Not in system config)
dbModels.forEach((dbModel) => { dbModels.forEach((dbModel) => {
if (global.systemModelList.find((item) => item.model === dbModel.model)) return; if (_systemModelList.find((item) => item.model === dbModel.model)) return;
pushModel({ pushModel({
...dbModel.metadata, ...dbModel.metadata,
...@@ -153,39 +164,52 @@ export const loadSystemModels = async (init = false, language = 'en') => { ...@@ -153,39 +164,52 @@ export const loadSystemModels = async (init = false, language = 'en') => {
}); });
// Default model check // Default model check
if (!global.systemDefaultModel.llm) { {
global.systemDefaultModel.llm = Array.from(global.llmModelMap.values())[0]; if (!_systemDefaultModel.llm) {
_systemDefaultModel.llm = Array.from(_llmModelMap.values())[0];
} }
if (!global.systemDefaultModel.datasetTextLLM) { if (!_systemDefaultModel.datasetTextLLM) {
global.systemDefaultModel.datasetTextLLM = Array.from(global.llmModelMap.values()).find( _systemDefaultModel.datasetTextLLM = Array.from(_llmModelMap.values()).find(
(item) => item.datasetProcess (item) => item.datasetProcess
); );
} }
if (!global.systemDefaultModel.datasetImageLLM) { if (!_systemDefaultModel.datasetImageLLM) {
global.systemDefaultModel.datasetImageLLM = Array.from(global.llmModelMap.values()).find( _systemDefaultModel.datasetImageLLM = Array.from(_llmModelMap.values()).find(
(item) => item.vision (item) => item.vision
); );
} }
if (!global.systemDefaultModel.embedding) { if (!_systemDefaultModel.embedding) {
global.systemDefaultModel.embedding = Array.from(global.embeddingModelMap.values())[0]; _systemDefaultModel.embedding = Array.from(_embeddingModelMap.values())[0];
}
if (!_systemDefaultModel.tts) {
_systemDefaultModel.tts = Array.from(_ttsModelMap.values())[0];
} }
if (!global.systemDefaultModel.tts) { if (!_systemDefaultModel.stt) {
global.systemDefaultModel.tts = Array.from(global.ttsModelMap.values())[0]; _systemDefaultModel.stt = Array.from(_sttModelMap.values())[0];
} }
if (!global.systemDefaultModel.stt) { if (!_systemDefaultModel.rerank) {
global.systemDefaultModel.stt = Array.from(global.sttModelMap.values())[0]; _systemDefaultModel.rerank = Array.from(_reRankModelMap.values())[0];
} }
if (!global.systemDefaultModel.rerank) {
global.systemDefaultModel.rerank = Array.from(global.reRankModelMap.values())[0];
} }
// Sort model list // Sort model list
global.systemActiveModelList.sort((a, b) => { _systemActiveModelList.sort((a, b) => {
const providerA = getModelProvider(a.provider, language); const providerA = getModelProvider(a.provider, language);
const providerB = getModelProvider(b.provider, language); const providerB = getModelProvider(b.provider, language);
return providerA.order - providerB.order; return providerA.order - providerB.order;
}); });
global.systemActiveDesensitizedModels = global.systemActiveModelList.map((model) => ({
// 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, ...model,
defaultSystemChatPrompt: undefined, defaultSystemChatPrompt: undefined,
fieldMap: undefined, fieldMap: undefined,
...@@ -196,18 +220,19 @@ export const loadSystemModels = async (init = false, language = 'en') => { ...@@ -196,18 +220,19 @@ export const loadSystemModels = async (init = false, language = 'en') => {
requestUrl: undefined, requestUrl: undefined,
requestAuth: undefined requestAuth: undefined
})) as SystemModelItemType[]; })) as SystemModelItemType[];
}
console.log( console.log(
`Load models success, total: ${global.systemModelList.length}, active: ${global.systemActiveModelList.length}`,
JSON.stringify( JSON.stringify(
global.systemActiveModelList.map((item) => ({ _systemActiveModelList.map((item) => ({
provider: item.provider, provider: item.provider,
model: item.model, model: item.model,
name: item.name name: item.name
})), })),
null, null,
2 2
) ),
`Load models success, total: ${_systemModelList.length}, active: ${_systemActiveModelList.length}`
); );
} catch (error) { } catch (error) {
console.error('Load models error', error); console.error('Load models error', error);
......
...@@ -25,5 +25,5 @@ export const getModelProvider = (provider?: string, language = 'en') => { ...@@ -25,5 +25,5 @@ export const getModelProvider = (provider?: string, language = 'en') => {
return defaultProvider; return defaultProvider;
} }
return ModelProviderMapCache[language as langType][provider] ?? defaultProvider; return global.ModelProviderMapCache[language as langType][provider] ?? defaultProvider;
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment