mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00

* Training status (#4424) * dataset data training state (#4311) * dataset data training state * fix * fix ts * fix * fix api format * fix * fix * perf: count training * format * fix: dataset training state (#4417) * fix * add test * fix * fix * fix test * fix test * perf: training count * count * loading status --------- Co-authored-by: heheer <heheer@sealos.io> * doc * website sync feature (#4429) * perf: introduce BullMQ for website sync (#4403) * perf: introduce BullMQ for website sync * feat: new redis module * fix: remove graceful shutdown * perf: improve UI in dataset detail - Updated the "change" icon SVG file. - Modified i18n strings. - Added new i18n string "immediate_sync". - Improved UI in dataset detail page, including button icons and background colors. * refactor: Add chunkSettings to DatasetSchema * perf: website sync ux * env template * fix: clean up website dataset when updating chunk settings (#4420) * perf: check setting updated * perf: worker currency * feat: init script for website sync refactor (#4425) * website feature doc --------- Co-authored-by: a.e. <49438478+I-Info@users.noreply.github.com> * pro migration (#4388) (#4433) * pro migration * reuse customPdfParseType Co-authored-by: gggaaallleee <91131304+gggaaallleee@users.noreply.github.com> * perf: remove loading ui * feat: config chat file expired time * Redis cache (#4436) * perf: add Redis cache for vector counting (#4432) * feat: cache * perf: get cache key --------- Co-authored-by: a.e. <49438478+I-Info@users.noreply.github.com> * perf: mobile voice input (#4437) * update:Mobile voice interaction (#4362) * Add files via upload * Add files via upload * Update ollama.md * Update ollama.md * Add files via upload * Update useSpeech.ts * Update ChatInput.tsx * Update useSpeech.ts * Update ChatInput.tsx * Update useSpeech.ts * Update constants.ts * Add files via upload * Update ChatInput.tsx * Update useSpeech.ts * Update useSpeech.ts * Update useSpeech.ts * Update ChatInput.tsx * Add files via upload * Update common.json * Update VoiceInput.tsx * Update ChatInput.tsx * Update VoiceInput.tsx * Update useSpeech.ts * Update useSpeech.ts * Update common.json * Update common.json * Update common.json * Update VoiceInput.tsx * Update VoiceInput.tsx * Update ChatInput.tsx * Update VoiceInput.tsx * Update ChatInput.tsx * Update VoiceInput.tsx * Update ChatInput.tsx * Update useSpeech.ts * Update common.json * Update chat.json * Update common.json * Update chat.json * Update common.json * Update chat.json * Update VoiceInput.tsx * Update ChatInput.tsx * Update useSpeech.ts * Update VoiceInput.tsx * speech ui * 优化语音输入组件,调整输入框显示逻辑,修复语音输入遮罩层样式,更新画布背景透明度,增强用户交互体验。 (#4435) * perf: mobil voice input --------- Co-authored-by: dreamer6680 <1468683855@qq.com> * Test completion v2 (#4438) * add v2 completions (#4364) * add v2 completions * completion config * config version * fix * frontend * doc * fix * fix: completions v2 api --------- Co-authored-by: heheer <heheer@sealos.io> * package * Test mongo log (#4443) * feat: mongodb-log (#4426) * perf: mongo log * feat: completions stop reasoner * mongo db log --------- Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com> * update doc * Update doc * fix external var ui (#4444) * action * fix: ts (#4458) * preview doc action add docs preview permission update preview action udpate action * update doc (#4460) * update preview action * update doc * remove * update * schema * update mq export;perf: redis cache (#4465) * perf: redis cache * update mq export * perf: website sync error tip * add error worker * website sync ui (#4466) * Updated the dynamic display of the voice input pop-up (#4469) * Update VoiceInput.tsx * Update VoiceInput.tsx * Update VoiceInput.tsx * fix: voice input --------- Co-authored-by: heheer <heheer@sealos.io> Co-authored-by: a.e. <49438478+I-Info@users.noreply.github.com> Co-authored-by: gggaaallleee <91131304+gggaaallleee@users.noreply.github.com> Co-authored-by: dreamer6680 <1468683855@qq.com> Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com>
400 lines
10 KiB
TypeScript
400 lines
10 KiB
TypeScript
import {
|
|
DatasetCollectionTypeEnum,
|
|
DatasetCollectionDataProcessModeEnum,
|
|
DatasetTypeEnum
|
|
} from '@fastgpt/global/core/dataset/constants';
|
|
import type { CreateDatasetCollectionParams } from '@fastgpt/global/core/dataset/api.d';
|
|
import { MongoDatasetCollection } from './schema';
|
|
import { DatasetCollectionSchemaType, DatasetSchemaType } from '@fastgpt/global/core/dataset/type';
|
|
import { MongoDatasetTraining } from '../training/schema';
|
|
import { MongoDatasetData } from '../data/schema';
|
|
import { delImgByRelatedId } from '../../../common/file/image/controller';
|
|
import { deleteDatasetDataVector } from '../../../common/vectorStore/controller';
|
|
import { delFileByFileIdList } from '../../../common/file/gridfs/controller';
|
|
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
|
import { ClientSession } from '../../../common/mongo';
|
|
import { createOrGetCollectionTags } from './utils';
|
|
import { rawText2Chunks } from '../read';
|
|
import { checkDatasetLimit } from '../../../support/permission/teamLimit';
|
|
import { predictDataLimitLength } from '../../../../global/core/dataset/utils';
|
|
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
|
|
import { createTrainingUsage } from '../../../support/wallet/usage/controller';
|
|
import { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
|
|
import { getLLMModel, getEmbeddingModel, getVlmModel } from '../../ai/model';
|
|
import { pushDataListToTrainingQueue } from '../training/controller';
|
|
import { MongoImage } from '../../../common/file/image/schema';
|
|
import { hashStr } from '@fastgpt/global/common/string/tools';
|
|
import { addDays } from 'date-fns';
|
|
import { MongoDatasetDataText } from '../data/dataTextSchema';
|
|
import { retryFn } from '@fastgpt/global/common/system/utils';
|
|
import { getTrainingModeByCollection } from './utils';
|
|
import {
|
|
computeChunkSize,
|
|
computeChunkSplitter,
|
|
getLLMMaxChunkSize
|
|
} from '@fastgpt/global/core/dataset/training/utils';
|
|
|
|
export const createCollectionAndInsertData = async ({
|
|
dataset,
|
|
rawText,
|
|
relatedId,
|
|
createCollectionParams,
|
|
isQAImport = false,
|
|
billId,
|
|
session
|
|
}: {
|
|
dataset: DatasetSchemaType;
|
|
rawText: string;
|
|
relatedId?: string;
|
|
createCollectionParams: CreateOneCollectionParams;
|
|
|
|
isQAImport?: boolean;
|
|
billId?: string;
|
|
session?: ClientSession;
|
|
}) => {
|
|
// Adapter 4.9.0
|
|
if (createCollectionParams.trainingType === DatasetCollectionDataProcessModeEnum.auto) {
|
|
createCollectionParams.trainingType = DatasetCollectionDataProcessModeEnum.chunk;
|
|
createCollectionParams.autoIndexes = true;
|
|
}
|
|
|
|
const teamId = createCollectionParams.teamId;
|
|
const tmbId = createCollectionParams.tmbId;
|
|
|
|
// Set default params
|
|
const trainingType =
|
|
createCollectionParams.trainingType || DatasetCollectionDataProcessModeEnum.chunk;
|
|
const chunkSize = computeChunkSize({
|
|
...createCollectionParams,
|
|
trainingType,
|
|
llmModel: getLLMModel(dataset.agentModel)
|
|
});
|
|
const chunkSplitter = computeChunkSplitter(createCollectionParams);
|
|
|
|
// 1. split chunks
|
|
const chunks = rawText2Chunks({
|
|
rawText,
|
|
chunkSize,
|
|
maxSize: getLLMMaxChunkSize(getLLMModel(dataset.agentModel)),
|
|
overlapRatio: trainingType === DatasetCollectionDataProcessModeEnum.chunk ? 0.2 : 0,
|
|
customReg: chunkSplitter ? [chunkSplitter] : [],
|
|
isQAImport
|
|
});
|
|
|
|
// 2. auth limit
|
|
await checkDatasetLimit({
|
|
teamId,
|
|
insertLen: predictDataLimitLength(
|
|
getTrainingModeByCollection({
|
|
trainingType: trainingType,
|
|
autoIndexes: createCollectionParams.autoIndexes,
|
|
imageIndex: createCollectionParams.imageIndex
|
|
}),
|
|
chunks
|
|
)
|
|
});
|
|
|
|
const fn = async (session: ClientSession) => {
|
|
// 3. create collection
|
|
const { _id: collectionId } = await createOneCollection({
|
|
...createCollectionParams,
|
|
trainingType,
|
|
chunkSize,
|
|
chunkSplitter,
|
|
|
|
hashRawText: hashStr(rawText),
|
|
rawTextLength: rawText.length,
|
|
nextSyncTime: (() => {
|
|
// ignore auto collections sync for website datasets
|
|
if (!dataset.autoSync && dataset.type === DatasetTypeEnum.websiteDataset) return undefined;
|
|
if (
|
|
[DatasetCollectionTypeEnum.link, DatasetCollectionTypeEnum.apiFile].includes(
|
|
createCollectionParams.type
|
|
)
|
|
) {
|
|
return addDays(new Date(), 1);
|
|
}
|
|
return undefined;
|
|
})(),
|
|
session
|
|
});
|
|
|
|
// 4. create training bill
|
|
const traingBillId = await (async () => {
|
|
if (billId) return billId;
|
|
const { billId: newBillId } = await createTrainingUsage({
|
|
teamId,
|
|
tmbId,
|
|
appName: createCollectionParams.name,
|
|
billSource: UsageSourceEnum.training,
|
|
vectorModel: getEmbeddingModel(dataset.vectorModel)?.name,
|
|
agentModel: getLLMModel(dataset.agentModel)?.name,
|
|
vllmModel: getVlmModel(dataset.vlmModel)?.name,
|
|
session
|
|
});
|
|
return newBillId;
|
|
})();
|
|
|
|
// 5. insert to training queue
|
|
const insertResults = await pushDataListToTrainingQueue({
|
|
teamId,
|
|
tmbId,
|
|
datasetId: dataset._id,
|
|
collectionId,
|
|
agentModel: dataset.agentModel,
|
|
vectorModel: dataset.vectorModel,
|
|
vlmModel: dataset.vlmModel,
|
|
indexSize: createCollectionParams.indexSize,
|
|
mode: getTrainingModeByCollection({
|
|
trainingType: trainingType,
|
|
autoIndexes: createCollectionParams.autoIndexes,
|
|
imageIndex: createCollectionParams.imageIndex
|
|
}),
|
|
prompt: createCollectionParams.qaPrompt,
|
|
billId: traingBillId,
|
|
data: chunks.map((item, index) => ({
|
|
...item,
|
|
chunkIndex: index
|
|
})),
|
|
session
|
|
});
|
|
|
|
// 6. remove related image ttl
|
|
if (relatedId) {
|
|
await MongoImage.updateMany(
|
|
{
|
|
teamId,
|
|
'metadata.relatedId': relatedId
|
|
},
|
|
{
|
|
// Remove expiredTime to avoid ttl expiration
|
|
$unset: {
|
|
expiredTime: 1
|
|
}
|
|
},
|
|
{
|
|
session
|
|
}
|
|
);
|
|
}
|
|
|
|
return {
|
|
collectionId,
|
|
insertResults
|
|
};
|
|
};
|
|
|
|
if (session) {
|
|
return fn(session);
|
|
}
|
|
return mongoSessionRun(fn);
|
|
};
|
|
|
|
export type CreateOneCollectionParams = CreateDatasetCollectionParams & {
|
|
teamId: string;
|
|
tmbId: string;
|
|
session?: ClientSession;
|
|
};
|
|
export async function createOneCollection({
|
|
teamId,
|
|
tmbId,
|
|
name,
|
|
parentId,
|
|
datasetId,
|
|
type,
|
|
|
|
createTime,
|
|
updateTime,
|
|
|
|
hashRawText,
|
|
rawTextLength,
|
|
metadata = {},
|
|
tags,
|
|
|
|
nextSyncTime,
|
|
|
|
fileId,
|
|
rawLink,
|
|
externalFileId,
|
|
externalFileUrl,
|
|
apiFileId,
|
|
|
|
// Parse settings
|
|
customPdfParse,
|
|
imageIndex,
|
|
autoIndexes,
|
|
|
|
// Chunk settings
|
|
trainingType,
|
|
chunkSettingMode,
|
|
chunkSplitMode,
|
|
chunkSize,
|
|
indexSize,
|
|
chunkSplitter,
|
|
qaPrompt,
|
|
|
|
session
|
|
}: CreateOneCollectionParams) {
|
|
// Create collection tags
|
|
const collectionTags = await createOrGetCollectionTags({ tags, teamId, datasetId, session });
|
|
|
|
// Create collection
|
|
const [collection] = await MongoDatasetCollection.create(
|
|
[
|
|
{
|
|
teamId,
|
|
tmbId,
|
|
parentId: parentId || null,
|
|
datasetId,
|
|
name,
|
|
type,
|
|
|
|
rawTextLength,
|
|
hashRawText,
|
|
tags: collectionTags,
|
|
metadata,
|
|
|
|
createTime,
|
|
updateTime,
|
|
nextSyncTime,
|
|
|
|
...(fileId ? { fileId } : {}),
|
|
...(rawLink ? { rawLink } : {}),
|
|
...(externalFileId ? { externalFileId } : {}),
|
|
...(externalFileUrl ? { externalFileUrl } : {}),
|
|
...(apiFileId ? { apiFileId } : {}),
|
|
|
|
// Parse settings
|
|
customPdfParse,
|
|
imageIndex,
|
|
autoIndexes,
|
|
|
|
// Chunk settings
|
|
trainingType,
|
|
chunkSettingMode,
|
|
chunkSplitMode,
|
|
chunkSize,
|
|
indexSize,
|
|
chunkSplitter,
|
|
qaPrompt
|
|
}
|
|
],
|
|
{ session, ordered: true }
|
|
);
|
|
|
|
return collection;
|
|
}
|
|
|
|
/* delete collection related images/files */
|
|
export const delCollectionRelatedSource = async ({
|
|
collections,
|
|
session
|
|
}: {
|
|
collections: {
|
|
teamId: string;
|
|
fileId?: string;
|
|
metadata?: {
|
|
relatedImgId?: string;
|
|
};
|
|
}[];
|
|
session?: ClientSession;
|
|
}) => {
|
|
if (collections.length === 0) return;
|
|
|
|
const teamId = collections[0].teamId;
|
|
|
|
if (!teamId) return Promise.reject('teamId is not exist');
|
|
|
|
const fileIdList = collections.map((item) => item?.fileId || '').filter(Boolean);
|
|
const relatedImageIds = collections
|
|
.map((item) => item?.metadata?.relatedImgId || '')
|
|
.filter(Boolean);
|
|
|
|
// Delete files
|
|
await delFileByFileIdList({
|
|
bucketName: BucketNameEnum.dataset,
|
|
fileIdList
|
|
});
|
|
// Delete images
|
|
await delImgByRelatedId({
|
|
teamId,
|
|
relateIds: relatedImageIds,
|
|
session
|
|
});
|
|
};
|
|
/**
|
|
* delete collection and it related data
|
|
*/
|
|
export async function delCollection({
|
|
collections,
|
|
session,
|
|
delImg = true,
|
|
delFile = true
|
|
}: {
|
|
collections: DatasetCollectionSchemaType[];
|
|
session: ClientSession;
|
|
delImg: boolean;
|
|
delFile: boolean;
|
|
}) {
|
|
if (collections.length === 0) return;
|
|
|
|
const teamId = collections[0].teamId;
|
|
|
|
if (!teamId) return Promise.reject('teamId is not exist');
|
|
|
|
const datasetIds = Array.from(new Set(collections.map((item) => String(item.datasetId))));
|
|
const collectionIds = collections.map((item) => String(item._id));
|
|
|
|
await retryFn(async () => {
|
|
await Promise.all([
|
|
// Delete training data
|
|
MongoDatasetTraining.deleteMany({
|
|
teamId,
|
|
datasetId: { $in: datasetIds },
|
|
collectionId: { $in: collectionIds }
|
|
}),
|
|
// Delete dataset_data_texts
|
|
MongoDatasetDataText.deleteMany({
|
|
teamId,
|
|
datasetId: { $in: datasetIds },
|
|
collectionId: { $in: collectionIds }
|
|
}),
|
|
// Delete dataset_datas
|
|
MongoDatasetData.deleteMany({
|
|
teamId,
|
|
datasetId: { $in: datasetIds },
|
|
collectionId: { $in: collectionIds }
|
|
}),
|
|
...(delImg
|
|
? [
|
|
delImgByRelatedId({
|
|
teamId,
|
|
relateIds: collections
|
|
.map((item) => item?.metadata?.relatedImgId || '')
|
|
.filter(Boolean)
|
|
})
|
|
]
|
|
: []),
|
|
...(delFile
|
|
? [
|
|
delFileByFileIdList({
|
|
bucketName: BucketNameEnum.dataset,
|
|
fileIdList: collections.map((item) => item?.fileId || '').filter(Boolean)
|
|
})
|
|
]
|
|
: []),
|
|
// Delete vector data
|
|
deleteDatasetDataVector({ teamId, datasetIds, collectionIds })
|
|
]);
|
|
|
|
// delete collections
|
|
await MongoDatasetCollection.deleteMany(
|
|
{
|
|
teamId,
|
|
_id: { $in: collectionIds }
|
|
},
|
|
{ session }
|
|
);
|
|
});
|
|
}
|