You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

top-k-item.tsx 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import ParamItem from '.'
  6. type Props = {
  7. className?: string
  8. value: number
  9. onChange: (key: string, value: number) => void
  10. enable: boolean
  11. }
  12. const maxTopK = (() => {
  13. const configValue = Number.parseInt(globalThis.document?.body?.getAttribute('data-public-top-k-max-value') || '', 10)
  14. if (configValue && !isNaN(configValue))
  15. return configValue
  16. return 10
  17. })()
  18. const VALUE_LIMIT = {
  19. default: 2,
  20. step: 1,
  21. min: 1,
  22. max: maxTopK,
  23. }
  24. const TopKItem: FC<Props> = ({
  25. className,
  26. value,
  27. enable,
  28. onChange,
  29. }) => {
  30. const { t } = useTranslation()
  31. const handleParamChange = (key: string, value: number) => {
  32. let notOutRangeValue = Number.parseFloat(value.toFixed(2))
  33. notOutRangeValue = Math.max(VALUE_LIMIT.min, notOutRangeValue)
  34. notOutRangeValue = Math.min(VALUE_LIMIT.max, notOutRangeValue)
  35. onChange(key, notOutRangeValue)
  36. }
  37. return (
  38. <ParamItem
  39. className={className}
  40. id='top_k'
  41. name={t('appDebug.datasetConfig.top_k')}
  42. tip={t('appDebug.datasetConfig.top_kTip') as string}
  43. {...VALUE_LIMIT}
  44. value={value}
  45. enable={enable}
  46. onChange={handleParamChange}
  47. />
  48. )
  49. }
  50. export default React.memo(TopKItem)