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.

fix(web): persist reranking_enable for Hybrid Search datasets (#42563)

Neo1Noira7b1151a2cb8
diff --git a/web/app/components/app/configuration/dataset-config/settings-modal/__tests__/index.spec.tsx b/web/app/components/app/configuration/dataset-config/settings-modal/__tests__/index.spec.tsx
index 9260952c107..003ae8c6535 100644
--- a/web/app/components/app/configuration/dataset-config/settings-modal/__tests__/index.spec.tsx
+++ b/web/app/components/app/configuration/dataset-config/settings-modal/__tests__/index.spec.tsx
@@ -509,6 +509,87 @@ describe('SettingsModal', () => {
)
})
+ it('should persist reranking_enable=true for hybrid search with a rerank model and a stale false flag', async () => {
+ // Arrange
+ const user = userEvent.setup()
+ const hybridRetrieval = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: {
+ reranking_provider_name: 'rerank-provider',
+ reranking_model_name: 'rerank-model',
+ },
+ })
+ const dataset = createDataset({
+ retrieval_model: hybridRetrieval,
+ retrieval_model_dict: hybridRetrieval,
+ })
+
+ // Act
+ await renderSettingsModal(dataset)
+ await user.click(screen.getByRole('button', { name: 'common.operation.save' }))
+
+ // Assert
+ await waitFor(() => expect(mockUpdateDatasetSetting).toHaveBeenCalled())
+
+ expect(mockUpdateDatasetSetting).toHaveBeenCalledWith(
+ expect.objectContaining({
+ body: expect.objectContaining({
+ retrieval_model: expect.objectContaining({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: true,
+ }),
+ }),
+ }),
+ )
+ })
+
+ it('should keep reranking_enable=false for hybrid search using weighted score', async () => {
+ // Arrange
+ const user = userEvent.setup()
+ const hybridRetrieval = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.WeightedScore,
+ reranking_model: {
+ reranking_provider_name: 'rerank-provider',
+ reranking_model_name: 'rerank-model',
+ },
+ weights: {
+ vector_setting: {
+ vector_weight: 0.7,
+ embedding_provider_name: 'embed-provider',
+ embedding_model_name: 'embed-model',
+ },
+ keyword_setting: {
+ keyword_weight: 0.3,
+ },
+ },
+ } as Partial<RetrievalConfig>)
+ const dataset = createDataset({
+ retrieval_model: hybridRetrieval,
+ retrieval_model_dict: hybridRetrieval,
+ })
+
+ // Act
+ await renderSettingsModal(dataset)
+ await user.click(screen.getByRole('button', { name: 'common.operation.save' }))
+
+ // Assert
+ await waitFor(() => expect(mockUpdateDatasetSetting).toHaveBeenCalled())
+
+ expect(mockUpdateDatasetSetting).toHaveBeenCalledWith(
+ expect.objectContaining({
+ body: expect.objectContaining({
+ retrieval_model: expect.objectContaining({
+ reranking_enable: false,
+ }),
+ }),
+ }),
+ )
+ })
+
it('should save external dataset changes when partial members configured', async () => {
// Arrange
const user = userEvent.setup()
diff --git a/web/app/components/app/configuration/dataset-config/settings-modal/index.tsx b/web/app/components/app/configuration/dataset-config/settings-modal/index.tsx
index c20fe6381af..8a6efa298b3 100644
--- a/web/app/components/app/configuration/dataset-config/settings-modal/index.tsx
+++ b/web/app/components/app/configuration/dataset-config/settings-modal/index.tsx
@@ -14,7 +14,10 @@ import { useQueryState } from 'nuqs'
import { useEffect, useId, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/components/app/configuration/toast'
-import { isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
+import {
+ isReRankModelSelected,
+ normalizeRetrievalConfigForSave,
+} from '@/app/components/datasets/common/check-rerank-model'
import { IndexingType } from '@/app/components/datasets/create/step-two'
import IndexMethod from '@/app/components/datasets/settings/index-method'
import PermissionSelector from '@/app/components/datasets/settings/permission-selector'
@@ -139,6 +142,9 @@ const SettingsModal: FC<SettingsModalProps> = ({
try {
setLoading(true)
const { id, name, description, permission } = localeCurrentDataset
+ // Hybrid Search renders no rerank on/off switch, so derive `reranking_enable` from the
+ // selected rerank model on save. See `normalizeRetrievalConfigForSave` for details.
+ const retrievalConfigForSave = normalizeRetrievalConfigForSave(retrievalConfig)
const requestParams = {
datasetId: id,
body: {
@@ -148,9 +154,9 @@ const SettingsModal: FC<SettingsModalProps> = ({
indexing_technique: indexMethod,
keyword_number: keywordNumber,
retrieval_model: {
- ...retrievalConfig,
- score_threshold: retrievalConfig.score_threshold_enabled
- ? retrievalConfig.score_threshold
+ ...retrievalConfigForSave,
+ score_threshold: retrievalConfigForSave.score_threshold_enabled
+ ? retrievalConfigForSave.score_threshold
: 0,
},
embedding_model: localeCurrentDataset.embedding_model,
diff --git a/web/app/components/datasets/common/__tests__/check-rerank-model.spec.ts b/web/app/components/datasets/common/__tests__/check-rerank-model.spec.ts
index 8ed9980b9e2..74b1e3443db 100644
--- a/web/app/components/datasets/common/__tests__/check-rerank-model.spec.ts
+++ b/web/app/components/datasets/common/__tests__/check-rerank-model.spec.ts
@@ -11,7 +11,7 @@ import {
} from '@/app/components/header/account-setting/model-provider-page/declarations'
import { RerankingModeEnum } from '@/models/datasets'
import { RETRIEVE_METHOD } from '@/types/app'
-import { isReRankModelSelected } from '../check-rerank-model'
+import { isReRankModelSelected, normalizeRetrievalConfigForSave } from '../check-rerank-model'
// Test data factory
const createRetrievalConfig = (overrides: Partial<RetrievalConfig> = {}): RetrievalConfig => ({
@@ -255,4 +255,107 @@ describe('check-rerank-model', () => {
})
})
})
+
+ describe('normalizeRetrievalConfigForSave', () => {
+ const rerankModel = {
+ reranking_provider_name: 'cohere',
+ reranking_model_name: 'rerank-english-v2.0',
+ }
+
+ it('should force reranking_enable to true for hybrid search with a rerank model and a stale false flag', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: rerankModel,
+ })
+
+ const result = normalizeRetrievalConfigForSave(config)
+
+ expect(result.reranking_enable).toBe(true)
+ })
+
+ it('should not mutate the original config', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: rerankModel,
+ })
+
+ normalizeRetrievalConfigForSave(config)
+
+ expect(config.reranking_enable).toBe(false)
+ })
+
+ it('should leave a weighted-score hybrid config untouched', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.WeightedScore,
+ reranking_model: rerankModel,
+ })
+
+ const result = normalizeRetrievalConfigForSave(config)
+
+ expect(result).toBe(config)
+ expect(result.reranking_enable).toBe(false)
+ })
+
+ it('should leave semantic search untouched', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.semantic,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: rerankModel,
+ })
+
+ const result = normalizeRetrievalConfigForSave(config)
+
+ expect(result).toBe(config)
+ })
+
+ it('should leave fullText search untouched', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.fullText,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: rerankModel,
+ })
+
+ const result = normalizeRetrievalConfigForSave(config)
+
+ expect(result).toBe(config)
+ })
+
+ it('should leave hybrid search without a complete rerank model untouched', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: false,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: {
+ reranking_provider_name: '',
+ reranking_model_name: '',
+ },
+ })
+
+ const result = normalizeRetrievalConfigForSave(config)
+
+ expect(result).toBe(config)
+ expect(result.reranking_enable).toBe(false)
+ })
+
+ it('should keep reranking_enable true when it is already set', () => {
+ const config = createRetrievalConfig({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: true,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: rerankModel,
+ })
+
+ const result = normalizeRetrievalConfigForSave(config)
+
+ expect(result.reranking_enable).toBe(true)
+ })
+ })
})
diff --git a/web/app/components/datasets/common/check-rerank-model.ts b/web/app/components/datasets/common/check-rerank-model.ts
index e5271c1bd10..97b651e02be 100644
--- a/web/app/components/datasets/common/check-rerank-model.ts
+++ b/web/app/components/datasets/common/check-rerank-model.ts
@@ -3,6 +3,31 @@ import type { RetrievalConfig } from '@/types/app'
import { RerankingModeEnum } from '@/models/datasets'
import { RETRIEVE_METHOD } from '@/types/app'
+/**
+ * Hybrid Search renders no rerank on/off switch, so `reranking_enable` is only ever written when
+ * the retrieval method is switched. A dataset configured as "open -> pick rerank model -> save"
+ * therefore keeps the stale `false` default and silently never reranks, even though the selected
+ * model is displayed in the UI. In Hybrid Search a chosen rerank model is exactly what
+ * `isReRankModelSelected` already validates, so derive the flag from the selection on save.
+ */
+export const normalizeRetrievalConfigForSave = (
+ retrievalConfig: RetrievalConfig,
+): RetrievalConfig => {
+ if (
+ retrievalConfig.search_method === RETRIEVE_METHOD.hybrid &&
+ retrievalConfig.reranking_mode === RerankingModeEnum.RerankingModel &&
+ retrievalConfig.reranking_model?.reranking_provider_name &&
+ retrievalConfig.reranking_model?.reranking_model_name
+ ) {
+ return {
+ ...retrievalConfig,
+ reranking_enable: true,
+ }
+ }
+
+ return retrievalConfig
+}
+
export const isReRankModelSelected = ({
retrievalConfig,
rerankModelList,
diff --git a/web/app/components/datasets/settings/form/__tests__/index.spec.tsx b/web/app/components/datasets/settings/form/__tests__/index.spec.tsx
index a8f6e484d02..a0cbf856677 100644
--- a/web/app/components/datasets/settings/form/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/settings/form/__tests__/index.spec.tsx
@@ -204,7 +204,8 @@ vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', ()
useModelModalHandler: () => vi.fn(),
}))
-vi.mock('@/app/components/datasets/common/check-rerank-model', () => ({
+vi.mock('@/app/components/datasets/common/check-rerank-model', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/app/components/datasets/common/check-rerank-model')>()),
isReRankModelSelected: () => true,
}))
diff --git a/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts b/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts
index 53ccd46eeaf..cc26c0b116f 100644
--- a/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts
+++ b/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts
@@ -7,6 +7,7 @@ import {
ChunkingMode,
DatasetPermission,
DataSourceType,
+ RerankingModeEnum,
WeightedScoreEnum,
} from '@/models/datasets'
import { createAccountProfileQueryWrapper } from '@/test/console/account-profile'
@@ -178,7 +179,8 @@ vi.mock('@/service/use-common', () => ({
}),
}))
-vi.mock('@/app/components/datasets/common/check-rerank-model', () => ({
+vi.mock('@/app/components/datasets/common/check-rerank-model', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/app/components/datasets/common/check-rerank-model')>()),
isReRankModelSelected: () => true,
}))
@@ -759,6 +761,126 @@ describe('useFormState', () => {
}),
})
})
+
+ it('should send the configured score threshold when score_threshold_enabled is true', async () => {
+ const { updateDatasetSetting } = await import('@/service/datasets')
+ const { result } = renderHook(() => useFormState())
+
+ act(() => {
+ result.current.setRetrievalConfig({
+ ...result.current.retrievalConfig,
+ score_threshold_enabled: true,
+ score_threshold: 0.62,
+ })
+ })
+
+ await act(async () => {
+ await result.current.handleSave()
+ })
+
+ expect(updateDatasetSetting).toHaveBeenCalledWith({
+ datasetId: 'dataset-1',
+ body: expect.objectContaining({
+ retrieval_model: expect.objectContaining({
+ score_threshold_enabled: true,
+ score_threshold: 0.62,
+ }),
+ }),
+ })
+ })
+ })
+
+ describe('Hybrid Search reranking_enable derivation', () => {
+ const hybridConfigWithRerankModel = (
+ base: RetrievalConfig,
+ rerankingEnable: boolean,
+ ): RetrievalConfig => ({
+ ...base,
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: rerankingEnable,
+ reranking_mode: RerankingModeEnum.RerankingModel,
+ reranking_model: {
+ reranking_provider_name: 'langgenius/xinference/xinference',
+ reranking_model_name: 'bge-reranker-v2-m3',
+ },
+ })
+
+ it('should persist reranking_enable=true when a rerank model is configured but the stale flag is false', async () => {
+ const { updateDatasetSetting } = await import('@/service/datasets')
+ const { result } = renderHook(() => useFormState())
+
+ // Reproduces the reported bug: Hybrid Search renders no rerank switch, so the flag keeps
+ // the stale `false` default even though a rerank model is selected.
+ act(() => {
+ result.current.setRetrievalConfig(
+ hybridConfigWithRerankModel(result.current.retrievalConfig, false),
+ )
+ })
+
+ await act(async () => {
+ await result.current.handleSave()
+ })
+
+ expect(updateDatasetSetting).toHaveBeenCalledWith({
+ datasetId: 'dataset-1',
+ body: expect.objectContaining({
+ retrieval_model: expect.objectContaining({
+ search_method: RETRIEVE_METHOD.hybrid,
+ reranking_enable: true,
+ }),
+ }),
+ })
+ })
+
+ it('should leave reranking_enable untouched when Hybrid Search uses weighted score', async () => {
+ const { updateDatasetSetting } = await import('@/service/datasets')
+ const { result } = renderHook(() => useFormState())
+
+ act(() => {
+ result.current.setRetrievalConfig({
+ ...hybridConfigWithRerankModel(result.current.retrievalConfig, false),
+ reranking_mode: RerankingModeEnum.WeightedScore,
+ })
+ })
+
+ await act(async () => {
+ await result.current.handleSave()
+ })
+
+ expect(updateDatasetSetting).toHaveBeenCalledWith({
+ datasetId: 'dataset-1',
+ body: expect.objectContaining({
+ retrieval_model: expect.objectContaining({
+ reranking_enable: false,
+ }),
+ }),
+ })
+ })
+
+ it('should leave reranking_enable untouched for semantic search', async () => {
+ const { updateDatasetSetting } = await import('@/service/datasets')
+ const { result } = renderHook(() => useFormState())
+
+ act(() => {
+ result.current.setRetrievalConfig({
+ ...hybridConfigWithRerankModel(result.current.retrievalConfig, false),
+ search_method: RETRIEVE_METHOD.semantic,
+ })
+ })
+
+ await act(async () => {
+ await result.current.handleSave()
+ })
+
+ expect(updateDatasetSetting).toHaveBeenCalledWith({
+ datasetId: 'dataset-1',
+ body: expect.objectContaining({
+ retrieval_model: expect.objectContaining({
+ reranking_enable: false,
+ }),
+ }),
+ })
+ })
})
describe('External Provider', () => {
diff --git a/web/app/components/datasets/settings/form/hooks/use-form-state.ts b/web/app/components/datasets/settings/form/hooks/use-form-state.ts
index 354487d14e7..6198fe41ad9 100644
--- a/web/app/components/datasets/settings/form/hooks/use-form-state.ts
+++ b/web/app/components/datasets/settings/form/hooks/use-form-state.ts
@@ -8,7 +8,10 @@ import { useQuery, useSuspenseQuery } from '@tanstack/react-query'
import { useAtomValue } from 'jotai'
import { useCallback, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
-import { isReRankModelSelected } from '@/app/components/datasets/common/check-rerank-model'
+import {
+ isReRankModelSelected,
+ normalizeRetrievalConfigForSave,
+} from '@/app/components/datasets/common/check-rerank-model'
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { toast } from '@/app/notifications'
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
@@ -174,6 +177,10 @@ export const useFormState = () => {
retrievalConfig.weights.vector_setting.embedding_model_name = embeddingModel.model || ''
}
+ // Hybrid Search renders no rerank on/off switch, so `reranking_enable` is only ever written
+ // when the retrieval method is switched. Derive it from the selected rerank model on save.
+ const retrievalConfigForSave = normalizeRetrievalConfigForSave(retrievalConfig)
+
try {
setLoading(true)
const body: Record<string, unknown> = {
@@ -184,9 +191,9 @@ export const useFormState = () => {
permission,
indexing_technique: indexMethod,
retrieval_model: {
- ...retrievalConfig,
- score_threshold: retrievalConfig.score_threshold_enabled
- ? retrievalConfig.score_threshold
+ ...retrievalConfigForSave,
+ score_threshold: retrievalConfigForSave.score_threshold_enabled
+ ? retrievalConfigForSave.score_threshold
: 0,
},
embedding_model: embeddingModel.model,