index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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: postRetrievalConfig,
  96. embedding_model: localeCurrentDataset.embedding_model,
  97. embedding_model_provider: localeCurrentDataset.embedding_model_provider,
  98. },
  99. })
  100. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  101. onSave({
  102. ...localeCurrentDataset,
  103. indexing_technique: indexMethod,
  104. retrieval_model_dict: postRetrievalConfig,
  105. })
  106. }
  107. catch (e) {
  108. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  109. }
  110. finally {
  111. setLoading(false)
  112. }
  113. }
  114. return (
  115. <div
  116. className='overflow-hidden w-full flex flex-col bg-white border-[0.5px] border-gray-200 rounded-xl shadow-xl'
  117. style={{
  118. height: 'calc(100vh - 72px)',
  119. }}
  120. ref={ref}
  121. >
  122. <div className='shrink-0 flex justify-between items-center pl-6 pr-5 h-14 border-b border-b-gray-100'>
  123. <div className='flex flex-col text-base font-semibold text-gray-900'>
  124. <div className='leading-6'>{t('datasetSettings.title')}</div>
  125. </div>
  126. <div className='flex items-center'>
  127. <div
  128. onClick={onCancel}
  129. className='flex justify-center items-center w-6 h-6 cursor-pointer'
  130. >
  131. <RiCloseLine className='w-4 h-4 text-gray-500' />
  132. </div>
  133. </div>
  134. </div>
  135. {/* Body */}
  136. <div className='p-6 pt-5 border-b overflow-y-auto pb-[68px]' style={{
  137. borderBottom: 'rgba(0, 0, 0, 0.05)',
  138. }}>
  139. <div className={cn(rowClass, 'items-center')}>
  140. <div className={labelClass}>
  141. {t('datasetSettings.form.name')}
  142. </div>
  143. <input
  144. value={localeCurrentDataset.name}
  145. onChange={e => handleValueChange('name', e.target.value)}
  146. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  147. placeholder={t('datasetSettings.form.namePlaceholder') || ''}
  148. />
  149. </div>
  150. <div className={cn(rowClass)}>
  151. <div className={labelClass}>
  152. {t('datasetSettings.form.desc')}
  153. </div>
  154. <div className='w-full'>
  155. <textarea
  156. value={localeCurrentDataset.description || ''}
  157. onChange={e => handleValueChange('description', e.target.value)}
  158. className='block px-3 py-2 w-full h-[88px] rounded-lg bg-gray-100 text-sm outline-none appearance-none resize-none'
  159. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  160. />
  161. <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'>
  162. <BookOpenIcon className='w-3 h-[18px] mr-1' />
  163. {t('datasetSettings.form.descWrite')}
  164. </a>
  165. </div>
  166. </div>
  167. <div className={rowClass}>
  168. <div className={labelClass}>
  169. <div>{t('datasetSettings.form.permissions')}</div>
  170. </div>
  171. <div className='w-full'>
  172. <PermissionsRadio
  173. disable={!localeCurrentDataset?.embedding_available}
  174. value={localeCurrentDataset.permission}
  175. onChange={v => handleValueChange('permission', v!)}
  176. itemClassName='sm:!w-[280px]'
  177. />
  178. </div>
  179. </div>
  180. <div className="w-full h-0 border-b-[0.5px] border-b-gray-200 my-2"></div>
  181. <div className={cn(rowClass)}>
  182. <div className={labelClass}>
  183. {t('datasetSettings.form.indexMethod')}
  184. </div>
  185. <div className='grow'>
  186. <IndexMethodRadio
  187. disable={!localeCurrentDataset?.embedding_available}
  188. value={indexMethod}
  189. onChange={v => setIndexMethod(v!)}
  190. itemClassName='sm:!w-[280px]'
  191. />
  192. </div>
  193. </div>
  194. {indexMethod === 'high_quality' && (
  195. <div className={cn(rowClass)}>
  196. <div className={labelClass}>
  197. {t('datasetSettings.form.embeddingModel')}
  198. </div>
  199. <div className='w-full'>
  200. <div className='w-full h-9 rounded-lg bg-gray-100 opacity-60'>
  201. <ModelSelector
  202. readonly
  203. defaultModel={{
  204. provider: localeCurrentDataset.embedding_model_provider,
  205. model: localeCurrentDataset.embedding_model,
  206. }}
  207. modelList={embeddingsModelList}
  208. />
  209. </div>
  210. <div className='mt-2 w-full text-xs leading-6 text-gray-500'>
  211. {t('datasetSettings.form.embeddingModelTip')}
  212. <span className='text-[#155eef] cursor-pointer' onClick={() => setShowAccountSettingModal({ payload: 'provider' })}>{t('datasetSettings.form.embeddingModelTipLink')}</span>
  213. </div>
  214. </div>
  215. </div>
  216. )}
  217. {/* Retrieval Method Config */}
  218. <div className={rowClass}>
  219. <div className={labelClass}>
  220. <div>
  221. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  222. <div className='leading-[18px] text-xs font-normal text-gray-500'>
  223. <a target='_blank' rel='noopener noreferrer' href='https://docs.dify.ai/features/retrieval-augment' className='text-[#155eef]'>{t('datasetSettings.form.retrievalSetting.learnMore')}</a>
  224. {t('datasetSettings.form.retrievalSetting.description')}
  225. </div>
  226. </div>
  227. </div>
  228. <div className='w-[480px]'>
  229. {indexMethod === 'high_quality'
  230. ? (
  231. <RetrievalMethodConfig
  232. value={retrievalConfig}
  233. onChange={setRetrievalConfig}
  234. />
  235. )
  236. : (
  237. <EconomicalRetrievalMethodConfig
  238. value={retrievalConfig}
  239. onChange={setRetrievalConfig}
  240. />
  241. )}
  242. </div>
  243. </div>
  244. </div>
  245. {isRetrievalChanged && !isHideChangedTip && (
  246. <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'>
  247. <div className='flex items-center'>
  248. <AlertTriangle className='mr-1 w-3 h-3 text-[#F79009]' />
  249. <div className='leading-[18px] text-xs font-medium text-gray-700'>{t('appDebug.datasetConfig.retrieveChangeTip')}</div>
  250. </div>
  251. <div className='p-1 cursor-pointer' onClick={(e) => {
  252. setIsHideChangedTip(true)
  253. e.stopPropagation()
  254. e.nativeEvent.stopImmediatePropagation()
  255. }}>
  256. <RiCloseLine className='w-4 h-4 text-gray-500 ' />
  257. </div>
  258. </div>
  259. )}
  260. <div
  261. className='sticky z-[5] bottom-0 w-full flex justify-end py-4 px-6 border-t bg-white '
  262. style={{
  263. borderColor: 'rgba(0, 0, 0, 0.05)',
  264. }}
  265. >
  266. <Button
  267. onClick={onCancel}
  268. className='mr-2 text-sm font-medium'
  269. >
  270. {t('common.operation.cancel')}
  271. </Button>
  272. <Button
  273. variant='primary'
  274. className='text-sm font-medium'
  275. disabled={loading}
  276. onClick={handleSave}
  277. >
  278. {t('common.operation.save')}
  279. </Button>
  280. </div>
  281. </div>
  282. )
  283. }
  284. export default SettingsModal