feat: set openai account

This commit is contained in:
archer
2023-07-28 12:02:23 +08:00
parent 7a56680935
commit dfda5285bd
21 changed files with 211 additions and 156 deletions

View File

@@ -1,62 +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 { TrainingModeEnum } from '@/constants/plugin';
import mongoose from 'mongoose';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
const { amount, userId, type } = req.body as {
amount: number;
userId: number;
type: 'withdraw';
};
await connectToDatabase();
if (!userId || !amount || type !== 'withdraw' || amount <= 0) {
throw new Error('params is error');
}
// check promotion balance
const countResidue: { totalAmount: number }[] = await promotionRecord.aggregate([
{ $match: { userId: new mongoose.Types.ObjectId(userId) } },
{
$group: {
_id: null, // 分组条件,这里使用 null 表示不分组
totalAmount: { $sum: '$amount' } // 计算 amount 字段的总和
}
},
{
$project: {
_id: false, // 排除 _id 字段
totalAmount: true // 只返回 totalAmount 字段
}
}
]);
const balance = countResidue[0].totalAmount;
if (balance < amount) {
throw new Error('可提现余额不足');
}
// add record
await promotionRecord.create({
userId,
type,
amount: -amount
});
jsonRes(res, {
data: balance
});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}

View File

@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authBalanceByUid, authUser } from '@/service/utils/auth';
import { withNextCors } from '@/service/utils/tools';
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
import { getAIChatApi, axiosConfig } from '@/service/ai/openai';
import { pushGenerateVectorBill } from '@/service/events/pushBill';
type Props = {
@@ -49,7 +49,7 @@ export async function getVector({
}
// 获取 chatAPI
const chatAPI = getOpenAIApi();
const chatAPI = getAIChatApi();
// 把输入的内容转成向量
const result = await chatAPI

View File

@@ -1,7 +1,7 @@
// 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, getSystemOpenAiKey } from '@/service/utils/auth';
import { authUser } from '@/service/utils/auth';
import axios from 'axios';
import { axiosConfig } from '@/service/ai/openai';

View File

@@ -5,22 +5,44 @@ import { User } from '@/service/models/user';
import { connectToDatabase } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { UserUpdateParams } from '@/types/user';
import { getAIChatApi, openaiBaseUrl } from '@/service/ai/openai';
/* 更新一些基本信息 */
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { avatar } = req.body as UserUpdateParams;
let { avatar, openaiAccount } = req.body as UserUpdateParams;
const { userId } = await authUser({ req, authToken: true });
await connectToDatabase();
// auth key
if (openaiAccount?.key) {
console.log('auth user openai key', openaiAccount?.key);
const chatAPI = getAIChatApi({
base: openaiAccount?.baseUrl || openaiBaseUrl,
apikey: openaiAccount?.key
});
const response = await chatAPI.createChatCompletion({
model: 'gpt-3.5-turbo',
max_tokens: 1,
messages: [{ role: 'user', content: 'hi' }]
});
if (!response?.data?.choices?.[0]?.message?.content) {
throw new Error(JSON.stringify(response?.data));
}
}
// 更新对应的记录
await User.updateOne(
{
_id: userId
},
{
...(avatar && { avatar })
...(avatar && { avatar }),
...(openaiAccount && { openaiAccount })
}
);