Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add training retry time #3187

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading