modify-retrieval-modal.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useRef, useState } from 'react'
  4. import { useClickAway } from 'ahooks'
  5. import { useTranslation } from 'react-i18next'
  6. import Toast from '../../base/toast'
  7. import { XClose } from '@/app/components/base/icons/src/vender/line/general'
  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 { useProviderContext } from '@/context/provider-context'
  13. import { ensureRerankModelSelected, isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
  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. rerankDefaultModel,
  37. isRerankDefaultModelVaild,
  38. rerankModelList,
  39. } = useProviderContext()
  40. const handleSave = () => {
  41. if (
  42. !isReRankModelSelected({
  43. rerankDefaultModel,
  44. isRerankDefaultModelVaild,
  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='fixed top-16 right-2 flex flex-col bg-white border-[0.5px] border-gray-200 rounded-xl shadow-xl z-10'
  64. style={{
  65. width: 632,
  66. height: 'calc(100vh - 72px)',
  67. }}
  68. ref={ref}
  69. >
  70. <div className='shrink-0 flex justify-between items-center pl-6 pr-5 h-14 border-b border-b-gray-100'>
  71. <div className='text-base font-semibold text-gray-900'>
  72. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  73. <div className='leading-[18px] text-xs font-normal text-gray-500'>
  74. <a target='_blank' href='https://docs.dify.ai/advanced/retrieval-augment' className='text-[#155eef]'>{t('datasetSettings.form.retrievalSetting.learnMore')}</a>
  75. {t('datasetSettings.form.retrievalSetting.description')}
  76. </div>
  77. </div>
  78. <div className='flex items-center'>
  79. <div
  80. onClick={onHide}
  81. className='flex justify-center items-center w-6 h-6 cursor-pointer'
  82. >
  83. <XClose className='w-4 h-4 text-gray-500' />
  84. </div>
  85. </div>
  86. </div>
  87. <div className='p-6 border-b' style={{
  88. borderBottom: 'rgba(0, 0, 0, 0.05)',
  89. }}>
  90. {indexMethod === 'high_quality'
  91. ? (
  92. <RetrievalMethodConfig
  93. value={retrievalConfig}
  94. onChange={setRetrievalConfig}
  95. />
  96. )
  97. : (
  98. <EconomicalRetrievalMethodConfig
  99. value={retrievalConfig}
  100. onChange={setRetrievalConfig}
  101. />
  102. )}
  103. </div>
  104. <div
  105. className='flex justify-end pt-6 px-6 border-t'
  106. style={{
  107. borderColor: 'rgba(0, 0, 0, 0.05)',
  108. }}
  109. >
  110. <Button className='mr-2 flex-shrink-0' onClick={onHide}>{t('common.operation.cancel')}</Button>
  111. <Button type='primary' className='flex-shrink-0' onClick={handleSave} >{t('common.operation.save')}</Button>
  112. </div>
  113. </div>
  114. )
  115. }
  116. export default React.memo(ModifyRetrievalModal)