diff --git a/.gitignore b/.gitignore index 0f2b0d56e..a1ce0b9ff 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,5 @@ yarn-error.log* next-env.d.ts /public/trainData/ /.vscode/ -platform.json \ No newline at end of file +platform.json +testApi/ \ No newline at end of file diff --git a/src/pages/api/timer/clearAuthCode.ts b/src/pages/api/timer/clearAuthCode.ts deleted file mode 100644 index ffb046f1a..000000000 --- a/src/pages/api/timer/clearAuthCode.ts +++ /dev/null @@ -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 - }); - } -} diff --git a/src/pages/api/timer/clearChatWindow.ts b/src/pages/api/timer/clearChatWindow.ts deleted file mode 100644 index f7e13b751..000000000 --- a/src/pages/api/timer/clearChatWindow.ts +++ /dev/null @@ -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 - }); - } -} diff --git a/src/pages/api/timer/initBill.ts b/src/pages/api/timer/initBill.ts deleted file mode 100644 index 33ba5c3c7..000000000 --- a/src/pages/api/timer/initBill.ts +++ /dev/null @@ -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 - }); - } -} diff --git a/src/pages/api/timer/test.ts b/src/pages/api/timer/test.ts deleted file mode 100644 index 1611628e6..000000000 --- a/src/pages/api/timer/test.ts +++ /dev/null @@ -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 - }); - } -} diff --git a/src/pages/api/timer/updateTraining.ts b/src/pages/api/timer/updateTraining.ts deleted file mode 100644 index a6140554a..000000000 --- a/src/pages/api/timer/updateTraining.ts +++ /dev/null @@ -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(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 - }); - } -}