mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 20:37:48 +00:00

* feat: add third party account config (#3443) * temp * editor workflow variable style * add team to dispatch * i18n * delete console * change openai account position * fix * fix * fix * fix * fix * 4.8.17 test (#3461) * perf: external provider config * perf: ui * feat: add template config (#3434) * change template position * template config * delete console * delete * fix * fix * perf: Mongo visutal field (#3464) * remve invalid code * perf: team member visutal code * perf: virtual search; perf: search test data * fix: ts * fix: image response headers * perf: template code * perf: auth layout;perf: auto save (#3472) * perf: auth layout * perf: auto save * perf: auto save * fix: template guide display & http input support external variables (#3475) * fix: template guide display * http editor support external workflow variables * perf: auto save;fix: ifelse checker line break; (#3478) * perf: auto save * perf: auto save * fix: ifelse checker line break * perf: doc * perf: doc * fix: update var type error * 4.8.17 test (#3479) * perf: auto save * perf: auto save * perf: template code * 4.8.17 test (#3480) * perf: auto save * perf: auto save * perf: model price model * feat: add react memo * perf: model provider filter * fix: ts (#3481) * perf: auto save * perf: auto save * fix: ts * simple app tool select (#3473) * workflow plugin userguide & simple tool ui * simple tool filter * reuse component * change component to hook * fix * perf: too selector modal (#3484) * perf: auto save * perf: auto save * perf: markdown render * perf: too selector * fix: app version require tmbId * perf: templates refresh * perf: templates refresh * hide auto save error tip * perf: toolkit guide --------- Co-authored-by: heheer <heheer@sealos.io>
206 lines
5.1 KiB
TypeScript
206 lines
5.1 KiB
TypeScript
import { MongoDatasetTraining } from './schema';
|
|
import type {
|
|
PushDatasetDataChunkProps,
|
|
PushDatasetDataProps,
|
|
PushDatasetDataResponse
|
|
} from '@fastgpt/global/core/dataset/api.d';
|
|
import { TrainingModeEnum } from '@fastgpt/global/core/dataset/constants';
|
|
import { simpleText } from '@fastgpt/global/common/string/tools';
|
|
import { ClientSession } from '../../../common/mongo';
|
|
import { getLLMModel, getVectorModel } from '../../ai/model';
|
|
import { addLog } from '../../../common/system/log';
|
|
import { getCollectionWithDataset } from '../controller';
|
|
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
|
|
|
|
export const lockTrainingDataByTeamId = async (teamId: string): Promise<any> => {
|
|
try {
|
|
await MongoDatasetTraining.updateMany(
|
|
{
|
|
teamId
|
|
},
|
|
{
|
|
lockTime: new Date('2999/5/5')
|
|
}
|
|
);
|
|
} catch (error) {}
|
|
};
|
|
|
|
export const pushDataListToTrainingQueueByCollectionId = async ({
|
|
collectionId,
|
|
...props
|
|
}: {
|
|
teamId: string;
|
|
tmbId: string;
|
|
session?: ClientSession;
|
|
} & PushDatasetDataProps) => {
|
|
const {
|
|
dataset: { _id: datasetId, agentModel, vectorModel }
|
|
} = await getCollectionWithDataset(collectionId);
|
|
return pushDataListToTrainingQueue({
|
|
...props,
|
|
datasetId,
|
|
collectionId,
|
|
agentModel,
|
|
vectorModel
|
|
});
|
|
};
|
|
|
|
export async function pushDataListToTrainingQueue({
|
|
teamId,
|
|
tmbId,
|
|
datasetId,
|
|
collectionId,
|
|
agentModel,
|
|
vectorModel,
|
|
data,
|
|
prompt,
|
|
billId,
|
|
trainingMode = TrainingModeEnum.chunk,
|
|
session
|
|
}: {
|
|
teamId: string;
|
|
tmbId: string;
|
|
datasetId: string;
|
|
agentModel: string;
|
|
vectorModel: string;
|
|
session?: ClientSession;
|
|
} & PushDatasetDataProps): Promise<PushDatasetDataResponse> {
|
|
const { model, maxToken, weight } = await (async () => {
|
|
const agentModelData = getLLMModel(agentModel);
|
|
if (!agentModelData) {
|
|
return Promise.reject(`File model ${agentModel} is inValid`);
|
|
}
|
|
const vectorModelData = getVectorModel(vectorModel);
|
|
if (!vectorModelData) {
|
|
return Promise.reject(`Vector model ${vectorModel} is inValid`);
|
|
}
|
|
|
|
if (trainingMode === TrainingModeEnum.chunk) {
|
|
return {
|
|
maxToken: vectorModelData.maxToken * 1.5,
|
|
model: vectorModelData.model,
|
|
weight: vectorModelData.weight
|
|
};
|
|
}
|
|
|
|
if (trainingMode === TrainingModeEnum.qa || trainingMode === TrainingModeEnum.auto) {
|
|
return {
|
|
maxToken: agentModelData.maxContext * 0.8,
|
|
model: agentModelData.model,
|
|
weight: 0
|
|
};
|
|
}
|
|
|
|
return Promise.reject(`Training mode "${trainingMode}" is inValid`);
|
|
})();
|
|
|
|
// filter repeat or equal content
|
|
const set = new Set();
|
|
const filterResult: Record<string, PushDatasetDataChunkProps[]> = {
|
|
success: [],
|
|
overToken: [],
|
|
repeat: [],
|
|
error: []
|
|
};
|
|
|
|
// format q and a, remove empty char
|
|
data.forEach((item) => {
|
|
item.q = simpleText(item.q);
|
|
item.a = simpleText(item.a);
|
|
|
|
item.indexes = item.indexes
|
|
?.map((index) => {
|
|
return {
|
|
...index,
|
|
text: simpleText(index.text)
|
|
};
|
|
})
|
|
.filter(Boolean);
|
|
|
|
// filter repeat content
|
|
if (!item.q) {
|
|
filterResult.error.push(item);
|
|
return;
|
|
}
|
|
|
|
const text = item.q + item.a;
|
|
|
|
if (text.length > maxToken) {
|
|
filterResult.overToken.push(item);
|
|
return;
|
|
}
|
|
|
|
if (set.has(text)) {
|
|
console.log('repeat', item);
|
|
filterResult.repeat.push(item);
|
|
} else {
|
|
filterResult.success.push(item);
|
|
set.add(text);
|
|
}
|
|
});
|
|
|
|
// insert data to db
|
|
const insertLen = filterResult.success.length;
|
|
const failedDocuments: PushDatasetDataChunkProps[] = [];
|
|
|
|
// 使用 insertMany 批量插入
|
|
const batchSize = 200;
|
|
const insertData = async (startIndex: number, session: ClientSession) => {
|
|
const list = filterResult.success.slice(startIndex, startIndex + batchSize);
|
|
|
|
if (list.length === 0) return;
|
|
|
|
try {
|
|
await MongoDatasetTraining.insertMany(
|
|
list.map((item) => ({
|
|
teamId,
|
|
tmbId,
|
|
datasetId,
|
|
collectionId,
|
|
billId,
|
|
mode: trainingMode,
|
|
prompt,
|
|
model,
|
|
q: item.q,
|
|
a: item.a,
|
|
chunkIndex: item.chunkIndex ?? 0,
|
|
weight: weight ?? 0,
|
|
indexes: item.indexes,
|
|
retryCount: 5
|
|
})),
|
|
{
|
|
session,
|
|
ordered: true
|
|
}
|
|
);
|
|
} catch (error: any) {
|
|
addLog.error(`Insert error`, error);
|
|
// 如果有错误,将失败的文档添加到失败列表中
|
|
error.writeErrors?.forEach((writeError: any) => {
|
|
failedDocuments.push(data[writeError.index]);
|
|
});
|
|
console.log('failed', failedDocuments);
|
|
}
|
|
|
|
// 对于失败的文档,尝试单独插入
|
|
await MongoDatasetTraining.create(failedDocuments, { session });
|
|
|
|
return insertData(startIndex + batchSize, session);
|
|
};
|
|
|
|
if (session) {
|
|
await insertData(0, session);
|
|
} else {
|
|
await mongoSessionRun(async (session) => {
|
|
await insertData(0, session);
|
|
});
|
|
}
|
|
|
|
delete filterResult.success;
|
|
|
|
return {
|
|
insertLen,
|
|
...filterResult
|
|
};
|
|
}
|