perf: completion chatId

This commit is contained in:
archer
2023-07-23 20:07:35 +08:00
parent b7d18e38d1
commit 67e10d6f2c
35 changed files with 447 additions and 385 deletions

View File

@@ -0,0 +1,61 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authUser } from '@/service/utils/auth';
import { connectToDatabase, TrainingData, User, promotionRecord, Chat } from '@/service/mongo';
import { PRICE_SCALE } from '@/constants/common';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
await connectToDatabase();
const { limit = 1000 } = req.body as { limit: number };
let skip = 0;
const total = await Chat.countDocuments({
chatId: { $exists: false }
});
let promise = Promise.resolve();
console.log(total);
for (let i = 0; i < total; i += limit) {
const skipVal = skip;
skip += limit;
promise = promise
.then(() => init(limit, skipVal))
.then(() => {
console.log(skipVal);
});
}
await promise;
jsonRes(res, {});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}
async function init(limit: number, skip: number) {
// 遍历 app
const chats = await Chat.find(
{
chatId: { $exists: false }
},
'_id'
)
.limit(limit)
.skip(skip);
await Promise.all(
chats.map((chat) =>
Chat.findByIdAndUpdate(chat._id, {
chatId: String(chat._id),
source: 'online'
})
)
);
}

View File

@@ -1,47 +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 { authUser } from '@/service/utils/auth';
import { connectToDatabase, TrainingData, User, promotionRecord } from '@/service/mongo';
import { PRICE_SCALE } from '@/constants/common';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
await connectToDatabase();
// 计算剩余金额
const countResidue: { userId: string; totalAmount: number }[] = await promotionRecord.aggregate(
[
{
$group: {
_id: '$userId', // Group by userId
totalAmount: { $sum: '$amount' } // Calculate the sum of amount field
}
},
{
$project: {
_id: false, // Exclude _id field
userId: '$_id', // Include userId field
totalAmount: true // Include totalAmount field
}
}
]
);
await Promise.all(
countResidue.map((item) =>
User.findByIdAndUpdate(item.userId, {
$inc: { balance: item.totalAmount * PRICE_SCALE }
})
)
);
jsonRes(res, { data: countResidue });
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}

View File

@@ -16,7 +16,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 凭证校验
const { userId } = await authUser({ req, authToken: true });
const chatRecord = await Chat.findById(chatId);
const chatRecord = await Chat.findOne({ chatId });
if (!chatRecord) {
throw new Error('找不到对话');

View File

@@ -17,14 +17,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId,
...(appId && { appId })
},
'_id title top customTitle appId updateTime'
'chatId title top customTitle appId updateTime'
)
.sort({ top: -1, updateTime: -1 })
.limit(20);
jsonRes<ChatHistoryItemType[]>(res, {
data: data.map((item) => ({
_id: item._id,
chatId: item.chatId,
updateTime: item.updateTime,
appId: item.appId,
customTitle: item.customTitle,

View File

@@ -3,7 +3,6 @@ import { jsonRes } from '@/service/response';
import { connectToDatabase, Chat } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { Types } from 'mongoose';
import { rawSearchKey } from '@/constants/chat';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
@@ -36,13 +35,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
{
$project: {
[rawSearchKey]: `$content.${rawSearchKey}`
// [rawSearchKey]: `$content.${rawSearchKey}`
}
}
]);
jsonRes(res, {
data: history[0]?.[rawSearchKey] || []
// data: history[0]?.[rawSearchKey] || []
});
} catch (err) {
jsonRes(res, {

View File

@@ -20,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await Chat.findOneAndUpdate(
{
_id: chatId,
chatId,
userId
},
{

View File

@@ -27,7 +27,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await Chat.updateOne(
{
_id: new Types.ObjectId(chatId),
chatId,
userId: new Types.ObjectId(userId),
'content._id': new Types.ObjectId(contentId)
},

View File

@@ -6,8 +6,7 @@ import { authUser } from '@/service/utils/auth';
import { ChatItemType } from '@/types/chat';
import { authApp } from '@/service/utils/auth';
import mongoose from 'mongoose';
import type { AppSchema, ChatSchema } from '@/types/mongoSchema';
import { quoteLenKey, rawSearchKey } from '@/constants/chat';
import type { ChatSchema } from '@/types/mongoSchema';
import { getSpecialModule } from '@/components/ChatBox';
/* 初始化我的聊天框,需要身份验证 */
@@ -20,72 +19,62 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
chatId: '' | string;
};
if (!appId) {
return jsonRes(res, {
code: 501,
message: "You don't have an app yet"
});
}
await connectToDatabase();
// 没有 appId 时直接获取用户的第一个id
const app = await (async () => {
if (!appId) {
const myModel = await App.findOne({ userId });
if (!myModel) {
const { _id } = await App.create({
name: '应用1',
userId
});
return (await App.findById(_id)) as AppSchema;
} else {
return myModel;
}
} else {
// 校验使用权限
const authRes = await authApp({
appId,
userId,
authUser: false,
authOwner: false
});
return authRes.app;
}
})();
appId = appId || app._id;
// 校验使用权限
const app = (
await authApp({
appId,
userId,
authUser: false,
authOwner: false
})
).app;
// 历史记录
const { chat, history = [] }: { chat?: ChatSchema; history?: ChatItemType[] } =
await (async () => {
if (chatId) {
// auth chatId
const chat = await Chat.findOne({
_id: chatId,
userId
});
const [chat, history] = await Promise.all([
Chat.findOne({
chatId,
userId
}),
Chat.aggregate([
{
$match: {
chatId,
userId: new mongoose.Types.ObjectId(userId)
}
},
{
$project: {
content: {
$slice: ['$content', -50] // 返回 content 数组的最后50个元素
}
}
},
{ $unwind: '$content' },
{
$project: {
_id: '$content._id',
obj: '$content.obj',
value: '$content.value'
}
}
])
]);
if (!chat) {
throw new Error('聊天框不存在');
}
// 获取 chat.content 数据
const history = await Chat.aggregate([
{
$match: {
_id: new mongoose.Types.ObjectId(chatId),
userId: new mongoose.Types.ObjectId(userId)
}
},
{
$project: {
content: {
$slice: ['$content', -50] // 返回 content 数组的最后50个元素
}
}
},
{ $unwind: '$content' },
{
$project: {
_id: '$content._id',
obj: '$content.obj',
value: '$content.value',
[quoteLenKey]: { $size: { $ifNull: [`$content.${rawSearchKey}`, []] } }
}
}
]);
return { history, chat };
}
return {};

View File

@@ -6,13 +6,13 @@ import { authUser } from '@/service/utils/auth';
/* 获取历史记录 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { id } = req.query;
const { chatId } = req.query;
const { userId } = await authUser({ req, authToken: true });
await connectToDatabase();
await Chat.findOneAndRemove({
_id: id,
chatId,
userId
});

View File

@@ -1,104 +0,0 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { ChatItemType } from '@/types/chat';
import { connectToDatabase, Chat, App } from '@/service/mongo';
import { authApp } from '@/service/utils/auth';
import { authUser } from '@/service/utils/auth';
import { Types } from 'mongoose';
type Props = {
chatId?: string;
appId: string;
variables?: Record<string, any>;
prompts: [ChatItemType, ChatItemType];
};
/* 聊天内容存存储 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { chatId, appId, prompts } = req.body as Props;
if (!prompts) {
throw new Error('缺少参数');
}
const { userId } = await authUser({ req, authToken: true });
const response = await saveChat({
chatId,
appId,
prompts,
userId
});
jsonRes(res, {
data: response
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}
export async function saveChat({
newChatId,
chatId,
appId,
prompts,
variables,
userId
}: Props & { newChatId?: Types.ObjectId; userId: string }): Promise<{ newChatId: string }> {
await connectToDatabase();
const { app } = await authApp({ appId, userId, authOwner: false });
if (String(app.userId) === userId) {
await App.findByIdAndUpdate(appId, {
updateTime: new Date()
});
}
const [response] = await Promise.all([
...(chatId
? [
Chat.findByIdAndUpdate(chatId, {
$push: {
content: {
$each: prompts
}
},
variables,
title: prompts[0].value.slice(0, 20),
updateTime: new Date()
}).then(() => ({
newChatId: ''
}))
]
: [
Chat.create({
_id: newChatId,
userId,
appId,
variables,
content: prompts,
title: prompts[0].value.slice(0, 20)
}).then((res) => ({
newChatId: String(res._id)
}))
]),
// update app
...(String(app.userId) === userId
? [
App.findByIdAndUpdate(appId, {
updateTime: new Date()
})
]
: [])
]);
return {
// @ts-ignore
newChatId: response?.newChatId || ''
};
}

View File

@@ -9,7 +9,7 @@ const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 24);
/* create a shareChat */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { appId, name, maxContext } = req.body as ShareChatEditType & {
const { appId, name } = req.body as ShareChatEditType & {
appId: string;
};
@@ -27,8 +27,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
shareId,
userId,
appId,
name,
maxContext
name
});
jsonRes(res, {

View File

@@ -3,10 +3,7 @@ import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat, User } from '@/service/mongo';
import type { InitShareChatResponse } from '@/api/response/chat';
import { authApp } from '@/service/utils/auth';
import { hashPassword } from '@/service/utils/tools';
import { HUMAN_ICON } from '@/constants/chat';
import { FlowModuleTypeEnum } from '@/constants/flow';
import { SystemInputEnum } from '@/constants/app';
import { getSpecialModule } from '@/components/ChatBox';
/* 初始化我的聊天框,需要身份验证 */
@@ -33,21 +30,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
// 校验使用权限
const { app } = await authApp({
appId: shareChat.appId,
userId: String(shareChat.userId),
authOwner: false
});
const user = await User.findById(shareChat.userId, 'avatar');
const [{ app }, user] = await Promise.all([
authApp({
appId: shareChat.appId,
userId: String(shareChat.userId),
authOwner: false
}),
User.findById(shareChat.userId, 'avatar')
]);
jsonRes<InitShareChatResponse>(res, {
data: {
userAvatar: user?.avatar || HUMAN_ICON,
maxContext:
app.modules
?.find((item) => item.flowType === FlowModuleTypeEnum.historyNode)
?.inputs?.find((item) => item.key === 'maxContext')?.value || 0,
app: {
...getSpecialModule(app.modules),
name: app.name,

View File

@@ -28,7 +28,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
shareId: item.shareId,
name: item.name,
total: item.total,
maxContext: item.maxContext,
lastTime: item.lastTime
}))
});

View File

@@ -3,7 +3,7 @@ import { connectToDatabase } from '@/service/mongo';
import { authUser, authApp, authShareChat } from '@/service/utils/auth';
import { sseErrRes, jsonRes } from '@/service/response';
import { withNextCors } from '@/service/utils/tools';
import { ChatRoleEnum, sseResponseEventEnum } from '@/constants/chat';
import { ChatRoleEnum, ChatSourceEnum, sseResponseEventEnum } from '@/constants/chat';
import {
dispatchHistory,
dispatchChatInput,
@@ -15,12 +15,11 @@ import {
import type { CreateChatCompletionRequest } from 'openai';
import { gptMessage2ChatType } from '@/utils/adapt';
import { getChatHistory } from './getHistory';
import { saveChat } from '@/pages/api/chat/saveChat';
import { saveChat } from '@/service/utils/chat/saveChat';
import { sseResponse } from '@/service/utils/tools';
import { type ChatCompletionRequestMessage } from 'openai';
import { TaskResponseKeyEnum } from '@/constants/chat';
import { FlowModuleTypeEnum, initModuleType } from '@/constants/flow';
import { Types } from 'mongoose';
import { AppModuleItemType, RunningModuleItemType } from '@/types/app';
import { pushTaskBill } from '@/service/events/pushBill';
import { BillSourceEnum } from '@/constants/user';
@@ -101,16 +100,10 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}
// user question
const prompt = prompts.pop();
if (!prompt) {
throw new Error('Question is empty');
}
const newChatId = chatId === '' ? new Types.ObjectId() : undefined;
if (stream && newChatId) {
res.setHeader('newChatId', String(newChatId));
}
/* start process */
const { responseData, answerText } = await dispatchModules({
res,
@@ -122,29 +115,39 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
},
stream
});
console.log(responseData, '===', answerText);
// console.log(responseData, '===', answerText);
if (!answerText) {
throw new Error('回复内容为空,可能模块编排出现问题');
}
// if (!answerText) {
// throw new Error('回复内容为空,可能模块编排出现问题');
// }
// save chat
if (typeof chatId === 'string') {
await saveChat({
chatId,
newChatId,
appId,
userId,
variables,
prompts: [
isOwner,
shareId,
source: (() => {
if (shareId) {
return ChatSourceEnum.share;
}
if (authType === 'apikey') {
return ChatSourceEnum.api;
}
return ChatSourceEnum.online;
})(),
content: [
prompt,
{
_id: messages[messages.length - 1]._id,
obj: ChatRoleEnum.AI,
value: answerText,
...responseData
responseData
}
],
userId
]
});
}

View File

@@ -43,7 +43,7 @@ export async function getChatHistory({
}
const history = await Chat.aggregate([
{ $match: { _id: new Types.ObjectId(chatId), userId: new Types.ObjectId(userId) } },
{ $match: { chatId, userId: new Types.ObjectId(userId) } },
{
$project: {
content: {
@@ -54,10 +54,8 @@ export async function getChatHistory({
{ $unwind: '$content' },
{
$project: {
_id: '$content._id',
obj: '$content.obj',
value: '$content.value',
quote: '$content.quote'
value: '$content.value'
}
}
]);

View File

@@ -42,6 +42,66 @@ export async function getInitConfig() {
global.qaModels = res.QAModels;
global.vectorModels = res.VectorModels;
} catch (error) {
setDefaultData();
return Promise.reject('get init config error');
}
}
export function setDefaultData() {
global.systemEnv = {
vectorMaxProcess: 15,
qaMaxProcess: 15,
pgIvfflatProbe: 20,
sensitiveCheck: false
};
global.feConfigs = {
show_emptyChat: true,
show_register: true,
show_appStore: true,
show_userDetail: true,
show_git: true,
systemTitle: 'FastAI',
authorText: 'Made by FastAI Team.'
};
global.chatModels = [
{
model: 'gpt-3.5-turbo',
name: 'FastAI-4k',
contextMaxToken: 4000,
systemMaxToken: 2400,
maxTemperature: 1.2,
price: 1.5
},
{
model: 'gpt-3.5-turbo-16k',
name: 'FastAI-16k',
contextMaxToken: 16000,
systemMaxToken: 8000,
maxTemperature: 1.2,
price: 3
},
{
model: 'gpt-4',
name: 'FastAI-Plus',
contextMaxToken: 8000,
systemMaxToken: 4000,
maxTemperature: 1.2,
price: 45
}
];
global.qaModels = [
{
model: 'gpt-3.5-turbo-16k',
name: 'FastAI-16k',
maxToken: 16000,
price: 3
}
];
global.vectorModels = [
{
model: 'text-embedding-ada-002',
name: 'Embedding-2',
price: 0.2
}
];
}