perf: bill logs

This commit is contained in:
archer
2023-03-27 13:58:57 +08:00
parent 9280a21d12
commit 5249297cb1
6 changed files with 160 additions and 152 deletions

View File

@@ -5,55 +5,58 @@ import { formatPrice } from '@/utils/user';
import type { DataType } from '@/types/data';
export const pushChatBill = async ({
isPay,
modelName,
userId,
chatId,
text
}: {
isPay: boolean;
modelName: string;
userId: string;
chatId: string;
text: string;
}) => {
await connectToDatabase();
let billId;
try {
// 获取模型单价格
const modelItem = modelList.find((item) => item.model === modelName);
const unitPrice = modelItem?.price || 5;
// 计算 token 数量
const tokens = encode(text);
// 计算价格
const price = unitPrice * tokens.length;
console.log('chat bill');
console.log('token len:', tokens.length);
console.log('text len: ', text.length);
console.log('price: ', `${formatPrice(price)}`);
console.log('token len:', tokens.length);
try {
// 插入 Bill 记录
const res = await Bill.create({
userId,
type: 'chat',
modelName,
chatId,
textLen: text.length,
tokenLen: tokens.length,
price
});
billId = res._id;
if (isPay) {
await connectToDatabase();
// 账号扣费
await User.findByIdAndUpdate(userId, {
$inc: { balance: -price }
});
} catch (error) {
console.log('创建账单失败:', error);
billId && Bill.findByIdAndDelete(billId);
// 获取模型单价格
const modelItem = modelList.find((item) => item.model === modelName);
// 计算价格
const unitPrice = modelItem?.price || 5;
const price = unitPrice * tokens.length;
console.log(`chat bill, price: ${formatPrice(price)}`);
try {
// 插入 Bill 记录
const res = await Bill.create({
userId,
type: 'chat',
modelName,
chatId,
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);
@@ -61,10 +64,12 @@ export const pushChatBill = async ({
};
export const pushSplitDataBill = async ({
isPay,
userId,
text,
type
}: {
isPay: boolean;
userId: string;
text: string;
type: DataType;
@@ -74,39 +79,41 @@ export const pushSplitDataBill = async ({
let billId;
try {
// 获取模型单价格, 都是用 gpt35 拆分
const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35);
const unitPrice = modelItem?.price || 5;
// 计算 token 数量
const tokens = encode(text);
// 计算价格
const price = unitPrice * tokens.length;
console.log('splitData bill');
console.log('token len:', tokens.length);
console.log('text len: ', text.length);
console.log('price: ', `${formatPrice(price)}`);
console.log('token len:', tokens.length);
try {
// 插入 Bill 记录
const res = await Bill.create({
userId,
type,
modelName: ChatModelNameEnum.GPT35,
textLen: text.length,
tokenLen: tokens.length,
price
});
billId = res._id;
if (isPay) {
try {
// 获取模型单价格, 都是用 gpt35 拆分
const modelItem = modelList.find((item) => item.model === ChatModelNameEnum.GPT35);
const unitPrice = modelItem?.price || 5;
// 计算价格
const price = unitPrice * tokens.length;
// 账号扣费
await User.findByIdAndUpdate(userId, {
$inc: { balance: -price }
});
} catch (error) {
console.log('创建账单失败:', error);
billId && Bill.findByIdAndDelete(billId);
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);