feat: 增加账单

This commit is contained in:
archer
2023-03-21 14:01:35 +08:00
parent dc467c26b5
commit 42c26bd155
15 changed files with 211 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
import { Configuration, OpenAIApi } from 'openai';
import { Chat } from '../mongo';
import type { ChatPopulate } from '@/types/mongoSchema';
import { formatPrice } from '@/utils/user';
export const getOpenAIApi = (apiKey: string) => {
const configuration = new Configuration({
@@ -40,12 +41,14 @@ export const authChat = async (chatId: string) => {
const userApiKey = user.accounts?.find((item: any) => item.type === 'openai')?.value;
if (!userApiKey) {
return Promise.reject('缺少ApiKey, 无法请求');
if (!userApiKey && formatPrice(user.balance) <= -1) {
return Promise.reject('该账号余额不足');
}
return {
userApiKey,
chat
systemKey: process.env.OPENAIKEY as string,
chat,
userId: user._id
};
};