You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 11KB

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