Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

7.5 KB
Raw Blame
'use client'
import type { FC } from 'react'
import type { RetrievalConfig } from '@/types/app'
import { RadioGroup } from '@langgenius/dify-ui/radio-group'
import { useQuery } from '@tanstack/react-query'
import * as React from 'react'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
import { DEFAULT_WEIGHTED_SCORE, RerankingModeEnum, WeightedScoreEnum } from '@/models/datasets'
import { consoleQuery } from '@/service/console'
import { RETRIEVE_METHOD } from '@/types/app'
import { EffectColor } from '../../settings/chunk-structure/types'
import OptionCard from '../../settings/option-card'
import RetrievalParamConfig from '../retrieval-param-config'

type Props = Readonly<{
  disabled?: boolean
  value: RetrievalConfig
  showMultiModalTip?: boolean
  onChange: (value: RetrievalConfig) => void
}>

const RetrievalMethodConfig: FC<Props> = ({
  disabled = false,
  value,
  showMultiModalTip = false,
  onChange,
}) => {
  const { t } = useTranslation(['dataset', 'datasetSettings'])
  const { data: retrievalSetting } = useQuery(
    consoleQuery.datasets.retrievalSetting.get.queryOptions(),
  )
  const supportedRetrievalMethods = new Set(retrievalSetting?.retrieval_method ?? [])
  const { defaultModel: rerankDefaultModel, currentModel: isRerankDefaultModelValid } =
    useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.rerank)

  const onSwitch = useCallback(
    (retrieveMethod: RETRIEVE_METHOD) => {
      if ([RETRIEVE_METHOD.semantic, RETRIEVE_METHOD.fullText].includes(retrieveMethod)) {
        onChange({
          ...value,
          search_method: retrieveMethod,
          ...(!value.reranking_model.reranking_model_name ||
          !value.reranking_model.reranking_provider_name
            ? {
                reranking_model: {
                  reranking_provider_name: isRerankDefaultModelValid
                    ? (rerankDefaultModel?.provider?.provider ?? '')
                    : '',
                  reranking_model_name: isRerankDefaultModelValid
                    ? (rerankDefaultModel?.model ?? '')
                    : '',
                },
                reranking_enable: !!isRerankDefaultModelValid,
              }
            : {
                reranking_enable: true,
              }),
        })
      }
      if (retrieveMethod === RETRIEVE_METHOD.hybrid) {
        onChange({
          ...value,
          search_method: retrieveMethod,
          ...(!value.reranking_model.reranking_model_name ||
          !value.reranking_model.reranking_provider_name
            ? {
                reranking_model: {
                  reranking_provider_name: isRerankDefaultModelValid
                    ? (rerankDefaultModel?.provider?.provider ?? '')
                    : '',
                  reranking_model_name: isRerankDefaultModelValid
                    ? (rerankDefaultModel?.model ?? '')
                    : '',
                },
                reranking_enable: !!isRerankDefaultModelValid,
                reranking_mode: isRerankDefaultModelValid
                  ? RerankingModeEnum.RerankingModel
                  : RerankingModeEnum.WeightedScore,
              }
            : {
                reranking_enable: true,
                reranking_mode: RerankingModeEnum.RerankingModel,
              }),
          ...(!value.weights
            ? {
                weights: {
                  weight_type: WeightedScoreEnum.Customized,
                  vector_setting: {
                    vector_weight: DEFAULT_WEIGHTED_SCORE.other.semantic,
                    embedding_provider_name: '',
                    embedding_model_name: '',
                  },
                  keyword_setting: {
                    keyword_weight: DEFAULT_WEIGHTED_SCORE.other.keyword,
                  },
                },
              }
            : {}),
        })
      }
    },
    [value, rerankDefaultModel, isRerankDefaultModelValid, onChange],
  )

  return (
    <RadioGroup<RETRIEVE_METHOD>
      aria-label={t(($) => $['form.retrievalSetting.method'], { ns: 'datasetSettings' })}
      value={value.search_method}
      onValueChange={onSwitch}
      disabled={disabled}
      className="flex flex-col items-stretch gap-x-0 gap-y-2"
    >
      {supportedRetrievalMethods.has(RETRIEVE_METHOD.semantic) && (
        <OptionCard
          id={RETRIEVE_METHOD.semantic}
          disabled={disabled}
          icon={<span aria-hidden className="i-custom-vender-knowledge-vector-search size-4" />}
          iconActiveColor="text-util-colors-purple-purple-600"
          title={t(($) => $['retrieval.semantic_search.title'], { ns: 'dataset' })}
          description={t(($) => $['retrieval.semantic_search.description'], { ns: 'dataset' })}
          isActive={value.search_method === RETRIEVE_METHOD.semantic}
          effectColor={EffectColor.purple}
          showEffectColor
          showChildren={value.search_method === RETRIEVE_METHOD.semantic}
          className="gap-x-2"
        >
          <RetrievalParamConfig
            type={RETRIEVE_METHOD.semantic}
            value={value}
            onChange={onChange}
            showMultiModalTip={showMultiModalTip}
            disabled={disabled}
          />
        </OptionCard>
      )}
      {supportedRetrievalMethods.has(RETRIEVE_METHOD.fullText) && (
        <OptionCard
          id={RETRIEVE_METHOD.fullText}
          disabled={disabled}
          icon={<span aria-hidden className="i-custom-vender-knowledge-full-text-search size-4" />}
          iconActiveColor="text-util-colors-purple-purple-600"
          title={t(($) => $['retrieval.full_text_search.title'], { ns: 'dataset' })}
          description={t(($) => $['retrieval.full_text_search.description'], { ns: 'dataset' })}
          isActive={value.search_method === RETRIEVE_METHOD.fullText}
          effectColor={EffectColor.purple}
          showEffectColor
          showChildren={value.search_method === RETRIEVE_METHOD.fullText}
          className="gap-x-2"
        >
          <RetrievalParamConfig
            type={RETRIEVE_METHOD.fullText}
            value={value}
            onChange={onChange}
            showMultiModalTip={showMultiModalTip}
            disabled={disabled}
          />
        </OptionCard>
      )}
      {supportedRetrievalMethods.has(RETRIEVE_METHOD.hybrid) && (
        <OptionCard
          id={RETRIEVE_METHOD.hybrid}
          disabled={disabled}
          icon={<span aria-hidden className="i-custom-vender-knowledge-hybrid-search size-4" />}
          iconActiveColor="text-util-colors-purple-purple-600"
          title={t(($) => $['retrieval.hybrid_search.title'], { ns: 'dataset' })}
          description={t(($) => $['retrieval.hybrid_search.description'], { ns: 'dataset' })}
          isActive={value.search_method === RETRIEVE_METHOD.hybrid}
          effectColor={EffectColor.purple}
          showEffectColor
          isRecommended
          showChildren={value.search_method === RETRIEVE_METHOD.hybrid}
          className="gap-x-2"
        >
          <RetrievalParamConfig
            type={RETRIEVE_METHOD.hybrid}
            value={value}
            onChange={onChange}
            showMultiModalTip={showMultiModalTip}
            disabled={disabled}
          />
        </OptionCard>
      )}
    </RadioGroup>
  )
}
export default React.memo(RetrievalMethodConfig)