feat: auth openapi key

This commit is contained in:
archer
2023-04-07 23:33:59 +08:00
parent f6c4b4c96d
commit ea1681e1eb
6 changed files with 37 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { connectToDatabase, Model } from '@/service/mongo';
import { getOpenAIApi } from '@/service/utils/chat';
import { authToken } from '@/service/utils/tools';
import { authOpenApiKey } from '@/service/utils/tools';
import { httpsAgent, openaiChatFilter, systemPromptFilter } from '@/service/utils/tools';
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
import { ChatItemType } from '@/types/chat';
@@ -36,7 +36,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
modelId: string;
};
const { authorization } = req.headers;
if (!prompt) {
throw new Error('缺少参数');
}
@@ -46,7 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
let startTime = Date.now();
/* 凭证校验 */
const userId = await authToken(authorization);
const userId = await authOpenApiKey(req);
const { userApiKey, systemKey } = await getOpenApiKey(userId);
/* 查找数据库里的模型信息 */