mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-01 20:27:45 +00:00

* perf: insert mongo dataset data session * perf: dataset data index * remove delay * rename bill schema * rename bill record * perf: bill table * perf: prompt * perf: sub plan * change the usage count * feat: usage bill * publish usages * doc * 新增团队聊天功能 (#20) * perf: doc * feat 添加标签部分 feat 信息团队标签配置 feat 新增团队同步管理 feat team分享页面 feat 完成team分享页面 feat 实现模糊搜索 style 格式化 fix 修复迷糊匹配 style 样式修改 fix 团队标签功能修复 * fix 修复鉴权功能 * merge 合并代码 * fix 修复引用错误 * fix 修复pr问题 * fix 修复ts格式问题 --------- Co-authored-by: archer <545436317@qq.com> Co-authored-by: liuxingwan <liuxingwan.lxw@alibaba-inc.com> * update extra plan * fix: ts * format * perf: bill field * feat: standard plan * fix: ts * feat 个人账号页面修改 (#22) * feat 添加标签部分 feat 信息团队标签配置 feat 新增团队同步管理 feat team分享页面 feat 完成team分享页面 feat 实现模糊搜索 style 格式化 fix 修复迷糊匹配 style 样式修改 fix 团队标签功能修复 * fix 修复鉴权功能 * merge 合并代码 * fix 修复引用错误 * fix 修复pr问题 * fix 修复ts格式问题 * feat 修改个人账号页 --------- Co-authored-by: liuxingwan <liuxingwan.lxw@alibaba-inc.com> * fix chunk index; error page text * feat: dataset process Integral prediction * feat: stand plan field * feat: sub plan limit * perf: index * query extension * perf: share link push app name * perf: plan point unit * perf: get sub plan * perf: account page --------- Co-authored-by: yst <77910600+yu-and-liu@users.noreply.github.com> Co-authored-by: liuxingwan <liuxingwan.lxw@alibaba-inc.com>
275 lines
5.5 KiB
TypeScript
275 lines
5.5 KiB
TypeScript
import { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
|
|
import { ModelTypeEnum } from '@/service/core/ai/model';
|
|
import type { ChatHistoryItemResType } from '@fastgpt/global/core/chat/type.d';
|
|
import { addLog } from '@fastgpt/service/common/system/log';
|
|
import { createUsage, concatUsage } from './controller';
|
|
import { formatModelChars2Points } from '@/service/support/wallet/usage/utils';
|
|
import { ChatModuleBillType } from '@fastgpt/global/support/wallet/bill/type';
|
|
|
|
export const pushChatUsage = ({
|
|
appName,
|
|
appId,
|
|
teamId,
|
|
tmbId,
|
|
source,
|
|
moduleDispatchBills
|
|
}: {
|
|
appName: string;
|
|
appId: string;
|
|
teamId: string;
|
|
tmbId: string;
|
|
source: `${UsageSourceEnum}`;
|
|
moduleDispatchBills: ChatModuleBillType[];
|
|
}) => {
|
|
const totalPoints = moduleDispatchBills.reduce((sum, item) => sum + (item.totalPoints || 0), 0);
|
|
|
|
createUsage({
|
|
teamId,
|
|
tmbId,
|
|
appName,
|
|
appId,
|
|
totalPoints,
|
|
source,
|
|
list: moduleDispatchBills.map((item) => ({
|
|
moduleName: item.moduleName,
|
|
amount: item.totalPoints || 0,
|
|
model: item.model,
|
|
charsLength: item.charsLength
|
|
}))
|
|
});
|
|
addLog.info(`finish completions`, {
|
|
source,
|
|
teamId,
|
|
tmbId,
|
|
totalPoints
|
|
});
|
|
return { totalPoints };
|
|
};
|
|
|
|
export const pushQAUsage = async ({
|
|
teamId,
|
|
tmbId,
|
|
model,
|
|
charsLength,
|
|
billId
|
|
}: {
|
|
teamId: string;
|
|
tmbId: string;
|
|
model: string;
|
|
charsLength: number;
|
|
billId: string;
|
|
}) => {
|
|
// 计算价格
|
|
const { totalPoints } = formatModelChars2Points({
|
|
model,
|
|
modelType: ModelTypeEnum.llm,
|
|
charsLength
|
|
});
|
|
|
|
concatUsage({
|
|
billId,
|
|
teamId,
|
|
tmbId,
|
|
totalPoints,
|
|
charsLength,
|
|
listIndex: 1
|
|
});
|
|
|
|
return { totalPoints };
|
|
};
|
|
|
|
export const pushGenerateVectorUsage = ({
|
|
billId,
|
|
teamId,
|
|
tmbId,
|
|
charsLength,
|
|
model,
|
|
source = UsageSourceEnum.fastgpt,
|
|
extensionModel,
|
|
extensionCharsLength
|
|
}: {
|
|
billId?: string;
|
|
teamId: string;
|
|
tmbId: string;
|
|
charsLength: number;
|
|
model: string;
|
|
source?: `${UsageSourceEnum}`;
|
|
|
|
extensionModel?: string;
|
|
extensionCharsLength?: number;
|
|
}) => {
|
|
const { totalPoints: totalVector, modelName: vectorModelName } = formatModelChars2Points({
|
|
modelType: ModelTypeEnum.vector,
|
|
model,
|
|
charsLength
|
|
});
|
|
|
|
const { extensionTotalPoints, extensionModelName } = (() => {
|
|
if (!extensionModel || !extensionCharsLength)
|
|
return {
|
|
extensionTotalPoints: 0,
|
|
extensionModelName: ''
|
|
};
|
|
const { totalPoints, modelName } = formatModelChars2Points({
|
|
modelType: ModelTypeEnum.llm,
|
|
model: extensionModel,
|
|
charsLength: extensionCharsLength
|
|
});
|
|
return {
|
|
extensionTotalPoints: totalPoints,
|
|
extensionModelName: modelName
|
|
};
|
|
})();
|
|
|
|
const totalPoints = totalVector + extensionTotalPoints;
|
|
|
|
// 插入 Bill 记录
|
|
if (billId) {
|
|
concatUsage({
|
|
teamId,
|
|
tmbId,
|
|
totalPoints,
|
|
billId,
|
|
charsLength,
|
|
listIndex: 0
|
|
});
|
|
} else {
|
|
createUsage({
|
|
teamId,
|
|
tmbId,
|
|
appName: 'support.wallet.moduleName.index',
|
|
totalPoints,
|
|
source,
|
|
list: [
|
|
{
|
|
moduleName: 'support.wallet.moduleName.index',
|
|
amount: totalVector,
|
|
model: vectorModelName,
|
|
charsLength
|
|
},
|
|
...(extensionModel !== undefined
|
|
? [
|
|
{
|
|
moduleName: 'core.module.template.Query extension',
|
|
amount: extensionTotalPoints,
|
|
model: extensionModelName,
|
|
charsLength: extensionCharsLength
|
|
}
|
|
]
|
|
: [])
|
|
]
|
|
});
|
|
}
|
|
return { totalPoints };
|
|
};
|
|
|
|
export const pushQuestionGuideUsage = ({
|
|
charsLength,
|
|
teamId,
|
|
tmbId
|
|
}: {
|
|
charsLength: number;
|
|
teamId: string;
|
|
tmbId: string;
|
|
}) => {
|
|
const qgModel = global.llmModels[0];
|
|
const { totalPoints, modelName } = formatModelChars2Points({
|
|
charsLength,
|
|
model: qgModel.model,
|
|
modelType: ModelTypeEnum.llm
|
|
});
|
|
|
|
createUsage({
|
|
teamId,
|
|
tmbId,
|
|
appName: 'core.app.Next Step Guide',
|
|
totalPoints,
|
|
source: UsageSourceEnum.fastgpt,
|
|
list: [
|
|
{
|
|
moduleName: 'core.app.Next Step Guide',
|
|
amount: totalPoints,
|
|
model: modelName,
|
|
charsLength
|
|
}
|
|
]
|
|
});
|
|
};
|
|
|
|
export function pushAudioSpeechUsage({
|
|
appName = 'support.wallet.bill.Audio Speech',
|
|
model,
|
|
charsLength,
|
|
teamId,
|
|
tmbId,
|
|
source = UsageSourceEnum.fastgpt
|
|
}: {
|
|
appName?: string;
|
|
model: string;
|
|
charsLength: number;
|
|
teamId: string;
|
|
tmbId: string;
|
|
source: `${UsageSourceEnum}`;
|
|
}) {
|
|
const { totalPoints, modelName } = formatModelChars2Points({
|
|
model,
|
|
charsLength,
|
|
modelType: ModelTypeEnum.audioSpeech
|
|
});
|
|
|
|
createUsage({
|
|
teamId,
|
|
tmbId,
|
|
appName,
|
|
totalPoints,
|
|
source,
|
|
list: [
|
|
{
|
|
moduleName: appName,
|
|
amount: totalPoints,
|
|
model: modelName,
|
|
charsLength
|
|
}
|
|
]
|
|
});
|
|
}
|
|
|
|
export function pushWhisperUsage({
|
|
teamId,
|
|
tmbId,
|
|
duration
|
|
}: {
|
|
teamId: string;
|
|
tmbId: string;
|
|
duration: number;
|
|
}) {
|
|
const whisperModel = global.whisperModel;
|
|
|
|
if (!whisperModel) return;
|
|
|
|
const { totalPoints, modelName } = formatModelChars2Points({
|
|
model: whisperModel.model,
|
|
charsLength: duration,
|
|
modelType: ModelTypeEnum.whisper,
|
|
multiple: 60
|
|
});
|
|
|
|
const name = 'support.wallet.bill.Whisper';
|
|
|
|
createUsage({
|
|
teamId,
|
|
tmbId,
|
|
appName: name,
|
|
totalPoints,
|
|
source: UsageSourceEnum.fastgpt,
|
|
list: [
|
|
{
|
|
moduleName: name,
|
|
amount: totalPoints,
|
|
model: modelName,
|
|
duration
|
|
}
|
|
]
|
|
});
|
|
}
|