mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
Merge branch 'dev2.1' into dev2.2
This commit is contained in:
@@ -1,17 +1,25 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, Chat } from '@/service/mongo';
|
import { connectToDatabase, Chat } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
const { chatId, index } = req.query as { chatId: string; index: string };
|
const { chatId, index } = req.query as { chatId: string; index: string };
|
||||||
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
|
if (!authorization) {
|
||||||
|
throw new Error('无权操作');
|
||||||
|
}
|
||||||
if (!chatId || !index) {
|
if (!chatId || !index) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
}
|
}
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
|
// 凭证校验
|
||||||
|
const userId = await authToken(authorization);
|
||||||
|
|
||||||
const chatRecord = await Chat.findById(chatId);
|
const chatRecord = await Chat.findById(chatId);
|
||||||
|
|
||||||
if (!chatRecord) {
|
if (!chatRecord) {
|
||||||
@@ -31,12 +39,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 删除最一条数据库记录, 也就是预发送的那一条
|
// 删除最一条数据库记录, 也就是预发送的那一条
|
||||||
await Chat.findByIdAndUpdate(chatId, {
|
await Chat.updateOne(
|
||||||
$set: {
|
{
|
||||||
[`content.${deletedIndex}.deleted`]: true,
|
_id: chatId,
|
||||||
updateTime: Date.now()
|
userId
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
[`content.${deletedIndex}.deleted`]: true,
|
||||||
|
updateTime: Date.now()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
jsonRes(res);
|
jsonRes(res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@@ -4,6 +4,7 @@ import { connectToDatabase, DataItem, Data } from '@/service/mongo';
|
|||||||
import { authToken } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { generateQA } from '@/service/events/generateQA';
|
import { generateQA } from '@/service/events/generateQA';
|
||||||
import { generateAbstract } from '@/service/events/generateAbstract';
|
import { generateAbstract } from '@/service/events/generateAbstract';
|
||||||
|
import { encode } from 'gpt-token-utils';
|
||||||
|
|
||||||
/* 拆分数据成QA */
|
/* 拆分数据成QA */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
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) => {
|
chunks.forEach((chunk) => {
|
||||||
splitText += chunk;
|
splitText += chunk;
|
||||||
if (splitText.length >= 980) {
|
const tokens = encode(splitText).length;
|
||||||
|
if (tokens >= 980) {
|
||||||
dataItems.push({
|
dataItems.push({
|
||||||
userId,
|
userId,
|
||||||
dataId,
|
dataId,
|
||||||
|
38
src/pages/api/model/data/delMoedlDataById.ts
Normal file
38
src/pages/api/model/data/delMoedlDataById.ts
Normal file
@@ -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<any>) {
|
||||||
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
52
src/pages/api/model/data/getModelData.ts
Normal file
52
src/pages/api/model/data/getModelData.ts
Normal file
@@ -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<any>) {
|
||||||
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
55
src/pages/api/model/data/pushModelData.ts
Normal file
55
src/pages/api/model/data/pushModelData.ts
Normal file
@@ -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<any>) {
|
||||||
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
44
src/pages/api/model/data/putModelData.ts
Normal file
44
src/pages/api/model/data/putModelData.ts
Normal file
@@ -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<any>) {
|
||||||
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
36
src/service/models/modelData.ts
Normal file
36
src/service/models/modelData.ts
Normal file
@@ -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<ModelDataType> =
|
||||||
|
models['modelData'] || model('modelData', ModelDataSchema);
|
@@ -33,6 +33,7 @@ export async function connectToDatabase(): Promise<void> {
|
|||||||
export * from './models/authCode';
|
export * from './models/authCode';
|
||||||
export * from './models/chat';
|
export * from './models/chat';
|
||||||
export * from './models/model';
|
export * from './models/model';
|
||||||
|
export * from './models/modelData';
|
||||||
export * from './models/user';
|
export * from './models/user';
|
||||||
export * from './models/training';
|
export * from './models/training';
|
||||||
export * from './models/bill';
|
export * from './models/bill';
|
||||||
|
@@ -34,7 +34,10 @@ export const authChat = async (chatId: string, authorization?: string) => {
|
|||||||
|
|
||||||
// 凭证校验
|
// 凭证校验
|
||||||
if (!chat.isShare) {
|
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()) {
|
} else if (chat.loadAmount === 0 || chat.expiredTime <= Date.now()) {
|
||||||
return Promise.reject('聊天框已过期');
|
return Promise.reject('聊天框已过期');
|
||||||
}
|
}
|
||||||
|
8
src/types/mongoSchema.d.ts
vendored
8
src/types/mongoSchema.d.ts
vendored
@@ -51,6 +51,14 @@ export interface ModelPopulate extends ModelSchema {
|
|||||||
userId: UserModelSchema;
|
userId: UserModelSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ModelDataSchema {
|
||||||
|
_id: string;
|
||||||
|
q: string;
|
||||||
|
a: string;
|
||||||
|
status: 0 | 1 | 2;
|
||||||
|
createTime: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export interface TrainingSchema {
|
export interface TrainingSchema {
|
||||||
_id: string;
|
_id: string;
|
||||||
serviceName: ServiceName;
|
serviceName: ServiceName;
|
||||||
|
Reference in New Issue
Block a user