index.tsx 9.9 KB

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