modify-retrieval-modal.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useRef, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { RiCloseLine } from '@remixicon/react'
  6. import Toast from '../../base/toast'
  7. import { ModelTypeEnum } from '../../header/account-setting/model-provider-page/declarations'
  8. import type { RetrievalConfig } from '@/types/app'
  9. import RetrievalMethodConfig from '@/app/components/datasets/common/retrieval-method-config'
  10. import EconomicalRetrievalMethodConfig from '@/app/components/datasets/common/economical-retrieval-method-config'
  11. import Button from '@/app/components/base/button'
  12. import { ensureRerankModelSelected, isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
  13. import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  14. type Props = {
  15. indexMethod: string
  16. value: RetrievalConfig
  17. isShow: boolean
  18. onHide: () => void
  19. onSave: (value: RetrievalConfig) => void
  20. }
  21. const ModifyRetrievalModal: FC<Props> = ({
  22. indexMethod,
  23. value,
  24. isShow,
  25. onHide,
  26. onSave,
  27. }) => {
  28. const ref = useRef(null)
  29. const { t } = useTranslation()
  30. const [retrievalConfig, setRetrievalConfig] = useState(value)
  31. // useClickAway(() => {
  32. // if (ref)
  33. // onHide()
  34. // }, ref)
  35. const {
  36. modelList: rerankModelList,
  37. defaultModel: rerankDefaultModel,
  38. currentModel: isRerankDefaultModelValid,
  39. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.rerank)
  40. const handleSave = () => {
  41. if (
  42. !isReRankModelSelected({
  43. rerankDefaultModel,
  44. isRerankDefaultModelValid: !!isRerankDefaultModelValid,
  45. rerankModelList,
  46. retrievalConfig,
  47. indexMethod,
  48. })
  49. ) {
  50. Toast.notify({ type: 'error', message: t('appDebug.datasetConfig.rerankModelRequired') })
  51. return
  52. }
  53. onSave(ensureRerankModelSelected({
  54. rerankDefaultModel: rerankDefaultModel!,
  55. retrievalConfig,
  56. indexMethod,
  57. }))
  58. }
  59. if (!isShow)
  60. return null
  61. return (
  62. <div
  63. className='w-full flex flex-col bg-white border-[0.5px] border-gray-200 rounded-xl shadow-xl'
  64. style={{
  65. height: 'calc(100vh - 72px)',
  66. }}
  67. ref={ref}
  68. >
  69. <div className='shrink-0 flex justify-between items-center pl-6 pr-5 h-14 border-b border-b-gray-100'>
  70. <div className='text-base font-semibold text-gray-900'>
  71. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  72. <div className='leading-[18px] text-xs font-normal text-gray-500'>
  73. <a target='_blank' rel='noopener noreferrer' href='https://docs.dify.ai/guides/knowledge-base/create-knowledge-and-upload-documents#id-4-retrieval-settings' className='text-[#155eef]'>{t('datasetSettings.form.retrievalSetting.learnMore')}</a>
  74. {t('datasetSettings.form.retrievalSetting.description')}
  75. </div>
  76. </div>
  77. <div className='flex items-center'>
  78. <div
  79. onClick={onHide}
  80. className='flex justify-center items-center w-6 h-6 cursor-pointer'
  81. >
  82. <RiCloseLine className='w-4 h-4 text-gray-500' />
  83. </div>
  84. </div>
  85. </div>
  86. <div className='p-6 border-b' style={{
  87. borderBottom: 'rgba(0, 0, 0, 0.05)',
  88. }}>
  89. {indexMethod === 'high_quality'
  90. ? (
  91. <RetrievalMethodConfig
  92. value={retrievalConfig}
  93. onChange={setRetrievalConfig}
  94. />
  95. )
  96. : (
  97. <EconomicalRetrievalMethodConfig
  98. value={retrievalConfig}
  99. onChange={setRetrievalConfig}
  100. />
  101. )}
  102. </div>
  103. <div
  104. className='flex justify-end pt-6 px-6 border-t'
  105. style={{
  106. borderColor: 'rgba(0, 0, 0, 0.05)',
  107. }}
  108. >
  109. <Button className='mr-2 flex-shrink-0' onClick={onHide}>{t('common.operation.cancel')}</Button>
  110. <Button variant='primary' className='flex-shrink-0' onClick={handleSave} >{t('common.operation.save')}</Button>
  111. </div>
  112. </div>
  113. )
  114. }
  115. export default React.memo(ModifyRetrievalModal)