index.tsx 11 KB

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