From 56750cbed5dcb740ef667aa4d93e20b39bf45648 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Mon, 30 Dec 2024 10:07:57 +0800 Subject: [PATCH] perf: io price computed --- packages/global/core/ai/model.d.ts | 25 +++++---- .../service/support/wallet/usage/utils.ts | 2 +- .../core/ai/AISettingModal/index.tsx | 45 ++++++++-------- .../components/core/ai/ModelTable/index.tsx | 51 +++++++++---------- .../detail/components/Import/Context.tsx | 6 +-- projects/app/src/web/common/system/utils.ts | 5 -- 6 files changed, 64 insertions(+), 70 deletions(-) diff --git a/packages/global/core/ai/model.d.ts b/packages/global/core/ai/model.d.ts index 76854e30b2a5..83f13eb812aa 100644 --- a/packages/global/core/ai/model.d.ts +++ b/packages/global/core/ai/model.d.ts @@ -1,6 +1,13 @@ import type { ModelProviderIdType } from './provider'; -export type LLMModelItemType = { +type PriceType = { + charsPointsPrice?: number; // 1k chars=n points; 60s=n points; + + // If inputPrice is set, the input-output charging scheme is adopted + inputPrice?: number; // 1k tokens=n points + outputPrice?: number; // 1k tokens=n points +}; +export type LLMModelItemType = PriceType & { provider: ModelProviderIdType; model: string; name: string; @@ -10,10 +17,6 @@ export type LLMModelItemType = { quoteMaxToken: number; maxTemperature: number; - charsPointsPrice: number; // 1k chars=n points - inputPrice?: number; // 1k tokens=n points - outputPrice?: number; // 1k tokens=n points - censor?: boolean; vision?: boolean; @@ -35,13 +38,12 @@ export type LLMModelItemType = { fieldMap?: Record; }; -export type VectorModelItemType = { +export type VectorModelItemType = PriceType & { provider: ModelProviderIdType; model: string; // model name name: string; // show name avatar?: string; defaultToken: number; // split text default token - charsPointsPrice: number; // 1k tokens=n points maxToken: number; // model max token weight: number; // training weight hidden?: boolean; // Disallow creation @@ -50,25 +52,22 @@ export type VectorModelItemType = { queryConfig?: Record; // Custom parameters for query }; -export type ReRankModelItemType = { +export type ReRankModelItemType = PriceType & { model: string; name: string; - charsPointsPrice: number; requestUrl: string; requestAuth: string; }; -export type AudioSpeechModelType = { +export type AudioSpeechModelType = PriceType & { provider: ModelProviderIdType; model: string; name: string; - charsPointsPrice: number; voices: { label: string; value: string; bufferId: string }[]; }; -export type STTModelType = { +export type STTModelType = PriceType & { provider: ModelProviderIdType; model: string; name: string; - charsPointsPrice: number; // 60s = n points }; diff --git a/packages/service/support/wallet/usage/utils.ts b/packages/service/support/wallet/usage/utils.ts index ed6e1014307f..d4c21347b0e1 100644 --- a/packages/service/support/wallet/usage/utils.ts +++ b/packages/service/support/wallet/usage/utils.ts @@ -22,7 +22,7 @@ export const formatModelChars2Points = ({ }; } - const isIOPriceType = modelType === ModelTypeEnum.llm && global.llmModelPriceType === 'IO'; + const isIOPriceType = typeof modelData.inputPrice === 'number'; const totalPoints = isIOPriceType ? (modelData.inputPrice || 0) * (inputTokens / multiple) + diff --git a/projects/app/src/components/core/ai/AISettingModal/index.tsx b/projects/app/src/components/core/ai/AISettingModal/index.tsx index b89e2497abd2..2d6b17e863de 100644 --- a/projects/app/src/components/core/ai/AISettingModal/index.tsx +++ b/projects/app/src/components/core/ai/AISettingModal/index.tsx @@ -18,7 +18,6 @@ import { Thead, Tr, Table, - useDisclosure, FlexProps } from '@chakra-ui/react'; import { useSystemStore } from '@/web/common/system/useSystemStore'; @@ -28,7 +27,7 @@ import { getDocPath } from '@/web/common/system/doc'; import AIModelSelector from '@/components/Select/AIModelSelector'; import { LLMModelItemType } from '@fastgpt/global/core/ai/model.d'; import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip'; -import { getWebLLMModel, getWebLLMModelPriceType } from '@/web/common/system/utils'; +import { getWebLLMModel } from '@/web/common/system/utils'; import MyIcon from '@fastgpt/web/components/common/Icon'; import dynamic from 'next/dynamic'; import InputSlider from '@fastgpt/web/components/common/MySlider/InputSlider'; @@ -82,7 +81,6 @@ const AIChatSettingsModal = ({ const useVision = watch('aiChatVision'); const selectedModel = getWebLLMModel(model); - const isIOType = getWebLLMModelPriceType(); const llmSupportVision = !!selectedModel?.vision; const tokenLimit = useMemo(() => { @@ -175,26 +173,29 @@ const AIChatSettingsModal = ({ - {!isIOType ? ( - - {t('common:support.wallet.Ai point every thousand tokens', { - points: selectedModel?.charsPointsPrice || 0 - })} - - ) : ( - - - {t('common:support.wallet.Ai point every thousand tokens_input', { - points: selectedModel?.inputPrice || 0 - })} - - - {t('common:support.wallet.Ai point every thousand tokens_output', { - points: selectedModel?.outputPrice || 0 + + {typeof selectedModel?.inputPrice === 'number' ? ( + <> + + {t('common:support.wallet.Ai point every thousand tokens_input', { + points: selectedModel?.inputPrice || 0 + })} + + + {t('common:support.wallet.Ai point every thousand tokens_output', { + points: selectedModel?.outputPrice || 0 + })} + + + ) : ( + <> + {t('common:support.wallet.Ai point every thousand tokens', { + points: selectedModel?.charsPointsPrice || 0 })} - - - )} + + )} + + {Math.round((selectedModel?.maxContext || 4096) / 1000)}K diff --git a/projects/app/src/components/core/ai/ModelTable/index.tsx b/projects/app/src/components/core/ai/ModelTable/index.tsx index 7abc3575f43a..41547b6aa1df 100644 --- a/projects/app/src/components/core/ai/ModelTable/index.tsx +++ b/projects/app/src/components/core/ai/ModelTable/index.tsx @@ -27,7 +27,6 @@ import Avatar from '@fastgpt/web/components/common/Avatar'; import MyTag from '@fastgpt/web/components/common/Tag/index'; import MyTooltip from '@fastgpt/web/components/common/MyTooltip'; import dynamic from 'next/dynamic'; -import { getWebLLMModelPriceType } from '@/web/common/system/utils'; const MyModal = dynamic(() => import('@fastgpt/web/components/common/MyModal')); @@ -56,37 +55,37 @@ const ModelTable = () => { const [search, setSearch] = useState(''); const { llmModelList, audioSpeechModelList, vectorModelList, whisperModel } = useSystemStore(); - const isIOType = getWebLLMModelPriceType(); const modelList = useMemo(() => { const formatLLMModelList = llmModelList.map((item) => ({ ...item, typeLabel: t('common:model.type.chat'), - priceLabel: isIOType ? ( - - - {`${t('common:common.Input')}:`} - - {item.inputPrice || 0} - - {`${t('common:support.wallet.subscription.point')} / 1K Tokens`} - - - {`${t('common:common.Output')}:`} - - {item.outputPrice || 0} + priceLabel: + typeof item.inputPrice === 'number' ? ( + + + {`${t('common:common.Input')}:`} + + {item.inputPrice || 0} + + {`${t('common:support.wallet.subscription.point')} / 1K Tokens`} + + + {`${t('common:common.Output')}:`} + + {item.outputPrice || 0} + + {`${t('common:support.wallet.subscription.point')} / 1K Tokens`} + + + ) : ( + + + {item.charsPointsPrice} {`${t('common:support.wallet.subscription.point')} / 1K Tokens`} - - ) : ( - - - {item.charsPointsPrice} - - {`${t('common:support.wallet.subscription.point')} / 1K Tokens`} - - ), + ), tagColor: 'blue' })); const formatVectorModelList = vectorModelList.map((item) => ({ @@ -168,13 +167,13 @@ const ModelTable = () => { return filterList; }, [ - provider, - modelType, llmModelList, vectorModelList, audioSpeechModelList, whisperModel, t, + modelType, + provider, search ]); diff --git a/projects/app/src/pages/dataset/detail/components/Import/Context.tsx b/projects/app/src/pages/dataset/detail/components/Import/Context.tsx index 5d86e4351462..52eacd9bfae8 100644 --- a/projects/app/src/pages/dataset/detail/components/Import/Context.tsx +++ b/projects/app/src/pages/dataset/detail/components/Import/Context.tsx @@ -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 }), diff --git a/projects/app/src/web/common/system/utils.ts b/projects/app/src/web/common/system/utils.ts index abcc4e852e79..3d5ea1ef8a9c 100644 --- a/projects/app/src/web/common/system/utils.ts +++ b/projects/app/src/web/common/system/utils.ts @@ -13,8 +13,3 @@ export const getWebLLMModel = (model?: string) => { const list = useSystemStore.getState().llmModelList; return list.find((item) => item.model === model || item.name === model) ?? list[0]; }; - -export const getWebLLMModelPriceType = () => { - const list = useSystemStore.getState().llmModelList; - return list.some((item) => item.inputPrice || item.outputPrice); -};