index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import type { FC } from 'react'
  2. import { useRef, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { isEqual } from 'lodash-es'
  5. import cn from 'classnames'
  6. import { RiCloseLine } from '@remixicon/react'
  7. import { BookOpenIcon } from '@heroicons/react/24/outline'
  8. import IndexMethodRadio from '@/app/components/datasets/settings/index-method-radio'
  9. import Button from '@/app/components/base/button'
  10. import type { DataSet } from '@/models/datasets'
  11. import { useToastContext } from '@/app/components/base/toast'
  12. import { updateDatasetSetting } from '@/service/datasets'
  13. import { useModalContext } from '@/context/modal-context'
  14. import type { RetrievalConfig } from '@/types/app'
  15. import RetrievalMethodConfig from '@/app/components/datasets/common/retrieval-method-config'
  16. import EconomicalRetrievalMethodConfig from '@/app/components/datasets/common/economical-retrieval-method-config'
  17. import { ensureRerankModelSelected, isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
  18. import { AlertTriangle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback'
  19. import PermissionsRadio from '@/app/components/datasets/settings/permissions-radio'
  20. import ModelSelector from '@/app/components/header/account-setting/model-provider-page/model-selector'
  21. import {
  22. useModelList,
  23. useModelListAndDefaultModelAndCurrentProviderAndModel,
  24. } from '@/app/components/header/account-setting/model-provider-page/hooks'
  25. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  26. type SettingsModalProps = {
  27. currentDataset: DataSet
  28. onCancel: () => void
  29. onSave: (newDataset: DataSet) => void
  30. }
  31. const rowClass = `
  32. flex justify-between py-4 flex-wrap gap-y-2
  33. `
  34. const labelClass = `
  35. flex w-[168px] shrink-0
  36. `
  37. const SettingsModal: FC<SettingsModalProps> = ({
  38. currentDataset,
  39. onCancel,
  40. onSave,
  41. }) => {
  42. const { data: embeddingsModelList } = useModelList(ModelTypeEnum.textEmbedding)
  43. const {
  44. modelList: rerankModelList,
  45. defaultModel: rerankDefaultModel,
  46. currentModel: isRerankDefaultModelVaild,
  47. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.rerank)
  48. const { t } = useTranslation()
  49. const { notify } = useToastContext()
  50. const ref = useRef(null)
  51. const { setShowAccountSettingModal } = useModalContext()
  52. const [loading, setLoading] = useState(false)
  53. const [localeCurrentDataset, setLocaleCurrentDataset] = useState({ ...currentDataset })
  54. const [indexMethod, setIndexMethod] = useState(currentDataset.indexing_technique)
  55. const [retrievalConfig, setRetrievalConfig] = useState(localeCurrentDataset?.retrieval_model_dict as RetrievalConfig)
  56. const handleValueChange = (type: string, value: string) => {
  57. setLocaleCurrentDataset({ ...localeCurrentDataset, [type]: value })
  58. }
  59. const [isHideChangedTip, setIsHideChangedTip] = useState(false)
  60. const isRetrievalChanged = !isEqual(retrievalConfig, localeCurrentDataset?.retrieval_model_dict) || indexMethod !== localeCurrentDataset?.indexing_technique
  61. const handleSave = async () => {
  62. if (loading)
  63. return
  64. if (!localeCurrentDataset.name?.trim()) {
  65. notify({ type: 'error', message: t('datasetSettings.form.nameError') })
  66. return
  67. }
  68. if (
  69. !isReRankModelSelected({
  70. rerankDefaultModel,
  71. isRerankDefaultModelVaild: !!isRerankDefaultModelVaild,
  72. rerankModelList,
  73. retrievalConfig,
  74. indexMethod,
  75. })
  76. ) {
  77. notify({ type: 'error', message: t('appDebug.datasetConfig.rerankModelRequired') })
  78. return
  79. }
  80. const postRetrievalConfig = ensureRerankModelSelected({
  81. rerankDefaultModel: rerankDefaultModel!,
  82. retrievalConfig,
  83. indexMethod,
  84. })
  85. try {
  86. setLoading(true)
  87. const { id, name, description, permission } = localeCurrentDataset
  88. await updateDatasetSetting({
  89. datasetId: id,
  90. body: {
  91. name,
  92. description,
  93. permission,
  94. indexing_technique: indexMethod,
  95. retrieval_model: {
  96. ...postRetrievalConfig,
  97. score_threshold: postRetrievalConfig.score_threshold_enabled ? postRetrievalConfig.score_threshold : 0,
  98. },
  99. embedding_model: localeCurrentDataset.embedding_model,
  100. embedding_model_provider: localeCurrentDataset.embedding_model_provider,
  101. },
  102. })
  103. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  104. onSave({
  105. ...localeCurrentDataset,
  106. indexing_technique: indexMethod,
  107. retrieval_model_dict: postRetrievalConfig,
  108. })
  109. }
  110. catch (e) {
  111. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  112. }
  113. finally {
  114. setLoading(false)
  115. }
  116. }
  117. return (
  118. <div
  119. className='overflow-hidden w-full flex flex-col bg-white border-[0.5px] border-gray-200 rounded-xl shadow-xl'
  120. style={{
  121. height: 'calc(100vh - 72px)',
  122. }}
  123. ref={ref}
  124. >
  125. <div className='shrink-0 flex justify-between items-center pl-6 pr-5 h-14 border-b border-b-gray-100'>
  126. <div className='flex flex-col text-base font-semibold text-gray-900'>
  127. <div className='leading-6'>{t('datasetSettings.title')}</div>
  128. </div>
  129. <div className='flex items-center'>
  130. <div
  131. onClick={onCancel}
  132. className='flex justify-center items-center w-6 h-6 cursor-pointer'
  133. >
  134. <RiCloseLine className='w-4 h-4 text-gray-500' />
  135. </div>
  136. </div>
  137. </div>
  138. {/* Body */}
  139. <div className='p-6 pt-5 border-b overflow-y-auto pb-[68px]' style={{
  140. borderBottom: 'rgba(0, 0, 0, 0.05)',
  141. }}>
  142. <div className={cn(rowClass, 'items-center')}>
  143. <div className={labelClass}>
  144. {t('datasetSettings.form.name')}
  145. </div>
  146. <input
  147. value={localeCurrentDataset.name}
  148. onChange={e => handleValueChange('name', e.target.value)}
  149. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  150. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  151. />
  152. </div>
  153. <div className={cn(rowClass)}>
  154. <div className={labelClass}>
  155. {t('datasetSettings.form.desc')}
  156. </div>
  157. <div className='w-full'>
  158. <textarea
  159. value={localeCurrentDataset.description || ''}
  160. onChange={e => handleValueChange('description', e.target.value)}
  161. className='block px-3 py-2 w-full h-[88px] rounded-lg bg-gray-100 text-sm outline-none appearance-none resize-none'
  162. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  163. />
  164. <a className='mt-2 flex items-center h-[18px] px-3 text-xs text-gray-500' href="https://docs.dify.ai/features/datasets#how-to-write-a-good-dataset-description" target='_blank' rel='noopener noreferrer'>
  165. <BookOpenIcon className='w-3 h-[18px] mr-1' />
  166. {t('datasetSettings.form.descWrite')}
  167. </a>
  168. </div>
  169. </div>
  170. <div className={rowClass}>
  171. <div className={labelClass}>
  172. <div>{t('datasetSettings.form.permissions')}</div>
  173. </div>
  174. <div className='w-full'>
  175. <PermissionsRadio
  176. disable={!localeCurrentDataset?.embedding_available}
  177. value={localeCurrentDataset.permission}
  178. onChange={v => handleValueChange('permission', v!)}
  179. itemClassName='sm:!w-[280px]'
  180. />
  181. </div>
  182. </div>
  183. <div className="w-full h-0 border-b-[0.5px] border-b-gray-200 my-2"></div>
  184. <div className={cn(rowClass)}>
  185. <div className={labelClass}>
  186. {t('datasetSettings.form.indexMethod')}
  187. </div>
  188. <div className='grow'>
  189. <IndexMethodRadio
  190. disable={!localeCurrentDataset?.embedding_available}
  191. value={indexMethod}
  192. onChange={v => setIndexMethod(v!)}
  193. itemClassName='sm:!w-[280px]'
  194. />
  195. </div>
  196. </div>
  197. {indexMethod === 'high_quality' && (
  198. <div className={cn(rowClass)}>
  199. <div className={labelClass}>
  200. {t('datasetSettings.form.embeddingModel')}
  201. </div>
  202. <div className='w-full'>
  203. <div className='w-full h-9 rounded-lg bg-gray-100 opacity-60'>
  204. <ModelSelector
  205. readonly
  206. defaultModel={{
  207. provider: localeCurrentDataset.embedding_model_provider,
  208. model: localeCurrentDataset.embedding_model,
  209. }}
  210. modelList={embeddingsModelList}
  211. />
  212. </div>
  213. <div className='mt-2 w-full text-xs leading-6 text-gray-500'>
  214. {t('datasetSettings.form.embeddingModelTip')}
  215. <span className='text-[#155eef] cursor-pointer' onClick={() => setShowAccountSettingModal({ payload: 'provider' })}>{t('datasetSettings.form.embeddingModelTipLink')}</span>
  216. </div>
  217. </div>
  218. </div>
  219. )}
  220. {/* Retrieval Method Config */}
  221. <div className={rowClass}>
  222. <div className={labelClass}>
  223. <div>
  224. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  225. <div className='leading-[18px] text-xs font-normal text-gray-500'>
  226. <a target='_blank' rel='noopener noreferrer' href='https://docs.dify.ai/guides/knowledge-base/create-knowledge-and-upload-documents#id-6-retrieval-settings' className='text-[#155eef]'>{t('datasetSettings.form.retrievalSetting.learnMore')}</a>
  227. {t('datasetSettings.form.retrievalSetting.description')}
  228. </div>
  229. </div>
  230. </div>
  231. <div className='w-[480px]'>
  232. {indexMethod === 'high_quality'
  233. ? (
  234. <RetrievalMethodConfig
  235. value={retrievalConfig}
  236. onChange={setRetrievalConfig}
  237. />
  238. )
  239. : (
  240. <EconomicalRetrievalMethodConfig
  241. value={retrievalConfig}
  242. onChange={setRetrievalConfig}
  243. />
  244. )}
  245. </div>
  246. </div>
  247. </div>
  248. {isRetrievalChanged && !isHideChangedTip && (
  249. <div className='absolute z-10 left-[30px] right-[30px] bottom-[76px] flex h-10 items-center px-3 rounded-lg border border-[#FEF0C7] bg-[#FFFAEB] shadow-lg justify-between'>
  250. <div className='flex items-center'>
  251. <AlertTriangle className='mr-1 w-3 h-3 text-[#F79009]' />
  252. <div className='leading-[18px] text-xs font-medium text-gray-700'>{t('appDebug.datasetConfig.retrieveChangeTip')}</div>
  253. </div>
  254. <div className='p-1 cursor-pointer' onClick={(e) => {
  255. setIsHideChangedTip(true)
  256. e.stopPropagation()
  257. e.nativeEvent.stopImmediatePropagation()
  258. }}>
  259. <RiCloseLine className='w-4 h-4 text-gray-500 ' />
  260. </div>
  261. </div>
  262. )}
  263. <div
  264. className='sticky z-[5] bottom-0 w-full flex justify-end py-4 px-6 border-t bg-white '
  265. style={{
  266. borderColor: 'rgba(0, 0, 0, 0.05)',
  267. }}
  268. >
  269. <Button
  270. onClick={onCancel}
  271. className='mr-2'
  272. >
  273. {t('common.operation.cancel')}
  274. </Button>
  275. <Button
  276. variant='primary'
  277. disabled={loading}
  278. onClick={handleSave}
  279. >
  280. {t('common.operation.save')}
  281. </Button>
  282. </div>
  283. </div>
  284. )
  285. }
  286. export default SettingsModal