Skip to content

Commit

Permalink
feat: add training retry time (#3187)
Browse files Browse the repository at this point in the history
* feat: add training retry time

* remoce log
  • Loading branch information
c121914yu authored Nov 18, 2024
1 parent fdb3720 commit a9db5b5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/service/core/dataset/training/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const TrainingDataSchema = new Schema({
type: Date,
default: () => new Date('2000/1/1')
},
retryCount: {
type: Number,
default: 5
},

model: {
// ai model
type: String,
Expand Down Expand Up @@ -97,7 +102,7 @@ try {
// lock training data(teamId); delete training data
TrainingDataSchema.index({ teamId: 1, datasetId: 1 });
// get training data and sort
TrainingDataSchema.index({ mode: 1, lockTime: 1, weight: -1 });
TrainingDataSchema.index({ mode: 1, retryCount: 1, lockTime: 1, weight: -1 });
TrainingDataSchema.index({ expireAt: 1 }, { expireAfterSeconds: 7 * 24 * 60 * 60 }); // 7 days
} catch (error) {
console.log(error);
Expand Down
35 changes: 34 additions & 1 deletion projects/app/src/pages/api/core/dataset/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@fastgpt/global/support/permission/constant';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
import { DatasetTypeEnum } from '@fastgpt/global/core/dataset/constants';
import { DatasetTypeEnum, TrainingModeEnum } from '@fastgpt/global/core/dataset/constants';
import { ClientSession } from 'mongoose';
import { parseParentIdInMongo } from '@fastgpt/global/common/parentFolder/utils';
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
Expand All @@ -21,6 +21,7 @@ import {
import { authUserPer } from '@fastgpt/service/support/permission/user/auth';
import { TeamWritePermissionVal } from '@fastgpt/global/support/permission/user/constant';
import { DatasetErrEnum } from '@fastgpt/global/common/error/code/dataset';
import { MongoDatasetTraining } from '@fastgpt/service/core/dataset/training/schema';

export type DatasetUpdateQuery = {};
export type DatasetUpdateResponse = any;
Expand Down Expand Up @@ -84,6 +85,12 @@ async function handler(

const isFolder = dataset.type === DatasetTypeEnum.folder;

updateTraining({
teamId: dataset.teamId,
datasetId: id,
agentModel: agentModel?.model
});

const onUpdate = async (session?: ClientSession) => {
await MongoDataset.findByIdAndUpdate(
id,
Expand Down Expand Up @@ -137,3 +144,29 @@ async function handler(
}
}
export default NextAPI(handler);

async function updateTraining({
teamId,
datasetId,
agentModel
}: {
teamId: string;
datasetId: string;
agentModel?: string;
}) {
if (!agentModel) return;

await MongoDatasetTraining.updateMany(
{
teamId,
datasetId,
mode: { $in: [TrainingModeEnum.qa, TrainingModeEnum.auto] }
},
{
$set: {
model: agentModel,
retryCount: 5
}
}
);
}
8 changes: 5 additions & 3 deletions projects/app/src/service/events/generateQA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export async function generateQA(): Promise<any> {
try {
const data = await MongoDatasetTraining.findOneAndUpdate(
{
lockTime: { $lte: addMinutes(new Date(), -6) },
mode: TrainingModeEnum.qa
mode: TrainingModeEnum.qa,
retryCount: { $gte: 0 },
lockTime: { $lte: addMinutes(new Date(), -6) }
},
{
lockTime: new Date()
lockTime: new Date(),
$inc: { retryCount: -1 }
}
)
.select({
Expand Down
6 changes: 4 additions & 2 deletions projects/app/src/service/events/generateVector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export async function generateVector(): Promise<any> {
const data = await MongoDatasetTraining.findOneAndUpdate(
{
mode: TrainingModeEnum.chunk,
lockTime: { $lte: addMinutes(new Date(), -1) }
retryCount: { $gte: 0 },
lockTime: { $lte: addMinutes(new Date(), -6) }
},
{
lockTime: new Date()
lockTime: new Date(),
$inc: { retryCount: -1 }
}
).select({
_id: 1,
Expand Down

0 comments on commit a9db5b5

Please sign in to comment.