top-k-item.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 key = 'top_k'
  25. const TopKItem: FC<Props> = ({
  26. className,
  27. value,
  28. enable,
  29. onChange,
  30. }) => {
  31. const { t } = useTranslation()
  32. const handleParamChange = (key: string, value: number) => {
  33. let notOutRangeValue = Number.parseFloat(value.toFixed(2))
  34. notOutRangeValue = Math.max(VALUE_LIMIT.min, notOutRangeValue)
  35. notOutRangeValue = Math.min(VALUE_LIMIT.max, notOutRangeValue)
  36. onChange(key, notOutRangeValue)
  37. }
  38. return (
  39. <ParamItem
  40. className={className}
  41. id={key}
  42. name={t(`appDebug.datasetConfig.${key}`)}
  43. tip={t(`appDebug.datasetConfig.${key}Tip`) as string}
  44. {...VALUE_LIMIT}
  45. value={value}
  46. enable={enable}
  47. onChange={handleParamChange}
  48. />
  49. )
  50. }
  51. export default React.memo(TopKItem)