perf: init chat content.use mongo aggregate

This commit is contained in:
archer
2023-04-24 17:49:39 +08:00
parent 1f112f7715
commit e60c36b423
6 changed files with 49 additions and 57 deletions

View File

@@ -4,6 +4,7 @@ import type { ModelSchema } from '@/types/mongoSchema';
import { authToken } from './tools';
import { getOpenApiKey } from './openai';
import type { ChatItemType } from '@/types/chat';
import mongoose from 'mongoose';
export const getOpenAIApi = (apiKey: string) => {
const configuration = new Configuration({
@@ -47,14 +48,17 @@ export const authChat = async ({
if (chatId) {
// 获取 chat 数据
const chat = await Chat.findById(chatId);
if (!chat) {
return Promise.reject('对话不存在');
}
// filter 掉被 deleted 的内容
content = chat.content.filter((item) => item.deleted !== true);
content = await Chat.aggregate([
{ $match: { _id: new mongoose.Types.ObjectId(chatId) } },
{ $unwind: '$content' },
{ $match: { 'content.deleted': false } },
{
$project: {
obj: '$content.obj',
value: '$content.value'
}
}
]);
}
// 获取 user 的 apiKey