diff --git a/web/app/components/base/param-item/top-k-item.tsx b/web/app/components/base/param-item/top-k-item.tsx index f59c0f273e..0bd552e48d 100644 --- a/web/app/components/base/param-item/top-k-item.tsx +++ b/web/app/components/base/param-item/top-k-item.tsx @@ -32,7 +32,7 @@ const TopKItem: FC = ({ }) => { const { t } = useTranslation() const handleParamChange = (key: string, value: number) => { - let notOutRangeValue = Number.parseFloat(value.toFixed(2)) + let notOutRangeValue = Number.parseInt(value.toFixed(0)) notOutRangeValue = Math.max(VALUE_LIMIT.min, notOutRangeValue) notOutRangeValue = Math.min(VALUE_LIMIT.max, notOutRangeValue) onChange(key, notOutRangeValue) diff --git a/web/app/components/workflow/nodes/knowledge-base/components/retrieval-setting/top-k-and-score-threshold.tsx b/web/app/components/workflow/nodes/knowledge-base/components/retrieval-setting/top-k-and-score-threshold.tsx index 9d46037d84..9eb1cb39c9 100644 --- a/web/app/components/workflow/nodes/knowledge-base/components/retrieval-setting/top-k-and-score-threshold.tsx +++ b/web/app/components/workflow/nodes/knowledge-base/components/retrieval-setting/top-k-and-score-threshold.tsx @@ -1,8 +1,8 @@ -import { memo } from 'react' +import { memo, useCallback } from 'react' import { useTranslation } from 'react-i18next' import Tooltip from '@/app/components/base/tooltip' -import Input from '@/app/components/base/input' import Switch from '@/app/components/base/switch' +import { InputNumber } from '@/app/components/base/input-number' export type TopKAndScoreThresholdProps = { topK: number @@ -14,6 +14,24 @@ export type TopKAndScoreThresholdProps = { readonly?: boolean hiddenScoreThreshold?: boolean } + +const maxTopK = (() => { + const configValue = Number.parseInt(globalThis.document?.body?.getAttribute('data-public-top-k-max-value') || '', 10) + if (configValue && !isNaN(configValue)) + return configValue + return 10 +})() +const TOP_K_VALUE_LIMIT = { + amount: 1, + min: 1, + max: maxTopK, +} +const SCORE_THRESHOLD_VALUE_LIMIT = { + step: 0.01, + min: 0, + max: 1, +} + const TopKAndScoreThreshold = ({ topK, onTopKChange, @@ -25,18 +43,18 @@ const TopKAndScoreThreshold = ({ hiddenScoreThreshold, }: TopKAndScoreThresholdProps) => { const { t } = useTranslation() - const handleTopKChange = (e: React.ChangeEvent) => { - const value = Number(e.target.value) - if (Number.isNaN(value)) - return - onTopKChange?.(value) - } + const handleTopKChange = useCallback((value: number) => { + let notOutRangeValue = Number.parseInt(value.toFixed(0)) + notOutRangeValue = Math.max(TOP_K_VALUE_LIMIT.min, notOutRangeValue) + notOutRangeValue = Math.min(TOP_K_VALUE_LIMIT.max, notOutRangeValue) + onTopKChange?.(notOutRangeValue) + }, [onTopKChange]) - const handleScoreThresholdChange = (e: React.ChangeEvent) => { - const value = Number(e.target.value) - if (Number.isNaN(value)) - return - onScoreThresholdChange?.(value) + const handleScoreThresholdChange = (value: number) => { + let notOutRangeValue = Number.parseFloat(value.toFixed(2)) + notOutRangeValue = Math.max(SCORE_THRESHOLD_VALUE_LIMIT.min, notOutRangeValue) + notOutRangeValue = Math.min(SCORE_THRESHOLD_VALUE_LIMIT.max, notOutRangeValue) + onScoreThresholdChange?.(notOutRangeValue) } return ( @@ -49,11 +67,13 @@ const TopKAndScoreThreshold = ({ popupContent={t('appDebug.datasetConfig.top_kTip')} /> - { @@ -74,11 +94,13 @@ const TopKAndScoreThreshold = ({ popupContent={t('appDebug.datasetConfig.score_thresholdTip')} /> - )