index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React 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 { useDefaultModel } 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. value: RetrievalConfig
  23. onChange: (value: RetrievalConfig) => void
  24. }
  25. const RetrievalMethodConfig: FC<Props> = ({
  26. value: passValue,
  27. onChange,
  28. }) => {
  29. const { t } = useTranslation()
  30. const { supportRetrievalMethods } = useProviderContext()
  31. const { data: rerankDefaultModel } = useDefaultModel(ModelTypeEnum.rerank)
  32. const value = (() => {
  33. if (!passValue.reranking_model.reranking_model_name) {
  34. return {
  35. ...passValue,
  36. reranking_model: {
  37. reranking_provider_name: rerankDefaultModel?.provider.provider || '',
  38. reranking_model_name: rerankDefaultModel?.model || '',
  39. },
  40. reranking_mode: passValue.reranking_mode || (rerankDefaultModel ? RerankingModeEnum.RerankingModel : RerankingModeEnum.WeightedScore),
  41. weights: passValue.weights || {
  42. weight_type: WeightedScoreEnum.Customized,
  43. vector_setting: {
  44. vector_weight: DEFAULT_WEIGHTED_SCORE.other.semantic,
  45. embedding_provider_name: '',
  46. embedding_model_name: '',
  47. },
  48. keyword_setting: {
  49. keyword_weight: DEFAULT_WEIGHTED_SCORE.other.keyword,
  50. },
  51. },
  52. }
  53. }
  54. return passValue
  55. })()
  56. return (
  57. <div className='space-y-2'>
  58. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  59. <OptionCard icon={<Image className='w-4 h-4' src={retrievalIcon.vector} alt='' />}
  60. title={t('dataset.retrieval.semantic_search.title')}
  61. description={t('dataset.retrieval.semantic_search.description')}
  62. isActive={
  63. value.search_method === RETRIEVE_METHOD.semantic
  64. }
  65. onSwitched={() => onChange({
  66. ...value,
  67. search_method: RETRIEVE_METHOD.semantic,
  68. })}
  69. effectImg={Effect.src}
  70. activeHeaderClassName='bg-dataset-option-card-purple-gradient'
  71. >
  72. <RetrievalParamConfig
  73. type={RETRIEVE_METHOD.semantic}
  74. value={value}
  75. onChange={onChange}
  76. />
  77. </OptionCard>
  78. )}
  79. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  80. <OptionCard icon={<Image className='w-4 h-4' src={retrievalIcon.fullText} alt='' />}
  81. title={t('dataset.retrieval.full_text_search.title')}
  82. description={t('dataset.retrieval.full_text_search.description')}
  83. isActive={
  84. value.search_method === RETRIEVE_METHOD.fullText
  85. }
  86. onSwitched={() => onChange({
  87. ...value,
  88. search_method: RETRIEVE_METHOD.fullText,
  89. })}
  90. effectImg={Effect.src}
  91. activeHeaderClassName='bg-dataset-option-card-purple-gradient'
  92. >
  93. <RetrievalParamConfig
  94. type={RETRIEVE_METHOD.fullText}
  95. value={value}
  96. onChange={onChange}
  97. />
  98. </OptionCard>
  99. )}
  100. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  101. <OptionCard icon={<Image className='w-4 h-4' src={retrievalIcon.hybrid} alt='' />}
  102. title={
  103. <div className='flex items-center space-x-1'>
  104. <div>{t('dataset.retrieval.hybrid_search.title')}</div>
  105. <Badge text={t('dataset.retrieval.hybrid_search.recommend')!} className='border-text-accent-secondary text-text-accent-secondary ml-1 h-[18px]' uppercase />
  106. </div>
  107. }
  108. description={t('dataset.retrieval.hybrid_search.description')} isActive={
  109. value.search_method === RETRIEVE_METHOD.hybrid
  110. }
  111. onSwitched={() => onChange({
  112. ...value,
  113. search_method: RETRIEVE_METHOD.hybrid,
  114. reranking_enable: true,
  115. })}
  116. effectImg={Effect.src}
  117. activeHeaderClassName='bg-dataset-option-card-purple-gradient'
  118. >
  119. <RetrievalParamConfig
  120. type={RETRIEVE_METHOD.hybrid}
  121. value={value}
  122. onChange={onChange}
  123. />
  124. </OptionCard>
  125. )}
  126. </div>
  127. )
  128. }
  129. export default React.memo(RetrievalMethodConfig)