feat: 拆分测试环境

This commit is contained in:
archer
2023-04-02 23:38:28 +08:00
parent ae4243b522
commit 05b2e9e99c
9 changed files with 177 additions and 12 deletions

View File

@@ -7,8 +7,6 @@ import { ChatModelNameEnum } from '@/constants/model';
import { pushSplitDataBill } from '@/service/events/pushBill';
export async function generateAbstract(next = false): Promise<any> {
if (process.env.NODE_ENV === 'development') return;
if (global.generatingAbstract && !next) return;
global.generatingAbstract = true;

View File

@@ -119,3 +119,60 @@ export const pushSplitDataBill = async ({
console.log(error);
}
};
export const pushGenerateVectorBill = async ({
isPay,
userId,
text,
type
}: {
isPay: boolean;
userId: string;
text: string;
type: DataType;
}) => {
await connectToDatabase();
let billId;
try {
// 计算 token 数量
const tokens = encode(text);
console.log('text len: ', text.length);
console.log('token len:', tokens.length);
if (isPay) {
try {
// 获取模型单价格, 都是用 gpt35 拆分
const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35);
const unitPrice = modelItem?.price || 5;
// 计算价格
const price = unitPrice * tokens.length;
console.log(`splitData bill, price: ${formatPrice(price)}`);
// 插入 Bill 记录
const res = await Bill.create({
userId,
type,
modelName: ChatModelNameEnum.GPT35,
textLen: text.length,
tokenLen: tokens.length,
price
});
billId = res._id;
// 账号扣费
await User.findByIdAndUpdate(userId, {
$inc: { balance: -price }
});
} catch (error) {
console.log('创建账单失败:', error);
billId && Bill.findByIdAndDelete(billId);
}
}
} catch (error) {
console.log(error);
}
};

View File

@@ -17,7 +17,7 @@ export async function connectToDatabase(): Promise<void> {
mongoose.set('strictQuery', true);
global.mongodb = await mongoose.connect(process.env.MONGODB_URI as string, {
bufferCommands: true,
dbName: 'doc_gpt',
dbName: process.env.NODE_ENV === 'development' ? 'doc_gpt_test' : 'doc_gpt',
maxPoolSize: 5,
minPoolSize: 1,
maxConnecting: 5

View File

@@ -30,7 +30,7 @@ export const connectRedis = async () => {
await global.redisClient.connect();
// 1 - 测试库0 - 正式
await global.redisClient.select(process.env.NODE_ENV === 'development' ? 0 : 0);
await global.redisClient.SELECT(0);
return global.redisClient;
} catch (error) {