v4.6 -1 (#459)
This commit is contained in:
@@ -1,201 +0,0 @@
|
||||
import { Bill } from '@/service/mongo';
|
||||
import { MongoUser } from '@fastgpt/service/support/user/schema';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
import { getModelMap, ModelTypeEnum } from '@/service/core/ai/model';
|
||||
import { ChatHistoryItemResType } from '@/types/chat';
|
||||
import { formatPrice } from '@fastgpt/global/common/bill/tools';
|
||||
import { addLog } from '@/service/utils/tools';
|
||||
import type { CreateBillType } from '@/types/common/bill';
|
||||
import { defaultQGModels } from '@/constants/model';
|
||||
|
||||
async function createBill(data: CreateBillType) {
|
||||
try {
|
||||
await Promise.all([
|
||||
MongoUser.findByIdAndUpdate(data.userId, {
|
||||
$inc: { balance: -data.total }
|
||||
}),
|
||||
Bill.create(data)
|
||||
]);
|
||||
} catch (error) {
|
||||
addLog.error(`createBill error`, error);
|
||||
}
|
||||
}
|
||||
async function concatBill({
|
||||
billId,
|
||||
total,
|
||||
listIndex,
|
||||
tokens = 0,
|
||||
userId
|
||||
}: {
|
||||
billId?: string;
|
||||
total: number;
|
||||
listIndex?: number;
|
||||
tokens?: number;
|
||||
userId: string;
|
||||
}) {
|
||||
if (!billId) return;
|
||||
try {
|
||||
await Promise.all([
|
||||
Bill.findOneAndUpdate(
|
||||
{
|
||||
_id: billId,
|
||||
userId
|
||||
},
|
||||
{
|
||||
$inc: {
|
||||
total,
|
||||
...(listIndex !== undefined && {
|
||||
[`list.${listIndex}.amount`]: total,
|
||||
[`list.${listIndex}.tokenLen`]: tokens
|
||||
})
|
||||
}
|
||||
}
|
||||
),
|
||||
MongoUser.findByIdAndUpdate(userId, {
|
||||
$inc: { balance: -total }
|
||||
})
|
||||
]);
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
export const pushChatBill = ({
|
||||
appName,
|
||||
appId,
|
||||
userId,
|
||||
source,
|
||||
response
|
||||
}: {
|
||||
appName: string;
|
||||
appId: string;
|
||||
userId: string;
|
||||
source: `${BillSourceEnum}`;
|
||||
response: ChatHistoryItemResType[];
|
||||
}) => {
|
||||
const total = response.reduce((sum, item) => sum + item.price, 0);
|
||||
|
||||
createBill({
|
||||
userId,
|
||||
appName,
|
||||
appId,
|
||||
total,
|
||||
source,
|
||||
list: response.map((item) => ({
|
||||
moduleName: item.moduleName,
|
||||
amount: item.price || 0,
|
||||
model: item.model,
|
||||
tokenLen: item.tokens
|
||||
}))
|
||||
});
|
||||
addLog.info(`finish completions`, {
|
||||
source,
|
||||
userId,
|
||||
price: formatPrice(total)
|
||||
});
|
||||
return { total };
|
||||
};
|
||||
|
||||
export const pushQABill = async ({
|
||||
userId,
|
||||
totalTokens,
|
||||
billId
|
||||
}: {
|
||||
userId: string;
|
||||
totalTokens: number;
|
||||
billId: string;
|
||||
}) => {
|
||||
addLog.info('splitData generate success', { totalTokens });
|
||||
|
||||
// 获取模型单价格, 都是用 gpt35 拆分
|
||||
const unitPrice = global.qaModels?.[0]?.price || 3;
|
||||
// 计算价格
|
||||
const total = unitPrice * totalTokens;
|
||||
|
||||
concatBill({
|
||||
billId,
|
||||
userId,
|
||||
total,
|
||||
tokens: totalTokens,
|
||||
listIndex: 1
|
||||
});
|
||||
|
||||
return { total };
|
||||
};
|
||||
|
||||
export const pushGenerateVectorBill = async ({
|
||||
billId,
|
||||
userId,
|
||||
tokenLen,
|
||||
model
|
||||
}: {
|
||||
billId?: string;
|
||||
userId: string;
|
||||
tokenLen: number;
|
||||
model: string;
|
||||
}) => {
|
||||
// 计算价格. 至少为1
|
||||
const vectorModel =
|
||||
global.vectorModels.find((item) => item.model === model) || global.vectorModels[0];
|
||||
const unitPrice = vectorModel.price || 0.2;
|
||||
let total = unitPrice * tokenLen;
|
||||
total = total > 1 ? total : 1;
|
||||
|
||||
// 插入 Bill 记录
|
||||
if (billId) {
|
||||
concatBill({
|
||||
userId,
|
||||
total,
|
||||
billId,
|
||||
tokens: tokenLen,
|
||||
listIndex: 0
|
||||
});
|
||||
} else {
|
||||
createBill({
|
||||
userId,
|
||||
appName: '索引生成',
|
||||
total,
|
||||
source: BillSourceEnum.fastgpt,
|
||||
list: [
|
||||
{
|
||||
moduleName: '索引生成',
|
||||
amount: total,
|
||||
model: vectorModel.name,
|
||||
tokenLen
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
return { total };
|
||||
};
|
||||
|
||||
export const countModelPrice = ({
|
||||
model,
|
||||
tokens,
|
||||
type
|
||||
}: {
|
||||
model: string;
|
||||
tokens: number;
|
||||
type: `${ModelTypeEnum}`;
|
||||
}) => {
|
||||
const modelData = getModelMap?.[type]?.(model);
|
||||
if (!modelData) return 0;
|
||||
return modelData.price * tokens;
|
||||
};
|
||||
|
||||
export const pushQuestionGuideBill = ({ tokens, userId }: { tokens: number; userId: string }) => {
|
||||
const qgModel = global.qgModels?.[0] || defaultQGModels[0];
|
||||
const total = qgModel.price * tokens;
|
||||
createBill({
|
||||
userId,
|
||||
appName: '下一步指引',
|
||||
total,
|
||||
source: BillSourceEnum.fastgpt,
|
||||
list: [
|
||||
{
|
||||
moduleName: '下一步指引',
|
||||
amount: total,
|
||||
model: qgModel.name,
|
||||
tokenLen: tokens
|
||||
}
|
||||
]
|
||||
});
|
||||
};
|
||||
@@ -1,47 +0,0 @@
|
||||
import { connectionMongo, type Model } from '@fastgpt/service/common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { BillSchema as BillType } from '@/types/common/bill';
|
||||
import { BillSourceMap } from '@/constants/user';
|
||||
|
||||
const BillSchema = new Schema({
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
required: true
|
||||
},
|
||||
appName: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
appId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'model',
|
||||
required: false
|
||||
},
|
||||
time: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
total: {
|
||||
type: Number,
|
||||
required: true
|
||||
},
|
||||
source: {
|
||||
type: String,
|
||||
enum: Object.keys(BillSourceMap),
|
||||
required: true
|
||||
},
|
||||
list: {
|
||||
type: Array,
|
||||
default: []
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
BillSchema.index({ userId: 1 });
|
||||
BillSchema.index({ time: 1 }, { expireAfterSeconds: 90 * 24 * 60 * 60 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const Bill: Model<BillType> = models['bill'] || model('bill', BillSchema);
|
||||
15
projects/app/src/service/common/censor/index.ts
Normal file
15
projects/app/src/service/common/censor/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { POST } from '@fastgpt/service/common/api/plusRequest';
|
||||
|
||||
export const postTextCensor = (data: { text: string }) =>
|
||||
POST<{ code?: number; message: string }>('/common/censor/text_baidu', data)
|
||||
.then((res) => {
|
||||
if (res?.code === 5000) {
|
||||
return Promise.reject(res);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err?.code === 5000) {
|
||||
return Promise.reject(err.message);
|
||||
}
|
||||
return Promise.resolve('');
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChatItemType } from '@/types/chat';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import type { ChatItemType } from '@fastgpt/global/core/chat/type.d';
|
||||
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
|
||||
import type { NextApiResponse } from 'next';
|
||||
import { countMessagesTokens, countPromptTokens } from '@/global/common/tiktoken';
|
||||
import { adaptRole_Chat2Message } from '@/utils/common/adapt/message';
|
||||
|
||||
Reference in New Issue
Block a user