From 586607a9ced06a6e68cb2f9e64ccd202cd5aa76c Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Tue, 28 Mar 2023 17:56:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20modeldata=E6=8E=A5=E5=8F=A3=E3=80=82fix?= =?UTF-8?q?:=20=E9=83=A8=E5=88=86=E6=9D=83=E9=99=90=E6=A0=A1=E9=AA=8Cbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/chat/delChatRecordByIndex.ts | 24 ++++++-- src/pages/api/data/splitData.ts | 4 +- src/pages/api/model/data/delMoedlDataById.ts | 38 +++++++++++++ src/pages/api/model/data/getModelData.ts | 52 ++++++++++++++++++ src/pages/api/model/data/pushModelData.ts | 55 +++++++++++++++++++ src/pages/api/model/data/putModelData.ts | 44 +++++++++++++++ .../api/model/{ => train}/getTrainings.ts | 0 .../api/model/{ => train}/putTrainStatus.ts | 0 src/pages/api/model/{ => train}/train.ts | 0 src/service/models/modelData.ts | 36 ++++++++++++ src/service/mongo.ts | 1 + src/service/utils/chat.ts | 5 +- src/types/mongoSchema.d.ts | 8 +++ 13 files changed, 260 insertions(+), 7 deletions(-) create mode 100644 src/pages/api/model/data/delMoedlDataById.ts create mode 100644 src/pages/api/model/data/getModelData.ts create mode 100644 src/pages/api/model/data/pushModelData.ts create mode 100644 src/pages/api/model/data/putModelData.ts rename src/pages/api/model/{ => train}/getTrainings.ts (100%) rename src/pages/api/model/{ => train}/putTrainStatus.ts (100%) rename src/pages/api/model/{ => train}/train.ts (100%) create mode 100644 src/service/models/modelData.ts diff --git a/src/pages/api/chat/delChatRecordByIndex.ts b/src/pages/api/chat/delChatRecordByIndex.ts index f3c5278da..2b8b95ee7 100644 --- a/src/pages/api/chat/delChatRecordByIndex.ts +++ b/src/pages/api/chat/delChatRecordByIndex.ts @@ -1,17 +1,25 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { jsonRes } from '@/service/response'; import { connectToDatabase, Chat } from '@/service/mongo'; +import { authToken } from '@/service/utils/tools'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { const { chatId, index } = req.query as { chatId: string; index: string }; + const { authorization } = req.headers; + if (!authorization) { + throw new Error('无权操作'); + } if (!chatId || !index) { throw new Error('缺少参数'); } await connectToDatabase(); + // 凭证校验 + const userId = await authToken(authorization); + const chatRecord = await Chat.findById(chatId); if (!chatRecord) { @@ -31,12 +39,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) } // 删除最一条数据库记录, 也就是预发送的那一条 - await Chat.findByIdAndUpdate(chatId, { - $set: { - [`content.${deletedIndex}.deleted`]: true, - updateTime: Date.now() + await Chat.updateOne( + { + _id: chatId, + userId + }, + { + $set: { + [`content.${deletedIndex}.deleted`]: true, + updateTime: Date.now() + } } - }); + ); jsonRes(res); } catch (err) { diff --git a/src/pages/api/data/splitData.ts b/src/pages/api/data/splitData.ts index f25dd3244..16bded265 100644 --- a/src/pages/api/data/splitData.ts +++ b/src/pages/api/data/splitData.ts @@ -4,6 +4,7 @@ import { connectToDatabase, DataItem, Data } from '@/service/mongo'; import { authToken } from '@/service/utils/tools'; import { generateQA } from '@/service/events/generateQA'; import { generateAbstract } from '@/service/events/generateAbstract'; +import { encode } from 'gpt-token-utils'; /* 拆分数据成QA */ export default async function handler(req: NextApiRequest, res: NextApiResponse) { @@ -33,7 +34,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) chunks.forEach((chunk) => { splitText += chunk; - if (splitText.length >= 980) { + const tokens = encode(splitText).length; + if (tokens >= 980) { dataItems.push({ userId, dataId, diff --git a/src/pages/api/model/data/delMoedlDataById.ts b/src/pages/api/model/data/delMoedlDataById.ts new file mode 100644 index 000000000..13c8ea07a --- /dev/null +++ b/src/pages/api/model/data/delMoedlDataById.ts @@ -0,0 +1,38 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { jsonRes } from '@/service/response'; +import { connectToDatabase, ModelData } from '@/service/mongo'; +import { authToken } from '@/service/utils/tools'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + let { modelId } = req.query as { + modelId: string; + }; + const { authorization } = req.headers; + + if (!authorization) { + throw new Error('无权操作'); + } + + if (!modelId) { + throw new Error('缺少参数'); + } + + // 凭证校验 + const userId = await authToken(authorization); + + await connectToDatabase(); + + await ModelData.deleteOne({ + modelId, + userId + }); + + jsonRes(res); + } catch (err) { + jsonRes(res, { + code: 500, + error: err + }); + } +} diff --git a/src/pages/api/model/data/getModelData.ts b/src/pages/api/model/data/getModelData.ts new file mode 100644 index 000000000..3c41ec458 --- /dev/null +++ b/src/pages/api/model/data/getModelData.ts @@ -0,0 +1,52 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { jsonRes } from '@/service/response'; +import { connectToDatabase, ModelData } from '@/service/mongo'; +import { authToken } from '@/service/utils/tools'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + let { + modelId, + pageNum = 1, + pageSize = 10 + } = req.query as { + modelId: string; + pageNum: string; + pageSize: string; + }; + const { authorization } = req.headers; + + pageNum = +pageNum; + pageSize = +pageSize; + + if (!authorization) { + throw new Error('无权操作'); + } + + if (!modelId) { + throw new Error('缺少参数'); + } + + // 凭证校验 + const userId = await authToken(authorization); + + await connectToDatabase(); + + const data = await ModelData.find({ + modelId, + userId + }) + .sort({ _id: -1 }) // 按照创建时间倒序排列 + .skip((pageNum - 1) * pageSize) + .limit(pageSize); + + jsonRes(res, { + data + }); + } catch (err) { + jsonRes(res, { + code: 500, + error: err + }); + } +} diff --git a/src/pages/api/model/data/pushModelData.ts b/src/pages/api/model/data/pushModelData.ts new file mode 100644 index 000000000..426dfab69 --- /dev/null +++ b/src/pages/api/model/data/pushModelData.ts @@ -0,0 +1,55 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { jsonRes } from '@/service/response'; +import { connectToDatabase, ModelData, Model } from '@/service/mongo'; +import { authToken } from '@/service/utils/tools'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const { modelId, data } = req.body as { + modelId: string; + data: { q: string; a: string }[]; + }; + const { authorization } = req.headers; + + if (!authorization) { + throw new Error('无权操作'); + } + + if (!modelId || !Array.isArray(data)) { + throw new Error('缺少参数'); + } + + // 凭证校验 + const userId = await authToken(authorization); + + await connectToDatabase(); + + // 验证是否是该用户的 model + const model = await Model.findOne({ + _id: modelId, + userId + }); + + if (!model) { + throw new Error('无权操作该模型'); + } + + // push data + await ModelData.insertMany( + data.map((item) => ({ + ...item, + modelId, + userId + })) + ); + + jsonRes(res, { + data: model + }); + } catch (err) { + jsonRes(res, { + code: 500, + error: err + }); + } +} diff --git a/src/pages/api/model/data/putModelData.ts b/src/pages/api/model/data/putModelData.ts new file mode 100644 index 000000000..776865f40 --- /dev/null +++ b/src/pages/api/model/data/putModelData.ts @@ -0,0 +1,44 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { jsonRes } from '@/service/response'; +import { connectToDatabase, ModelData } from '@/service/mongo'; +import { authToken } from '@/service/utils/tools'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + let { modelId, answer } = req.body as { + modelId: string; + answer: string; + }; + const { authorization } = req.headers; + + if (!authorization) { + throw new Error('无权操作'); + } + + if (!modelId) { + throw new Error('缺少参数'); + } + + // 凭证校验 + const userId = await authToken(authorization); + + await connectToDatabase(); + + await ModelData.updateOne( + { + modelId, + userId + }, + { + a: answer + } + ); + + jsonRes(res); + } catch (err) { + jsonRes(res, { + code: 500, + error: err + }); + } +} diff --git a/src/pages/api/model/getTrainings.ts b/src/pages/api/model/train/getTrainings.ts similarity index 100% rename from src/pages/api/model/getTrainings.ts rename to src/pages/api/model/train/getTrainings.ts diff --git a/src/pages/api/model/putTrainStatus.ts b/src/pages/api/model/train/putTrainStatus.ts similarity index 100% rename from src/pages/api/model/putTrainStatus.ts rename to src/pages/api/model/train/putTrainStatus.ts diff --git a/src/pages/api/model/train.ts b/src/pages/api/model/train/train.ts similarity index 100% rename from src/pages/api/model/train.ts rename to src/pages/api/model/train/train.ts diff --git a/src/service/models/modelData.ts b/src/service/models/modelData.ts new file mode 100644 index 000000000..06de10891 --- /dev/null +++ b/src/service/models/modelData.ts @@ -0,0 +1,36 @@ +/* 模型的知识库 */ +import { Schema, model, models, Model as MongoModel } from 'mongoose'; +import { ModelDataSchema as ModelDataType } from '@/types/mongoSchema'; + +const ModelDataSchema = new Schema({ + modelId: { + type: Schema.Types.ObjectId, + ref: 'model', + required: true + }, + userId: { + type: Schema.Types.ObjectId, + ref: 'user', + required: true + }, + q: { + type: String, + required: true + }, + a: { + type: String, + default: '' + }, + status: { + type: Number, + enum: [0, 1, 2], + default: 1 + }, + createTime: { + type: Date, + default: () => new Date() + } +}); + +export const ModelData: MongoModel = + models['modelData'] || model('modelData', ModelDataSchema); diff --git a/src/service/mongo.ts b/src/service/mongo.ts index 5eb6c7365..428effcdd 100644 --- a/src/service/mongo.ts +++ b/src/service/mongo.ts @@ -33,6 +33,7 @@ export async function connectToDatabase(): Promise { export * from './models/authCode'; export * from './models/chat'; export * from './models/model'; +export * from './models/modelData'; export * from './models/user'; export * from './models/training'; export * from './models/bill'; diff --git a/src/service/utils/chat.ts b/src/service/utils/chat.ts index d78d62c17..0cdf62f4c 100644 --- a/src/service/utils/chat.ts +++ b/src/service/utils/chat.ts @@ -34,7 +34,10 @@ export const authChat = async (chatId: string, authorization?: string) => { // 凭证校验 if (!chat.isShare) { - await authToken(authorization); + const userId = await authToken(authorization); + if (userId !== String(chat.userId._id)) { + return Promise.reject('无权使用该对话'); + } } else if (chat.loadAmount === 0 || chat.expiredTime <= Date.now()) { return Promise.reject('聊天框已过期'); } diff --git a/src/types/mongoSchema.d.ts b/src/types/mongoSchema.d.ts index 03af5b15c..020a06233 100644 --- a/src/types/mongoSchema.d.ts +++ b/src/types/mongoSchema.d.ts @@ -51,6 +51,14 @@ export interface ModelPopulate extends ModelSchema { userId: UserModelSchema; } +export interface ModelDataSchema { + _id: string; + q: string; + a: string; + status: 0 | 1 | 2; + createTime: Date; +} + export interface TrainingSchema { _id: string; serviceName: ServiceName;