mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-24 13:53:50 +00:00
perf: 去掉testapi
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -36,4 +36,5 @@ yarn-error.log*
|
|||||||
next-env.d.ts
|
next-env.d.ts
|
||||||
/public/trainData/
|
/public/trainData/
|
||||||
/.vscode/
|
/.vscode/
|
||||||
platform.json
|
platform.json
|
||||||
|
testApi/
|
@@ -1,27 +0,0 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { AuthCode } from '@/service/models/authCode';
|
|
||||||
import { connectToDatabase } from '@/service/mongo';
|
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
if (process.env.NODE_ENV !== 'development') {
|
|
||||||
throw new Error('不是开发环境');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
const authCode = await AuthCode.deleteMany({
|
|
||||||
expiredTime: { $lt: Date.now() }
|
|
||||||
});
|
|
||||||
|
|
||||||
jsonRes(res, {
|
|
||||||
message: `删除了${authCode.deletedCount}条记录`
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,28 +0,0 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { connectToDatabase, Chat } from '@/service/mongo';
|
|
||||||
|
|
||||||
/* 定时删除那些不活跃的内容 */
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
if (process.env.NODE_ENV !== 'development') {
|
|
||||||
throw new Error('不是开发环境');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
const response = await Chat.deleteMany(
|
|
||||||
{ $expr: { $lt: [{ $size: '$content' }, 5] } },
|
|
||||||
// 使用 $pull 操作符删除数组中的元素
|
|
||||||
{ $pull: { content: { $exists: true } } }
|
|
||||||
);
|
|
||||||
|
|
||||||
jsonRes(res, {
|
|
||||||
message: `删除了${response.deletedCount}条记录`
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,35 +0,0 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { connectToDatabase, Bill } from '@/service/mongo';
|
|
||||||
import { authToken } from '@/service/utils/tools';
|
|
||||||
import type { BillSchema } from '@/types/mongoSchema';
|
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
try {
|
|
||||||
if (process.env.NODE_ENV !== 'development') {
|
|
||||||
throw new Error('不是开发环境');
|
|
||||||
}
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
const bills = await Bill.find({
|
|
||||||
tokenLen: { $exists: false }
|
|
||||||
});
|
|
||||||
await Promise.all(
|
|
||||||
bills.map((bill) =>
|
|
||||||
Bill.findByIdAndUpdate(bill._id, {
|
|
||||||
tokenLen: bill.textLen
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
jsonRes(res, {
|
|
||||||
data: {}
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,35 +0,0 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { connectToDatabase, SplitData } from '@/service/mongo';
|
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
try {
|
|
||||||
if (process.env.NODE_ENV !== 'development') {
|
|
||||||
throw new Error('不是开发环境');
|
|
||||||
}
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
const data = await SplitData.aggregate([
|
|
||||||
{ $match: { textList: { $exists: true, $ne: [] } } },
|
|
||||||
{ $sample: { size: 1 } }
|
|
||||||
]);
|
|
||||||
|
|
||||||
const dataItem: any = data[0];
|
|
||||||
const textList: string[] = dataItem.textList.slice(-5);
|
|
||||||
console.log(textList);
|
|
||||||
console.log(dataItem.textList.slice(0, -5));
|
|
||||||
await SplitData.findByIdAndUpdate(dataItem._id, {
|
|
||||||
textList: dataItem.textList.slice(0, -5)
|
|
||||||
});
|
|
||||||
|
|
||||||
jsonRes(res, {
|
|
||||||
data: {}
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,79 +0,0 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { connectToDatabase, Training, Model } from '@/service/mongo';
|
|
||||||
import type { TrainingItemType } from '@/types/training';
|
|
||||||
import { TrainingStatusEnum, ModelStatusEnum } from '@/constants/model';
|
|
||||||
import { getUserApiOpenai } from '@/service/utils/openai';
|
|
||||||
import { OpenAiTuneStatusEnum } from '@/service/constants/training';
|
|
||||||
import { sendTrainSucceed } from '@/service/utils/sendEmail';
|
|
||||||
import { httpsAgent } from '@/service/utils/tools';
|
|
||||||
import { ModelPopulate } from '@/types/mongoSchema';
|
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
if (process.env.NODE_ENV !== 'development') {
|
|
||||||
throw new Error('不是开发环境');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await connectToDatabase();
|
|
||||||
|
|
||||||
// 查询正在训练中的训练记录
|
|
||||||
const trainingRecords: TrainingItemType[] = await Training.find({
|
|
||||||
status: TrainingStatusEnum.pending
|
|
||||||
});
|
|
||||||
|
|
||||||
const { openai } = await getUserApiOpenai('63f9a14228d2a688d8dc9e1b');
|
|
||||||
|
|
||||||
const response = await Promise.all(
|
|
||||||
trainingRecords.map(async (item) => {
|
|
||||||
const { data } = await openai.retrieveFineTune(item.tuneId, { httpsAgent });
|
|
||||||
if (data.status === OpenAiTuneStatusEnum.succeeded) {
|
|
||||||
// 删除训练文件
|
|
||||||
openai.deleteFile(data.training_files[0].id, { httpsAgent });
|
|
||||||
|
|
||||||
const model = await Model.findById<ModelPopulate>(item.modelId).populate({
|
|
||||||
path: 'userId',
|
|
||||||
options: {
|
|
||||||
strictPopulate: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!model) {
|
|
||||||
throw new Error('模型不存在');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新模型
|
|
||||||
await Model.findByIdAndUpdate(item.modelId, {
|
|
||||||
status: ModelStatusEnum.running,
|
|
||||||
updateTime: new Date(),
|
|
||||||
service: {
|
|
||||||
...model.service,
|
|
||||||
trainId: data.fine_tuned_model, // 训练完后,再次训练和对话使用的 model 是一样的
|
|
||||||
chatModel: data.fine_tuned_model
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 更新训练数据
|
|
||||||
await Training.findByIdAndUpdate(item._id, {
|
|
||||||
status: TrainingStatusEnum.succeed
|
|
||||||
});
|
|
||||||
|
|
||||||
// 发送邮件通知
|
|
||||||
await sendTrainSucceed(model.userId.email as string, model.name);
|
|
||||||
return 'succeed';
|
|
||||||
}
|
|
||||||
return 'pending';
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
jsonRes(res, {
|
|
||||||
data: `${response.length}个训练线程,${
|
|
||||||
response.filter((item) => item === 'succeed').length
|
|
||||||
}个完成`
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user