index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import type { Dispatch } from 'react'
  4. import { useContext } from 'use-context-selector'
  5. import { BookOpenIcon } from '@heroicons/react/24/outline'
  6. import { useTranslation } from 'react-i18next'
  7. import cn from 'classnames'
  8. import { useSWRConfig } from 'swr'
  9. import { unstable_serialize } from 'swr/infinite'
  10. import PermissionsRadio from '../permissions-radio'
  11. import IndexMethodRadio from '../index-method-radio'
  12. import RetrievalMethodConfig from '@/app/components/datasets/common/retrieval-method-config'
  13. import EconomicalRetrievalMethodConfig from '@/app/components/datasets/common/economical-retrieval-method-config'
  14. import { ToastContext } from '@/app/components/base/toast'
  15. import Button from '@/app/components/base/button'
  16. import { updateDatasetSetting } from '@/service/datasets'
  17. import type { DataSet, DataSetListResponse } from '@/models/datasets'
  18. import ModelSelector from '@/app/components/header/account-setting/model-page/model-selector'
  19. import type { ProviderEnum } from '@/app/components/header/account-setting/model-page/declarations'
  20. import { ModelType } from '@/app/components/header/account-setting/model-page/declarations'
  21. import DatasetDetailContext from '@/context/dataset-detail'
  22. import { type RetrievalConfig } from '@/types/app'
  23. import { useModalContext } from '@/context/modal-context'
  24. import { useProviderContext } from '@/context/provider-context'
  25. import { ensureRerankModelSelected, isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
  26. const rowClass = `
  27. flex justify-between py-4
  28. `
  29. const labelClass = `
  30. flex items-center w-[168px] h-9
  31. `
  32. const inputClass = `
  33. w-[480px] px-3 bg-gray-100 text-sm text-gray-800 rounded-lg outline-none appearance-none
  34. `
  35. const useInitialValue: <T>(depend: T, dispatch: Dispatch<T>) => void = (depend, dispatch) => {
  36. useEffect(() => {
  37. dispatch(depend)
  38. }, [depend])
  39. }
  40. const getKey = (pageIndex: number, previousPageData: DataSetListResponse) => {
  41. if (!pageIndex || previousPageData.has_more)
  42. return { url: 'datasets', params: { page: pageIndex + 1, limit: 30 } }
  43. return null
  44. }
  45. const Form = () => {
  46. const { t } = useTranslation()
  47. const { notify } = useContext(ToastContext)
  48. const { mutate } = useSWRConfig()
  49. const { dataset: currentDataset, mutateDatasetRes: mutateDatasets } = useContext(DatasetDetailContext)
  50. const { setShowAccountSettingModal } = useModalContext()
  51. const [loading, setLoading] = useState(false)
  52. const [name, setName] = useState(currentDataset?.name ?? '')
  53. const [description, setDescription] = useState(currentDataset?.description ?? '')
  54. const [permission, setPermission] = useState(currentDataset?.permission)
  55. const [indexMethod, setIndexMethod] = useState(currentDataset?.indexing_technique)
  56. const [retrievalConfig, setRetrievalConfig] = useState(currentDataset?.retrieval_model_dict as RetrievalConfig)
  57. const {
  58. rerankDefaultModel,
  59. isRerankDefaultModelVaild,
  60. rerankModelList,
  61. } = useProviderContext()
  62. const handleSave = async () => {
  63. if (loading)
  64. return
  65. if (!name?.trim()) {
  66. notify({ type: 'error', message: t('datasetSettings.form.nameError') })
  67. return
  68. }
  69. if (
  70. !isReRankModelSelected({
  71. rerankDefaultModel,
  72. isRerankDefaultModelVaild,
  73. rerankModelList,
  74. retrievalConfig,
  75. indexMethod,
  76. })
  77. ) {
  78. notify({ type: 'error', message: t('appDebug.datasetConfig.rerankModelRequired') })
  79. return
  80. }
  81. const postRetrievalConfig = ensureRerankModelSelected({
  82. rerankDefaultModel: rerankDefaultModel!,
  83. retrievalConfig,
  84. indexMethod,
  85. })
  86. try {
  87. setLoading(true)
  88. await updateDatasetSetting({
  89. datasetId: currentDataset!.id,
  90. body: {
  91. name,
  92. description,
  93. permission,
  94. indexing_technique: indexMethod,
  95. retrieval_model: postRetrievalConfig,
  96. },
  97. })
  98. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  99. if (mutateDatasets) {
  100. await mutateDatasets()
  101. mutate(unstable_serialize(getKey))
  102. }
  103. }
  104. catch (e) {
  105. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  106. }
  107. finally {
  108. setLoading(false)
  109. }
  110. }
  111. useInitialValue<string>(currentDataset?.name ?? '', setName)
  112. useInitialValue<string>(currentDataset?.description ?? '', setDescription)
  113. useInitialValue<DataSet['permission'] | undefined>(currentDataset?.permission, setPermission)
  114. useInitialValue<DataSet['indexing_technique'] | undefined>(currentDataset?.indexing_technique, setIndexMethod)
  115. return (
  116. <div className='w-[800px] px-16 py-6'>
  117. <div className={rowClass}>
  118. <div className={labelClass}>
  119. <div>{t('datasetSettings.form.name')}</div>
  120. </div>
  121. <input
  122. disabled={!currentDataset?.embedding_available}
  123. className={cn(inputClass, !currentDataset?.embedding_available && 'opacity-60')}
  124. value={name}
  125. onChange={e => setName(e.target.value)}
  126. />
  127. </div>
  128. <div className={rowClass}>
  129. <div className={labelClass}>
  130. <div>{t('datasetSettings.form.desc')}</div>
  131. </div>
  132. <div>
  133. <textarea
  134. disabled={!currentDataset?.embedding_available}
  135. className={cn(`${inputClass} block mb-2 h-[120px] py-2 resize-none`, !currentDataset?.embedding_available && 'opacity-60')}
  136. placeholder={t('datasetSettings.form.descPlaceholder') || ''}
  137. value={description}
  138. onChange={e => setDescription(e.target.value)}
  139. />
  140. <a className='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'>
  141. <BookOpenIcon className='w-3 h-[18px] mr-1' />
  142. {t('datasetSettings.form.descWrite')}
  143. </a>
  144. </div>
  145. </div>
  146. <div className={rowClass}>
  147. <div className={labelClass}>
  148. <div>{t('datasetSettings.form.permissions')}</div>
  149. </div>
  150. <div className='w-[480px]'>
  151. <PermissionsRadio
  152. disable={!currentDataset?.embedding_available}
  153. value={permission}
  154. onChange={v => setPermission(v)}
  155. />
  156. </div>
  157. </div>
  158. {currentDataset && currentDataset.indexing_technique && (
  159. <>
  160. <div className='w-full h-0 border-b-[0.5px] border-b-gray-200 my-2' />
  161. <div className={rowClass}>
  162. <div className={labelClass}>
  163. <div>{t('datasetSettings.form.indexMethod')}</div>
  164. </div>
  165. <div className='w-[480px]'>
  166. <IndexMethodRadio
  167. disable={!currentDataset?.embedding_available}
  168. value={indexMethod}
  169. onChange={v => setIndexMethod(v)}
  170. />
  171. </div>
  172. </div>
  173. </>
  174. )}
  175. {currentDataset && currentDataset.indexing_technique === 'high_quality' && (
  176. <div className={rowClass}>
  177. <div className={labelClass}>
  178. <div>{t('datasetSettings.form.embeddingModel')}</div>
  179. </div>
  180. <div className='w-[480px]'>
  181. <div className='w-full h-9 rounded-lg bg-gray-100 opacity-60'>
  182. <ModelSelector
  183. readonly
  184. value={{
  185. providerName: currentDataset.embedding_model_provider as ProviderEnum,
  186. modelName: currentDataset.embedding_model,
  187. }}
  188. modelType={ModelType.embeddings}
  189. onChange={() => {}}
  190. />
  191. </div>
  192. <div className='mt-2 w-full text-xs leading-6 text-gray-500'>
  193. {t('datasetSettings.form.embeddingModelTip')}
  194. <span className='text-[#155eef] cursor-pointer' onClick={() => setShowAccountSettingModal({ payload: 'provider' })}>{t('datasetSettings.form.embeddingModelTipLink')}</span>
  195. </div>
  196. </div>
  197. </div>
  198. )}
  199. {/* Retrieval Method Config */}
  200. <div className={rowClass}>
  201. <div className={labelClass}>
  202. <div>
  203. <div>{t('datasetSettings.form.retrievalSetting.title')}</div>
  204. <div className='leading-[18px] text-xs font-normal text-gray-500'>
  205. <a target='_blank' href='https://docs.dify.ai/advanced/retrieval-augment' className='text-[#155eef]'>{t('datasetSettings.form.retrievalSetting.learnMore')}</a>
  206. {t('datasetSettings.form.retrievalSetting.description')}
  207. </div>
  208. </div>
  209. </div>
  210. <div className='w-[480px]'>
  211. {indexMethod === 'high_quality'
  212. ? (
  213. <RetrievalMethodConfig
  214. value={retrievalConfig}
  215. onChange={setRetrievalConfig}
  216. />
  217. )
  218. : (
  219. <EconomicalRetrievalMethodConfig
  220. value={retrievalConfig}
  221. onChange={setRetrievalConfig}
  222. />
  223. )}
  224. </div>
  225. </div>
  226. {currentDataset?.embedding_available && (
  227. <div className={rowClass}>
  228. <div className={labelClass} />
  229. <div className='w-[480px]'>
  230. <Button
  231. className='min-w-24 text-sm'
  232. type='primary'
  233. onClick={handleSave}
  234. >
  235. {t('datasetSettings.form.save')}
  236. </Button>
  237. </div>
  238. </div>
  239. )}
  240. </div>
  241. )
  242. }
  243. export default Form