V4.8.17 feature (#3493)

* split tokens into input and output (#3477)

* split tokens into input and output

* query extension & tool call & question guide

* fix

* perf: input and output tokens

* perf: tool call if else

* perf: remove code

* fix: extract usage count

* fix: qa usage count

---------

Co-authored-by: heheer <heheer@sealos.io>
This commit is contained in:
Archer
2024-12-30 10:13:25 +08:00
committed by GitHub
parent da2831b948
commit 50bf7f9a3b
46 changed files with 467 additions and 230 deletions

View File

@@ -18,7 +18,6 @@ import {
Thead,
Tr,
Table,
useDisclosure,
FlexProps
} from '@chakra-ui/react';
import { useSystemStore } from '@/web/common/system/useSystemStore';
@@ -175,10 +174,28 @@ const AIChatSettingsModal = ({
<Tbody>
<Tr color={'myGray.900'}>
<Td pt={0} pb={2}>
{t('common:support.wallet.Ai point every thousand tokens', {
points: selectedModel?.charsPointsPrice || 0
})}
{typeof selectedModel?.inputPrice === 'number' ? (
<>
<Box>
{t('common:support.wallet.Ai point every thousand tokens_input', {
points: selectedModel?.inputPrice || 0
})}
</Box>
<Box>
{t('common:support.wallet.Ai point every thousand tokens_output', {
points: selectedModel?.outputPrice || 0
})}
</Box>
</>
) : (
<>
{t('common:support.wallet.Ai point every thousand tokens', {
points: selectedModel?.charsPointsPrice || 0
})}
</>
)}
</Td>
<Td pt={0} pb={2}>
{Math.round((selectedModel?.maxContext || 4096) / 1000)}K
</Td>

View File

@@ -60,14 +60,32 @@ const ModelTable = () => {
const formatLLMModelList = llmModelList.map((item) => ({
...item,
typeLabel: t('common:model.type.chat'),
priceLabel: (
<Flex color={'myGray.700'}>
<Box fontWeight={'bold'} color={'myGray.900'} mr={0.5}>
{item.charsPointsPrice}
priceLabel:
typeof item.inputPrice === 'number' ? (
<Box>
<Flex>
{`${t('common:common.Input')}:`}
<Box fontWeight={'bold'} color={'myGray.900'} mr={0.5} ml={2}>
{item.inputPrice || 0}
</Box>
{`${t('common:support.wallet.subscription.point')} / 1K Tokens`}
</Flex>
<Flex>
{`${t('common:common.Output')}:`}
<Box fontWeight={'bold'} color={'myGray.900'} mr={0.5} ml={2}>
{item.outputPrice || 0}
</Box>
{`${t('common:support.wallet.subscription.point')} / 1K Tokens`}
</Flex>
</Box>
{`${t('common:support.wallet.subscription.point')} / 1K Tokens`}
</Flex>
),
) : (
<Flex color={'myGray.700'}>
<Box fontWeight={'bold'} color={'myGray.900'} mr={0.5}>
{item.charsPointsPrice}
</Box>
{`${t('common:support.wallet.subscription.point')} / 1K Tokens`}
</Flex>
),
tagColor: 'blue'
}));
const formatVectorModelList = vectorModelList.map((item) => ({
@@ -149,13 +167,13 @@ const ModelTable = () => {
return filterList;
}, [
provider,
modelType,
llmModelList,
vectorModelList,
audioSpeechModelList,
whisperModel,
t,
modelType,
provider,
search
]);

View File

@@ -155,10 +155,26 @@ export const WholeResponseContent = ({
label={t('common:core.chat.response.module tokens')}
value={`${activeModule?.tokens}`}
/>
<Row
label={t('common:core.chat.response.module input tokens')}
value={`${activeModule?.inputTokens}`}
/>
<Row
label={t('common:core.chat.response.module output tokens')}
value={`${activeModule?.outputTokens}`}
/>
<Row
label={t('common:core.chat.response.Tool call tokens')}
value={`${activeModule?.toolCallTokens}`}
/>
<Row
label={t('common:core.chat.response.Tool call input tokens')}
value={`${activeModule?.toolCallInputTokens}`}
/>
<Row
label={t('common:core.chat.response.Tool call output tokens')}
value={`${activeModule?.toolCallOutputTokens}`}
/>
<Row label={t('common:core.chat.response.module query')} value={activeModule?.query} />
<Row

View File

@@ -26,37 +26,48 @@ const UsageDetail = ({ usage, onClose }: { usage: UsageItemType; onClose: () =>
[usage.list]
);
const { hasModel, hasToken, hasCharsLen, hasDuration } = useMemo(() => {
let hasModel = false;
let hasToken = false;
let hasCharsLen = false;
let hasDuration = false;
let hasDataLen = false;
const { hasModel, hasToken, hasInputToken, hasOutputToken, hasCharsLen, hasDuration } =
useMemo(() => {
let hasModel = false;
let hasToken = false;
let hasInputToken = false;
let hasOutputToken = false;
let hasCharsLen = false;
let hasDuration = false;
let hasDataLen = false;
usage.list.forEach((item) => {
if (item.model !== undefined) {
hasModel = true;
}
usage.list.forEach((item) => {
if (item.model !== undefined) {
hasModel = true;
}
if (typeof item.tokens === 'number') {
hasToken = true;
}
if (typeof item.charsLength === 'number') {
hasCharsLen = true;
}
if (typeof item.duration === 'number') {
hasDuration = true;
}
});
if (typeof item.tokens === 'number') {
hasToken = true;
}
if (typeof item.inputTokens === 'number') {
hasInputToken = true;
}
if (typeof item.outputTokens === 'number') {
hasOutputToken = true;
}
if (typeof item.charsLength === 'number') {
hasCharsLen = true;
}
if (typeof item.duration === 'number') {
hasDuration = true;
}
});
return {
hasModel,
hasToken,
hasCharsLen,
hasDuration,
hasDataLen
};
}, [usage.list]);
return {
hasModel,
hasToken,
hasInputToken,
hasOutputToken,
hasCharsLen,
hasDuration,
hasDataLen
};
}, [usage.list]);
return (
<MyModal
@@ -98,6 +109,8 @@ const UsageDetail = ({ usage, onClose }: { usage: UsageItemType; onClose: () =>
<Th>{t('account_usage:module_name')}</Th>
{hasModel && <Th>{t('account_usage:ai_model')}</Th>}
{hasToken && <Th>{t('account_usage:token_length')}</Th>}
{hasInputToken && <Th>{t('account_usage:input_token_length')}</Th>}
{hasOutputToken && <Th>{t('account_usage:output_token_length')}</Th>}
{hasCharsLen && <Th>{t('account_usage:text_length')}</Th>}
{hasDuration && <Th>{t('account_usage:duration_seconds')}</Th>}
<Th>{t('account_usage:total_points_consumed')}</Th>
@@ -109,6 +122,8 @@ const UsageDetail = ({ usage, onClose }: { usage: UsageItemType; onClose: () =>
<Td>{t(item.moduleName as any)}</Td>
{hasModel && <Td>{item.model ?? '-'}</Td>}
{hasToken && <Td>{item.tokens ?? '-'}</Td>}
{hasInputToken && <Td>{item.inputTokens ?? '-'}</Td>}
{hasOutputToken && <Td>{item.outputTokens ?? '-'}</Td>}
{hasCharsLen && <Td>{item.charsLength ?? '-'}</Td>}
{hasDuration && <Td>{item.duration ?? '-'}</Td>}
<Td>{formatNumber(item.amount)}</Td>

View File

@@ -37,7 +37,7 @@ async function handler(
const qgModel = global.llmModels[0];
const { result, tokens } = await createQuestionGuide({
const { result, inputTokens, outputTokens } = await createQuestionGuide({
messages,
model: qgModel.model
});
@@ -47,7 +47,8 @@ async function handler(
});
pushQuestionGuideUsage({
tokens,
inputTokens,
outputTokens,
teamId,
tmbId
});

View File

@@ -52,14 +52,15 @@ async function handler(req: ApiRequestProps<CreateQuestionGuideParams>, res: Nex
const qgModel = questionGuide?.model || global.llmModels[0].model;
const { result, tokens } = await createQuestionGuide({
const { result, inputTokens, outputTokens } = await createQuestionGuide({
messages,
model: qgModel,
customPrompt: questionGuide?.customPrompt
});
pushQuestionGuideUsage({
tokens,
inputTokens,
outputTokens,
teamId,
tmbId
});

View File

@@ -89,7 +89,7 @@ async function handler(req: NextApiRequest) {
pushGenerateVectorUsage({
teamId,
tmbId,
tokens,
inputTokens: tokens,
model: vectorModelData.model
});

View File

@@ -36,7 +36,7 @@ async function handler(req: ApiRequestProps<UpdateDatasetDataProps>) {
pushGenerateVectorUsage({
teamId,
tmbId,
tokens,
inputTokens: tokens,
model: vectorModel
});
} else {

View File

@@ -74,14 +74,15 @@ async function handler(req: NextApiRequest) {
const { totalPoints } = pushGenerateVectorUsage({
teamId,
tmbId,
tokens,
inputTokens: tokens,
model: dataset.vectorModel,
source: apikey ? UsageSourceEnum.api : UsageSourceEnum.fastgpt,
...(aiExtensionResult &&
extensionModel && {
extensionModel: extensionModel.name,
extensionTokens: aiExtensionResult.tokens
extensionInputTokens: aiExtensionResult.inputTokens,
extensionOutputTokens: aiExtensionResult.outputTokens
})
});
if (apikey) {

View File

@@ -57,7 +57,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
const { totalPoints } = pushGenerateVectorUsage({
teamId,
tmbId,
tokens,
inputTokens: tokens,
model,
billId,
source: getUsageSourceByAuthType({ authType })

View File

@@ -196,7 +196,7 @@ const DatasetImportContextProvider = ({ children }: { children: React.ReactNode
chunkSize: vectorModel?.defaultToken ? vectorModel?.defaultToken * 2 : 1024,
showChunkInput: false,
showPromptInput: false,
charsPointsPrice: agentModel.charsPointsPrice,
charsPointsPrice: agentModel.charsPointsPrice || 0,
priceTip: t('dataset:import.Auto mode Estimated Price Tips', {
price: agentModel.charsPointsPrice
}),
@@ -211,7 +211,7 @@ const DatasetImportContextProvider = ({ children }: { children: React.ReactNode
chunkSize: embeddingChunkSize,
showChunkInput: true,
showPromptInput: false,
charsPointsPrice: vectorModel.charsPointsPrice,
charsPointsPrice: vectorModel.charsPointsPrice || 0,
priceTip: t('dataset:import.Embedding Estimated Price Tips', {
price: vectorModel.charsPointsPrice
}),
@@ -226,7 +226,7 @@ const DatasetImportContextProvider = ({ children }: { children: React.ReactNode
chunkSize: qaChunkSize,
showChunkInput: true,
showPromptInput: true,
charsPointsPrice: agentModel.charsPointsPrice,
charsPointsPrice: agentModel.charsPointsPrice || 0,
priceTip: t('dataset:import.Auto mode Estimated Price Tips', {
price: agentModel.charsPointsPrice
}),

View File

@@ -12,7 +12,10 @@ import { getLLMModel } from '@fastgpt/service/core/ai/model';
import { checkTeamAiPointsAndLock } from './utils';
import { checkInvalidChunkAndLock } from '@fastgpt/service/core/dataset/training/utils';
import { addMinutes } from 'date-fns';
import { countGptMessagesTokens } from '@fastgpt/service/common/string/tiktoken/index';
import {
countGptMessagesTokens,
countPromptTokens
} from '@fastgpt/service/common/string/tiktoken/index';
import { pushDataListToTrainingQueueByCollectionId } from '@fastgpt/service/core/dataset/training/controller';
import { loadRequestMessages } from '@fastgpt/service/core/chat/utils';
import { llmCompletionsBodyFormat } from '@fastgpt/service/core/ai/utils';
@@ -153,7 +156,8 @@ ${replaceVariable(Prompt_AgentQA.fixedText, { text })}`;
pushQAUsage({
teamId: data.teamId,
tmbId: data.tmbId,
tokens: await countGptMessagesTokens(messages),
inputTokens: await countGptMessagesTokens(messages),
outputTokens: await countPromptTokens(answer),
billId: data.billId,
model: modelData.model
});

View File

@@ -111,7 +111,7 @@ export async function generateVector(): Promise<any> {
pushGenerateVectorUsage({
teamId: data.teamId,
tmbId: data.tmbId,
tokens,
inputTokens: tokens,
model: data.model,
billId: data.billId
});

View File

@@ -37,7 +37,8 @@ export const pushChatUsage = ({
moduleName: item.moduleName,
amount: item.totalPoints || 0,
model: item.model,
tokens: item.tokens
inputTokens: item.inputTokens,
outputTokens: item.outputTokens
}))
});
addLog.info(`finish completions`, {
@@ -52,20 +53,23 @@ export const pushQAUsage = async ({
teamId,
tmbId,
model,
tokens,
inputTokens,
outputTokens,
billId
}: {
teamId: string;
tmbId: string;
model: string;
tokens: number;
inputTokens: number;
outputTokens: number;
billId: string;
}) => {
// 计算价格
const { totalPoints } = formatModelChars2Points({
model,
modelType: ModelTypeEnum.llm,
tokens
inputTokens,
outputTokens
});
concatUsage({
@@ -73,7 +77,8 @@ export const pushQAUsage = async ({
teamId,
tmbId,
totalPoints,
tokens,
inputTokens,
outputTokens,
listIndex: 1
});
@@ -84,30 +89,32 @@ export const pushGenerateVectorUsage = ({
billId,
teamId,
tmbId,
tokens,
inputTokens,
model,
source = UsageSourceEnum.fastgpt,
extensionModel,
extensionTokens
extensionInputTokens,
extensionOutputTokens
}: {
billId?: string;
teamId: string;
tmbId: string;
tokens: number;
inputTokens: number;
model: string;
source?: UsageSourceEnum;
extensionModel?: string;
extensionTokens?: number;
extensionInputTokens?: number;
extensionOutputTokens?: number;
}) => {
const { totalPoints: totalVector, modelName: vectorModelName } = formatModelChars2Points({
modelType: ModelTypeEnum.vector,
model,
tokens
inputTokens
});
const { extensionTotalPoints, extensionModelName } = (() => {
if (!extensionModel || !extensionTokens)
if (!extensionModel || !extensionInputTokens)
return {
extensionTotalPoints: 0,
extensionModelName: ''
@@ -115,7 +122,8 @@ export const pushGenerateVectorUsage = ({
const { totalPoints, modelName } = formatModelChars2Points({
modelType: ModelTypeEnum.llm,
model: extensionModel,
tokens: extensionTokens
inputTokens: extensionInputTokens,
outputTokens: extensionOutputTokens
});
return {
extensionTotalPoints: totalPoints,
@@ -132,7 +140,7 @@ export const pushGenerateVectorUsage = ({
tmbId,
totalPoints,
billId,
tokens,
inputTokens,
listIndex: 0
});
} else {
@@ -147,7 +155,7 @@ export const pushGenerateVectorUsage = ({
moduleName: 'support.wallet.moduleName.index',
amount: totalVector,
model: vectorModelName,
tokens
inputTokens
},
...(extensionModel !== undefined
? [
@@ -155,7 +163,8 @@ export const pushGenerateVectorUsage = ({
moduleName: 'core.module.template.Query extension',
amount: extensionTotalPoints,
model: extensionModelName,
tokens: extensionTokens
inputTokens: extensionInputTokens,
outputTokens: extensionOutputTokens
}
]
: [])
@@ -166,17 +175,20 @@ export const pushGenerateVectorUsage = ({
};
export const pushQuestionGuideUsage = ({
tokens,
inputTokens,
outputTokens,
teamId,
tmbId
}: {
tokens: number;
inputTokens: number;
outputTokens: number;
teamId: string;
tmbId: string;
}) => {
const qgModel = global.llmModels[0];
const { totalPoints, modelName } = formatModelChars2Points({
tokens,
inputTokens,
outputTokens,
model: qgModel.model,
modelType: ModelTypeEnum.llm
});
@@ -192,7 +204,8 @@ export const pushQuestionGuideUsage = ({
moduleName: 'core.app.Question Guide',
amount: totalPoints,
model: modelName,
tokens
inputTokens,
outputTokens
}
]
});
@@ -215,7 +228,7 @@ export function pushAudioSpeechUsage({
}) {
const { totalPoints, modelName } = formatModelChars2Points({
model,
tokens: charsLength,
inputTokens: charsLength,
modelType: ModelTypeEnum.audioSpeech
});
@@ -251,7 +264,7 @@ export function pushWhisperUsage({
const { totalPoints, modelName } = formatModelChars2Points({
model: whisperModel.model,
tokens: duration,
inputTokens: duration,
modelType: ModelTypeEnum.whisper,
multiple: 60
});