您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import RetrievalParamConfig from '../retrieval-param-config'
  6. import type { RetrievalConfig } from '@/types/app'
  7. import { RETRIEVE_METHOD } from '@/types/app'
  8. import { useProviderContext } from '@/context/provider-context'
  9. import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  10. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  11. import {
  12. DEFAULT_WEIGHTED_SCORE,
  13. RerankingModeEnum,
  14. WeightedScoreEnum,
  15. } from '@/models/datasets'
  16. import OptionCard from '../../settings/option-card'
  17. import { FullTextSearch, HybridSearch, VectorSearch } from '@/app/components/base/icons/src/vender/knowledge'
  18. import { EffectColor } from '../../settings/chunk-structure/types'
  19. type Props = {
  20. disabled?: boolean
  21. value: RetrievalConfig
  22. onChange: (value: RetrievalConfig) => void
  23. }
  24. const RetrievalMethodConfig: FC<Props> = ({
  25. disabled = false,
  26. value,
  27. onChange,
  28. }) => {
  29. const { t } = useTranslation()
  30. const { supportRetrievalMethods } = useProviderContext()
  31. const {
  32. defaultModel: rerankDefaultModel,
  33. currentModel: isRerankDefaultModelValid,
  34. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.rerank)
  35. const onSwitch = useCallback((retrieveMethod: RETRIEVE_METHOD) => {
  36. if ([RETRIEVE_METHOD.semantic, RETRIEVE_METHOD.fullText].includes(retrieveMethod)) {
  37. onChange({
  38. ...value,
  39. search_method: retrieveMethod,
  40. ...((!value.reranking_model.reranking_model_name || !value.reranking_model.reranking_provider_name)
  41. ? {
  42. reranking_model: {
  43. reranking_provider_name: isRerankDefaultModelValid ? rerankDefaultModel?.provider?.provider ?? '' : '',
  44. reranking_model_name: isRerankDefaultModelValid ? rerankDefaultModel?.model ?? '' : '',
  45. },
  46. reranking_enable: !!isRerankDefaultModelValid,
  47. }
  48. : {
  49. reranking_enable: true,
  50. }),
  51. })
  52. }
  53. if (retrieveMethod === RETRIEVE_METHOD.hybrid) {
  54. onChange({
  55. ...value,
  56. search_method: retrieveMethod,
  57. ...((!value.reranking_model.reranking_model_name || !value.reranking_model.reranking_provider_name)
  58. ? {
  59. reranking_model: {
  60. reranking_provider_name: isRerankDefaultModelValid ? rerankDefaultModel?.provider?.provider ?? '' : '',
  61. reranking_model_name: isRerankDefaultModelValid ? rerankDefaultModel?.model ?? '' : '',
  62. },
  63. reranking_enable: !!isRerankDefaultModelValid,
  64. reranking_mode: isRerankDefaultModelValid ? RerankingModeEnum.RerankingModel : RerankingModeEnum.WeightedScore,
  65. }
  66. : {
  67. reranking_enable: true,
  68. reranking_mode: RerankingModeEnum.RerankingModel,
  69. }),
  70. ...(!value.weights
  71. ? {
  72. weights: {
  73. weight_type: WeightedScoreEnum.Customized,
  74. vector_setting: {
  75. vector_weight: DEFAULT_WEIGHTED_SCORE.other.semantic,
  76. embedding_provider_name: '',
  77. embedding_model_name: '',
  78. },
  79. keyword_setting: {
  80. keyword_weight: DEFAULT_WEIGHTED_SCORE.other.keyword,
  81. },
  82. },
  83. }
  84. : {}),
  85. })
  86. }
  87. }, [value, rerankDefaultModel, isRerankDefaultModelValid, onChange])
  88. return (
  89. <div className='flex flex-col gap-y-2'>
  90. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  91. <OptionCard
  92. id={RETRIEVE_METHOD.semantic}
  93. disabled={disabled}
  94. icon={<VectorSearch className='size-4' />}
  95. iconActiveColor='text-util-colors-purple-purple-600'
  96. title={t('dataset.retrieval.semantic_search.title')}
  97. description={t('dataset.retrieval.semantic_search.description')}
  98. isActive={value.search_method === RETRIEVE_METHOD.semantic}
  99. onClick={onSwitch}
  100. effectColor={EffectColor.purple}
  101. showEffectColor
  102. showChildren={value.search_method === RETRIEVE_METHOD.semantic}
  103. className='gap-x-2'
  104. >
  105. <RetrievalParamConfig
  106. type={RETRIEVE_METHOD.semantic}
  107. value={value}
  108. onChange={onChange}
  109. />
  110. </OptionCard>
  111. )}
  112. {supportRetrievalMethods.includes(RETRIEVE_METHOD.fullText) && (
  113. <OptionCard
  114. id={RETRIEVE_METHOD.fullText}
  115. disabled={disabled}
  116. icon={<FullTextSearch className='size-4' />}
  117. iconActiveColor='text-util-colors-purple-purple-600'
  118. title={t('dataset.retrieval.full_text_search.title')}
  119. description={t('dataset.retrieval.full_text_search.description')}
  120. isActive={value.search_method === RETRIEVE_METHOD.fullText}
  121. onClick={onSwitch}
  122. effectColor={EffectColor.purple}
  123. showEffectColor
  124. showChildren={value.search_method === RETRIEVE_METHOD.fullText}
  125. className='gap-x-2'
  126. >
  127. <RetrievalParamConfig
  128. type={RETRIEVE_METHOD.fullText}
  129. value={value}
  130. onChange={onChange}
  131. />
  132. </OptionCard>
  133. )}
  134. {supportRetrievalMethods.includes(RETRIEVE_METHOD.hybrid) && (
  135. <OptionCard
  136. id={RETRIEVE_METHOD.hybrid}
  137. disabled={disabled}
  138. icon={<HybridSearch className='size-4' />}
  139. iconActiveColor='text-util-colors-purple-purple-600'
  140. title={t('dataset.retrieval.hybrid_search.title')}
  141. description={t('dataset.retrieval.hybrid_search.description')}
  142. isActive={value.search_method === RETRIEVE_METHOD.hybrid}
  143. onClick={onSwitch}
  144. effectColor={EffectColor.purple}
  145. showEffectColor
  146. isRecommended
  147. showChildren={value.search_method === RETRIEVE_METHOD.hybrid}
  148. className='gap-x-2'
  149. >
  150. <RetrievalParamConfig
  151. type={RETRIEVE_METHOD.hybrid}
  152. value={value}
  153. onChange={onChange}
  154. />
  155. </OptionCard>
  156. )}
  157. </div>
  158. )
  159. }
  160. export default React.memo(RetrievalMethodConfig)