index.tsx 6.1 KB

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