index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React 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 RadioCard from '@/app/components/base/radio-card'
  9. import { PatternRecognition, Semantic } from '@/app/components/base/icons/src/vender/solid/development'
  10. import { FileSearch02 } from '@/app/components/base/icons/src/vender/solid/files'
  11. import { useProviderContext } from '@/context/provider-context'
  12. import { useDefaultModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  13. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  14. import {
  15. DEFAULT_WEIGHTED_SCORE,
  16. RerankingModeEnum,
  17. WeightedScoreEnum,
  18. } from '@/models/datasets'
  19. type Props = {
  20. value: RetrievalConfig
  21. onChange: (value: RetrievalConfig) => void
  22. }
  23. const RetrievalMethodConfig: FC<Props> = ({
  24. value: passValue,
  25. onChange,
  26. }) => {
  27. const { t } = useTranslation()
  28. const { supportRetrievalMethods } = useProviderContext()
  29. const { data: rerankDefaultModel } = useDefaultModel(ModelTypeEnum.rerank)
  30. const value = (() => {
  31. if (!passValue.reranking_model.reranking_model_name) {
  32. return {
  33. ...passValue,
  34. reranking_model: {
  35. reranking_provider_name: rerankDefaultModel?.provider.provider || '',
  36. reranking_model_name: rerankDefaultModel?.model || '',
  37. },
  38. reranking_mode: passValue.reranking_mode || (rerankDefaultModel ? RerankingModeEnum.RerankingModel : RerankingModeEnum.WeightedScore),
  39. weights: passValue.weights || {
  40. weight_type: WeightedScoreEnum.Customized,
  41. vector_setting: {
  42. vector_weight: DEFAULT_WEIGHTED_SCORE.other.semantic,
  43. embedding_provider_name: '',
  44. embedding_model_name: '',
  45. },
  46. keyword_setting: {
  47. keyword_weight: DEFAULT_WEIGHTED_SCORE.other.keyword,
  48. },
  49. },
  50. }
  51. }
  52. return passValue
  53. })()
  54. return (
  55. <div className='space-y-2'>
  56. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  57. <RadioCard
  58. icon={<Semantic className='w-4 h-4 text-[#7839EE]' />}
  59. title={t('dataset.retrieval.semantic_search.title')}
  60. description={t('dataset.retrieval.semantic_search.description')}
  61. isChosen={value.search_method === RETRIEVE_METHOD.semantic}
  62. onChosen={() => onChange({
  63. ...value,
  64. search_method: RETRIEVE_METHOD.semantic,
  65. })}
  66. chosenConfig={
  67. <RetrievalParamConfig
  68. type={RETRIEVE_METHOD.semantic}
  69. value={value}
  70. onChange={onChange}
  71. />
  72. }
  73. />
  74. )}
  75. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  76. <RadioCard
  77. icon={<FileSearch02 className='w-4 h-4 text-[#7839EE]' />}
  78. title={t('dataset.retrieval.full_text_search.title')}
  79. description={t('dataset.retrieval.full_text_search.description')}
  80. isChosen={value.search_method === RETRIEVE_METHOD.fullText}
  81. onChosen={() => onChange({
  82. ...value,
  83. search_method: RETRIEVE_METHOD.fullText,
  84. })}
  85. chosenConfig={
  86. <RetrievalParamConfig
  87. type={RETRIEVE_METHOD.fullText}
  88. value={value}
  89. onChange={onChange}
  90. />
  91. }
  92. />
  93. )}
  94. {supportRetrievalMethods.includes(RETRIEVE_METHOD.semantic) && (
  95. <RadioCard
  96. icon={<PatternRecognition className='w-4 h-4 text-[#7839EE]' />}
  97. title={
  98. <div className='flex items-center space-x-1'>
  99. <div>{t('dataset.retrieval.hybrid_search.title')}</div>
  100. <div className='flex h-full items-center px-1.5 rounded-md border border-[#E0EAFF] text-xs font-medium text-[#444CE7]'>{t('dataset.retrieval.hybrid_search.recommend')}</div>
  101. </div>
  102. }
  103. description={t('dataset.retrieval.hybrid_search.description')}
  104. isChosen={value.search_method === RETRIEVE_METHOD.hybrid}
  105. onChosen={() => onChange({
  106. ...value,
  107. search_method: RETRIEVE_METHOD.hybrid,
  108. reranking_enable: true,
  109. })}
  110. chosenConfig={
  111. <RetrievalParamConfig
  112. type={RETRIEVE_METHOD.hybrid}
  113. value={value}
  114. onChange={onChange}
  115. />
  116. }
  117. />
  118. )}
  119. </div>
  120. )
  121. }
  122. export default React.memo(RetrievalMethodConfig)