chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
+162
@@ -0,0 +1,162 @@
|
||||
import type { KnowledgeBaseNodeType } from '../types'
|
||||
import type { CommonNodeType } from '@/app/components/workflow/types'
|
||||
import { zModelWithProviderEntityResponse } from '@dify/contracts/api/console/workspaces/zod.gen'
|
||||
import { screen } from '@testing-library/react'
|
||||
import { ModelStatusEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { BlockEnum } from '@/app/components/workflow/types'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import {
|
||||
createCatalogModelFixture,
|
||||
createDefaultModelFixture,
|
||||
createModelCatalogFixture,
|
||||
seedModelProviderQueries,
|
||||
} from '@/test/console/model-provider'
|
||||
import { createConsoleQueryWrapper, renderWithConsoleQuery } from '@/test/console/query-data'
|
||||
import Node from '../node'
|
||||
import { ChunkStructureEnum, IndexMethodEnum, RetrievalSearchMethodEnum } from '../types'
|
||||
|
||||
const createNodeData = (
|
||||
overrides: Partial<CommonNodeType<KnowledgeBaseNodeType>> = {},
|
||||
): CommonNodeType<KnowledgeBaseNodeType> => ({
|
||||
title: 'Knowledge Base',
|
||||
desc: '',
|
||||
type: BlockEnum.KnowledgeBase,
|
||||
index_chunk_variable_selector: ['result'],
|
||||
chunk_structure: ChunkStructureEnum.general,
|
||||
indexing_technique: IndexMethodEnum.QUALIFIED,
|
||||
embedding_model: 'text-embedding-3-large',
|
||||
embedding_model_provider: 'openai',
|
||||
keyword_number: 10,
|
||||
retrieval_model: {
|
||||
top_k: 3,
|
||||
score_threshold_enabled: false,
|
||||
score_threshold: 0.5,
|
||||
search_method: RetrievalSearchMethodEnum.semantic,
|
||||
},
|
||||
...overrides,
|
||||
})
|
||||
|
||||
function renderNode(
|
||||
data = createNodeData(),
|
||||
model = createCatalogModelFixture({
|
||||
model: 'text-embedding-3-large',
|
||||
label: { en_US: 'Text Embedding 3 Large' },
|
||||
model_type: 'text-embedding',
|
||||
}),
|
||||
) {
|
||||
const { queryClient } = createConsoleQueryWrapper()
|
||||
seedModelProviderQueries(queryClient, [createModelCatalogFixture({ models: [model] })])
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.workspaces.current.modelProviders.byProvider.models.get.queryKey({
|
||||
input: { params: { provider: 'openai' } },
|
||||
}),
|
||||
{
|
||||
data: [
|
||||
zModelWithProviderEntityResponse.parse({
|
||||
...model,
|
||||
provider: createDefaultModelFixture({ model_type: 'text-embedding' }).provider,
|
||||
}),
|
||||
],
|
||||
},
|
||||
)
|
||||
return renderWithConsoleQuery(<Node id="knowledge-base-1" data={data} />, { queryClient })
|
||||
}
|
||||
|
||||
describe('KnowledgeBaseNode', () => {
|
||||
// Embedding model row should mirror the selector status labels.
|
||||
describe('Embedding Model Status', () => {
|
||||
it.each([
|
||||
{ status: ModelStatusEnum.active, label: 'Text Embedding 3 Large' },
|
||||
{
|
||||
status: ModelStatusEnum.noConfigure,
|
||||
label: 'modelProvider.modelProvider.selector.configureRequired',
|
||||
},
|
||||
{ status: ModelStatusEnum.disabled, label: 'modelProvider.modelProvider.selector.disabled' },
|
||||
])('renders $status from the selected model in the provider catalog', ({ status, label }) => {
|
||||
renderNode(
|
||||
createNodeData(),
|
||||
createCatalogModelFixture({
|
||||
model: 'text-embedding-3-large',
|
||||
model_type: 'text-embedding',
|
||||
label: { en_US: 'Text Embedding 3 Large' },
|
||||
status,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(screen.getByText(label)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should render incompatible when embedding model status is incompatible', () => {
|
||||
renderNode(
|
||||
createNodeData(),
|
||||
createCatalogModelFixture({
|
||||
model: 'another-model',
|
||||
model_type: 'text-embedding',
|
||||
}),
|
||||
)
|
||||
|
||||
expect(
|
||||
screen.getByText('modelProvider.modelProvider.selector.incompatible'),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should render configure model prompt when no embedding model is selected', () => {
|
||||
renderNode(
|
||||
createNodeData({
|
||||
embedding_model: undefined,
|
||||
embedding_model_provider: undefined,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(screen.getByText('plugin.detailPanel.configureModel')).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Validation warnings', () => {
|
||||
it('should render a warning banner when chunk structure is missing', () => {
|
||||
renderNode(
|
||||
createNodeData({
|
||||
chunk_structure: undefined,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(screen.getByText(/chunkIsRequired/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should render a warning value for the chunks input row when no chunk variable is selected', () => {
|
||||
renderNode(
|
||||
createNodeData({
|
||||
index_chunk_variable_selector: [],
|
||||
}),
|
||||
)
|
||||
|
||||
expect(screen.getByText(/chunksVariableIsRequired/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should render a warning value for retrieval settings when reranking is incomplete', async () => {
|
||||
renderNode(
|
||||
createNodeData({
|
||||
retrieval_model: {
|
||||
top_k: 3,
|
||||
score_threshold_enabled: false,
|
||||
score_threshold: 0.5,
|
||||
search_method: RetrievalSearchMethodEnum.semantic,
|
||||
reranking_enable: true,
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
expect(await screen.findByText(/rerankingModelIsRequired/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should hide the embedding model row when the index method is not qualified', () => {
|
||||
renderNode(
|
||||
createNodeData({
|
||||
indexing_technique: IndexMethodEnum.ECONOMICAL,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(screen.queryByText('Text Embedding 3 Large')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
+401
@@ -0,0 +1,401 @@
|
||||
import type { KnowledgeBaseNodeType } from '../types'
|
||||
import type { PanelProps } from '@/types/workflow'
|
||||
import { act, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useNodes, useStoreApi } from 'reactflow'
|
||||
import { createEdge, createNode } from '@/app/components/workflow/__tests__/fixtures'
|
||||
import { renderWorkflowFlowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
|
||||
import { BlockEnum } from '@/app/components/workflow/types'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import {
|
||||
createCatalogModelFixture,
|
||||
createModelCatalogFixture,
|
||||
seedModelProviderQueries,
|
||||
} from '@/test/console/model-provider'
|
||||
import { createConsoleQueryWrapper } from '@/test/console/query-data'
|
||||
import { createToolProviderFixture } from '@/test/console/tool-provider-fixture'
|
||||
import { seedToolProviders } from '@/test/console/tool-providers'
|
||||
import { createNuqsTestWrapper } from '@/test/nuqs-testing'
|
||||
import Panel from '../panel'
|
||||
import {
|
||||
ChunkStructureEnum,
|
||||
HybridSearchModeEnum,
|
||||
IndexMethodEnum,
|
||||
RetrievalSearchMethodEnum,
|
||||
WeightedScoreEnum,
|
||||
} from '../types'
|
||||
|
||||
const createData = (overrides: Partial<KnowledgeBaseNodeType> = {}): KnowledgeBaseNodeType => ({
|
||||
title: 'Knowledge Base',
|
||||
desc: '',
|
||||
type: BlockEnum.KnowledgeBase,
|
||||
index_chunk_variable_selector: [],
|
||||
chunk_structure: ChunkStructureEnum.general,
|
||||
indexing_technique: IndexMethodEnum.QUALIFIED,
|
||||
embedding_model: 'text-embedding-3-large',
|
||||
embedding_model_provider: 'openai',
|
||||
keyword_number: 10,
|
||||
retrieval_model: {
|
||||
search_method: RetrievalSearchMethodEnum.semantic,
|
||||
reranking_enable: false,
|
||||
top_k: 3,
|
||||
score_threshold_enabled: false,
|
||||
score_threshold: 0.5,
|
||||
},
|
||||
...overrides,
|
||||
})
|
||||
const panelProps: PanelProps = {
|
||||
getInputVars: () => [],
|
||||
toVarInputs: () => [],
|
||||
runInputData: {},
|
||||
runInputDataRef: { current: {} },
|
||||
setRunInputData: vi.fn(),
|
||||
runResult: undefined,
|
||||
}
|
||||
let flowStore: ReturnType<typeof useStoreApi>
|
||||
const getData = () =>
|
||||
flowStore
|
||||
.getState()
|
||||
.getNodes()
|
||||
.find((node) => node.id === 'knowledge')!.data as KnowledgeBaseNodeType
|
||||
function NodePanel() {
|
||||
flowStore = useStoreApi()
|
||||
const node = useNodes<KnowledgeBaseNodeType>().find((node) => node.id === 'knowledge')
|
||||
return node ? <Panel id={node.id} data={node.data} panelProps={panelProps} /> : null
|
||||
}
|
||||
function setup(data = createData(), readonly = false) {
|
||||
const { queryClient } = createConsoleQueryWrapper({
|
||||
systemFeatures: { deployment_edition: 'COMMUNITY' },
|
||||
})
|
||||
const catalog = createModelCatalogFixture({
|
||||
models: [
|
||||
createCatalogModelFixture({
|
||||
model: 'text-embedding-3-large',
|
||||
label: { en_US: 'Embedding Large' },
|
||||
model_type: 'text-embedding',
|
||||
}),
|
||||
createCatalogModelFixture({
|
||||
model: 'text-embedding-3-small',
|
||||
label: { en_US: 'Embedding Small' },
|
||||
model_type: 'text-embedding',
|
||||
}),
|
||||
createCatalogModelFixture({
|
||||
model: 'reranker',
|
||||
label: { en_US: 'Reranker' },
|
||||
model_type: 'rerank',
|
||||
}),
|
||||
createCatalogModelFixture(),
|
||||
],
|
||||
})
|
||||
seedModelProviderQueries(queryClient, [catalog])
|
||||
const chunkSchema = { type: 'array', items: { type: 'string' } }
|
||||
seedToolProviders(queryClient, {
|
||||
builtInTools: [
|
||||
createToolProviderFixture({
|
||||
tools: [
|
||||
{
|
||||
name: 'chunk',
|
||||
author: 'Dify',
|
||||
label: { en_US: 'Chunk' },
|
||||
description: { en_US: 'Chunk text' },
|
||||
parameters: [],
|
||||
output_schema: {
|
||||
type: 'object',
|
||||
properties: { chunks: chunkSchema, status: { type: 'string' } },
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
customTools: [],
|
||||
workflowTools: [],
|
||||
mcpTools: [],
|
||||
})
|
||||
queryClient.setQueryData(consoleQuery.spec.schemaDefinitions.get.queryKey(), [
|
||||
{ name: 'general_structure', label: 'General Structure', schema: chunkSchema },
|
||||
])
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.workspaces.current.modelProviders.byProvider.models.get.queryKey({
|
||||
input: { params: { provider: 'openai' } },
|
||||
}),
|
||||
{ data: [] },
|
||||
)
|
||||
const { wrapper: UrlState } = createNuqsTestWrapper()
|
||||
const sync = vi.fn(async () => null)
|
||||
const rendered = renderWorkflowFlowComponent(
|
||||
<UrlState>
|
||||
<NodePanel />
|
||||
</UrlState>,
|
||||
{
|
||||
queryClient,
|
||||
resource: { kind: 'knowledge-pipeline', pipelineId: 'pipeline-1' },
|
||||
nodes: [
|
||||
createNode({
|
||||
id: 'chunker',
|
||||
data: {
|
||||
type: BlockEnum.Tool,
|
||||
title: 'Chunker',
|
||||
provider_id: 'provider-1',
|
||||
provider_type: 'builtin',
|
||||
tool_name: 'chunk',
|
||||
},
|
||||
}),
|
||||
createNode({ id: 'knowledge', data }),
|
||||
],
|
||||
edges: [createEdge({ source: 'chunker', target: 'knowledge' })],
|
||||
initialStoreState: { canvasReadOnly: readonly },
|
||||
hooksStoreProps: { doSyncWorkflowDraft: sync },
|
||||
},
|
||||
)
|
||||
return { ...rendered, user: userEvent.setup(), sync }
|
||||
}
|
||||
|
||||
it('starts with chunk selection and exposes configuration after choosing a structure', async () => {
|
||||
const { user, store, sync } = setup(
|
||||
createData({ chunk_structure: undefined, indexing_technique: undefined }),
|
||||
)
|
||||
expect(screen.queryByText('workflow.nodes.knowledgeBase.chunksInput')).not.toBeInTheDocument()
|
||||
await user.click(
|
||||
screen.getByRole('button', { name: 'workflow.nodes.knowledgeBase.chooseChunkStructure' }),
|
||||
)
|
||||
await user.click(
|
||||
within(await screen.findByRole('dialog')).getByRole('button', {
|
||||
name: /datasetCreation.stepTwo.general/,
|
||||
}),
|
||||
)
|
||||
expect(screen.getByText('workflow.nodes.knowledgeBase.chunksInput')).toBeInTheDocument()
|
||||
expect(getData().chunk_structure).toBe(ChunkStructureEnum.general)
|
||||
act(() => store.getState().flushPendingSync())
|
||||
expect(sync).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('switches indexing modes and retains keyword settings while updating retrieval defaults', async () => {
|
||||
const { user } = setup()
|
||||
expect(
|
||||
screen.getByRole('switch', { name: 'datasetSettings.form.summaryAutoGen' }),
|
||||
).toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: /datasetSettings.form.indexMethodEconomy/ }))
|
||||
expect(getData()).toMatchObject({
|
||||
indexing_technique: IndexMethodEnum.ECONOMICAL,
|
||||
retrieval_model: { search_method: RetrievalSearchMethodEnum.keywordSearch },
|
||||
})
|
||||
expect(screen.queryByText('datasetSettings.form.embeddingModel')).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.queryByRole('switch', { name: 'datasetSettings.form.summaryAutoGen' }),
|
||||
).not.toBeInTheDocument()
|
||||
const keywords = screen.getByRole('textbox', { name: 'datasetSettings.form.numberOfKeywords' })
|
||||
await user.clear(keywords)
|
||||
await user.type(keywords, '9')
|
||||
await user.tab()
|
||||
expect(getData().keyword_number).toBe(9)
|
||||
await user.click(screen.getByRole('button', { name: /datasetCreation.stepTwo.qualified/ }))
|
||||
expect(getData()).toMatchObject({
|
||||
indexing_technique: IndexMethodEnum.QUALIFIED,
|
||||
keyword_number: 9,
|
||||
retrieval_model: { search_method: RetrievalSearchMethodEnum.semantic },
|
||||
})
|
||||
expect(screen.getByText('datasetSettings.form.embeddingModel')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('forces high-quality indexing when changing an economical node to parent-child chunks', async () => {
|
||||
const { user } = setup(
|
||||
createData({
|
||||
indexing_technique: IndexMethodEnum.ECONOMICAL,
|
||||
retrieval_model: {
|
||||
...createData().retrieval_model,
|
||||
search_method: RetrievalSearchMethodEnum.keywordSearch,
|
||||
},
|
||||
}),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'workflow.panel.change' }))
|
||||
await user.click(
|
||||
within(await screen.findByRole('dialog')).getByRole('button', {
|
||||
name: /datasetCreation.stepTwo.parentChild/,
|
||||
}),
|
||||
)
|
||||
expect(getData()).toMatchObject({
|
||||
chunk_structure: ChunkStructureEnum.parent_child,
|
||||
indexing_technique: IndexMethodEnum.QUALIFIED,
|
||||
})
|
||||
expect(
|
||||
screen.queryByRole('button', { name: /datasetSettings.form.indexMethodEconomy/ }),
|
||||
).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByRole('switch', { name: 'datasetSettings.form.summaryAutoGen' }),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('persists embedding selection and retrieval limits in the node draft', async () => {
|
||||
const { user } = setup()
|
||||
await user.click(screen.getByRole('button', { name: /Embedding Large/ }))
|
||||
await user.click(within(await screen.findByRole('dialog')).getByText('Embedding Small'))
|
||||
expect(getData()).toMatchObject({
|
||||
embedding_model: 'text-embedding-3-small',
|
||||
retrieval_model: {
|
||||
weights: {
|
||||
vector_setting: {
|
||||
embedding_model_name: 'text-embedding-3-small',
|
||||
embedding_provider_name: 'openai',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const topK = screen.getByRole('textbox', { name: 'appDebug.datasetConfig.top_k' })
|
||||
await user.clear(topK)
|
||||
await user.type(topK, '6')
|
||||
await user.tab()
|
||||
await user.click(screen.getByRole('switch', { name: 'appDebug.datasetConfig.score_threshold' }))
|
||||
const threshold = screen.getByRole('textbox', { name: 'appDebug.datasetConfig.score_threshold' })
|
||||
await user.clear(threshold)
|
||||
await user.type(threshold, '0.75')
|
||||
await user.tab()
|
||||
expect(getData().retrieval_model).toMatchObject({
|
||||
top_k: 6,
|
||||
score_threshold_enabled: true,
|
||||
score_threshold: 0.75,
|
||||
})
|
||||
})
|
||||
|
||||
it('preserves summary instructions when summary generation is disabled and enabled again', async () => {
|
||||
const { user } = setup(
|
||||
createData({
|
||||
summary_index_setting: {
|
||||
enable: false,
|
||||
summary_prompt: 'Summarize the content.',
|
||||
model_name: 'gpt-4',
|
||||
model_provider_name: 'openai',
|
||||
},
|
||||
}),
|
||||
)
|
||||
const toggle = screen.getByRole('switch', { name: 'datasetSettings.form.summaryAutoGen' })
|
||||
await user.click(toggle)
|
||||
const instructions = screen.getByRole('textbox', {
|
||||
name: 'datasetSettings.form.summaryInstructions',
|
||||
})
|
||||
await user.type(instructions, ' Include key facts.')
|
||||
await user.click(toggle)
|
||||
expect(getData().summary_index_setting).toMatchObject({
|
||||
enable: false,
|
||||
summary_prompt: 'Summarize the content. Include key facts.',
|
||||
})
|
||||
await user.click(toggle)
|
||||
expect(
|
||||
screen.getByRole('textbox', { name: 'datasetSettings.form.summaryInstructions' }),
|
||||
).toHaveValue('Summarize the content. Include key facts.')
|
||||
expect(getData().summary_index_setting).toMatchObject({
|
||||
enable: true,
|
||||
model_name: 'gpt-4',
|
||||
model_provider_name: 'openai',
|
||||
})
|
||||
})
|
||||
|
||||
it('prevents node edits on a readonly canvas', async () => {
|
||||
const { user, store, sync } = setup(createData(), true)
|
||||
const before = getData()
|
||||
expect(screen.getByRole('button', { name: 'workflow.panel.change' })).toBeDisabled()
|
||||
expect(screen.getByRole('button', { name: /Embedding Large/ })).toBeDisabled()
|
||||
const summary = screen.getByRole('switch', { name: 'datasetSettings.form.summaryAutoGen' })
|
||||
expect(summary).toHaveAttribute('aria-disabled', 'true')
|
||||
await user.click(summary)
|
||||
await user.click(screen.getByRole('button', { name: /datasetSettings.form.indexMethodEconomy/ }))
|
||||
expect(getData()).toEqual(before)
|
||||
act(() => store.getState().flushPendingSync())
|
||||
expect(sync).not.toHaveBeenCalled()
|
||||
expect(screen.getByRole('textbox', { name: 'appDebug.datasetConfig.top_k' })).toBeDisabled()
|
||||
})
|
||||
|
||||
it('selects only compatible chunk outputs, clears them, and resets them when the structure changes', async () => {
|
||||
const { user } = setup()
|
||||
const selectChunks = async () => {
|
||||
await user.click(screen.getByRole('button', { name: /workflow.common.setVarValuePlaceholder/ }))
|
||||
const picker = await screen.findByRole('dialog')
|
||||
expect(within(picker).queryByText('status')).not.toBeInTheDocument()
|
||||
await user.click(within(picker).getByText('chunks'))
|
||||
}
|
||||
const chooseStructure = async (name: RegExp) => {
|
||||
await user.click(screen.getByRole('button', { name: 'workflow.panel.change' }))
|
||||
await user.click(within(await screen.findByRole('dialog')).getByRole('button', { name }))
|
||||
}
|
||||
await selectChunks()
|
||||
expect(getData().index_chunk_variable_selector).toEqual(['chunker', 'chunks'])
|
||||
await chooseStructure(/datasetCreation.stepTwo.general/)
|
||||
expect(getData().index_chunk_variable_selector).toEqual(['chunker', 'chunks'])
|
||||
await user.click(screen.getByRole('button', { name: 'common.operation.clear' }))
|
||||
expect(getData().index_chunk_variable_selector).toEqual([])
|
||||
await user.keyboard('{Escape}')
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
|
||||
await selectChunks()
|
||||
await chooseStructure(/Q&A/)
|
||||
expect(
|
||||
screen.queryByRole('switch', { name: 'datasetSettings.form.summaryAutoGen' }),
|
||||
).not.toBeInTheDocument()
|
||||
expect(getData()).toMatchObject({
|
||||
index_chunk_variable_selector: [],
|
||||
chunk_structure: ChunkStructureEnum.question_answer,
|
||||
retrieval_model: { search_method: RetrievalSearchMethodEnum.semantic },
|
||||
})
|
||||
})
|
||||
|
||||
it('preserves custom weights through reranking mode and embedding model changes', async () => {
|
||||
const { user } = setup()
|
||||
await user.click(screen.getByRole('radio', { name: /dataset.retrieval.full_text_search.title/ }))
|
||||
expect(getData().retrieval_model).toMatchObject({
|
||||
search_method: RetrievalSearchMethodEnum.fullText,
|
||||
reranking_mode: HybridSearchModeEnum.RerankingModel,
|
||||
})
|
||||
await user.click(
|
||||
screen.getByRole('switch', { name: 'modelProvider.modelProvider.rerankModel.key' }),
|
||||
)
|
||||
expect(getData().retrieval_model.reranking_enable).toBe(true)
|
||||
await user.click(screen.getByRole('radio', { name: /dataset.retrieval.hybrid_search.title/ }))
|
||||
expect(getData().retrieval_model).toMatchObject({
|
||||
search_method: RetrievalSearchMethodEnum.hybrid,
|
||||
reranking_mode: HybridSearchModeEnum.RerankingModel,
|
||||
reranking_enable: true,
|
||||
})
|
||||
await user.click(screen.getByRole('radio', { name: /dataset.weightedScore.title/ }))
|
||||
expect(getData().retrieval_model).toMatchObject({
|
||||
reranking_mode: HybridSearchModeEnum.WeightedScore,
|
||||
reranking_enable: false,
|
||||
weights: {
|
||||
vector_setting: {
|
||||
vector_weight: 0.7,
|
||||
embedding_model_name: 'text-embedding-3-large',
|
||||
embedding_provider_name: 'openai',
|
||||
},
|
||||
keyword_setting: { keyword_weight: 0.3 },
|
||||
},
|
||||
})
|
||||
const slider = screen.getByRole('slider', { name: 'dataset.weightedScore.semantic' })
|
||||
act(() => slider.focus())
|
||||
await user.keyboard('{ArrowLeft}')
|
||||
expect(getData().retrieval_model.weights).toMatchObject({
|
||||
weight_type: WeightedScoreEnum.Customized,
|
||||
vector_setting: { vector_weight: 0.6 },
|
||||
keyword_setting: { keyword_weight: 0.4 },
|
||||
})
|
||||
await user.click(
|
||||
screen.getByRole('radio', { name: /modelProvider.modelProvider.rerankModel.key/ }),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.detailPanel.configureModel' }))
|
||||
await user.click(within(await screen.findByRole('dialog')).getByText('Reranker'))
|
||||
expect(getData().retrieval_model).toMatchObject({
|
||||
reranking_enable: true,
|
||||
reranking_model: { reranking_provider_name: 'openai', reranking_model_name: 'reranker' },
|
||||
})
|
||||
await user.click(screen.getByRole('radio', { name: /dataset.weightedScore.title/ }))
|
||||
expect(screen.getByRole('slider', { name: 'dataset.weightedScore.semantic' })).toHaveAttribute(
|
||||
'aria-valuenow',
|
||||
'0.6',
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: /Embedding Large/ }))
|
||||
await user.click(within(await screen.findByRole('dialog')).getByText('Embedding Small'))
|
||||
expect(getData().retrieval_model.weights).toMatchObject({
|
||||
vector_setting: {
|
||||
vector_weight: 0.6,
|
||||
embedding_model_name: 'text-embedding-3-small',
|
||||
embedding_provider_name: 'openai',
|
||||
},
|
||||
keyword_setting: { keyword_weight: 0.4 },
|
||||
})
|
||||
})
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
import type { KnowledgeBaseNodeType } from '../types'
|
||||
import { act, renderHook } from '@testing-library/react'
|
||||
import { BlockEnum, InputVarType } from '@/app/components/workflow/types'
|
||||
import { ChunkStructureEnum, IndexMethodEnum, RetrievalSearchMethodEnum } from '../types'
|
||||
import useSingleRunFormParams from '../use-single-run-form-params'
|
||||
|
||||
const createPayload = (overrides: Partial<KnowledgeBaseNodeType> = {}): KnowledgeBaseNodeType => ({
|
||||
title: 'Knowledge Base',
|
||||
desc: '',
|
||||
type: BlockEnum.KnowledgeBase,
|
||||
index_chunk_variable_selector: ['chunks', 'results'],
|
||||
chunk_structure: ChunkStructureEnum.general,
|
||||
indexing_technique: IndexMethodEnum.QUALIFIED,
|
||||
embedding_model: 'text-embedding-3-large',
|
||||
embedding_model_provider: 'openai',
|
||||
keyword_number: 10,
|
||||
retrieval_model: {
|
||||
search_method: RetrievalSearchMethodEnum.semantic,
|
||||
reranking_enable: false,
|
||||
top_k: 3,
|
||||
score_threshold_enabled: false,
|
||||
score_threshold: 0.5,
|
||||
},
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe('useSingleRunFormParams', () => {
|
||||
// The hook should expose the single query form and map chunk dependencies for single-run execution.
|
||||
describe('Forms', () => {
|
||||
it('builds the query form and preserves other run inputs when the query changes', () => {
|
||||
const setRunInputData = vi.fn()
|
||||
const { result } = renderHook(() =>
|
||||
useSingleRunFormParams({
|
||||
id: 'knowledge-base-1',
|
||||
payload: createPayload(),
|
||||
runInputData: { query: 'what is dify', locale: 'en-US' },
|
||||
getInputVars: vi.fn(),
|
||||
setRunInputData,
|
||||
toVarInputs: vi.fn(),
|
||||
}),
|
||||
)
|
||||
|
||||
expect(result.current.forms).toHaveLength(1)
|
||||
expect(result.current.forms[0]!.inputs).toEqual([
|
||||
{
|
||||
label: 'workflow.nodes.common.inputVars',
|
||||
variable: 'query',
|
||||
type: InputVarType.paragraph,
|
||||
required: true,
|
||||
},
|
||||
])
|
||||
expect(result.current.forms[0]!.values).toEqual({ query: 'what is dify' })
|
||||
act(() => result.current.forms[0]!.onChange({ query: 'new query' }))
|
||||
expect(setRunInputData).toHaveBeenCalledExactlyOnceWith({
|
||||
query: 'new query',
|
||||
locale: 'en-US',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Dependencies', () => {
|
||||
it('should expose the chunk selector as the only dependent variable', () => {
|
||||
const payload = createPayload({
|
||||
index_chunk_variable_selector: ['node-1', 'chunks'],
|
||||
})
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSingleRunFormParams({
|
||||
id: 'knowledge-base-1',
|
||||
payload,
|
||||
runInputData: {},
|
||||
getInputVars: vi.fn(),
|
||||
setRunInputData: vi.fn(),
|
||||
toVarInputs: vi.fn(),
|
||||
}),
|
||||
)
|
||||
|
||||
expect(result.current.getDependentVars()).toEqual([['node-1', 'chunks']])
|
||||
expect(result.current.getDependentVar('query')).toEqual(['node-1', 'chunks'])
|
||||
expect(result.current.getDependentVar('other')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
})
|
||||
+263
@@ -0,0 +1,263 @@
|
||||
import type { ProviderWithModelsResponse } from '@dify/contracts/api/console/workspaces/types.gen'
|
||||
import type { TFunction } from 'i18next'
|
||||
import type { KnowledgeBaseNodeType } from '../types'
|
||||
import type { ModelItem } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import {
|
||||
ConfigurationMethodEnum,
|
||||
ModelStatusEnum,
|
||||
ModelTypeEnum,
|
||||
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { withSelectorKey } from '@/test/i18n-mock'
|
||||
import { ChunkStructureEnum, IndexMethodEnum, RetrievalSearchMethodEnum } from '../types'
|
||||
import {
|
||||
getKnowledgeBaseValidationIssue,
|
||||
getKnowledgeBaseValidationMessage,
|
||||
isHighQualitySearchMethod,
|
||||
KnowledgeBaseValidationIssueCode,
|
||||
} from '../utils'
|
||||
|
||||
const makeEmbeddingModelList = (status: ModelStatusEnum): ProviderWithModelsResponse[] => {
|
||||
return [
|
||||
{
|
||||
tenant_id: 'test-workspace',
|
||||
provider: 'openai',
|
||||
icon_small: { en_US: '', zh_Hans: '' },
|
||||
label: { en_US: 'OpenAI', zh_Hans: 'OpenAI' },
|
||||
models: [
|
||||
{
|
||||
model: 'gpt-4o',
|
||||
label: { en_US: 'GPT-4o', zh_Hans: 'GPT-4o' },
|
||||
model_type: ModelTypeEnum.textEmbedding,
|
||||
fetch_from: ConfigurationMethodEnum.predefinedModel,
|
||||
status,
|
||||
model_properties: {},
|
||||
load_balancing_enabled: false,
|
||||
},
|
||||
],
|
||||
status: 'active',
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const makeEmbeddingProviderModelList = (status: ModelStatusEnum): ModelItem[] => {
|
||||
return [
|
||||
{
|
||||
model: 'gpt-4o',
|
||||
label: { en_US: 'GPT-4o', zh_Hans: 'GPT-4o' },
|
||||
model_type: ModelTypeEnum.textEmbedding,
|
||||
fetch_from: ConfigurationMethodEnum.predefinedModel,
|
||||
status,
|
||||
model_properties: {},
|
||||
load_balancing_enabled: false,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const makePayload = (overrides: Partial<KnowledgeBaseNodeType> = {}): KnowledgeBaseNodeType => {
|
||||
return {
|
||||
index_chunk_variable_selector: ['general_chunks', 'results'],
|
||||
chunk_structure: ChunkStructureEnum.general,
|
||||
indexing_technique: IndexMethodEnum.QUALIFIED,
|
||||
embedding_model: 'gpt-4o',
|
||||
embedding_model_provider: 'openai',
|
||||
keyword_number: 10,
|
||||
retrieval_model: {
|
||||
top_k: 3,
|
||||
score_threshold_enabled: false,
|
||||
score_threshold: 0.5,
|
||||
search_method: RetrievalSearchMethodEnum.semantic,
|
||||
},
|
||||
_embeddingModelList: makeEmbeddingModelList(ModelStatusEnum.active),
|
||||
_embeddingProviderModelList: makeEmbeddingProviderModelList(ModelStatusEnum.active),
|
||||
_rerankModelList: [],
|
||||
...overrides,
|
||||
} as KnowledgeBaseNodeType
|
||||
}
|
||||
|
||||
describe('knowledge-base validation issue', () => {
|
||||
it('identifies high quality retrieval methods', () => {
|
||||
expect(isHighQualitySearchMethod(RetrievalSearchMethodEnum.semantic)).toBe(true)
|
||||
expect(isHighQualitySearchMethod(RetrievalSearchMethodEnum.hybrid)).toBe(true)
|
||||
expect(isHighQualitySearchMethod(RetrievalSearchMethodEnum.fullText)).toBe(true)
|
||||
expect(isHighQualitySearchMethod('unknown-method' as RetrievalSearchMethodEnum)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns chunk structure issue when chunk structure is missing', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(makePayload({ chunk_structure: undefined }))
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.chunkStructureRequired)
|
||||
})
|
||||
|
||||
it('returns chunks variable issue when chunks selector is empty', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({ index_chunk_variable_selector: [] }),
|
||||
)
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.chunksVariableRequired)
|
||||
})
|
||||
|
||||
it('maps no-configure to configure required', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
_embeddingProviderModelList: makeEmbeddingProviderModelList(ModelStatusEnum.noConfigure),
|
||||
}),
|
||||
)
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelConfigureRequired)
|
||||
})
|
||||
|
||||
it('maps credential-removed to API key unavailable', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
_embeddingProviderModelList: makeEmbeddingProviderModelList(
|
||||
ModelStatusEnum.credentialRemoved,
|
||||
),
|
||||
}),
|
||||
)
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelApiKeyUnavailable)
|
||||
})
|
||||
|
||||
it('maps quota-exceeded to credits exhausted', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
_embeddingProviderModelList: makeEmbeddingProviderModelList(ModelStatusEnum.quotaExceeded),
|
||||
}),
|
||||
)
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelCreditsExhausted)
|
||||
})
|
||||
|
||||
it('maps disabled to disabled', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
_embeddingProviderModelList: makeEmbeddingProviderModelList(ModelStatusEnum.disabled),
|
||||
}),
|
||||
)
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelDisabled)
|
||||
})
|
||||
|
||||
it('maps missing provider plugin to incompatible when embedding model is already configured', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
embedding_model_provider: 'missing-provider',
|
||||
_embeddingProviderModelList: undefined,
|
||||
}),
|
||||
)
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelIncompatible)
|
||||
})
|
||||
|
||||
it('falls back to provider model list when provider scoped model list is empty', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(makePayload({ _embeddingProviderModelList: [] }))
|
||||
expect(issue).toBeNull()
|
||||
})
|
||||
|
||||
it('returns embedding-model-not-configured when the qualified index is missing provider details', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(makePayload({ embedding_model: undefined }))
|
||||
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelNotConfigured)
|
||||
})
|
||||
|
||||
it('maps no-permission embedding models to incompatible', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
_embeddingProviderModelList: makeEmbeddingProviderModelList(ModelStatusEnum.noPermission),
|
||||
}),
|
||||
)
|
||||
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.embeddingModelIncompatible)
|
||||
})
|
||||
|
||||
it('returns retrieval-setting-required when retrieval search method is missing', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({ retrieval_model: undefined as never }),
|
||||
)
|
||||
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.retrievalSettingRequired)
|
||||
})
|
||||
|
||||
it('returns reranking-model-required when reranking is enabled without a model', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
retrieval_model: {
|
||||
...makePayload().retrieval_model,
|
||||
reranking_enable: true,
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.rerankingModelRequired)
|
||||
})
|
||||
|
||||
it('returns reranking-model-invalid when the configured reranking model is unavailable', () => {
|
||||
const issue = getKnowledgeBaseValidationIssue(
|
||||
makePayload({
|
||||
retrieval_model: {
|
||||
...makePayload().retrieval_model,
|
||||
reranking_enable: true,
|
||||
reranking_model: {
|
||||
reranking_provider_name: 'missing-provider',
|
||||
reranking_model_name: 'missing-model',
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
expect(issue?.code).toBe(KnowledgeBaseValidationIssueCode.rerankingModelInvalid)
|
||||
})
|
||||
})
|
||||
|
||||
describe('knowledge-base validation messaging', () => {
|
||||
const t = withSelectorKey(
|
||||
(key: string, _options?: Record<string, unknown>) => key,
|
||||
) as unknown as TFunction<['common', 'workflow', 'modelProvider']>
|
||||
|
||||
it.each([
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.chunkStructureRequired,
|
||||
'nodes.knowledgeBase.chunkIsRequired',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.chunksVariableRequired,
|
||||
'nodes.knowledgeBase.chunksVariableIsRequired',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.indexMethodRequired,
|
||||
'nodes.knowledgeBase.indexMethodIsRequired',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.embeddingModelNotConfigured,
|
||||
'nodes.knowledgeBase.embeddingModelNotConfigured',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.embeddingModelConfigureRequired,
|
||||
'modelProvider.selector.configureRequired',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.embeddingModelApiKeyUnavailable,
|
||||
'modelProvider.selector.apiKeyUnavailable',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.embeddingModelCreditsExhausted,
|
||||
'modelProvider.selector.creditsExhausted',
|
||||
],
|
||||
[KnowledgeBaseValidationIssueCode.embeddingModelDisabled, 'modelProvider.selector.disabled'],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.embeddingModelIncompatible,
|
||||
'modelProvider.selector.incompatible',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.retrievalSettingRequired,
|
||||
'nodes.knowledgeBase.retrievalSettingIsRequired',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.rerankingModelRequired,
|
||||
'nodes.knowledgeBase.rerankingModelIsRequired',
|
||||
],
|
||||
[
|
||||
KnowledgeBaseValidationIssueCode.rerankingModelInvalid,
|
||||
'nodes.knowledgeBase.rerankingModelIsInvalid',
|
||||
],
|
||||
] as const)('maps %s to the expected translation key', (code, expectedKey) => {
|
||||
expect(getKnowledgeBaseValidationMessage({ code }, t)).toBe(expectedKey)
|
||||
})
|
||||
|
||||
it('returns an empty string when there is no issue', () => {
|
||||
expect(getKnowledgeBaseValidationMessage(undefined, t)).toBe('')
|
||||
})
|
||||
})
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { ChunkStructureEnum, IndexMethodEnum } from '../../types'
|
||||
import IndexMethod from '../index-method'
|
||||
|
||||
function KeywordSettings({ onChange }: { onChange: (value: number) => void }) {
|
||||
const [value, setValue] = useState(5)
|
||||
return (
|
||||
<IndexMethod
|
||||
chunkStructure={ChunkStructureEnum.general}
|
||||
indexMethod={IndexMethodEnum.ECONOMICAL}
|
||||
keywordNumber={value}
|
||||
onIndexMethodChange={vi.fn()}
|
||||
onKeywordNumberChange={(value) => {
|
||||
setValue(value)
|
||||
onChange(value)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
describe('IndexMethod', () => {
|
||||
it('should render both index method options for general chunks and notify option changes', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onIndexMethodChange = vi.fn()
|
||||
|
||||
render(
|
||||
<IndexMethod
|
||||
chunkStructure={ChunkStructureEnum.general}
|
||||
indexMethod={IndexMethodEnum.QUALIFIED}
|
||||
keywordNumber={5}
|
||||
onIndexMethodChange={onIndexMethodChange}
|
||||
onKeywordNumberChange={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(screen.getByText('datasetCreation.stepTwo.qualified')).toBeInTheDocument()
|
||||
expect(screen.getByText('datasetSettings.form.indexMethodEconomy')).toBeInTheDocument()
|
||||
expect(screen.getByText('datasetCreation.stepTwo.recommend')).toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByText('datasetSettings.form.indexMethodEconomy'))
|
||||
|
||||
expect(onIndexMethodChange).toHaveBeenCalledWith(IndexMethodEnum.ECONOMICAL)
|
||||
})
|
||||
|
||||
it('shares integer keyword edits with the slider and retains the value when cleared', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onKeywordNumberChange = vi.fn()
|
||||
render(<KeywordSettings onChange={onKeywordNumberChange} />)
|
||||
const input = screen.getByRole('textbox', { name: 'datasetSettings.form.numberOfKeywords' })
|
||||
await user.clear(input)
|
||||
await user.tab()
|
||||
expect(onKeywordNumberChange).not.toHaveBeenCalled()
|
||||
expect(input).toHaveValue('5')
|
||||
await user.clear(input)
|
||||
await user.type(input, '7.8')
|
||||
await user.tab()
|
||||
expect(input).toHaveValue('8')
|
||||
expect(onKeywordNumberChange).toHaveBeenLastCalledWith(8)
|
||||
expect(
|
||||
screen.getByRole('slider', { name: 'datasetSettings.form.numberOfKeywords' }),
|
||||
).toHaveAttribute('aria-valuenow', '8')
|
||||
await user.click(screen.getByRole('button', { name: 'Increment value' }))
|
||||
expect(input).toHaveValue('9')
|
||||
})
|
||||
|
||||
it('should disable keyword controls when readonly is enabled', () => {
|
||||
render(
|
||||
<IndexMethod
|
||||
chunkStructure={ChunkStructureEnum.general}
|
||||
indexMethod={IndexMethodEnum.ECONOMICAL}
|
||||
keywordNumber={5}
|
||||
onIndexMethodChange={vi.fn()}
|
||||
onKeywordNumberChange={vi.fn()}
|
||||
readonly
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(
|
||||
screen.getByRole('textbox', { name: 'datasetSettings.form.numberOfKeywords' }),
|
||||
).toBeDisabled()
|
||||
expect(screen.getByRole('button', { name: 'Increment value' })).toBeDisabled()
|
||||
})
|
||||
|
||||
it('should hide the economical option for non-general chunk structures', () => {
|
||||
render(
|
||||
<IndexMethod
|
||||
chunkStructure={ChunkStructureEnum.parent_child}
|
||||
indexMethod={IndexMethodEnum.QUALIFIED}
|
||||
keywordNumber={5}
|
||||
onIndexMethodChange={vi.fn()}
|
||||
onKeywordNumberChange={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(screen.getByText('datasetCreation.stepTwo.qualified')).toBeInTheDocument()
|
||||
expect(screen.queryByText('datasetSettings.form.indexMethodEconomy')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import OptionCard from '../option-card'
|
||||
|
||||
describe('OptionCard', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
// The card should expose selection, child expansion, and readonly click behavior.
|
||||
describe('Interaction', () => {
|
||||
it('should not trigger selection when the card is readonly', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onClick = vi.fn()
|
||||
|
||||
render(<OptionCard id="economical" title="Economical" readonly onClick={onClick} />)
|
||||
|
||||
await user.click(screen.getByText('Economical'))
|
||||
|
||||
expect(onClick).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should expose selection to keyboard users while keeping expanded inputs separate', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onClick = vi.fn()
|
||||
const { rerender } = render(
|
||||
<OptionCard id="economical" title="Economical" onClick={onClick} />,
|
||||
)
|
||||
|
||||
const choice = screen.getByRole('button', { name: 'Economical' })
|
||||
expect(choice).toHaveAttribute('aria-pressed', 'false')
|
||||
expect(screen.queryByRole('spinbutton', { name: 'Keywords' })).not.toBeInTheDocument()
|
||||
await user.tab()
|
||||
expect(choice).toHaveFocus()
|
||||
await user.keyboard('{Enter}')
|
||||
expect(onClick).toHaveBeenCalledWith('economical')
|
||||
|
||||
rerender(
|
||||
<OptionCard id="economical" selectedId="economical" title="Economical" onClick={onClick}>
|
||||
<label>
|
||||
Keywords
|
||||
<input type="number" defaultValue={5} />
|
||||
</label>
|
||||
</OptionCard>,
|
||||
)
|
||||
|
||||
expect(choice).toHaveAttribute('aria-pressed', 'true')
|
||||
onClick.mockClear()
|
||||
await user.tab()
|
||||
const input = screen.getByRole('spinbutton', { name: 'Keywords' })
|
||||
expect(input).toHaveFocus()
|
||||
await user.clear(input)
|
||||
await user.type(input, '10')
|
||||
expect(input).toHaveValue(10)
|
||||
expect(onClick).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should keep a nonselectable summary outside the tab order', () => {
|
||||
render(<OptionCard id="general" title="General" enableSelect={false} />)
|
||||
expect(screen.getByText('General')).toBeInTheDocument()
|
||||
expect(screen.queryByRole('button')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
import { screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
|
||||
import { ChunkStructureEnum } from '../../../types'
|
||||
import ChunkStructure from '../index'
|
||||
|
||||
it('selects an initial chunk structure and changes it through the selected-card action', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onChange = vi.fn()
|
||||
function Harness() {
|
||||
const [structure, setStructure] = useState<ChunkStructureEnum>()
|
||||
return (
|
||||
<ChunkStructure
|
||||
chunkStructure={structure}
|
||||
onChunkStructureChange={(next) => {
|
||||
setStructure(next)
|
||||
onChange(next)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
render(<Harness />)
|
||||
await user.click(
|
||||
screen.getByRole('button', { name: 'workflow.nodes.knowledgeBase.chooseChunkStructure' }),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: /datasetCreation.stepTwo.general/ }))
|
||||
expect(onChange).toHaveBeenLastCalledWith(ChunkStructureEnum.general)
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.queryByText('workflow.nodes.knowledgeBase.changeChunkStructure'),
|
||||
).not.toBeInTheDocument(),
|
||||
)
|
||||
expect(screen.getByText('datasetCreation.stepTwo.general')).toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'workflow.panel.change' }))
|
||||
await user.click(screen.getByRole('button', { name: /datasetCreation.stepTwo.parentChild/ }))
|
||||
expect(onChange).toHaveBeenLastCalledWith(ChunkStructureEnum.parent_child)
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.queryByText('workflow.nodes.knowledgeBase.changeChunkStructure'),
|
||||
).not.toBeInTheDocument(),
|
||||
)
|
||||
expect(screen.getByText('datasetCreation.stepTwo.parentChild')).toBeInTheDocument()
|
||||
expect(screen.queryByText('datasetCreation.stepTwo.general')).not.toBeInTheDocument()
|
||||
})
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useKeyPress } from 'reactflow'
|
||||
import { ChunkStructureEnum } from '../../../types'
|
||||
import Selector from '../selector'
|
||||
|
||||
// Exercise the same document-level shortcut mounted by the workflow canvas.
|
||||
function CanvasPanShortcut() {
|
||||
useKeyPress('Space')
|
||||
return null
|
||||
}
|
||||
|
||||
const options = [
|
||||
{
|
||||
id: ChunkStructureEnum.general,
|
||||
icon: <span>G</span>,
|
||||
title: 'General',
|
||||
description: 'General description',
|
||||
effectColor: 'blue',
|
||||
},
|
||||
{
|
||||
id: ChunkStructureEnum.parent_child,
|
||||
icon: <span>P</span>,
|
||||
title: 'Parent child',
|
||||
description: 'Parent child description',
|
||||
effectColor: 'purple',
|
||||
},
|
||||
]
|
||||
|
||||
describe('ChunkStructureSelector', () => {
|
||||
it('should open the selector panel and close it after selecting an option', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onChange = vi.fn()
|
||||
|
||||
render(
|
||||
<>
|
||||
<CanvasPanShortcut />
|
||||
<Selector options={options} value={ChunkStructureEnum.general} onChange={onChange} />
|
||||
</>,
|
||||
)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'workflow.panel.change' }))
|
||||
|
||||
expect(
|
||||
screen.getByRole('dialog', { name: 'workflow.nodes.knowledgeBase.changeChunkStructure' }),
|
||||
).toBeInTheDocument()
|
||||
|
||||
expect(screen.getByRole('button', { name: /General/, pressed: true })).toBeInTheDocument()
|
||||
const choice = screen.getByRole('button', { name: /Parent child/, pressed: false })
|
||||
choice.focus()
|
||||
await user.keyboard(' ')
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith(ChunkStructureEnum.parent_child)
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.queryByText('workflow.nodes.knowledgeBase.changeChunkStructure'),
|
||||
).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
it('should expose one custom trigger and open it from the keyboard', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(
|
||||
<>
|
||||
<CanvasPanShortcut />
|
||||
<Selector
|
||||
options={options}
|
||||
onChange={vi.fn()}
|
||||
trigger={<button type="button">Choose structure</button>}
|
||||
/>
|
||||
</>,
|
||||
)
|
||||
expect(screen.getAllByRole('button')).toHaveLength(1)
|
||||
await user.tab()
|
||||
expect(screen.getByRole('button', { name: 'Choose structure' })).toHaveFocus()
|
||||
await user.keyboard(' ')
|
||||
expect(await screen.findByRole('button', { name: /General/ })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should not open the selector when readonly is enabled', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(
|
||||
<Selector
|
||||
options={options}
|
||||
onChange={vi.fn()}
|
||||
readonly
|
||||
trigger={<button type="button">custom-trigger</button>}
|
||||
/>,
|
||||
)
|
||||
|
||||
const trigger = screen.getByRole('button', { name: 'custom-trigger' })
|
||||
expect(trigger).toBeDisabled()
|
||||
await user.click(trigger)
|
||||
|
||||
expect(
|
||||
screen.queryByText('workflow.nodes.knowledgeBase.changeChunkStructure'),
|
||||
).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
import type { Option } from './type'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { ChunkStructureEnum } from '../../types'
|
||||
|
||||
export const useChunkStructure = () => {
|
||||
const { t } = useTranslation(['datasetCreation'])
|
||||
const GeneralOption: Option = {
|
||||
id: ChunkStructureEnum.general,
|
||||
icon: (isActive: boolean) => (
|
||||
<span
|
||||
aria-hidden
|
||||
className={cn(
|
||||
'i-custom-vender-knowledge-general-chunk h-4.5 w-4.5',
|
||||
cn(
|
||||
'h-4.5 w-4.5 text-text-tertiary group-hover:text-util-colors-indigo-indigo-600',
|
||||
isActive && 'text-util-colors-indigo-indigo-600',
|
||||
),
|
||||
)}
|
||||
/>
|
||||
),
|
||||
title: t(($) => $['stepTwo.general'], { ns: 'datasetCreation' }),
|
||||
description: t(($) => $['stepTwo.generalTip'], { ns: 'datasetCreation' }),
|
||||
effectColor: 'blue',
|
||||
}
|
||||
const ParentChildOption: Option = {
|
||||
id: ChunkStructureEnum.parent_child,
|
||||
icon: (isActive: boolean) => (
|
||||
<span
|
||||
aria-hidden
|
||||
className={cn(
|
||||
'i-custom-vender-knowledge-parent-child-chunk h-4.5 w-4.5',
|
||||
cn(
|
||||
'h-4.5 w-4.5 text-text-tertiary group-hover:text-util-colors-blue-light-blue-light-500',
|
||||
isActive && 'text-util-colors-blue-light-blue-light-500',
|
||||
),
|
||||
)}
|
||||
/>
|
||||
),
|
||||
title: t(($) => $['stepTwo.parentChild'], { ns: 'datasetCreation' }),
|
||||
description: t(($) => $['stepTwo.parentChildTip'], { ns: 'datasetCreation' }),
|
||||
effectColor: 'blue-light',
|
||||
}
|
||||
const QuestionAnswerOption: Option = {
|
||||
id: ChunkStructureEnum.question_answer,
|
||||
icon: (isActive: boolean) => (
|
||||
<span
|
||||
aria-hidden
|
||||
className={cn(
|
||||
'i-custom-vender-knowledge-question-and-answer h-4.5 w-4.5',
|
||||
cn(
|
||||
'h-4.5 w-4.5 text-text-tertiary group-hover:text-util-colors-teal-teal-600',
|
||||
isActive && 'text-util-colors-teal-teal-600',
|
||||
),
|
||||
)}
|
||||
/>
|
||||
),
|
||||
title: 'Q&A',
|
||||
description: t(($) => $['stepTwo.qaTip'], { ns: 'datasetCreation' }),
|
||||
effectColor: 'teal',
|
||||
}
|
||||
|
||||
const optionMap: Record<ChunkStructureEnum, Option> = {
|
||||
[ChunkStructureEnum.general]: GeneralOption,
|
||||
[ChunkStructureEnum.parent_child]: ParentChildOption,
|
||||
[ChunkStructureEnum.question_answer]: QuestionAnswerOption,
|
||||
}
|
||||
|
||||
const options = [GeneralOption, ParentChildOption, QuestionAnswerOption]
|
||||
|
||||
return {
|
||||
options,
|
||||
optionMap,
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
import type { ChunkStructureEnum } from '../../types'
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { memo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Field } from '@/app/components/workflow/nodes/_base/components/layout'
|
||||
import OptionCard from '../option-card'
|
||||
import { useChunkStructure } from './hooks'
|
||||
import Instruction from './instruction'
|
||||
import Selector from './selector'
|
||||
|
||||
type ChunkStructureProps = {
|
||||
chunkStructure?: ChunkStructureEnum
|
||||
onChunkStructureChange: (value: ChunkStructureEnum) => void
|
||||
warningDot?: boolean
|
||||
readonly?: boolean
|
||||
}
|
||||
const ChunkStructure = ({
|
||||
chunkStructure,
|
||||
onChunkStructureChange,
|
||||
warningDot = false,
|
||||
readonly = false,
|
||||
}: ChunkStructureProps) => {
|
||||
const { t } = useTranslation(['workflow'])
|
||||
const { options, optionMap } = useChunkStructure()
|
||||
|
||||
return (
|
||||
<Field
|
||||
fieldTitleProps={{
|
||||
title: t(($) => $['nodes.knowledgeBase.chunkStructure'], { ns: 'workflow' }),
|
||||
tooltip: t(($) => $['nodes.knowledgeBase.chunkStructureTip.message'], { ns: 'workflow' }),
|
||||
warningDot,
|
||||
operation: chunkStructure && (
|
||||
<Selector
|
||||
options={options}
|
||||
value={chunkStructure}
|
||||
onChange={onChunkStructureChange}
|
||||
readonly={readonly}
|
||||
/>
|
||||
),
|
||||
}}
|
||||
>
|
||||
{!!chunkStructure && (
|
||||
<OptionCard
|
||||
{...optionMap[chunkStructure]}
|
||||
selectedId={chunkStructure}
|
||||
enableSelect={false}
|
||||
enableHighlightBorder={false}
|
||||
/>
|
||||
)}
|
||||
{!chunkStructure && (
|
||||
<>
|
||||
<Selector
|
||||
options={options}
|
||||
onChange={onChunkStructureChange}
|
||||
readonly={readonly}
|
||||
trigger={
|
||||
<Button className="w-full" variant="secondary-accent">
|
||||
<span className="i-ri-add-line size-4" />
|
||||
{t(($) => $['nodes.knowledgeBase.chooseChunkStructure'], { ns: 'workflow' })}
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<Instruction className="mt-2" />
|
||||
</>
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(ChunkStructure)
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import Instruction from '../index'
|
||||
|
||||
const mockUseDocLink = vi.hoisted(() => vi.fn())
|
||||
|
||||
vi.mock('@/context/i18n', () => ({
|
||||
useDocLink: mockUseDocLink,
|
||||
}))
|
||||
|
||||
describe('ChunkStructureInstruction', () => {
|
||||
beforeEach(() => {
|
||||
mockUseDocLink.mockReturnValue((path: string) => `https://docs.example.com${path}`)
|
||||
})
|
||||
|
||||
// The instruction card should render the learning copy and link to the chunking guide.
|
||||
describe('Rendering', () => {
|
||||
it('links to the chunking guide', () => {
|
||||
render(<Instruction />)
|
||||
|
||||
expect(
|
||||
screen.getByRole('link', {
|
||||
name: 'workflow.nodes.knowledgeBase.chunkStructureTip.learnMore',
|
||||
}),
|
||||
).toHaveAttribute(
|
||||
'href',
|
||||
'https://docs.example.com/use-dify/knowledge/create-knowledge/chunking-and-cleaning-text',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useDocLink } from '@/context/i18n'
|
||||
import Line from './line'
|
||||
|
||||
type InstructionProps = {
|
||||
className?: string
|
||||
}
|
||||
|
||||
const Instruction = ({ className }: InstructionProps) => {
|
||||
const { t } = useTranslation(['workflow'])
|
||||
const docLink = useDocLink()
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'flex flex-col gap-y-2 overflow-hidden rounded-[10px] bg-workflow-process-bg p-4',
|
||||
className,
|
||||
)}
|
||||
>
|
||||
<div className="relative flex size-10 items-center justify-center rounded-[10px] border-[0.5px] border-components-card-border bg-components-card-bg shadow-lg backdrop-blur-[5px]">
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-knowledge-add-chunks size-5 text-text-accent"
|
||||
/>
|
||||
<Line className="absolute -bottom-19 -left-px" type="vertical" />
|
||||
<Line className="absolute -right-px -bottom-19" type="vertical" />
|
||||
<Line className="absolute -top-px -right-46" type="horizontal" />
|
||||
<Line className="absolute -right-46 -bottom-px" type="horizontal" />
|
||||
</div>
|
||||
<div className="flex flex-col gap-y-1">
|
||||
<div className="system-sm-medium text-text-secondary">
|
||||
{t(($) => $['nodes.knowledgeBase.chunkStructureTip.title'], { ns: 'workflow' })}
|
||||
</div>
|
||||
<div className="system-xs-regular">
|
||||
<p className="text-text-tertiary">
|
||||
{t(($) => $['nodes.knowledgeBase.chunkStructureTip.message'], { ns: 'workflow' })}
|
||||
</p>
|
||||
<a
|
||||
href={docLink('/use-dify/knowledge/create-knowledge/chunking-and-cleaning-text')}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-text-accent"
|
||||
>
|
||||
{t(($) => $['nodes.knowledgeBase.chunkStructureTip.learnMore'], { ns: 'workflow' })}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Instruction)
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
import * as React from 'react'
|
||||
|
||||
type LineProps = {
|
||||
type?: 'vertical' | 'horizontal'
|
||||
className?: string
|
||||
}
|
||||
|
||||
const Line = ({ type = 'vertical', className }: LineProps) => {
|
||||
if (type === 'vertical') {
|
||||
return (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="2"
|
||||
height="132"
|
||||
viewBox="0 0 2 132"
|
||||
fill="none"
|
||||
className={className}
|
||||
>
|
||||
<path d="M1 0L1 132" stroke="url(#paint0_linear_10882_18766)" />
|
||||
<defs>
|
||||
<linearGradient
|
||||
id="paint0_linear_10882_18766"
|
||||
x1="-7.99584"
|
||||
y1="132"
|
||||
x2="-7.96108"
|
||||
y2="6.4974e-07"
|
||||
gradientUnits="userSpaceOnUse"
|
||||
>
|
||||
<stop stopColor="var(--color-background-gradient-mask-transparent)" />
|
||||
<stop offset="0.877606" stopColor="var(--color-divider-subtle)" />
|
||||
<stop offset="1" stopColor="var(--color-background-gradient-mask-transparent)" />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="240"
|
||||
height="2"
|
||||
viewBox="0 0 240 2"
|
||||
fill="none"
|
||||
className={className}
|
||||
>
|
||||
<path d="M0 1H240" stroke="url(#paint0_linear_10882_18763)" />
|
||||
<defs>
|
||||
<linearGradient
|
||||
id="paint0_linear_10882_18763"
|
||||
x1="240"
|
||||
y1="9.99584"
|
||||
x2="3.95539e-05"
|
||||
y2="9.88094"
|
||||
gradientUnits="userSpaceOnUse"
|
||||
>
|
||||
<stop stopColor="var(--color-background-gradient-mask-transparent)" />
|
||||
<stop offset="0.9031" stopColor="var(--color-divider-subtle)" />
|
||||
<stop offset="1" stopColor="var(--color-background-gradient-mask-transparent)" />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Line)
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
import type { ReactElement } from 'react'
|
||||
import type { ChunkStructureEnum } from '../../types'
|
||||
import type { Option } from './type'
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { Popover, PopoverContent, PopoverTitle, PopoverTrigger } from '@langgenius/dify-ui/popover'
|
||||
import { useCallback, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import OptionCard from '../option-card'
|
||||
|
||||
type SelectorProps = {
|
||||
options: Option[]
|
||||
value?: ChunkStructureEnum
|
||||
onChange: (key: ChunkStructureEnum) => void
|
||||
readonly?: boolean
|
||||
trigger?: ReactElement
|
||||
}
|
||||
const Selector = ({ options, value, onChange, readonly, trigger }: SelectorProps) => {
|
||||
const { t } = useTranslation(['workflow'])
|
||||
const [open, setOpen] = useState(false)
|
||||
|
||||
const handleSelect = useCallback(
|
||||
(optionId: ChunkStructureEnum) => {
|
||||
onChange(optionId)
|
||||
setOpen(false)
|
||||
},
|
||||
[onChange],
|
||||
)
|
||||
const handleOpenChange = useCallback(
|
||||
(nextOpen: boolean) => {
|
||||
if (readonly && nextOpen) return
|
||||
setOpen(nextOpen)
|
||||
},
|
||||
[readonly],
|
||||
)
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={handleOpenChange}>
|
||||
{/* Keep Space activation outside React Flow's document-level pan shortcut. */}
|
||||
{trigger ? (
|
||||
<PopoverTrigger className="nokey" disabled={readonly} render={trigger} />
|
||||
) : (
|
||||
<PopoverTrigger
|
||||
className="nokey"
|
||||
disabled={readonly}
|
||||
render={<Button size="small" variant="ghost-accent" />}
|
||||
>
|
||||
{t(($) => $['panel.change'], { ns: 'workflow' })}
|
||||
</PopoverTrigger>
|
||||
)}
|
||||
<PopoverContent
|
||||
align="end"
|
||||
sideOffset={0}
|
||||
alignOffset={-8}
|
||||
className="border-none bg-transparent shadow-none"
|
||||
>
|
||||
<div className="w-101 rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-xl backdrop-blur-[5px]">
|
||||
<PopoverTitle className="px-3 pt-3.5 system-sm-semibold text-text-primary">
|
||||
{t(($) => $['nodes.knowledgeBase.changeChunkStructure'], { ns: 'workflow' })}
|
||||
</PopoverTitle>
|
||||
<div className="space-y-1 p-3 pt-2">
|
||||
{options.map((option) => (
|
||||
<OptionCard
|
||||
key={option.id}
|
||||
id={option.id}
|
||||
selectedId={value}
|
||||
icon={option.icon}
|
||||
title={option.title}
|
||||
description={option.description}
|
||||
readonly={readonly}
|
||||
onClick={handleSelect}
|
||||
effectColor={option.effectColor}
|
||||
></OptionCard>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
export default Selector
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import type { ChunkStructureEnum } from '../../types'
|
||||
|
||||
export type Option = {
|
||||
id: ChunkStructureEnum
|
||||
icon: ReactNode | ((isActive: boolean) => ReactNode)
|
||||
title: string
|
||||
description: string
|
||||
effectColor?: string
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
import type { DefaultModel } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { memo, useCallback, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import {
|
||||
MultimodalRetrievalGuidance,
|
||||
MultimodalRetrievalGuidanceLearnMore,
|
||||
} from '@/app/components/datasets/common/multimodal-retrieval-guidance'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { ModelSelector } from '@/app/components/header/account-setting/model-provider-page/model-selector'
|
||||
import { Field } from '@/app/components/workflow/nodes/_base/components/layout'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
|
||||
type EmbeddingModelProps = {
|
||||
embeddingModel?: string
|
||||
embeddingModelProvider?: string
|
||||
onEmbeddingModelChange?: (model: {
|
||||
embeddingModel: string
|
||||
embeddingModelProvider: string
|
||||
}) => void
|
||||
warningDot?: boolean
|
||||
readonly?: boolean
|
||||
}
|
||||
const EmbeddingModel = ({
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
onEmbeddingModelChange,
|
||||
warningDot = false,
|
||||
readonly = false,
|
||||
}: EmbeddingModelProps) => {
|
||||
const { t } = useTranslation(['datasetSettings'])
|
||||
const { data: embeddingModelList = [] } = useQuery(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryOptions({
|
||||
input: { params: { model_type: ModelTypeEnum.textEmbedding } },
|
||||
select: (response) => response.data,
|
||||
}),
|
||||
)
|
||||
const embeddingModelConfig = useMemo(() => {
|
||||
if (!embeddingModel || !embeddingModelProvider) return undefined
|
||||
|
||||
return {
|
||||
providerName: embeddingModelProvider,
|
||||
modelName: embeddingModel,
|
||||
}
|
||||
}, [embeddingModel, embeddingModelProvider])
|
||||
|
||||
const handleEmbeddingModelChange = useCallback(
|
||||
(model: DefaultModel) => {
|
||||
onEmbeddingModelChange?.({
|
||||
embeddingModelProvider: model.provider,
|
||||
embeddingModel: model.model,
|
||||
})
|
||||
},
|
||||
[onEmbeddingModelChange],
|
||||
)
|
||||
|
||||
return (
|
||||
<Field
|
||||
fieldTitleProps={{
|
||||
title: t(($) => $['form.embeddingModel'], { ns: 'datasetSettings' }),
|
||||
subTitle: <MultimodalRetrievalGuidanceLearnMore />,
|
||||
warningDot,
|
||||
}}
|
||||
>
|
||||
<MultimodalRetrievalGuidance
|
||||
variant="pipeline"
|
||||
embeddingModel={
|
||||
embeddingModelConfig && {
|
||||
provider: embeddingModelConfig.providerName,
|
||||
model: embeddingModelConfig.modelName,
|
||||
}
|
||||
}
|
||||
embeddingModelList={embeddingModelList}
|
||||
className="mb-2"
|
||||
/>
|
||||
<ModelSelector
|
||||
value={
|
||||
embeddingModelConfig && {
|
||||
provider: embeddingModelConfig.providerName,
|
||||
model: embeddingModelConfig.modelName,
|
||||
}
|
||||
}
|
||||
models={embeddingModelList}
|
||||
onValueChange={handleEmbeddingModelChange}
|
||||
disabled={readonly}
|
||||
showDeprecatedWarnIcon
|
||||
/>
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
export default memo(EmbeddingModel)
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
|
||||
import {
|
||||
NumberField,
|
||||
NumberFieldControls,
|
||||
NumberFieldDecrement,
|
||||
NumberFieldGroup,
|
||||
NumberFieldIncrement,
|
||||
NumberFieldInput,
|
||||
} from '@langgenius/dify-ui/number-field'
|
||||
import {
|
||||
Slider,
|
||||
SliderControl,
|
||||
SliderIndicator,
|
||||
SliderLabel,
|
||||
SliderThumb,
|
||||
SliderTrack,
|
||||
} from '@langgenius/dify-ui/slider'
|
||||
import { memo, useCallback, useId } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Field } from '@/app/components/workflow/nodes/_base/components/layout'
|
||||
import { ChunkStructureEnum, IndexMethodEnum } from '../types'
|
||||
import OptionCard from './option-card'
|
||||
|
||||
type IndexMethodProps = {
|
||||
chunkStructure: ChunkStructureEnum
|
||||
indexMethod?: IndexMethodEnum
|
||||
onIndexMethodChange: (value: IndexMethodEnum) => void
|
||||
keywordNumber: number
|
||||
onKeywordNumberChange: (value: number) => void
|
||||
readonly?: boolean
|
||||
}
|
||||
const IndexMethod = ({
|
||||
chunkStructure,
|
||||
indexMethod,
|
||||
onIndexMethodChange,
|
||||
keywordNumber,
|
||||
onKeywordNumberChange,
|
||||
readonly = false,
|
||||
}: IndexMethodProps) => {
|
||||
const { t } = useTranslation(['datasetCreation', 'datasetSettings'])
|
||||
const keywordInputId = useId()
|
||||
const keywordNumberLabel = t(($) => $['form.numberOfKeywords'], { ns: 'datasetSettings' })
|
||||
const isHighQuality = indexMethod === IndexMethodEnum.QUALIFIED
|
||||
const isEconomy = indexMethod === IndexMethodEnum.ECONOMICAL
|
||||
|
||||
const handleIndexMethodChange = useCallback(
|
||||
(newIndexMethod: IndexMethodEnum) => {
|
||||
onIndexMethodChange(newIndexMethod)
|
||||
},
|
||||
[onIndexMethodChange],
|
||||
)
|
||||
|
||||
const handleInputChange = useCallback(
|
||||
(value: number | null) => {
|
||||
if (value !== null) onKeywordNumberChange(value)
|
||||
},
|
||||
[onKeywordNumberChange],
|
||||
)
|
||||
|
||||
return (
|
||||
<Field
|
||||
fieldTitleProps={{
|
||||
title: t(($) => $['stepTwo.indexMode'], { ns: 'datasetCreation' }),
|
||||
}}
|
||||
>
|
||||
<div className="space-y-1">
|
||||
<OptionCard<IndexMethodEnum>
|
||||
id={IndexMethodEnum.QUALIFIED}
|
||||
selectedId={indexMethod}
|
||||
icon={
|
||||
<span
|
||||
aria-hidden
|
||||
className={cn(
|
||||
'i-custom-vender-knowledge-high-quality h-4.5 w-4.5',
|
||||
cn(
|
||||
'h-3.75 w-3.75 text-text-tertiary group-hover:text-util-colors-orange-orange-500',
|
||||
isHighQuality && 'text-util-colors-orange-orange-500',
|
||||
),
|
||||
)}
|
||||
/>
|
||||
}
|
||||
title={t(($) => $['stepTwo.qualified'], { ns: 'datasetCreation' })}
|
||||
description={t(($) => $['form.indexMethodHighQualityTip'], { ns: 'datasetSettings' })}
|
||||
onClick={handleIndexMethodChange}
|
||||
isRecommended
|
||||
effectColor="orange"
|
||||
></OptionCard>
|
||||
{chunkStructure === ChunkStructureEnum.general && (
|
||||
<OptionCard
|
||||
id={IndexMethodEnum.ECONOMICAL}
|
||||
selectedId={indexMethod}
|
||||
icon={
|
||||
<span
|
||||
aria-hidden
|
||||
className={cn(
|
||||
'i-custom-vender-knowledge-economic h-4.5 w-4.5',
|
||||
cn(
|
||||
'h-3.75 w-3.75 text-text-tertiary group-hover:text-util-colors-indigo-indigo-500',
|
||||
isEconomy && 'text-util-colors-indigo-indigo-500',
|
||||
),
|
||||
)}
|
||||
/>
|
||||
}
|
||||
title={t(($) => $['form.indexMethodEconomy'], { ns: 'datasetSettings' })}
|
||||
description={t(($) => $['form.indexMethodEconomyTip'], {
|
||||
ns: 'datasetSettings',
|
||||
count: keywordNumber,
|
||||
})}
|
||||
onClick={handleIndexMethodChange}
|
||||
effectColor="blue"
|
||||
>
|
||||
<Fieldset className="flex items-center">
|
||||
<FieldsetLegend className="sr-only">{keywordNumberLabel}</FieldsetLegend>
|
||||
<div className="flex grow items-center">
|
||||
<label
|
||||
htmlFor={keywordInputId}
|
||||
className="truncate system-xs-medium text-text-secondary"
|
||||
>
|
||||
{keywordNumberLabel}
|
||||
</label>
|
||||
</div>
|
||||
<Slider
|
||||
disabled={readonly}
|
||||
className="mr-3 w-24 shrink-0"
|
||||
value={keywordNumber}
|
||||
onValueChange={onKeywordNumberChange}
|
||||
>
|
||||
<SliderLabel className="sr-only">{keywordNumberLabel}</SliderLabel>
|
||||
<SliderControl>
|
||||
<SliderTrack>
|
||||
<SliderIndicator />
|
||||
<SliderThumb />
|
||||
</SliderTrack>
|
||||
</SliderControl>
|
||||
</Slider>
|
||||
<NumberField
|
||||
id={keywordInputId}
|
||||
disabled={readonly}
|
||||
className="w-18 shrink-0"
|
||||
min={0}
|
||||
format={{ maximumFractionDigits: 0 }}
|
||||
value={keywordNumber}
|
||||
onValueChange={handleInputChange}
|
||||
>
|
||||
<NumberFieldGroup>
|
||||
<NumberFieldInput className="px-2" />
|
||||
<NumberFieldControls>
|
||||
<NumberFieldIncrement />
|
||||
<NumberFieldDecrement />
|
||||
</NumberFieldControls>
|
||||
</NumberFieldGroup>
|
||||
</NumberField>
|
||||
</Fieldset>
|
||||
</OptionCard>
|
||||
)}
|
||||
</div>
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(IndexMethod)
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { Toggle } from '@langgenius/dify-ui/toggle'
|
||||
import { memo, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Badge from '@/app/components/base/badge'
|
||||
|
||||
const HEADER_EFFECT_MAP: Record<string, ReactNode> = {
|
||||
blue: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-blue h-31 w-53.5" />
|
||||
),
|
||||
'blue-light': (
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-public-knowledge-option-card-effect-blue-light h-18.5 w-53"
|
||||
/>
|
||||
),
|
||||
orange: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-orange h-55 w-55" />
|
||||
),
|
||||
purple: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-purple h-55 w-55" />
|
||||
),
|
||||
teal: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-teal h-23 w-53" />
|
||||
),
|
||||
}
|
||||
type OptionCardProps<T> = {
|
||||
id?: T
|
||||
selectedId?: T
|
||||
enableSelect?: boolean
|
||||
enableHighlightBorder?: boolean
|
||||
enableRadio?: boolean
|
||||
wrapperClassName?: string | ((isActive: boolean) => string)
|
||||
className?: string | ((isActive: boolean) => string)
|
||||
icon?: ReactNode | ((isActive: boolean) => ReactNode)
|
||||
title: string
|
||||
description?: string
|
||||
isRecommended?: boolean
|
||||
children?: ReactNode
|
||||
effectColor?: string
|
||||
onClick?: (id: T) => void
|
||||
readonly?: boolean
|
||||
}
|
||||
const OptionCard = memo(
|
||||
({
|
||||
id,
|
||||
selectedId,
|
||||
enableSelect = true,
|
||||
enableHighlightBorder = true,
|
||||
enableRadio,
|
||||
wrapperClassName,
|
||||
className,
|
||||
icon,
|
||||
title,
|
||||
description,
|
||||
isRecommended,
|
||||
children,
|
||||
effectColor,
|
||||
onClick,
|
||||
readonly,
|
||||
}) => {
|
||||
const { t } = useTranslation(['datasetCreation'])
|
||||
const isActive = useMemo(() => {
|
||||
return id === selectedId
|
||||
}, [id, selectedId])
|
||||
|
||||
const effectElement = useMemo(() => {
|
||||
if (effectColor) {
|
||||
return (
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
'absolute -top-0.5 -left-0.5 hidden h-14 w-14 rounded-full',
|
||||
'group-hover:block',
|
||||
isActive && 'block',
|
||||
)}
|
||||
>
|
||||
{HEADER_EFFECT_MAP[effectColor]}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}, [effectColor, isActive])
|
||||
|
||||
const Header = enableSelect ? Toggle : 'div'
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'group overflow-hidden rounded-xl border border-components-option-card-option-border bg-components-option-card-option-bg',
|
||||
isActive &&
|
||||
enableHighlightBorder &&
|
||||
'border-[1.5px] border-components-option-card-option-selected-border',
|
||||
enableSelect && 'cursor-pointer hover:shadow-xs',
|
||||
readonly && 'cursor-not-allowed',
|
||||
wrapperClassName &&
|
||||
(typeof wrapperClassName === 'function'
|
||||
? wrapperClassName(isActive)
|
||||
: wrapperClassName),
|
||||
)}
|
||||
>
|
||||
<Header
|
||||
{...(enableSelect
|
||||
? {
|
||||
pressed: isActive,
|
||||
disabled: readonly,
|
||||
onPressedChange: () => {
|
||||
if (id !== undefined) onClick?.(id)
|
||||
},
|
||||
onClick: (event: React.MouseEvent) => event.stopPropagation(),
|
||||
}
|
||||
: {})}
|
||||
className={cn(
|
||||
'relative flex w-full rounded-t-xl p-2 text-left',
|
||||
// React Flow otherwise consumes Space before the native button can activate.
|
||||
enableSelect &&
|
||||
'nokey cursor-pointer focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden focus-visible:ring-inset disabled:cursor-not-allowed',
|
||||
className && (typeof className === 'function' ? className(isActive) : className),
|
||||
)}
|
||||
>
|
||||
{effectElement}
|
||||
{!!icon && (
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className="mr-1 flex h-4.5 w-4.5 shrink-0 items-center justify-center"
|
||||
>
|
||||
{typeof icon === 'function' ? icon(isActive) : icon}
|
||||
</span>
|
||||
)}
|
||||
<span className="block grow py-1 pt-px">
|
||||
<span className="flex items-center">
|
||||
<span className="flex grow items-center system-sm-medium text-text-secondary">
|
||||
{title}
|
||||
{isRecommended && (
|
||||
<Badge
|
||||
as="span"
|
||||
className="ml-1 h-4 border-text-accent-secondary text-text-accent-secondary"
|
||||
>
|
||||
{t(($) => $['stepTwo.recommend'], { ns: 'datasetCreation' })}
|
||||
</Badge>
|
||||
)}
|
||||
</span>
|
||||
{enableRadio && (
|
||||
<span
|
||||
className={cn(
|
||||
'ml-2 size-4 shrink-0 rounded-full border border-components-radio-border bg-components-radio-bg',
|
||||
isActive && 'border-[5px] border-components-radio-border-checked',
|
||||
)}
|
||||
></span>
|
||||
)}
|
||||
</span>
|
||||
{description && (
|
||||
<span className="mt-1 block system-xs-regular text-text-tertiary">{description}</span>
|
||||
)}
|
||||
</span>
|
||||
</Header>
|
||||
{!!(children && isActive) && (
|
||||
<div className="relative rounded-b-xl bg-components-panel-bg p-3">
|
||||
<span
|
||||
aria-hidden
|
||||
className="absolute -top-2.75 left-3.5 i-custom-vender-knowledge-arrow-shape h-4 w-4 text-components-panel-bg"
|
||||
/>
|
||||
{children}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
) as <T>(props: OptionCardProps<T>) => React.ReactElement
|
||||
|
||||
export default OptionCard
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { renderHook } from '@testing-library/react'
|
||||
import { HybridSearchModeEnum, IndexMethodEnum, RetrievalSearchMethodEnum } from '../../../types'
|
||||
import { useRetrievalSetting } from '../hooks'
|
||||
|
||||
describe('useRetrievalSetting', () => {
|
||||
// The hook should switch between economical and qualified retrieval option sets.
|
||||
describe('Options', () => {
|
||||
it('should return semantic, full-text, and hybrid options for qualified indexing', () => {
|
||||
const { result } = renderHook(() => useRetrievalSetting(IndexMethodEnum.QUALIFIED))
|
||||
|
||||
expect(result.current.options.map((option) => option.id)).toEqual([
|
||||
RetrievalSearchMethodEnum.semantic,
|
||||
RetrievalSearchMethodEnum.fullText,
|
||||
RetrievalSearchMethodEnum.hybrid,
|
||||
])
|
||||
expect(result.current.hybridSearchModeOptions.map((option) => option.id)).toEqual([
|
||||
HybridSearchModeEnum.WeightedScore,
|
||||
HybridSearchModeEnum.RerankingModel,
|
||||
])
|
||||
})
|
||||
|
||||
it('should return only keyword search for economical indexing', () => {
|
||||
const { result } = renderHook(() => useRetrievalSetting(IndexMethodEnum.ECONOMICAL))
|
||||
|
||||
expect(result.current.options.map((option) => option.id)).toEqual([
|
||||
RetrievalSearchMethodEnum.keywordSearch,
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { createDocLinkMock, resolveDocLink } from '@/app/components/workflow/__tests__/i18n'
|
||||
import { IndexMethodEnum, RetrievalSearchMethodEnum } from '../../../types'
|
||||
import RetrievalSetting from '../index'
|
||||
|
||||
const mockUseDocLink = createDocLinkMock()
|
||||
|
||||
vi.mock('@/context/i18n', () => ({
|
||||
useDocLink: () => mockUseDocLink,
|
||||
}))
|
||||
|
||||
const baseProps = {
|
||||
onRetrievalSearchMethodChange: vi.fn(),
|
||||
onHybridSearchModeChange: vi.fn(),
|
||||
onWeightedScoreChange: vi.fn(),
|
||||
onTopKChange: vi.fn(),
|
||||
onScoreThresholdChange: vi.fn(),
|
||||
onScoreThresholdEnabledChange: vi.fn(),
|
||||
onRerankingModelEnabledChange: vi.fn(),
|
||||
onRerankingModelChange: vi.fn(),
|
||||
topK: 3,
|
||||
scoreThreshold: 0.5,
|
||||
isScoreThresholdEnabled: false,
|
||||
}
|
||||
|
||||
describe('RetrievalSetting', () => {
|
||||
it('links to retrieval documentation and selects a qualified search method', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<RetrievalSetting {...baseProps} indexMethod={IndexMethodEnum.QUALIFIED} />)
|
||||
|
||||
expect(
|
||||
screen.getByRole('link', { name: 'datasetSettings.form.retrievalSetting.learnMore' }),
|
||||
).toHaveAttribute(
|
||||
'href',
|
||||
resolveDocLink('/use-dify/knowledge/create-knowledge/setting-indexing-methods'),
|
||||
)
|
||||
expect(screen.getByText('dataset.retrieval.semantic_search.title')).toBeInTheDocument()
|
||||
expect(screen.getByText('dataset.retrieval.full_text_search.title')).toBeInTheDocument()
|
||||
expect(screen.getByText('dataset.retrieval.hybrid_search.title')).toBeInTheDocument()
|
||||
await user.click(
|
||||
screen.getByRole('radio', { name: /dataset.retrieval.full_text_search.title/ }),
|
||||
)
|
||||
expect(baseProps.onRetrievalSearchMethodChange).toHaveBeenCalledWith(
|
||||
RetrievalSearchMethodEnum.fullText,
|
||||
)
|
||||
})
|
||||
|
||||
it('should render only the economical retrieval method for economical indexing', () => {
|
||||
render(<RetrievalSetting {...baseProps} indexMethod={IndexMethodEnum.ECONOMICAL} />)
|
||||
|
||||
expect(screen.getByText('dataset.retrieval.keyword_search.title')).toBeInTheDocument()
|
||||
expect(screen.queryByText('dataset.retrieval.semantic_search.title')).not.toBeInTheDocument()
|
||||
expect(screen.queryByText('dataset.retrieval.hybrid_search.title')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
import type { RerankingModel } from '../../../types'
|
||||
import { render, screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { NuqsTestingAdapter } from 'nuqs/adapters/testing'
|
||||
import { useState } from 'react'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import {
|
||||
createModel,
|
||||
createModelItem,
|
||||
} from '@/app/components/workflow/__tests__/model-provider-fixtures'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import {
|
||||
createModelCreditsFixture,
|
||||
createProviderSummaryFixture,
|
||||
} from '@/test/console/model-provider'
|
||||
import { createConsoleQueryWrapper } from '@/test/console/query-data'
|
||||
import RerankingModelSelector from '../reranking-model-selector'
|
||||
|
||||
const modelList = [
|
||||
createModel({
|
||||
provider: 'cohere',
|
||||
label: { en_US: 'Cohere', zh_Hans: 'Cohere' },
|
||||
models: [
|
||||
createModelItem({
|
||||
model: 'rerank-v3',
|
||||
model_type: ModelTypeEnum.rerank,
|
||||
label: { en_US: 'Rerank V3', zh_Hans: 'Rerank V3' },
|
||||
}),
|
||||
],
|
||||
}),
|
||||
]
|
||||
|
||||
vi.mock(
|
||||
'@/app/components/header/account-setting/model-provider-page/hooks',
|
||||
async (importOriginal) => ({
|
||||
...(await importOriginal<
|
||||
typeof import('@/app/components/header/account-setting/model-provider-page/hooks')
|
||||
>()),
|
||||
useModelListAndDefaultModel: () => ({ modelList, defaultModel: undefined }),
|
||||
}),
|
||||
)
|
||||
|
||||
it('selects a real rerank model and preserves the workflow field mapping on reopening', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onChange = vi.fn()
|
||||
const { queryClient, wrapper } = createConsoleQueryWrapper({
|
||||
systemFeatures: { enable_marketplace: false },
|
||||
})
|
||||
queryClient.setQueryData(consoleQuery.workspaces.current.modelProviders.summary.get.queryKey(), {
|
||||
data: [
|
||||
createProviderSummaryFixture({
|
||||
provider: 'cohere',
|
||||
label: { en_US: 'Cohere' },
|
||||
supported_model_types: ['rerank'],
|
||||
custom_configuration: { current_credential_usable: true },
|
||||
}),
|
||||
],
|
||||
plugins: {},
|
||||
})
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.workspaces.current.modelProviders.credits.get.queryKey(),
|
||||
createModelCreditsFixture(),
|
||||
)
|
||||
for (const model_type of ['llm', 'text-embedding', 'rerank', 'speech2text', 'tts'] as const) {
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryKey({
|
||||
input: { params: { model_type } },
|
||||
}),
|
||||
{ data: [] },
|
||||
)
|
||||
}
|
||||
function Picker() {
|
||||
const [model, setModel] = useState<RerankingModel>({
|
||||
reranking_provider_name: '',
|
||||
reranking_model_name: '',
|
||||
})
|
||||
return (
|
||||
<RerankingModelSelector
|
||||
rerankingModel={model}
|
||||
onRerankingModelChange={(next) => {
|
||||
onChange(next)
|
||||
setModel(next)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
render(
|
||||
<NuqsTestingAdapter>
|
||||
<Picker />
|
||||
</NuqsTestingAdapter>,
|
||||
{ wrapper },
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.detailPanel.configureModel' }))
|
||||
const dialog = await screen.findByRole('dialog')
|
||||
await user.click(within(dialog).getByRole('button', { name: /Rerank V3/ }))
|
||||
expect(onChange).toHaveBeenCalledExactlyOnceWith({
|
||||
reranking_provider_name: 'cohere',
|
||||
reranking_model_name: 'rerank-v3',
|
||||
})
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: /Rerank V3/ }))
|
||||
expect(
|
||||
within(await screen.findByRole('dialog')).getByRole('button', { name: /Rerank V3/ }),
|
||||
).toHaveAttribute('aria-current', 'true')
|
||||
})
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
import type { SearchMethodOptionProps } from '../search-method-option'
|
||||
import { Field } from '@langgenius/dify-ui/field'
|
||||
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
|
||||
import { RadioGroup } from '@langgenius/dify-ui/radio-group'
|
||||
import { screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import {
|
||||
createCatalogModelFixture,
|
||||
createModelCatalogFixture,
|
||||
seedModelProviderQueries,
|
||||
} from '@/test/console/model-provider'
|
||||
import { createConsoleQueryClient, renderWithConsoleQuery } from '@/test/console/query-data'
|
||||
import { createNuqsTestRenderer } from '@/test/nuqs-testing'
|
||||
import { HybridSearchModeEnum, RetrievalSearchMethodEnum, WeightedScoreEnum } from '../../../types'
|
||||
import { SearchMethodOption } from '../search-method-option'
|
||||
|
||||
const render = createNuqsTestRenderer((ui) => {
|
||||
const queryClient = createConsoleQueryClient()
|
||||
seedModelProviderQueries(queryClient, [
|
||||
createModelCatalogFixture({
|
||||
models: [
|
||||
createCatalogModelFixture({
|
||||
model: 'reranker',
|
||||
model_type: 'rerank',
|
||||
label: { en_US: 'Reranker' },
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
return renderWithConsoleQuery(ui, { queryClient })
|
||||
})
|
||||
|
||||
const hybridSearchModeOptions = [
|
||||
{
|
||||
id: HybridSearchModeEnum.WeightedScore,
|
||||
title: 'Weighted mode',
|
||||
description: 'Use weighted score',
|
||||
},
|
||||
{
|
||||
id: HybridSearchModeEnum.RerankingModel,
|
||||
title: 'Rerank mode',
|
||||
description: 'Use reranking model',
|
||||
},
|
||||
]
|
||||
|
||||
const weightedScore = {
|
||||
weight_type: WeightedScoreEnum.Customized,
|
||||
vector_setting: {
|
||||
vector_weight: 0.8,
|
||||
embedding_provider_name: 'openai',
|
||||
embedding_model_name: 'text-embedding-3-large',
|
||||
},
|
||||
keyword_setting: {
|
||||
keyword_weight: 0.2,
|
||||
},
|
||||
}
|
||||
|
||||
const createProps = () => ({
|
||||
option: {
|
||||
id: RetrievalSearchMethodEnum.semantic,
|
||||
iconClassName: 'i-custom-vender-knowledge-vector-search',
|
||||
title: 'Semantic title',
|
||||
description: 'Semantic description',
|
||||
effectColor: 'purple',
|
||||
},
|
||||
searchMethod: RetrievalSearchMethodEnum.semantic,
|
||||
onRetrievalSearchMethodChange: vi.fn(),
|
||||
hybridSearch: {
|
||||
mode: HybridSearchModeEnum.WeightedScore,
|
||||
options: hybridSearchModeOptions,
|
||||
onModeChange: vi.fn(),
|
||||
weightedScore,
|
||||
onWeightedScoreChange: vi.fn(),
|
||||
},
|
||||
reranking: {
|
||||
enabled: false,
|
||||
onEnabledChange: vi.fn(),
|
||||
rerankingModel: {
|
||||
reranking_provider_name: '',
|
||||
reranking_model_name: '',
|
||||
},
|
||||
onRerankingModelChange: vi.fn(),
|
||||
showMultiModalTip: false,
|
||||
},
|
||||
retrievalParameters: {
|
||||
topK: {
|
||||
value: 3,
|
||||
onChange: vi.fn(),
|
||||
},
|
||||
scoreThreshold: {
|
||||
value: 0.5,
|
||||
onChange: vi.fn(),
|
||||
enabled: true,
|
||||
onEnabledChange: vi.fn(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
function renderSearchMethodOption(
|
||||
props: SearchMethodOptionProps & {
|
||||
onRetrievalSearchMethodChange: (value: RetrievalSearchMethodEnum) => void
|
||||
},
|
||||
) {
|
||||
const { onRetrievalSearchMethodChange, ...optionProps } = props
|
||||
|
||||
render(
|
||||
<Field name="retrieval_search_method">
|
||||
<Fieldset
|
||||
render={
|
||||
<RadioGroup
|
||||
value={props.searchMethod}
|
||||
onValueChange={(value) => onRetrievalSearchMethodChange(value)}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<FieldsetLegend>Retrieval search method</FieldsetLegend>
|
||||
<SearchMethodOption {...optionProps} />
|
||||
</Fieldset>
|
||||
</Field>,
|
||||
)
|
||||
}
|
||||
|
||||
describe('SearchMethodOption', () => {
|
||||
it('should notify retrieval changes when an inactive option is selected', async () => {
|
||||
const user = userEvent.setup()
|
||||
const props = createProps()
|
||||
const fullTextProps = {
|
||||
...props,
|
||||
option: {
|
||||
...props.option,
|
||||
id: RetrievalSearchMethodEnum.fullText,
|
||||
title: 'Full-text title',
|
||||
},
|
||||
}
|
||||
|
||||
renderSearchMethodOption(fullTextProps)
|
||||
|
||||
expect(screen.getByText('Full-text title'))!.toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByText('Full-text title'))
|
||||
|
||||
expect(props.onRetrievalSearchMethodChange).toHaveBeenCalledWith(
|
||||
RetrievalSearchMethodEnum.fullText,
|
||||
)
|
||||
})
|
||||
|
||||
it('should render hybrid weighted-score controls without reranking model selector', async () => {
|
||||
const user = userEvent.setup()
|
||||
const props = createProps()
|
||||
const hybridProps = {
|
||||
...props,
|
||||
option: {
|
||||
...props.option,
|
||||
id: RetrievalSearchMethodEnum.hybrid,
|
||||
title: 'Hybrid title',
|
||||
},
|
||||
searchMethod: RetrievalSearchMethodEnum.hybrid,
|
||||
hybridSearch: {
|
||||
...props.hybridSearch,
|
||||
mode: HybridSearchModeEnum.WeightedScore,
|
||||
},
|
||||
reranking: {
|
||||
...props.reranking,
|
||||
showMultiModalTip: true,
|
||||
},
|
||||
}
|
||||
|
||||
renderSearchMethodOption(hybridProps)
|
||||
|
||||
expect(screen.getByText('Weighted mode'))!.toBeInTheDocument()
|
||||
expect(screen.getByText('Rerank mode'))!.toBeInTheDocument()
|
||||
expect(screen.getByTitle('dataset.weightedScore.semantic'))!.toBeVisible()
|
||||
expect(screen.getByTitle('dataset.weightedScore.keyword'))!.toBeVisible()
|
||||
expect(
|
||||
screen.queryByText('modelProvider.modelProvider.rerankModel.key'),
|
||||
).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.queryByText('datasetSettings.form.retrievalSetting.multiModalTip'),
|
||||
).not.toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByText('Rerank mode'))
|
||||
|
||||
expect(props.hybridSearch.onModeChange).toHaveBeenCalledWith(
|
||||
HybridSearchModeEnum.RerankingModel,
|
||||
)
|
||||
})
|
||||
|
||||
it('should render the hybrid reranking selector when reranking mode is selected', async () => {
|
||||
const user = userEvent.setup()
|
||||
const props = createProps()
|
||||
const hybridProps = {
|
||||
...props,
|
||||
option: {
|
||||
...props.option,
|
||||
id: RetrievalSearchMethodEnum.hybrid,
|
||||
title: 'Hybrid title',
|
||||
},
|
||||
searchMethod: RetrievalSearchMethodEnum.hybrid,
|
||||
hybridSearch: {
|
||||
...props.hybridSearch,
|
||||
mode: HybridSearchModeEnum.RerankingModel,
|
||||
},
|
||||
reranking: {
|
||||
...props.reranking,
|
||||
showMultiModalTip: true,
|
||||
},
|
||||
}
|
||||
|
||||
renderSearchMethodOption(hybridProps)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.detailPanel.configureModel' }))
|
||||
await user.click(await screen.findByRole('button', { name: /Reranker/ }))
|
||||
expect(props.reranking.onRerankingModelChange).toHaveBeenCalledWith({
|
||||
reranking_provider_name: 'openai',
|
||||
reranking_model_name: 'reranker',
|
||||
})
|
||||
expect(
|
||||
screen.queryByText('modelProvider.modelProvider.rerankModel.key'),
|
||||
).not.toBeInTheDocument()
|
||||
expect(screen.queryByText('dataset.weightedScore.semantic')).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByText('datasetSettings.form.retrievalSetting.multiModalTip'),
|
||||
)!.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should hide the score-threshold control for keyword search', async () => {
|
||||
const user = userEvent.setup()
|
||||
const props = createProps()
|
||||
const keywordProps = {
|
||||
...props,
|
||||
option: {
|
||||
...props.option,
|
||||
id: RetrievalSearchMethodEnum.keywordSearch,
|
||||
title: 'Keyword title',
|
||||
},
|
||||
searchMethod: RetrievalSearchMethodEnum.keywordSearch,
|
||||
}
|
||||
|
||||
renderSearchMethodOption(keywordProps)
|
||||
|
||||
await user.clear(screen.getByRole('textbox'))
|
||||
await user.type(screen.getByRole('textbox'), '9')
|
||||
|
||||
expect(screen.getAllByRole('textbox')).toHaveLength(1)
|
||||
expect(screen.queryAllByRole('switch')).toHaveLength(0)
|
||||
expect(props.retrievalParameters.topK.onChange).toHaveBeenCalledWith(9)
|
||||
})
|
||||
})
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { TopKAndScoreThreshold } from '../top-k-and-score-threshold'
|
||||
|
||||
const topKLabel = /datasetConfig\.top_k/
|
||||
const scoreThresholdLabel = /datasetConfig\.score_threshold/
|
||||
|
||||
function Settings({ onTopKChange = vi.fn(), onScoreChange = vi.fn() }) {
|
||||
const [topK, setTopK] = useState(3)
|
||||
const [score, setScore] = useState(0.4)
|
||||
const [enabled, setEnabled] = useState(true)
|
||||
return (
|
||||
<TopKAndScoreThreshold
|
||||
topK={{
|
||||
value: topK,
|
||||
onChange: (value) => {
|
||||
setTopK(value)
|
||||
onTopKChange(value)
|
||||
},
|
||||
}}
|
||||
scoreThreshold={{
|
||||
value: score,
|
||||
onChange: (value) => {
|
||||
setScore(value)
|
||||
onScoreChange(value)
|
||||
},
|
||||
enabled,
|
||||
onEnabledChange: setEnabled,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
it('preserves typed precision before committing retrieval values', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onTopKChange = vi.fn()
|
||||
const onScoreChange = vi.fn()
|
||||
render(<Settings onTopKChange={onTopKChange} onScoreChange={onScoreChange} />)
|
||||
const topK = screen.getByRole('textbox', { name: topKLabel })
|
||||
await user.clear(topK)
|
||||
await user.type(topK, '3.7')
|
||||
expect(onTopKChange).toHaveBeenLastCalledWith(3.7)
|
||||
const score = screen.getByRole('textbox', { name: scoreThresholdLabel })
|
||||
await user.clear(score)
|
||||
await user.type(score, '0.456')
|
||||
expect(onScoreChange).toHaveBeenLastCalledWith(0.456)
|
||||
})
|
||||
|
||||
it('maps clearing either retrieval value to zero', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onTopKChange = vi.fn()
|
||||
const onScoreChange = vi.fn()
|
||||
render(<Settings onTopKChange={onTopKChange} onScoreChange={onScoreChange} />)
|
||||
await user.clear(screen.getByRole('textbox', { name: topKLabel }))
|
||||
expect(onTopKChange).toHaveBeenLastCalledWith(0)
|
||||
await user.clear(screen.getByRole('textbox', { name: scoreThresholdLabel }))
|
||||
expect(onScoreChange).toHaveBeenLastCalledWith(0)
|
||||
})
|
||||
|
||||
it('disables threshold editing when filtering is switched off and restores its value when enabled', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<Settings />)
|
||||
const toggle = screen.getByRole('switch', { name: scoreThresholdLabel })
|
||||
const score = screen.getByRole('textbox', { name: scoreThresholdLabel })
|
||||
await user.click(toggle)
|
||||
expect(score).toBeDisabled()
|
||||
await user.click(toggle)
|
||||
expect(score).toBeEnabled()
|
||||
expect(score).toHaveValue('0.4')
|
||||
})
|
||||
|
||||
it('omits threshold controls when the retrieval mode hides them', () => {
|
||||
render(
|
||||
<TopKAndScoreThreshold
|
||||
topK={{ value: 3, onChange: vi.fn() }}
|
||||
scoreThreshold={{ hidden: true }}
|
||||
/>,
|
||||
)
|
||||
expect(screen.getByRole('textbox', { name: topKLabel })).toBeEnabled()
|
||||
expect(screen.queryByRole('textbox', { name: scoreThresholdLabel })).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('switch')).not.toBeInTheDocument()
|
||||
})
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
import type { HybridSearchModeOption, Option } from './type'
|
||||
import { useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { HybridSearchModeEnum, IndexMethodEnum, RetrievalSearchMethodEnum } from '../../types'
|
||||
|
||||
export const useRetrievalSetting = (indexMethod?: IndexMethodEnum) => {
|
||||
const { t } = useTranslation(['common', 'dataset', 'modelProvider'])
|
||||
const VectorSearchOption: Option = useMemo(() => {
|
||||
return {
|
||||
id: RetrievalSearchMethodEnum.semantic,
|
||||
iconClassName: 'i-custom-vender-knowledge-vector-search',
|
||||
title: t(($) => $['retrieval.semantic_search.title'], { ns: 'dataset' }),
|
||||
description: t(($) => $['retrieval.semantic_search.description'], { ns: 'dataset' }),
|
||||
effectColor: 'purple',
|
||||
}
|
||||
}, [t])
|
||||
const FullTextSearchOption: Option = useMemo(() => {
|
||||
return {
|
||||
id: RetrievalSearchMethodEnum.fullText,
|
||||
iconClassName: 'i-custom-vender-knowledge-full-text-search',
|
||||
title: t(($) => $['retrieval.full_text_search.title'], { ns: 'dataset' }),
|
||||
description: t(($) => $['retrieval.full_text_search.description'], { ns: 'dataset' }),
|
||||
effectColor: 'purple',
|
||||
}
|
||||
}, [t])
|
||||
const HybridSearchOption: Option = useMemo(() => {
|
||||
return {
|
||||
id: RetrievalSearchMethodEnum.hybrid,
|
||||
iconClassName: 'i-custom-vender-knowledge-hybrid-search',
|
||||
title: t(($) => $['retrieval.hybrid_search.title'], { ns: 'dataset' }),
|
||||
description: t(($) => $['retrieval.hybrid_search.description'], { ns: 'dataset' }),
|
||||
effectColor: 'purple',
|
||||
}
|
||||
}, [t])
|
||||
const InvertedIndexOption: Option = useMemo(() => {
|
||||
return {
|
||||
id: RetrievalSearchMethodEnum.keywordSearch,
|
||||
iconClassName: 'i-custom-vender-knowledge-hybrid-search',
|
||||
title: t(($) => $['retrieval.keyword_search.title'], { ns: 'dataset' }),
|
||||
description: t(($) => $['retrieval.keyword_search.description'], { ns: 'dataset' }),
|
||||
effectColor: 'purple',
|
||||
}
|
||||
}, [t])
|
||||
|
||||
const WeightedScoreModeOption: HybridSearchModeOption = useMemo(() => {
|
||||
return {
|
||||
id: HybridSearchModeEnum.WeightedScore,
|
||||
title: t(($) => $['weightedScore.title'], { ns: 'dataset' }),
|
||||
description: t(($) => $['weightedScore.description'], { ns: 'dataset' }),
|
||||
}
|
||||
}, [t])
|
||||
const RerankModelModeOption: HybridSearchModeOption = useMemo(() => {
|
||||
return {
|
||||
id: HybridSearchModeEnum.RerankingModel,
|
||||
title: t(($) => $['modelProvider.rerankModel.key'], { ns: 'modelProvider' }),
|
||||
description: t(($) => $['modelProvider.rerankModel.tip'], { ns: 'modelProvider' }),
|
||||
}
|
||||
}, [t])
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
options:
|
||||
indexMethod === IndexMethodEnum.ECONOMICAL
|
||||
? [InvertedIndexOption]
|
||||
: [VectorSearchOption, FullTextSearchOption, HybridSearchOption],
|
||||
hybridSearchModeOptions: [WeightedScoreModeOption, RerankModelModeOption],
|
||||
}),
|
||||
[
|
||||
VectorSearchOption,
|
||||
FullTextSearchOption,
|
||||
HybridSearchOption,
|
||||
InvertedIndexOption,
|
||||
indexMethod,
|
||||
WeightedScoreModeOption,
|
||||
RerankModelModeOption,
|
||||
],
|
||||
)
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
import type {
|
||||
HybridSearchModeEnum,
|
||||
IndexMethodEnum,
|
||||
RetrievalSearchMethodEnum,
|
||||
WeightedScore,
|
||||
} from '../../types'
|
||||
import type { RerankingModelSelectorProps } from './reranking-model-selector'
|
||||
import type { TopKFieldProps, VisibleScoreThresholdFieldProps } from './top-k-and-score-threshold'
|
||||
import { Field } from '@langgenius/dify-ui/field'
|
||||
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
|
||||
import { RadioGroup } from '@langgenius/dify-ui/radio-group'
|
||||
import { memo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Field as WorkflowField } from '@/app/components/workflow/nodes/_base/components/layout'
|
||||
import { useDocLink } from '@/context/i18n'
|
||||
import { useRetrievalSetting } from './hooks'
|
||||
import { SearchMethodOption } from './search-method-option'
|
||||
|
||||
type RetrievalSettingProps = {
|
||||
indexMethod?: IndexMethodEnum
|
||||
readonly?: boolean
|
||||
searchMethod?: RetrievalSearchMethodEnum
|
||||
onRetrievalSearchMethodChange: (value: RetrievalSearchMethodEnum) => void
|
||||
hybridSearchMode?: HybridSearchModeEnum
|
||||
onHybridSearchModeChange: (value: HybridSearchModeEnum) => void
|
||||
rerankingModelEnabled?: boolean
|
||||
onRerankingModelEnabledChange: (value: boolean) => void
|
||||
weightedScore?: WeightedScore
|
||||
onWeightedScoreChange: (value: { value: number[] }) => void
|
||||
showMultiModalTip?: boolean
|
||||
} & RerankingModelSelectorProps & {
|
||||
topK: TopKFieldProps['value']
|
||||
onTopKChange: TopKFieldProps['onChange']
|
||||
scoreThreshold: VisibleScoreThresholdFieldProps['value']
|
||||
onScoreThresholdChange: VisibleScoreThresholdFieldProps['onChange']
|
||||
isScoreThresholdEnabled?: VisibleScoreThresholdFieldProps['enabled']
|
||||
onScoreThresholdEnabledChange: VisibleScoreThresholdFieldProps['onEnabledChange']
|
||||
}
|
||||
|
||||
const RetrievalSetting = ({
|
||||
indexMethod,
|
||||
readonly,
|
||||
searchMethod,
|
||||
onRetrievalSearchMethodChange,
|
||||
hybridSearchMode,
|
||||
onHybridSearchModeChange,
|
||||
weightedScore,
|
||||
onWeightedScoreChange,
|
||||
rerankingModelEnabled,
|
||||
onRerankingModelEnabledChange,
|
||||
rerankingModel,
|
||||
onRerankingModelChange,
|
||||
topK,
|
||||
onTopKChange,
|
||||
scoreThreshold,
|
||||
onScoreThresholdChange,
|
||||
isScoreThresholdEnabled,
|
||||
onScoreThresholdEnabledChange,
|
||||
showMultiModalTip,
|
||||
}: RetrievalSettingProps) => {
|
||||
const { t } = useTranslation(['datasetSettings', 'workflow'])
|
||||
const docLink = useDocLink()
|
||||
const { options, hybridSearchModeOptions } = useRetrievalSetting(indexMethod)
|
||||
|
||||
return (
|
||||
<WorkflowField
|
||||
fieldTitleProps={{
|
||||
title: t(($) => $['form.retrievalSetting.title'], { ns: 'datasetSettings' }),
|
||||
subTitle: (
|
||||
<div className="flex items-center body-xs-regular text-text-tertiary">
|
||||
<a
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
href={docLink('/use-dify/knowledge/create-knowledge/setting-indexing-methods')}
|
||||
className="text-text-accent"
|
||||
>
|
||||
{t(($) => $['form.retrievalSetting.learnMore'], { ns: 'datasetSettings' })}
|
||||
</a>
|
||||
|
||||
{t(($) => $['nodes.knowledgeBase.aboutRetrieval'], { ns: 'workflow' })}
|
||||
</div>
|
||||
),
|
||||
}}
|
||||
>
|
||||
<Field name="retrieval_search_method" className="gap-0">
|
||||
<Fieldset
|
||||
render={
|
||||
<RadioGroup<RetrievalSearchMethodEnum>
|
||||
value={searchMethod}
|
||||
onValueChange={(value) => onRetrievalSearchMethodChange(value)}
|
||||
disabled={readonly}
|
||||
className="flex-col items-stretch gap-1"
|
||||
/>
|
||||
}
|
||||
>
|
||||
<FieldsetLegend className="sr-only">
|
||||
{t(($) => $['form.retrievalSetting.title'], { ns: 'datasetSettings' })}
|
||||
</FieldsetLegend>
|
||||
{options.map((option) => (
|
||||
<SearchMethodOption
|
||||
key={option.id}
|
||||
option={option}
|
||||
searchMethod={searchMethod}
|
||||
hybridSearch={{
|
||||
mode: hybridSearchMode,
|
||||
options: hybridSearchModeOptions,
|
||||
onModeChange: onHybridSearchModeChange,
|
||||
weightedScore,
|
||||
onWeightedScoreChange,
|
||||
}}
|
||||
retrievalParameters={{
|
||||
topK: {
|
||||
value: topK,
|
||||
onChange: onTopKChange,
|
||||
},
|
||||
scoreThreshold: {
|
||||
value: scoreThreshold,
|
||||
onChange: onScoreThresholdChange,
|
||||
enabled: isScoreThresholdEnabled,
|
||||
onEnabledChange: onScoreThresholdEnabledChange,
|
||||
},
|
||||
}}
|
||||
reranking={{
|
||||
enabled: rerankingModelEnabled,
|
||||
onEnabledChange: onRerankingModelEnabledChange,
|
||||
rerankingModel,
|
||||
onRerankingModelChange,
|
||||
showMultiModalTip,
|
||||
}}
|
||||
readonly={readonly}
|
||||
/>
|
||||
))}
|
||||
</Fieldset>
|
||||
</Field>
|
||||
</WorkflowField>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(RetrievalSetting)
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
import type { RerankingModel } from '../../types'
|
||||
import type { DefaultModel } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { memo, useMemo } from 'react'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { useModelListAndDefaultModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
import { ModelSelector } from '@/app/components/header/account-setting/model-provider-page/model-selector'
|
||||
|
||||
export type RerankingModelSelectorProps = {
|
||||
rerankingModel?: RerankingModel
|
||||
onRerankingModelChange?: (model: RerankingModel) => void
|
||||
readonly?: boolean
|
||||
}
|
||||
const RerankingModelSelector = ({
|
||||
rerankingModel,
|
||||
onRerankingModelChange,
|
||||
readonly = false,
|
||||
}: RerankingModelSelectorProps) => {
|
||||
const { modelList: rerankModelList } = useModelListAndDefaultModel(ModelTypeEnum.rerank)
|
||||
const rerankModel = useMemo(() => {
|
||||
if (!rerankingModel?.reranking_provider_name || !rerankingModel?.reranking_model_name)
|
||||
return undefined
|
||||
|
||||
return {
|
||||
provider: rerankingModel.reranking_provider_name,
|
||||
model: rerankingModel.reranking_model_name,
|
||||
}
|
||||
}, [rerankingModel])
|
||||
|
||||
const handleRerankingModelChange = (model: DefaultModel) => {
|
||||
onRerankingModelChange?.({
|
||||
reranking_provider_name: model.provider,
|
||||
reranking_model_name: model.model,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<ModelSelector
|
||||
value={rerankModel}
|
||||
models={rerankModelList}
|
||||
onValueChange={handleRerankingModelChange}
|
||||
disabled={readonly}
|
||||
showDeprecatedWarnIcon
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(RerankingModelSelector)
|
||||
+344
@@ -0,0 +1,344 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import type { WeightedScore } from '../../types'
|
||||
import type { RerankingModelSelectorProps } from './reranking-model-selector'
|
||||
import type { TopKFieldProps, VisibleScoreThresholdFieldProps } from './top-k-and-score-threshold'
|
||||
import type { HybridSearchModeOption, Option } from './type'
|
||||
import { Alert, AlertTitle } from '@langgenius/dify-ui/alert'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
|
||||
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
|
||||
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
|
||||
import { RadioControl, RadioGroup, RadioItem } from '@langgenius/dify-ui/radio-group'
|
||||
import { Switch } from '@langgenius/dify-ui/switch'
|
||||
import { useId } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import WeightedScoreComponent from '@/app/components/app/configuration/dataset-config/params-config/weighted-score'
|
||||
import Badge from '@/app/components/base/badge'
|
||||
import { DEFAULT_WEIGHTED_SCORE } from '@/models/datasets'
|
||||
import { HybridSearchModeEnum, RetrievalSearchMethodEnum } from '../../types'
|
||||
import RerankingModelSelector from './reranking-model-selector'
|
||||
import { TopKAndScoreThreshold } from './top-k-and-score-threshold'
|
||||
|
||||
type HybridSearchConfig = {
|
||||
mode?: HybridSearchModeEnum
|
||||
options: HybridSearchModeOption[]
|
||||
onModeChange: (value: HybridSearchModeEnum) => void
|
||||
weightedScore?: WeightedScore
|
||||
onWeightedScoreChange: (value: { value: number[] }) => void
|
||||
}
|
||||
|
||||
type RerankingConfig = RerankingModelSelectorProps & {
|
||||
enabled?: boolean
|
||||
onEnabledChange: (value: boolean) => void
|
||||
showMultiModalTip?: boolean
|
||||
}
|
||||
|
||||
type RetrievalParametersConfig = {
|
||||
topK: TopKFieldProps
|
||||
scoreThreshold: VisibleScoreThresholdFieldProps
|
||||
}
|
||||
|
||||
type SearchMethodRadioCardProps = {
|
||||
option: Option
|
||||
searchMethod?: RetrievalSearchMethodEnum
|
||||
readonly?: boolean
|
||||
isRecommended?: boolean
|
||||
children?: ReactNode
|
||||
}
|
||||
|
||||
export type SearchMethodOptionProps = {
|
||||
readonly?: boolean
|
||||
option: Option
|
||||
searchMethod?: RetrievalSearchMethodEnum
|
||||
hybridSearch: HybridSearchConfig
|
||||
reranking: RerankingConfig
|
||||
retrievalParameters: RetrievalParametersConfig
|
||||
}
|
||||
|
||||
const HEADER_EFFECT_MAP: Record<string, ReactNode> = {
|
||||
blue: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-blue h-31 w-53.5" />
|
||||
),
|
||||
'blue-light': (
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-public-knowledge-option-card-effect-blue-light h-18.5 w-53"
|
||||
/>
|
||||
),
|
||||
orange: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-orange h-55 w-55" />
|
||||
),
|
||||
purple: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-purple h-55 w-55" />
|
||||
),
|
||||
teal: (
|
||||
<span aria-hidden className="i-custom-public-knowledge-option-card-effect-teal h-23 w-53" />
|
||||
),
|
||||
}
|
||||
|
||||
function getWeightedScoreValue(weightedScore?: WeightedScore) {
|
||||
const semanticWeightedScore =
|
||||
weightedScore?.vector_setting.vector_weight ?? DEFAULT_WEIGHTED_SCORE.other.semantic
|
||||
const keywordWeightedScore =
|
||||
weightedScore?.keyword_setting.keyword_weight ?? DEFAULT_WEIGHTED_SCORE.other.keyword
|
||||
|
||||
return {
|
||||
value: [semanticWeightedScore, keywordWeightedScore],
|
||||
}
|
||||
}
|
||||
|
||||
function shouldShowRerankModelSelectorSwitch(searchMethod?: RetrievalSearchMethodEnum) {
|
||||
return (
|
||||
searchMethod === RetrievalSearchMethodEnum.semantic ||
|
||||
searchMethod === RetrievalSearchMethodEnum.fullText
|
||||
)
|
||||
}
|
||||
|
||||
function shouldShowRerankModelSelector(
|
||||
searchMethod: RetrievalSearchMethodEnum | undefined,
|
||||
hybridSearchMode: HybridSearchModeEnum | undefined,
|
||||
) {
|
||||
if (shouldShowRerankModelSelectorSwitch(searchMethod)) return true
|
||||
|
||||
return (
|
||||
searchMethod === RetrievalSearchMethodEnum.hybrid &&
|
||||
hybridSearchMode !== HybridSearchModeEnum.WeightedScore
|
||||
)
|
||||
}
|
||||
|
||||
function getSearchMethodEffect(effectColor: string | undefined, isActive: boolean) {
|
||||
const effect = effectColor ? HEADER_EFFECT_MAP[effectColor] : undefined
|
||||
|
||||
if (!effect) return null
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'absolute -top-0.5 -left-0.5 hidden h-14 w-14 rounded-full',
|
||||
'group-hover/search-method-radio:block',
|
||||
isActive && 'block',
|
||||
)}
|
||||
>
|
||||
{effect}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function renderSearchMethodIcon(iconClassName: Option['iconClassName'], isActive: boolean) {
|
||||
return (
|
||||
<span
|
||||
aria-hidden
|
||||
className={cn(
|
||||
iconClassName,
|
||||
'h-3.75 w-3.75 text-text-tertiary group-hover:text-util-colors-purple-purple-600',
|
||||
isActive && 'text-util-colors-purple-purple-600',
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SearchMethodRadioCard({
|
||||
option,
|
||||
searchMethod,
|
||||
readonly,
|
||||
isRecommended,
|
||||
children,
|
||||
}: SearchMethodRadioCardProps) {
|
||||
const { t } = useTranslation(['datasetCreation'])
|
||||
const isActive = option.id === searchMethod
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'group/search-method-radio overflow-hidden rounded-xl border border-components-option-card-option-border bg-components-option-card-option-bg',
|
||||
'has-data-checked:border-[1.5px] has-data-checked:border-components-option-card-option-selected-border',
|
||||
!readonly && 'cursor-pointer hover:shadow-xs',
|
||||
readonly && 'cursor-not-allowed',
|
||||
)}
|
||||
>
|
||||
<RadioItem<RetrievalSearchMethodEnum>
|
||||
value={option.id}
|
||||
nativeButton
|
||||
render={<button type="button" />}
|
||||
disabled={readonly}
|
||||
className={cn(
|
||||
'relative flex w-full rounded-t-xl p-2 text-left outline-hidden focus-visible:ring-1 focus-visible:ring-components-input-border-active',
|
||||
'cursor-pointer data-disabled:cursor-not-allowed',
|
||||
)}
|
||||
>
|
||||
{getSearchMethodEffect(option.effectColor, isActive)}
|
||||
<div className="mr-1 flex h-4.5 w-4.5 shrink-0 items-center justify-center">
|
||||
{renderSearchMethodIcon(option.iconClassName, isActive)}
|
||||
</div>
|
||||
<div className="grow py-1 pt-px">
|
||||
<div className="flex items-center">
|
||||
<div className="flex grow items-center system-sm-medium text-text-secondary">
|
||||
{option.title}
|
||||
{isRecommended ? (
|
||||
<Badge className="ml-1 h-4 border-text-accent-secondary text-text-accent-secondary">
|
||||
{t(($) => $['stepTwo.recommend'], { ns: 'datasetCreation' })}
|
||||
</Badge>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
{option.description ? (
|
||||
<div className="mt-1 system-xs-regular text-text-tertiary">{option.description}</div>
|
||||
) : null}
|
||||
</div>
|
||||
</RadioItem>
|
||||
{!!(children && isActive) && (
|
||||
<div className="relative rounded-b-xl bg-components-panel-bg p-3">
|
||||
<div className="absolute -top-2.75 left-3.5 i-custom-vender-knowledge-arrow-shape h-4 w-4 text-components-panel-bg" />
|
||||
{children}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function HybridSearchModeRadioCard({
|
||||
option,
|
||||
readonly,
|
||||
}: {
|
||||
option: HybridSearchModeOption
|
||||
readonly?: boolean
|
||||
}) {
|
||||
return (
|
||||
<FieldItem>
|
||||
<RadioItem<HybridSearchModeEnum>
|
||||
value={option.id}
|
||||
nativeButton
|
||||
render={<button type="button" />}
|
||||
disabled={readonly}
|
||||
className={cn(
|
||||
'w-full rounded-xl border border-components-option-card-option-border bg-components-option-card-option-bg p-3 text-left outline-hidden',
|
||||
'data-checked:border-[1.5px] data-checked:bg-components-option-card-option-selected-bg',
|
||||
'focus-visible:ring-1 focus-visible:ring-components-input-border-active',
|
||||
'cursor-pointer hover:shadow-xs data-disabled:cursor-not-allowed data-disabled:hover:shadow-none',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-start gap-2">
|
||||
<div className="min-w-0 grow">
|
||||
<div className="system-sm-medium text-text-secondary">{option.title}</div>
|
||||
<div className="mt-1 system-xs-regular text-text-tertiary">{option.description}</div>
|
||||
</div>
|
||||
<RadioControl className="mt-0.5" aria-hidden="true" />
|
||||
</div>
|
||||
</RadioItem>
|
||||
</FieldItem>
|
||||
)
|
||||
}
|
||||
|
||||
export function SearchMethodOption({
|
||||
readonly,
|
||||
option,
|
||||
searchMethod,
|
||||
hybridSearch,
|
||||
reranking,
|
||||
retrievalParameters,
|
||||
}: SearchMethodOptionProps) {
|
||||
const rerankLabelId = useId()
|
||||
|
||||
const { t } = useTranslation(['datasetSettings', 'modelProvider'])
|
||||
const isHybridSearch = option.id === RetrievalSearchMethodEnum.hybrid
|
||||
const isHybridSearchWeightedScoreMode = hybridSearch.mode === HybridSearchModeEnum.WeightedScore
|
||||
const showRerankModelSelectorSwitch = shouldShowRerankModelSelectorSwitch(option.id)
|
||||
const showRerankModelSelector = shouldShowRerankModelSelector(option.id, hybridSearch.mode)
|
||||
const rerankModelLabel = t(($) => $['modelProvider.rerankModel.key'], { ns: 'modelProvider' })
|
||||
const rerankModelTip = t(($) => $['modelProvider.rerankModel.tip'], { ns: 'modelProvider' })
|
||||
const scoreThresholdHidden = option.id === RetrievalSearchMethodEnum.keywordSearch
|
||||
const config = (
|
||||
<div className="space-y-3">
|
||||
{isHybridSearch ? (
|
||||
<Field name="hybrid_search_mode" className="gap-0">
|
||||
<Fieldset
|
||||
render={
|
||||
<RadioGroup<HybridSearchModeEnum>
|
||||
value={hybridSearch.mode}
|
||||
onValueChange={(value) => hybridSearch.onModeChange(value)}
|
||||
disabled={readonly}
|
||||
className="flex-col items-stretch gap-1"
|
||||
/>
|
||||
}
|
||||
>
|
||||
<FieldsetLegend className="sr-only">Hybrid search mode</FieldsetLegend>
|
||||
{hybridSearch.options.map((hybridOption) => (
|
||||
<HybridSearchModeRadioCard
|
||||
key={hybridOption.id}
|
||||
option={hybridOption}
|
||||
readonly={readonly}
|
||||
/>
|
||||
))}
|
||||
</Fieldset>
|
||||
</Field>
|
||||
) : null}
|
||||
{isHybridSearch && isHybridSearchWeightedScoreMode ? (
|
||||
<WeightedScoreComponent
|
||||
value={getWeightedScoreValue(hybridSearch.weightedScore)}
|
||||
onChange={hybridSearch.onWeightedScoreChange}
|
||||
readonly={readonly}
|
||||
/>
|
||||
) : null}
|
||||
{showRerankModelSelector ? (
|
||||
<div>
|
||||
{showRerankModelSelectorSwitch ? (
|
||||
<Field name="reranking_model_enabled" className="mb-1 gap-0">
|
||||
<div className="flex items-center">
|
||||
<FieldLabel className="flex min-w-0 cursor-pointer items-center py-0 system-sm-semibold text-text-secondary has-data-disabled:cursor-not-allowed">
|
||||
<Switch
|
||||
className="mr-1"
|
||||
checked={reranking.enabled ?? false}
|
||||
onCheckedChange={reranking.onEnabledChange}
|
||||
disabled={readonly}
|
||||
/>
|
||||
<span id={rerankLabelId} className="truncate">
|
||||
{rerankModelLabel}
|
||||
</span>
|
||||
</FieldLabel>
|
||||
<Infotip>
|
||||
<InfotipTrigger
|
||||
aria-labelledby={rerankLabelId}
|
||||
className="ml-0.5 size-3.5 shrink-0"
|
||||
/>
|
||||
<InfotipContent aria-labelledby={rerankLabelId}>{rerankModelTip}</InfotipContent>
|
||||
</Infotip>
|
||||
</div>
|
||||
</Field>
|
||||
) : null}
|
||||
<RerankingModelSelector
|
||||
rerankingModel={reranking.rerankingModel}
|
||||
onRerankingModelChange={reranking.onRerankingModelChange}
|
||||
readonly={readonly}
|
||||
/>
|
||||
{reranking.showMultiModalTip ? (
|
||||
<Alert type="warning" className="mt-2">
|
||||
<AlertTitle>
|
||||
{t(($) => $['form.retrievalSetting.multiModalTip'], { ns: 'datasetSettings' })}
|
||||
</AlertTitle>
|
||||
</Alert>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
<TopKAndScoreThreshold
|
||||
topK={retrievalParameters.topK}
|
||||
scoreThreshold={
|
||||
scoreThresholdHidden ? { hidden: true } : retrievalParameters.scoreThreshold
|
||||
}
|
||||
readonly={readonly}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
return (
|
||||
<FieldItem>
|
||||
<SearchMethodRadioCard
|
||||
option={option}
|
||||
searchMethod={searchMethod}
|
||||
isRecommended={option.id === RetrievalSearchMethodEnum.hybrid}
|
||||
readonly={readonly}
|
||||
>
|
||||
{config}
|
||||
</SearchMethodRadioCard>
|
||||
</FieldItem>
|
||||
)
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
import { Field, FieldLabel } from '@langgenius/dify-ui/field'
|
||||
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
|
||||
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
|
||||
import {
|
||||
NumberField,
|
||||
NumberFieldControls,
|
||||
NumberFieldDecrement,
|
||||
NumberFieldGroup,
|
||||
NumberFieldIncrement,
|
||||
NumberFieldInput,
|
||||
} from '@langgenius/dify-ui/number-field'
|
||||
import { Switch } from '@langgenius/dify-ui/switch'
|
||||
import { useId } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { env } from '@/env'
|
||||
|
||||
export type TopKFieldProps = {
|
||||
value: number
|
||||
onChange: (value: number) => void
|
||||
}
|
||||
|
||||
export type VisibleScoreThresholdFieldProps = {
|
||||
hidden?: false
|
||||
value?: number
|
||||
onChange: (value: number) => void
|
||||
enabled?: boolean
|
||||
onEnabledChange: (value: boolean) => void
|
||||
}
|
||||
|
||||
type ScoreThresholdFieldProps =
|
||||
| VisibleScoreThresholdFieldProps
|
||||
| {
|
||||
hidden: true
|
||||
}
|
||||
|
||||
export type TopKAndScoreThresholdProps = {
|
||||
topK: TopKFieldProps
|
||||
scoreThreshold: ScoreThresholdFieldProps
|
||||
readonly?: boolean
|
||||
}
|
||||
|
||||
const TOP_K_VALUE_LIMIT = {
|
||||
step: 1,
|
||||
min: 1,
|
||||
max: env.NEXT_PUBLIC_TOP_K_MAX_VALUE,
|
||||
}
|
||||
const SCORE_THRESHOLD_VALUE_LIMIT = {
|
||||
step: 0.01,
|
||||
min: 0,
|
||||
max: 1,
|
||||
}
|
||||
|
||||
export function TopKAndScoreThreshold({
|
||||
topK,
|
||||
scoreThreshold,
|
||||
readonly,
|
||||
}: TopKAndScoreThresholdProps) {
|
||||
const scoreThresholdLabelId = useId()
|
||||
const topKLabelId = useId()
|
||||
|
||||
const { t } = useTranslation(['appDebug'])
|
||||
const topKLabel = t(($) => $['datasetConfig.top_k'], { ns: 'appDebug' })
|
||||
const scoreThresholdLabel = t(($) => $['datasetConfig.score_threshold'], { ns: 'appDebug' })
|
||||
const topKTip = t(($) => $['datasetConfig.top_kTip'], { ns: 'appDebug' })
|
||||
const scoreThresholdTip = t(($) => $['datasetConfig.score_thresholdTip'], { ns: 'appDebug' })
|
||||
const scoreThresholdHidden = scoreThreshold.hidden === true
|
||||
const scoreThresholdEnabled = scoreThresholdHidden ? false : (scoreThreshold.enabled ?? false)
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<Field name="top_k" className="gap-0">
|
||||
<div className="mb-0.5 flex h-6 items-center">
|
||||
<FieldLabel id={topKLabelId} className="py-0 system-xs-medium text-text-secondary">
|
||||
{topKLabel}
|
||||
</FieldLabel>
|
||||
<Infotip>
|
||||
<InfotipTrigger aria-labelledby={topKLabelId} className="ml-0.5 size-3.5" />
|
||||
<InfotipContent aria-labelledby={topKLabelId}>{topKTip}</InfotipContent>
|
||||
</Infotip>
|
||||
</div>
|
||||
<NumberField
|
||||
disabled={readonly}
|
||||
step={TOP_K_VALUE_LIMIT.step}
|
||||
min={TOP_K_VALUE_LIMIT.min}
|
||||
max={TOP_K_VALUE_LIMIT.max}
|
||||
value={topK.value}
|
||||
onValueChange={(value) => topK.onChange(value ?? 0)}
|
||||
>
|
||||
<NumberFieldGroup>
|
||||
<NumberFieldInput />
|
||||
<NumberFieldControls>
|
||||
<NumberFieldIncrement />
|
||||
<NumberFieldDecrement />
|
||||
</NumberFieldControls>
|
||||
</NumberFieldGroup>
|
||||
</NumberField>
|
||||
</Field>
|
||||
{scoreThresholdHidden ? null : (
|
||||
<Fieldset className="min-w-0">
|
||||
<FieldsetLegend className="sr-only">{scoreThresholdLabel}</FieldsetLegend>
|
||||
<Field name="score_threshold_enabled" className="mb-0.5 gap-0">
|
||||
<div className="flex h-6 items-center">
|
||||
<FieldLabel className="flex w-full min-w-0 grow cursor-pointer items-center py-0 system-sm-medium text-text-secondary has-data-disabled:cursor-not-allowed">
|
||||
<Switch
|
||||
className="mr-2"
|
||||
checked={scoreThresholdEnabled}
|
||||
onCheckedChange={scoreThreshold.onEnabledChange}
|
||||
disabled={readonly}
|
||||
/>
|
||||
<span id={scoreThresholdLabelId} className="grow truncate">
|
||||
{scoreThresholdLabel}
|
||||
</span>
|
||||
</FieldLabel>
|
||||
<Infotip>
|
||||
<InfotipTrigger
|
||||
aria-labelledby={scoreThresholdLabelId}
|
||||
className="ml-0.5 size-3.5"
|
||||
/>
|
||||
<InfotipContent aria-labelledby={scoreThresholdLabelId}>
|
||||
{scoreThresholdTip}
|
||||
</InfotipContent>
|
||||
</Infotip>
|
||||
</div>
|
||||
</Field>
|
||||
<Field name="score_threshold" className="gap-0">
|
||||
<FieldLabel className="sr-only">{scoreThresholdLabel}</FieldLabel>
|
||||
<NumberField
|
||||
disabled={readonly || !scoreThresholdEnabled}
|
||||
step={SCORE_THRESHOLD_VALUE_LIMIT.step}
|
||||
min={SCORE_THRESHOLD_VALUE_LIMIT.min}
|
||||
max={SCORE_THRESHOLD_VALUE_LIMIT.max}
|
||||
value={scoreThreshold.value ?? null}
|
||||
onValueChange={(value) => scoreThreshold.onChange(value ?? 0)}
|
||||
>
|
||||
<NumberFieldGroup>
|
||||
<NumberFieldInput />
|
||||
<NumberFieldControls>
|
||||
<NumberFieldIncrement />
|
||||
<NumberFieldDecrement />
|
||||
</NumberFieldControls>
|
||||
</NumberFieldGroup>
|
||||
</NumberField>
|
||||
</Field>
|
||||
</Fieldset>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import type { HybridSearchModeEnum, RetrievalSearchMethodEnum } from '../../types'
|
||||
|
||||
export type Option = {
|
||||
id: RetrievalSearchMethodEnum
|
||||
iconClassName: string
|
||||
title: React.ReactNode
|
||||
description: string
|
||||
effectColor?: string
|
||||
showEffectColor?: boolean
|
||||
}
|
||||
|
||||
export type HybridSearchModeOption = {
|
||||
id: HybridSearchModeEnum
|
||||
title: string
|
||||
description: string
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import type { NodeDefault } from '../../types'
|
||||
import type { KnowledgeBaseNodeType } from './types'
|
||||
import { BlockEnum } from '@/app/components/workflow/types'
|
||||
import { genNodeMetaData } from '@/app/components/workflow/utils'
|
||||
import { getKnowledgeBaseValidationIssue, getKnowledgeBaseValidationMessage } from './utils'
|
||||
|
||||
const metaData = genNodeMetaData({
|
||||
sort: 3.1,
|
||||
type: BlockEnum.KnowledgeBase,
|
||||
isRequired: true,
|
||||
isUndeletable: true,
|
||||
isSingleton: true,
|
||||
isTypeFixed: true,
|
||||
})
|
||||
const nodeDefault: NodeDefault<KnowledgeBaseNodeType> = {
|
||||
metaData,
|
||||
defaultValue: {
|
||||
index_chunk_variable_selector: [],
|
||||
keyword_number: 10,
|
||||
retrieval_model: {
|
||||
top_k: 3,
|
||||
score_threshold_enabled: false,
|
||||
score_threshold: 0.5,
|
||||
},
|
||||
},
|
||||
checkValid(payload, t) {
|
||||
const issue = getKnowledgeBaseValidationIssue(payload)
|
||||
if (issue) return { isValid: false, errorMessage: getKnowledgeBaseValidationMessage(issue, t) }
|
||||
|
||||
return {
|
||||
isValid: true,
|
||||
errorMessage: '',
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default nodeDefault
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
import type { ModelProviderSummaryResponse } from '@dify/contracts/api/console/workspaces/types.gen'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import {
|
||||
createModel,
|
||||
createModelItem,
|
||||
} from '@/app/components/workflow/__tests__/model-provider-fixtures'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { createModelCreditsFixture } from '@/test/console/model-provider'
|
||||
import { createConsoleQueryClient, renderHookWithConsoleQuery } from '@/test/console/query-data'
|
||||
import { useEmbeddingModelStatus } from '../use-embedding-model-status'
|
||||
|
||||
const providerSummaryFixture = {
|
||||
provider: 'openai',
|
||||
plugin_id: 'langgenius/openai',
|
||||
label: { en_US: 'OpenAI' },
|
||||
configurate_methods: ['predefined-model'],
|
||||
supported_model_types: ['llm'],
|
||||
preferred_provider_type: 'custom',
|
||||
is_configured: true,
|
||||
system_configuration: { enabled: false },
|
||||
custom_configuration: {
|
||||
status: 'active',
|
||||
available_credentials: [{ credential_id: 'key-1', credential_name: 'API key' }],
|
||||
current_credential_id: 'key-1',
|
||||
current_credential_usable: true,
|
||||
has_custom_models: false,
|
||||
},
|
||||
} satisfies ModelProviderSummaryResponse
|
||||
|
||||
const renderHook: typeof renderHookWithConsoleQuery = (callback, options) => {
|
||||
const queryClient = createConsoleQueryClient()
|
||||
queryClient.setQueryData(consoleQuery.workspaces.current.modelProviders.summary.get.queryKey(), {
|
||||
data: [{ ...providerSummaryFixture, supported_model_types: [ModelTypeEnum.textEmbedding] }],
|
||||
plugins: {},
|
||||
})
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.workspaces.current.modelProviders.credits.get.queryKey(),
|
||||
createModelCreditsFixture(),
|
||||
)
|
||||
for (const model_type of ['llm', 'text-embedding', 'rerank', 'speech2text', 'tts'] as const) {
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryKey({
|
||||
input: { params: { model_type } },
|
||||
}),
|
||||
{ data: [] },
|
||||
)
|
||||
}
|
||||
return renderHookWithConsoleQuery(callback, { ...options, queryClient })
|
||||
}
|
||||
|
||||
describe('useEmbeddingModelStatus', () => {
|
||||
// The hook should resolve provider and model metadata before deriving the final status.
|
||||
describe('Resolution', () => {
|
||||
it('should return the matched provider, current model, and active status', () => {
|
||||
const embeddingModelList = [createModel()]
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useEmbeddingModelStatus({
|
||||
embeddingModel: 'text-embedding-3-large',
|
||||
embeddingModelProvider: 'openai',
|
||||
embeddingModelList,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(result.current.providerMeta?.provider).toBe('openai')
|
||||
expect(result.current.modelProvider?.provider).toBe('openai')
|
||||
expect(result.current.currentModel?.model).toBe('text-embedding-3-large')
|
||||
expect(result.current.status).toBe('active')
|
||||
})
|
||||
|
||||
it('should return incompatible when the provider exists but the selected model is missing', () => {
|
||||
const embeddingModelList = [
|
||||
createModel({
|
||||
models: [createModelItem({ model: 'another-model' })],
|
||||
}),
|
||||
]
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useEmbeddingModelStatus({
|
||||
embeddingModel: 'text-embedding-3-large',
|
||||
embeddingModelProvider: 'openai',
|
||||
embeddingModelList,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(result.current.providerMeta?.provider).toBe('openai')
|
||||
expect(result.current.currentModel).toBeUndefined()
|
||||
expect(result.current.status).toBe('incompatible')
|
||||
})
|
||||
|
||||
it('should return empty when no embedding model is configured', () => {
|
||||
const { result } = renderHook(() =>
|
||||
useEmbeddingModelStatus({
|
||||
embeddingModel: undefined,
|
||||
embeddingModelProvider: undefined,
|
||||
embeddingModelList: [],
|
||||
}),
|
||||
)
|
||||
|
||||
expect(result.current.providerMeta).toBeUndefined()
|
||||
expect(result.current.modelProvider).toBeUndefined()
|
||||
expect(result.current.currentModel).toBeUndefined()
|
||||
expect(result.current.status).toBe('empty')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,331 @@
|
||||
import type { KnowledgeBaseNodeType, RerankingModel, SummaryIndexSetting } from '../types'
|
||||
import type { ValueSelector } from '@/app/components/workflow/types'
|
||||
import { produce } from 'immer'
|
||||
import { useCallback } from 'react'
|
||||
import { useStoreApi } from 'reactflow'
|
||||
import { DEFAULT_WEIGHTED_SCORE, RerankingModeEnum } from '@/models/datasets'
|
||||
import { useNodeDataUpdate } from '../../../hooks/use-node-data-update'
|
||||
import {
|
||||
ChunkStructureEnum,
|
||||
HybridSearchModeEnum,
|
||||
IndexMethodEnum,
|
||||
RetrievalSearchMethodEnum,
|
||||
WeightedScoreEnum,
|
||||
} from '../types'
|
||||
import { isHighQualitySearchMethod } from '../utils'
|
||||
|
||||
export const useConfig = (id: string) => {
|
||||
const store = useStoreApi()
|
||||
const { handleNodeDataUpdateWithSyncDraft } = useNodeDataUpdate()
|
||||
|
||||
const getNodeData = useCallback(() => {
|
||||
const { getNodes } = store.getState()
|
||||
const nodes = getNodes()
|
||||
|
||||
return nodes.find((node) => node.id === id)
|
||||
}, [store, id])
|
||||
|
||||
const handleNodeDataUpdate = useCallback(
|
||||
(data: Partial<KnowledgeBaseNodeType>) => {
|
||||
handleNodeDataUpdateWithSyncDraft({
|
||||
id,
|
||||
data,
|
||||
})
|
||||
},
|
||||
[id, handleNodeDataUpdateWithSyncDraft],
|
||||
)
|
||||
|
||||
const getDefaultWeights = useCallback(
|
||||
({
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
}: {
|
||||
embeddingModel: string
|
||||
embeddingModelProvider: string
|
||||
}) => {
|
||||
return {
|
||||
vector_setting: {
|
||||
vector_weight: DEFAULT_WEIGHTED_SCORE.other.semantic,
|
||||
embedding_provider_name: embeddingModelProvider || '',
|
||||
embedding_model_name: embeddingModel,
|
||||
},
|
||||
keyword_setting: {
|
||||
keyword_weight: DEFAULT_WEIGHTED_SCORE.other.keyword,
|
||||
},
|
||||
}
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const handleChunkStructureChange = useCallback(
|
||||
(chunkStructure: ChunkStructureEnum) => {
|
||||
const nodeData = getNodeData()
|
||||
const {
|
||||
indexing_technique,
|
||||
retrieval_model,
|
||||
chunk_structure,
|
||||
index_chunk_variable_selector,
|
||||
} = nodeData?.data || {}
|
||||
const { search_method } = retrieval_model || {}
|
||||
handleNodeDataUpdate({
|
||||
chunk_structure: chunkStructure,
|
||||
indexing_technique:
|
||||
chunkStructure === ChunkStructureEnum.parent_child ||
|
||||
chunkStructure === ChunkStructureEnum.question_answer
|
||||
? IndexMethodEnum.QUALIFIED
|
||||
: indexing_technique,
|
||||
retrieval_model: {
|
||||
...retrieval_model,
|
||||
search_method:
|
||||
(chunkStructure === ChunkStructureEnum.parent_child ||
|
||||
chunkStructure === ChunkStructureEnum.question_answer) &&
|
||||
!isHighQualitySearchMethod(search_method)
|
||||
? RetrievalSearchMethodEnum.keywordSearch
|
||||
: search_method,
|
||||
},
|
||||
index_chunk_variable_selector:
|
||||
chunkStructure === chunk_structure ? index_chunk_variable_selector : [],
|
||||
})
|
||||
},
|
||||
[handleNodeDataUpdate, getNodeData],
|
||||
)
|
||||
|
||||
const handleIndexMethodChange = useCallback(
|
||||
(indexMethod: IndexMethodEnum) => {
|
||||
const nodeData = getNodeData()
|
||||
|
||||
handleNodeDataUpdate(
|
||||
produce(nodeData?.data as KnowledgeBaseNodeType, (draft) => {
|
||||
draft.indexing_technique = indexMethod
|
||||
|
||||
if (indexMethod === IndexMethodEnum.ECONOMICAL)
|
||||
draft.retrieval_model.search_method = RetrievalSearchMethodEnum.keywordSearch
|
||||
else if (indexMethod === IndexMethodEnum.QUALIFIED)
|
||||
draft.retrieval_model.search_method = RetrievalSearchMethodEnum.semantic
|
||||
}),
|
||||
)
|
||||
},
|
||||
[handleNodeDataUpdate, getNodeData],
|
||||
)
|
||||
|
||||
const handleKeywordNumberChange = useCallback(
|
||||
(keywordNumber: number) => {
|
||||
handleNodeDataUpdate({ keyword_number: keywordNumber })
|
||||
},
|
||||
[handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleEmbeddingModelChange = useCallback(
|
||||
({
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
}: {
|
||||
embeddingModel: string
|
||||
embeddingModelProvider: string
|
||||
}) => {
|
||||
const nodeData = getNodeData()
|
||||
const defaultWeights = getDefaultWeights({
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
})
|
||||
const changeData = {
|
||||
embedding_model: embeddingModel,
|
||||
embedding_model_provider: embeddingModelProvider,
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
},
|
||||
}
|
||||
if (changeData.retrieval_model.weights) {
|
||||
changeData.retrieval_model = {
|
||||
...changeData.retrieval_model,
|
||||
weights: {
|
||||
...changeData.retrieval_model.weights,
|
||||
vector_setting: {
|
||||
...changeData.retrieval_model.weights.vector_setting,
|
||||
embedding_provider_name: embeddingModelProvider,
|
||||
embedding_model_name: embeddingModel,
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
changeData.retrieval_model = {
|
||||
...changeData.retrieval_model,
|
||||
weights: defaultWeights,
|
||||
}
|
||||
}
|
||||
handleNodeDataUpdate(changeData)
|
||||
},
|
||||
[getNodeData, getDefaultWeights, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleRetrievalSearchMethodChange = useCallback(
|
||||
(searchMethod: RetrievalSearchMethodEnum) => {
|
||||
const nodeData = getNodeData()
|
||||
const changeData = {
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
search_method: searchMethod,
|
||||
reranking_mode:
|
||||
nodeData?.data.retrieval_model.reranking_mode || RerankingModeEnum.RerankingModel,
|
||||
},
|
||||
}
|
||||
if (searchMethod === RetrievalSearchMethodEnum.hybrid) {
|
||||
changeData.retrieval_model = {
|
||||
...changeData.retrieval_model,
|
||||
reranking_enable:
|
||||
changeData.retrieval_model.reranking_mode === RerankingModeEnum.RerankingModel,
|
||||
}
|
||||
}
|
||||
handleNodeDataUpdate(changeData)
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleHybridSearchModeChange = useCallback(
|
||||
(hybridSearchMode: HybridSearchModeEnum) => {
|
||||
const nodeData = getNodeData()
|
||||
const defaultWeights = getDefaultWeights({
|
||||
embeddingModel: nodeData?.data.embedding_model || '',
|
||||
embeddingModelProvider: nodeData?.data.embedding_model_provider || '',
|
||||
})
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
reranking_mode: hybridSearchMode,
|
||||
reranking_enable: hybridSearchMode === HybridSearchModeEnum.RerankingModel,
|
||||
weights: nodeData?.data.retrieval_model.weights || defaultWeights,
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, getDefaultWeights, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleRerankingModelEnabledChange = useCallback(
|
||||
(rerankingModelEnabled: boolean) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
reranking_enable: rerankingModelEnabled,
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleWeighedScoreChange = useCallback(
|
||||
(weightedScore: { value: number[] }) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
weights: {
|
||||
weight_type: WeightedScoreEnum.Customized,
|
||||
vector_setting: {
|
||||
...nodeData?.data.retrieval_model.weights?.vector_setting,
|
||||
vector_weight: weightedScore.value[0],
|
||||
},
|
||||
keyword_setting: {
|
||||
keyword_weight: weightedScore.value[1],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleRerankingModelChange = useCallback(
|
||||
(rerankingModel: RerankingModel) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
reranking_model: {
|
||||
reranking_provider_name: rerankingModel.reranking_provider_name,
|
||||
reranking_model_name: rerankingModel.reranking_model_name,
|
||||
},
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleTopKChange = useCallback(
|
||||
(topK: number) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
top_k: topK,
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleScoreThresholdChange = useCallback(
|
||||
(scoreThreshold: number) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
score_threshold: scoreThreshold,
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleScoreThresholdEnabledChange = useCallback(
|
||||
(isEnabled: boolean) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
retrieval_model: {
|
||||
...nodeData?.data.retrieval_model,
|
||||
score_threshold_enabled: isEnabled,
|
||||
},
|
||||
})
|
||||
},
|
||||
[getNodeData, handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleInputVariableChange = useCallback(
|
||||
(inputVariable: string | ValueSelector) => {
|
||||
handleNodeDataUpdate({
|
||||
index_chunk_variable_selector: Array.isArray(inputVariable) ? inputVariable : [],
|
||||
})
|
||||
},
|
||||
[handleNodeDataUpdate],
|
||||
)
|
||||
|
||||
const handleSummaryIndexSettingChange = useCallback(
|
||||
(summaryIndexSetting: SummaryIndexSetting) => {
|
||||
const nodeData = getNodeData()
|
||||
handleNodeDataUpdate({
|
||||
summary_index_setting: {
|
||||
...nodeData?.data.summary_index_setting,
|
||||
...summaryIndexSetting,
|
||||
},
|
||||
})
|
||||
},
|
||||
[handleNodeDataUpdate, getNodeData],
|
||||
)
|
||||
|
||||
return {
|
||||
handleChunkStructureChange,
|
||||
handleIndexMethodChange,
|
||||
handleKeywordNumberChange,
|
||||
handleEmbeddingModelChange,
|
||||
handleRetrievalSearchMethodChange,
|
||||
handleHybridSearchModeChange,
|
||||
handleRerankingModelEnabledChange,
|
||||
handleWeighedScoreChange,
|
||||
handleRerankingModelChange,
|
||||
handleTopKChange,
|
||||
handleScoreThresholdChange,
|
||||
handleScoreThresholdEnabledChange,
|
||||
handleInputVariableChange,
|
||||
handleSummaryIndexSettingChange,
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
import type {
|
||||
ModelProviderSummaryResponse,
|
||||
ProviderModelWithStatusEntity,
|
||||
ProviderWithModelsResponse,
|
||||
} from '@dify/contracts/api/console/workspaces/types.gen'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useMemo } from 'react'
|
||||
import { deriveModelStatus } from '@/app/components/header/account-setting/model-provider-page/derive-model-status'
|
||||
import { useCredentialPanelState } from '@/app/components/header/account-setting/model-provider-page/provider-added-card/use-credential-panel-state'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
|
||||
type UseEmbeddingModelStatusProps = {
|
||||
embeddingModel?: string
|
||||
embeddingModelProvider?: string
|
||||
embeddingModelList: ProviderWithModelsResponse[]
|
||||
}
|
||||
|
||||
type UseEmbeddingModelStatusResult = {
|
||||
providerMeta: ModelProviderSummaryResponse | undefined
|
||||
modelProvider: ProviderWithModelsResponse | undefined
|
||||
currentModel: ProviderModelWithStatusEntity | undefined
|
||||
status: ReturnType<typeof deriveModelStatus>
|
||||
}
|
||||
|
||||
export const useEmbeddingModelStatus = ({
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
embeddingModelList,
|
||||
}: UseEmbeddingModelStatusProps): UseEmbeddingModelStatusResult => {
|
||||
const { data: providerMeta } = useQuery(
|
||||
consoleQuery.workspaces.current.modelProviders.summary.get.queryOptions({
|
||||
select: (response) =>
|
||||
response.data.find((provider) => provider.provider === embeddingModelProvider),
|
||||
}),
|
||||
)
|
||||
|
||||
const modelProvider = useMemo(() => {
|
||||
return embeddingModelList.find((provider) => provider.provider === embeddingModelProvider)
|
||||
}, [embeddingModelList, embeddingModelProvider])
|
||||
|
||||
const currentModel = useMemo(() => {
|
||||
return modelProvider?.models.find((model) => model.model === embeddingModel)
|
||||
}, [embeddingModel, modelProvider])
|
||||
|
||||
const credentialState = useCredentialPanelState(providerMeta)
|
||||
|
||||
const status = useMemo(() => {
|
||||
return deriveModelStatus(
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
providerMeta,
|
||||
currentModel,
|
||||
credentialState,
|
||||
)
|
||||
}, [credentialState, currentModel, embeddingModel, embeddingModelProvider, providerMeta])
|
||||
|
||||
return {
|
||||
providerMeta,
|
||||
modelProvider,
|
||||
currentModel,
|
||||
status,
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { IndexMethodEnum, RetrievalSearchMethodEnum } from '../types'
|
||||
|
||||
export const useSettingsDisplay = () => {
|
||||
const { t } = useTranslation(['dataset', 'datasetCreation', 'datasetSettings'])
|
||||
|
||||
return {
|
||||
[IndexMethodEnum.QUALIFIED]: t(($) => $['stepTwo.qualified'], { ns: 'datasetCreation' }),
|
||||
[IndexMethodEnum.ECONOMICAL]: t(($) => $['form.indexMethodEconomy'], { ns: 'datasetSettings' }),
|
||||
[RetrievalSearchMethodEnum.semantic]: t(($) => $['retrieval.semantic_search.title'], {
|
||||
ns: 'dataset',
|
||||
}),
|
||||
[RetrievalSearchMethodEnum.fullText]: t(($) => $['retrieval.full_text_search.title'], {
|
||||
ns: 'dataset',
|
||||
}),
|
||||
[RetrievalSearchMethodEnum.hybrid]: t(($) => $['retrieval.hybrid_search.title'], {
|
||||
ns: 'dataset',
|
||||
}),
|
||||
[RetrievalSearchMethodEnum.keywordSearch]: t(($) => $['retrieval.keyword_search.title'], {
|
||||
ns: 'dataset',
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
import type { FC } from 'react'
|
||||
import type { KnowledgeBaseNodeType } from './types'
|
||||
import type { NodeProps } from '@/app/components/workflow/types'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { memo, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { DERIVED_MODEL_STATUS_BADGE_I18N } from '@/app/components/header/account-setting/model-provider-page/derive-model-status'
|
||||
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
import { normalizeModelProviderModelsResponse } from '@/app/components/header/account-setting/model-provider-page/utils'
|
||||
import { renderI18nObject } from '@/i18n/metadata'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { useEmbeddingModelStatus } from './hooks/use-embedding-model-status'
|
||||
import { useSettingsDisplay } from './hooks/use-settings-display'
|
||||
import { IndexMethodEnum } from './types'
|
||||
import {
|
||||
getKnowledgeBaseValidationIssue,
|
||||
getKnowledgeBaseValidationMessage,
|
||||
KnowledgeBaseValidationIssueCode,
|
||||
} from './utils'
|
||||
|
||||
type SettingRowProps = {
|
||||
label: string
|
||||
value: string
|
||||
warning?: boolean
|
||||
}
|
||||
|
||||
const SettingRow = memo(({ label, value, warning = false }: SettingRowProps) => {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'flex h-6 items-center rounded-md px-1.5',
|
||||
warning
|
||||
? 'border-[0.5px] border-state-warning-active bg-state-warning-hover'
|
||||
: 'bg-workflow-block-parma-bg',
|
||||
)}
|
||||
>
|
||||
<div className="mr-2 shrink-0 system-xs-medium-uppercase text-text-tertiary">{label}</div>
|
||||
<div
|
||||
className={cn(
|
||||
'grow truncate text-right system-xs-medium',
|
||||
warning ? 'text-text-warning' : 'text-text-secondary',
|
||||
)}
|
||||
title={value}
|
||||
>
|
||||
{value}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
const RETRIEVAL_WARNING_CODES = new Set<KnowledgeBaseValidationIssueCode>([
|
||||
KnowledgeBaseValidationIssueCode.retrievalSettingRequired,
|
||||
KnowledgeBaseValidationIssueCode.rerankingModelRequired,
|
||||
KnowledgeBaseValidationIssueCode.rerankingModelInvalid,
|
||||
])
|
||||
|
||||
const Node: FC<NodeProps<KnowledgeBaseNodeType>> = ({ data }) => {
|
||||
const { t } = useTranslation([
|
||||
'common',
|
||||
'datasetCreation',
|
||||
'datasetSettings',
|
||||
'plugin',
|
||||
'workflow',
|
||||
'modelProvider',
|
||||
])
|
||||
const language = useLanguage()
|
||||
const settingsDisplay = useSettingsDisplay()
|
||||
const { data: embeddingModelList = [] } = useQuery(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryOptions({
|
||||
input: { params: { model_type: ModelTypeEnum.textEmbedding } },
|
||||
select: (response) => response.data,
|
||||
}),
|
||||
)
|
||||
const { data: rerankModelList = [] } = useQuery(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryOptions({
|
||||
input: { params: { model_type: ModelTypeEnum.rerank } },
|
||||
select: (response) => response.data,
|
||||
}),
|
||||
)
|
||||
const chunkStructure = data.chunk_structure
|
||||
const indexChunkVariableSelector = data.index_chunk_variable_selector
|
||||
const indexingTechnique = data.indexing_technique
|
||||
const embeddingModel = data.embedding_model
|
||||
const retrievalModel = data.retrieval_model
|
||||
const retrievalSearchMethod = retrievalModel?.search_method
|
||||
const retrievalRerankingEnable = retrievalModel?.reranking_enable
|
||||
const embeddingModelProvider = data.embedding_model_provider
|
||||
const { data: embeddingProviderModelList } = useQuery(
|
||||
consoleQuery.workspaces.current.modelProviders.byProvider.models.get.queryOptions({
|
||||
input: { params: { provider: embeddingModelProvider || '' } },
|
||||
enabled: indexingTechnique === IndexMethodEnum.QUALIFIED && !!embeddingModelProvider,
|
||||
refetchOnWindowFocus: false,
|
||||
select: normalizeModelProviderModelsResponse,
|
||||
}),
|
||||
)
|
||||
|
||||
const validationPayload = useMemo(() => {
|
||||
return {
|
||||
chunk_structure: chunkStructure,
|
||||
index_chunk_variable_selector: indexChunkVariableSelector,
|
||||
indexing_technique: indexingTechnique,
|
||||
embedding_model: embeddingModel,
|
||||
embedding_model_provider: embeddingModelProvider,
|
||||
retrieval_model: {
|
||||
search_method: retrievalSearchMethod,
|
||||
reranking_enable: retrievalRerankingEnable,
|
||||
reranking_model: retrievalModel?.reranking_model,
|
||||
},
|
||||
_embeddingModelList: embeddingModelList,
|
||||
_embeddingProviderModelList: embeddingProviderModelList,
|
||||
_rerankModelList: rerankModelList,
|
||||
}
|
||||
}, [
|
||||
chunkStructure,
|
||||
indexChunkVariableSelector,
|
||||
indexingTechnique,
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
retrievalSearchMethod,
|
||||
retrievalRerankingEnable,
|
||||
retrievalModel?.reranking_model,
|
||||
embeddingModelList,
|
||||
embeddingProviderModelList,
|
||||
rerankModelList,
|
||||
])
|
||||
|
||||
const validationIssue = useMemo(() => {
|
||||
return getKnowledgeBaseValidationIssue({
|
||||
...validationPayload,
|
||||
})
|
||||
}, [validationPayload])
|
||||
|
||||
const validationIssueMessage = useMemo(() => {
|
||||
return getKnowledgeBaseValidationMessage(validationIssue, t)
|
||||
}, [validationIssue, t])
|
||||
const { currentModel: currentEmbeddingModel, status: embeddingModelStatus } =
|
||||
useEmbeddingModelStatus({
|
||||
embeddingModel: data.embedding_model,
|
||||
embeddingModelProvider: data.embedding_model_provider,
|
||||
embeddingModelList,
|
||||
})
|
||||
|
||||
const chunksDisplayValue = useMemo(() => {
|
||||
if (!data.index_chunk_variable_selector?.length) return '-'
|
||||
|
||||
const chunkVar = data.index_chunk_variable_selector.at(-1)
|
||||
return chunkVar || '-'
|
||||
}, [data.index_chunk_variable_selector])
|
||||
|
||||
const embeddingModelDisplay = useMemo(() => {
|
||||
if (data.indexing_technique !== IndexMethodEnum.QUALIFIED) return '-'
|
||||
|
||||
if (embeddingModelStatus === 'empty')
|
||||
return t(($) => $['detailPanel.configureModel'], { ns: 'plugin' })
|
||||
|
||||
if (embeddingModelStatus !== 'active') {
|
||||
const statusI18nKey =
|
||||
DERIVED_MODEL_STATUS_BADGE_I18N[
|
||||
embeddingModelStatus as keyof typeof DERIVED_MODEL_STATUS_BADGE_I18N
|
||||
]
|
||||
if (statusI18nKey) return t(($) => $[statusI18nKey], { ns: 'modelProvider' })
|
||||
}
|
||||
|
||||
return (
|
||||
(currentEmbeddingModel && renderI18nObject(currentEmbeddingModel.label, language)) ||
|
||||
data.embedding_model ||
|
||||
'-'
|
||||
)
|
||||
}, [
|
||||
currentEmbeddingModel,
|
||||
data.embedding_model,
|
||||
data.indexing_technique,
|
||||
embeddingModelStatus,
|
||||
language,
|
||||
t,
|
||||
])
|
||||
|
||||
const indexMethodDisplay =
|
||||
settingsDisplay[data.indexing_technique as keyof typeof settingsDisplay] || '-'
|
||||
const retrievalMethodDisplay =
|
||||
settingsDisplay[data.retrieval_model?.search_method as keyof typeof settingsDisplay] || '-'
|
||||
|
||||
const chunksWarning =
|
||||
validationIssue?.code === KnowledgeBaseValidationIssueCode.chunksVariableRequired
|
||||
const indexMethodWarning =
|
||||
validationIssue?.code === KnowledgeBaseValidationIssueCode.indexMethodRequired
|
||||
const embeddingWarning =
|
||||
data.indexing_technique === IndexMethodEnum.QUALIFIED && embeddingModelStatus !== 'active'
|
||||
const showEmbeddingModelRow = data.indexing_technique === IndexMethodEnum.QUALIFIED
|
||||
const retrievalWarning = !!(validationIssue && RETRIEVAL_WARNING_CODES.has(validationIssue.code))
|
||||
|
||||
if (!data.chunk_structure) {
|
||||
return (
|
||||
<div className="mb-1 space-y-0.5 px-3 py-1">
|
||||
<div className="flex h-6 items-center rounded-md border-[0.5px] border-state-warning-active bg-state-warning-hover px-1.5">
|
||||
<span className="mr-1 size-1 shrink-0 rounded-xs bg-text-warning-secondary" />
|
||||
<div
|
||||
className="grow truncate system-xs-medium text-text-warning"
|
||||
title={validationIssueMessage}
|
||||
>
|
||||
{validationIssueMessage}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mb-1 space-y-0.5 px-3 py-1">
|
||||
<SettingRow
|
||||
label={t(($) => $['nodes.knowledgeBase.chunksInput'], { ns: 'workflow' })}
|
||||
value={chunksWarning ? validationIssueMessage : chunksDisplayValue}
|
||||
warning={chunksWarning}
|
||||
/>
|
||||
<SettingRow
|
||||
label={t(($) => $['stepTwo.indexMode'], { ns: 'datasetCreation' })}
|
||||
value={indexMethodWarning ? validationIssueMessage : indexMethodDisplay}
|
||||
warning={indexMethodWarning}
|
||||
/>
|
||||
{showEmbeddingModelRow && (
|
||||
<SettingRow
|
||||
label={t(($) => $['form.embeddingModel'], { ns: 'datasetSettings' })}
|
||||
value={embeddingModelDisplay}
|
||||
warning={embeddingWarning}
|
||||
/>
|
||||
)}
|
||||
<SettingRow
|
||||
label={t(($) => $['form.retrievalSetting.method'], { ns: 'datasetSettings' })}
|
||||
value={retrievalWarning ? validationIssueMessage : retrievalMethodDisplay}
|
||||
warning={retrievalWarning}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(Node)
|
||||
@@ -0,0 +1,304 @@
|
||||
import type { FC } from 'react'
|
||||
import type { KnowledgeBaseNodeType } from './types'
|
||||
import type { NodePanelProps, Var } from '@/app/components/workflow/types'
|
||||
import { useQuery, useSuspenseQuery } from '@tanstack/react-query'
|
||||
import { memo, useCallback, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import SummaryIndexSetting from '@/app/components/datasets/settings/summary-index-setting'
|
||||
import { checkShowMultiModalTip } from '@/app/components/datasets/settings/utils'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { normalizeModelProviderModelsResponse } from '@/app/components/header/account-setting/model-provider-page/utils'
|
||||
import {
|
||||
BoxGroup,
|
||||
BoxGroupField,
|
||||
Group,
|
||||
} from '@/app/components/workflow/nodes/_base/components/layout'
|
||||
import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
|
||||
import { systemFeaturesQueryOptions } from '@/features/system-features/client'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { useNodesReadOnly } from '../../hooks/use-workflow'
|
||||
import Split from '../_base/components/split'
|
||||
import ChunkStructure from './components/chunk-structure'
|
||||
import EmbeddingModel from './components/embedding-model'
|
||||
import IndexMethod from './components/index-method'
|
||||
import RetrievalSetting from './components/retrieval-setting'
|
||||
import { useConfig } from './hooks/use-config'
|
||||
import { useEmbeddingModelStatus } from './hooks/use-embedding-model-status'
|
||||
import { ChunkStructureEnum, IndexMethodEnum } from './types'
|
||||
import { getKnowledgeBaseValidationIssue, KnowledgeBaseValidationIssueCode } from './utils'
|
||||
|
||||
const Panel: FC<NodePanelProps<KnowledgeBaseNodeType>> = ({ id, data }) => {
|
||||
const rerankingChunkStructures: readonly ChunkStructureEnum[] = [
|
||||
ChunkStructureEnum.general,
|
||||
ChunkStructureEnum.parent_child,
|
||||
]
|
||||
|
||||
const { t } = useTranslation(['workflow'])
|
||||
const { data: deploymentEdition } = useSuspenseQuery({
|
||||
...systemFeaturesQueryOptions(),
|
||||
select: ({ deployment_edition }) => deployment_edition,
|
||||
})
|
||||
const isNonCloudEdition = deploymentEdition === 'COMMUNITY' || deploymentEdition === 'ENTERPRISE'
|
||||
const { nodesReadOnly } = useNodesReadOnly()
|
||||
const { data: embeddingModelList = [] } = useQuery(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryOptions({
|
||||
input: { params: { model_type: ModelTypeEnum.textEmbedding } },
|
||||
select: (response) => response.data,
|
||||
}),
|
||||
)
|
||||
const { data: rerankModelList = [] } = useQuery(
|
||||
consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryOptions({
|
||||
input: { params: { model_type: ModelTypeEnum.rerank } },
|
||||
select: (response) => response.data,
|
||||
}),
|
||||
)
|
||||
const chunkStructure = data.chunk_structure
|
||||
const indexChunkVariableSelector = data.index_chunk_variable_selector
|
||||
const indexingTechnique = data.indexing_technique
|
||||
const embeddingModel = data.embedding_model
|
||||
const retrievalModel = data.retrieval_model
|
||||
const retrievalSearchMethod = retrievalModel?.search_method
|
||||
const retrievalRerankingEnable = retrievalModel?.reranking_enable
|
||||
const embeddingModelProvider = data.embedding_model_provider
|
||||
const { data: embeddingProviderModelList } = useQuery(
|
||||
consoleQuery.workspaces.current.modelProviders.byProvider.models.get.queryOptions({
|
||||
input: { params: { provider: embeddingModelProvider || '' } },
|
||||
enabled: indexingTechnique === IndexMethodEnum.QUALIFIED && !!embeddingModelProvider,
|
||||
refetchOnWindowFocus: false,
|
||||
select: normalizeModelProviderModelsResponse,
|
||||
}),
|
||||
)
|
||||
|
||||
const {
|
||||
handleChunkStructureChange,
|
||||
handleIndexMethodChange,
|
||||
handleKeywordNumberChange,
|
||||
handleEmbeddingModelChange,
|
||||
handleRetrievalSearchMethodChange,
|
||||
handleHybridSearchModeChange,
|
||||
handleRerankingModelEnabledChange,
|
||||
handleWeighedScoreChange,
|
||||
handleRerankingModelChange,
|
||||
handleTopKChange,
|
||||
handleScoreThresholdChange,
|
||||
handleScoreThresholdEnabledChange,
|
||||
handleInputVariableChange,
|
||||
handleSummaryIndexSettingChange,
|
||||
} = useConfig(id)
|
||||
|
||||
const filterVar = useCallback(
|
||||
(variable: Var) => {
|
||||
if (!data.chunk_structure) return false
|
||||
switch (data.chunk_structure) {
|
||||
case ChunkStructureEnum.general:
|
||||
return (
|
||||
variable.schemaType === 'general_structure' ||
|
||||
variable.schemaType === 'multimodal_general_structure'
|
||||
)
|
||||
case ChunkStructureEnum.parent_child:
|
||||
return (
|
||||
variable.schemaType === 'parent_child_structure' ||
|
||||
variable.schemaType === 'multimodal_parent_child_structure'
|
||||
)
|
||||
case ChunkStructureEnum.question_answer:
|
||||
return variable.schemaType === 'qa_structure'
|
||||
default:
|
||||
return false
|
||||
}
|
||||
},
|
||||
[data.chunk_structure],
|
||||
)
|
||||
|
||||
const chunkTypePlaceHolder = useMemo(() => {
|
||||
if (!data.chunk_structure) return ''
|
||||
let placeholder = ''
|
||||
switch (data.chunk_structure) {
|
||||
case ChunkStructureEnum.general:
|
||||
placeholder = '(multimodal_)general_structure'
|
||||
break
|
||||
case ChunkStructureEnum.parent_child:
|
||||
placeholder = '(multimodal_)parent_child_structure'
|
||||
break
|
||||
case ChunkStructureEnum.question_answer:
|
||||
placeholder = 'qa_structure'
|
||||
break
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
return placeholder.charAt(0).toUpperCase() + placeholder.slice(1)
|
||||
}, [data.chunk_structure])
|
||||
|
||||
const showMultiModalTip = useMemo(() => {
|
||||
return checkShowMultiModalTip({
|
||||
embeddingModel: {
|
||||
provider: data.embedding_model_provider ?? '',
|
||||
model: data.embedding_model ?? '',
|
||||
},
|
||||
rerankingEnable: !!data.retrieval_model?.reranking_enable,
|
||||
rerankModel: {
|
||||
rerankingProviderName: data.retrieval_model?.reranking_model?.reranking_provider_name ?? '',
|
||||
rerankingModelName: data.retrieval_model?.reranking_model?.reranking_model_name ?? '',
|
||||
},
|
||||
indexMethod: data.indexing_technique,
|
||||
embeddingModelList,
|
||||
rerankModelList,
|
||||
})
|
||||
}, [
|
||||
data.embedding_model_provider,
|
||||
data.embedding_model,
|
||||
data.retrieval_model?.reranking_enable,
|
||||
data.retrieval_model?.reranking_model,
|
||||
data.indexing_technique,
|
||||
embeddingModelList,
|
||||
rerankModelList,
|
||||
])
|
||||
|
||||
const validationPayload = useMemo(() => {
|
||||
return {
|
||||
chunk_structure: chunkStructure,
|
||||
index_chunk_variable_selector: indexChunkVariableSelector,
|
||||
indexing_technique: indexingTechnique,
|
||||
embedding_model: embeddingModel,
|
||||
embedding_model_provider: embeddingModelProvider,
|
||||
retrieval_model: {
|
||||
search_method: retrievalSearchMethod,
|
||||
reranking_enable: retrievalRerankingEnable,
|
||||
reranking_model: retrievalModel?.reranking_model,
|
||||
},
|
||||
_embeddingModelList: embeddingModelList,
|
||||
_embeddingProviderModelList: embeddingProviderModelList,
|
||||
_rerankModelList: rerankModelList,
|
||||
}
|
||||
}, [
|
||||
chunkStructure,
|
||||
indexChunkVariableSelector,
|
||||
indexingTechnique,
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
retrievalSearchMethod,
|
||||
retrievalRerankingEnable,
|
||||
retrievalModel?.reranking_model,
|
||||
embeddingModelList,
|
||||
embeddingProviderModelList,
|
||||
rerankModelList,
|
||||
])
|
||||
|
||||
const validationIssue = useMemo(() => {
|
||||
return getKnowledgeBaseValidationIssue(validationPayload)
|
||||
}, [validationPayload])
|
||||
const { status: embeddingModelStatus } = useEmbeddingModelStatus({
|
||||
embeddingModel,
|
||||
embeddingModelProvider,
|
||||
embeddingModelList,
|
||||
})
|
||||
|
||||
const chunkStructureWarning =
|
||||
validationIssue?.code === KnowledgeBaseValidationIssueCode.chunkStructureRequired
|
||||
const chunksInputWarning =
|
||||
validationIssue?.code === KnowledgeBaseValidationIssueCode.chunksVariableRequired
|
||||
const embeddingModelWarning =
|
||||
indexingTechnique === IndexMethodEnum.QUALIFIED && embeddingModelStatus !== 'active'
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Group className="py-3" withBorderBottom={!!data.chunk_structure}>
|
||||
<ChunkStructure
|
||||
chunkStructure={data.chunk_structure}
|
||||
onChunkStructureChange={handleChunkStructureChange}
|
||||
warningDot={chunkStructureWarning}
|
||||
readonly={nodesReadOnly}
|
||||
/>
|
||||
</Group>
|
||||
{!!data.chunk_structure && (
|
||||
<>
|
||||
<BoxGroupField
|
||||
boxGroupProps={{
|
||||
boxProps: { withBorderBottom: true },
|
||||
}}
|
||||
fieldProps={{
|
||||
fieldTitleProps: {
|
||||
title: t(($) => $['nodes.knowledgeBase.chunksInput'], { ns: 'workflow' }),
|
||||
tooltip: t(($) => $['nodes.knowledgeBase.chunksInputTip'], { ns: 'workflow' }),
|
||||
warningDot: chunksInputWarning,
|
||||
},
|
||||
}}
|
||||
>
|
||||
<VarReferencePicker
|
||||
nodeId={id}
|
||||
isShowNodeName
|
||||
value={data.index_chunk_variable_selector}
|
||||
onChange={handleInputVariableChange}
|
||||
readonly={nodesReadOnly}
|
||||
filterVar={filterVar}
|
||||
isFilterFileVar
|
||||
isSupportFileVar={false}
|
||||
preferSchemaType
|
||||
typePlaceHolder={chunkTypePlaceHolder}
|
||||
/>
|
||||
</BoxGroupField>
|
||||
<BoxGroup>
|
||||
<div className="space-y-3">
|
||||
<IndexMethod
|
||||
chunkStructure={data.chunk_structure}
|
||||
indexMethod={data.indexing_technique}
|
||||
onIndexMethodChange={handleIndexMethodChange}
|
||||
keywordNumber={data.keyword_number}
|
||||
onKeywordNumberChange={handleKeywordNumberChange}
|
||||
readonly={nodesReadOnly}
|
||||
/>
|
||||
{data.indexing_technique === IndexMethodEnum.QUALIFIED && (
|
||||
<EmbeddingModel
|
||||
embeddingModel={data.embedding_model}
|
||||
embeddingModelProvider={data.embedding_model_provider}
|
||||
onEmbeddingModelChange={handleEmbeddingModelChange}
|
||||
warningDot={embeddingModelWarning}
|
||||
readonly={nodesReadOnly}
|
||||
/>
|
||||
)}
|
||||
<div className="pt-1">
|
||||
<Split className="h-px" />
|
||||
</div>
|
||||
{data.indexing_technique === IndexMethodEnum.QUALIFIED &&
|
||||
rerankingChunkStructures.includes(data.chunk_structure) &&
|
||||
isNonCloudEdition && (
|
||||
<>
|
||||
<SummaryIndexSetting
|
||||
summaryIndexSetting={data.summary_index_setting}
|
||||
onSummaryIndexSettingChange={handleSummaryIndexSettingChange}
|
||||
readonly={nodesReadOnly}
|
||||
/>
|
||||
<div className="pt-1">
|
||||
<Split className="h-px" />
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<RetrievalSetting
|
||||
indexMethod={data.indexing_technique}
|
||||
searchMethod={data.retrieval_model.search_method}
|
||||
onRetrievalSearchMethodChange={handleRetrievalSearchMethodChange}
|
||||
hybridSearchMode={data.retrieval_model.reranking_mode}
|
||||
onHybridSearchModeChange={handleHybridSearchModeChange}
|
||||
weightedScore={data.retrieval_model.weights}
|
||||
onWeightedScoreChange={handleWeighedScoreChange}
|
||||
rerankingModelEnabled={data.retrieval_model.reranking_enable}
|
||||
onRerankingModelEnabledChange={handleRerankingModelEnabledChange}
|
||||
rerankingModel={data.retrieval_model.reranking_model}
|
||||
onRerankingModelChange={handleRerankingModelChange}
|
||||
topK={data.retrieval_model.top_k}
|
||||
onTopKChange={handleTopKChange}
|
||||
scoreThreshold={data.retrieval_model.score_threshold}
|
||||
onScoreThresholdChange={handleScoreThresholdChange}
|
||||
isScoreThresholdEnabled={data.retrieval_model.score_threshold_enabled}
|
||||
onScoreThresholdEnabledChange={handleScoreThresholdEnabledChange}
|
||||
showMultiModalTip={showMultiModalTip}
|
||||
readonly={nodesReadOnly}
|
||||
/>
|
||||
</div>
|
||||
</BoxGroup>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(Panel)
|
||||
@@ -0,0 +1,66 @@
|
||||
import type { ProviderWithModelsResponse } from '@dify/contracts/api/console/workspaces/types.gen'
|
||||
import type { IndexingType } from '@/app/components/datasets/create/step-two'
|
||||
import type { ModelItem } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import type { CommonNodeType } from '@/app/components/workflow/types'
|
||||
import type { RerankingModeEnum, WeightedScoreEnum } from '@/models/datasets'
|
||||
import type { RETRIEVE_METHOD } from '@/types/app'
|
||||
|
||||
export { IndexingType as IndexMethodEnum } from '@/app/components/datasets/create/step-two'
|
||||
export { WeightedScoreEnum } from '@/models/datasets'
|
||||
export { RerankingModeEnum as HybridSearchModeEnum } from '@/models/datasets'
|
||||
export { RETRIEVE_METHOD as RetrievalSearchMethodEnum } from '@/types/app'
|
||||
|
||||
export const ChunkStructureEnum = {
|
||||
general: 'text_model',
|
||||
parent_child: 'hierarchical_model',
|
||||
question_answer: 'qa_model',
|
||||
} as const
|
||||
|
||||
export type ChunkStructureEnum = (typeof ChunkStructureEnum)[keyof typeof ChunkStructureEnum]
|
||||
|
||||
export type RerankingModel = {
|
||||
reranking_provider_name: string
|
||||
reranking_model_name: string
|
||||
}
|
||||
|
||||
export type WeightedScore = {
|
||||
weight_type: WeightedScoreEnum
|
||||
vector_setting: {
|
||||
vector_weight: number
|
||||
embedding_provider_name: string
|
||||
embedding_model_name: string
|
||||
}
|
||||
keyword_setting: {
|
||||
keyword_weight: number
|
||||
}
|
||||
}
|
||||
|
||||
type RetrievalSetting = {
|
||||
search_method?: RETRIEVE_METHOD
|
||||
reranking_enable?: boolean
|
||||
reranking_model?: RerankingModel
|
||||
weights?: WeightedScore
|
||||
top_k: number
|
||||
score_threshold_enabled: boolean
|
||||
score_threshold: number
|
||||
reranking_mode?: RerankingModeEnum
|
||||
}
|
||||
export type SummaryIndexSetting = {
|
||||
enable?: boolean
|
||||
model_name?: string
|
||||
model_provider_name?: string
|
||||
summary_prompt?: string
|
||||
}
|
||||
export type KnowledgeBaseNodeType = CommonNodeType & {
|
||||
index_chunk_variable_selector: string[]
|
||||
chunk_structure?: ChunkStructureEnum
|
||||
indexing_technique?: IndexingType
|
||||
embedding_model?: string
|
||||
embedding_model_provider?: string
|
||||
keyword_number: number
|
||||
retrieval_model: RetrievalSetting
|
||||
_embeddingModelList?: ProviderWithModelsResponse[]
|
||||
_embeddingProviderModelList?: ModelItem[]
|
||||
_rerankModelList?: ProviderWithModelsResponse[]
|
||||
summary_index_setting?: SummaryIndexSetting
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
import type { KnowledgeBaseNodeType } from './types'
|
||||
import type { InputVar, Variable } from '@/app/components/workflow/types'
|
||||
import { useCallback, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { InputVarType } from '@/app/components/workflow/types'
|
||||
|
||||
type Params = {
|
||||
id: string
|
||||
payload: KnowledgeBaseNodeType
|
||||
runInputData: Record<string, any>
|
||||
getInputVars: (textList: string[]) => InputVar[]
|
||||
setRunInputData: (data: Record<string, any>) => void
|
||||
toVarInputs: (variables: Variable[]) => InputVar[]
|
||||
}
|
||||
const useSingleRunFormParams = ({ payload, runInputData, setRunInputData }: Params) => {
|
||||
const { t } = useTranslation(['workflow'])
|
||||
const query = runInputData.query
|
||||
const setQuery = useCallback(
|
||||
(newQuery: string) => {
|
||||
setRunInputData({
|
||||
...runInputData,
|
||||
query: newQuery,
|
||||
})
|
||||
},
|
||||
[runInputData, setRunInputData],
|
||||
)
|
||||
|
||||
const forms = useMemo(() => {
|
||||
return [
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
label: t(($) => $['nodes.common.inputVars'], { ns: 'workflow' }),
|
||||
variable: 'query',
|
||||
type: InputVarType.paragraph,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
values: { query },
|
||||
onChange: (keyValue: Record<string, any>) => setQuery(keyValue.query),
|
||||
},
|
||||
]
|
||||
}, [query, setQuery, t])
|
||||
|
||||
const getDependentVars = () => {
|
||||
return [payload.index_chunk_variable_selector]
|
||||
}
|
||||
const getDependentVar = (variable: string) => {
|
||||
if (variable === 'query') return payload.index_chunk_variable_selector
|
||||
}
|
||||
|
||||
return {
|
||||
forms,
|
||||
getDependentVars,
|
||||
getDependentVar,
|
||||
}
|
||||
}
|
||||
|
||||
export default useSingleRunFormParams
|
||||
@@ -0,0 +1,195 @@
|
||||
import type { TFunction } from 'i18next'
|
||||
import type { KnowledgeBaseNodeType } from './types'
|
||||
import { IndexingType } from '@/app/components/datasets/create/step-two'
|
||||
import { ModelStatusEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { RetrievalSearchMethodEnum } from './types'
|
||||
|
||||
export const isHighQualitySearchMethod = (searchMethod: RetrievalSearchMethodEnum) => {
|
||||
return (
|
||||
searchMethod === RetrievalSearchMethodEnum.semantic ||
|
||||
searchMethod === RetrievalSearchMethodEnum.hybrid ||
|
||||
searchMethod === RetrievalSearchMethodEnum.fullText
|
||||
)
|
||||
}
|
||||
|
||||
export const KnowledgeBaseValidationIssueCode = {
|
||||
chunkStructureRequired: 'chunk-structure-required',
|
||||
chunksVariableRequired: 'chunks-variable-required',
|
||||
indexMethodRequired: 'index-method-required',
|
||||
embeddingModelNotConfigured: 'embedding-model-not-configured',
|
||||
embeddingModelConfigureRequired: 'embedding-model-configure-required',
|
||||
embeddingModelApiKeyUnavailable: 'embedding-model-api-key-unavailable',
|
||||
embeddingModelCreditsExhausted: 'embedding-model-credits-exhausted',
|
||||
embeddingModelDisabled: 'embedding-model-disabled',
|
||||
embeddingModelIncompatible: 'embedding-model-incompatible',
|
||||
retrievalSettingRequired: 'retrieval-setting-required',
|
||||
rerankingModelRequired: 'reranking-model-required',
|
||||
rerankingModelInvalid: 'reranking-model-invalid',
|
||||
} as const
|
||||
|
||||
export type KnowledgeBaseValidationIssueCode =
|
||||
(typeof KnowledgeBaseValidationIssueCode)[keyof typeof KnowledgeBaseValidationIssueCode]
|
||||
|
||||
type KnowledgeBaseValidationIssue = {
|
||||
code: KnowledgeBaseValidationIssueCode
|
||||
}
|
||||
|
||||
type KnowledgeBaseValidationPayload = Pick<
|
||||
KnowledgeBaseNodeType,
|
||||
| 'chunk_structure'
|
||||
| 'index_chunk_variable_selector'
|
||||
| 'indexing_technique'
|
||||
| 'embedding_model'
|
||||
| 'embedding_model_provider'
|
||||
| '_embeddingModelList'
|
||||
| '_embeddingProviderModelList'
|
||||
| '_rerankModelList'
|
||||
> & {
|
||||
retrieval_model?: Pick<
|
||||
KnowledgeBaseNodeType['retrieval_model'],
|
||||
'search_method' | 'reranking_enable' | 'reranking_model'
|
||||
>
|
||||
}
|
||||
|
||||
const resolveIssue = (code: KnowledgeBaseValidationIssueCode): KnowledgeBaseValidationIssue => ({
|
||||
code,
|
||||
})
|
||||
|
||||
const resolveEmbeddingIssue = (
|
||||
payload: KnowledgeBaseValidationPayload,
|
||||
): KnowledgeBaseValidationIssue | null => {
|
||||
const {
|
||||
embedding_model,
|
||||
embedding_model_provider,
|
||||
_embeddingModelList,
|
||||
_embeddingProviderModelList,
|
||||
} = payload
|
||||
|
||||
if (!embedding_model || !embedding_model_provider)
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelNotConfigured)
|
||||
|
||||
const currentEmbeddingModelProvider = _embeddingModelList?.find(
|
||||
(provider) => provider.provider === embedding_model_provider,
|
||||
)
|
||||
const hasProviderScopedModelList =
|
||||
!!_embeddingProviderModelList && _embeddingProviderModelList.length > 0
|
||||
const embeddingModelCandidates = hasProviderScopedModelList
|
||||
? _embeddingProviderModelList
|
||||
: currentEmbeddingModelProvider?.models
|
||||
const currentEmbeddingModel = embeddingModelCandidates?.find(
|
||||
(model) => model.model === embedding_model,
|
||||
)
|
||||
|
||||
if (!currentEmbeddingModel) {
|
||||
if (!currentEmbeddingModelProvider)
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelIncompatible)
|
||||
|
||||
const providerExists = hasProviderScopedModelList || currentEmbeddingModelProvider
|
||||
return resolveIssue(
|
||||
providerExists
|
||||
? KnowledgeBaseValidationIssueCode.embeddingModelIncompatible
|
||||
: KnowledgeBaseValidationIssueCode.embeddingModelNotConfigured,
|
||||
)
|
||||
}
|
||||
|
||||
switch (currentEmbeddingModel.status) {
|
||||
case ModelStatusEnum.active:
|
||||
return null
|
||||
case ModelStatusEnum.noConfigure:
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelConfigureRequired)
|
||||
case ModelStatusEnum.credentialRemoved:
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelApiKeyUnavailable)
|
||||
case ModelStatusEnum.quotaExceeded:
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelCreditsExhausted)
|
||||
case ModelStatusEnum.disabled:
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelDisabled)
|
||||
case ModelStatusEnum.noPermission:
|
||||
default:
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.embeddingModelIncompatible)
|
||||
}
|
||||
}
|
||||
|
||||
export const getKnowledgeBaseValidationIssue = (
|
||||
payload: KnowledgeBaseValidationPayload,
|
||||
): KnowledgeBaseValidationIssue | null => {
|
||||
const {
|
||||
chunk_structure,
|
||||
indexing_technique,
|
||||
retrieval_model,
|
||||
index_chunk_variable_selector,
|
||||
_rerankModelList,
|
||||
} = payload
|
||||
|
||||
const { search_method, reranking_enable, reranking_model } = retrieval_model || {}
|
||||
|
||||
if (!chunk_structure) return resolveIssue(KnowledgeBaseValidationIssueCode.chunkStructureRequired)
|
||||
|
||||
if (index_chunk_variable_selector.length === 0)
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.chunksVariableRequired)
|
||||
|
||||
if (!indexing_technique) return resolveIssue(KnowledgeBaseValidationIssueCode.indexMethodRequired)
|
||||
|
||||
if (indexing_technique === IndexingType.QUALIFIED) {
|
||||
const embeddingIssue = resolveEmbeddingIssue(payload)
|
||||
if (embeddingIssue) return embeddingIssue
|
||||
}
|
||||
|
||||
if (!retrieval_model || !search_method)
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.retrievalSettingRequired)
|
||||
|
||||
if (reranking_enable) {
|
||||
if (
|
||||
!reranking_model ||
|
||||
!reranking_model.reranking_provider_name ||
|
||||
!reranking_model.reranking_model_name
|
||||
)
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.rerankingModelRequired)
|
||||
|
||||
const currentRerankingModelProvider = _rerankModelList?.find(
|
||||
(provider) => provider.provider === reranking_model.reranking_provider_name,
|
||||
)
|
||||
const currentRerankingModel = currentRerankingModelProvider?.models.find(
|
||||
(model) => model.model === reranking_model.reranking_model_name,
|
||||
)
|
||||
if (!currentRerankingModel)
|
||||
return resolveIssue(KnowledgeBaseValidationIssueCode.rerankingModelInvalid)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export const getKnowledgeBaseValidationMessage = (
|
||||
issue: KnowledgeBaseValidationIssue | null | undefined,
|
||||
t: TFunction<['common', 'workflow', 'modelProvider']>,
|
||||
) => {
|
||||
if (!issue) return ''
|
||||
|
||||
switch (issue.code) {
|
||||
case KnowledgeBaseValidationIssueCode.chunkStructureRequired:
|
||||
return t(($) => $['nodes.knowledgeBase.chunkIsRequired'], { ns: 'workflow' })
|
||||
case KnowledgeBaseValidationIssueCode.chunksVariableRequired:
|
||||
return t(($) => $['nodes.knowledgeBase.chunksVariableIsRequired'], { ns: 'workflow' })
|
||||
case KnowledgeBaseValidationIssueCode.indexMethodRequired:
|
||||
return t(($) => $['nodes.knowledgeBase.indexMethodIsRequired'], { ns: 'workflow' })
|
||||
case KnowledgeBaseValidationIssueCode.embeddingModelNotConfigured:
|
||||
return t(($) => $['nodes.knowledgeBase.embeddingModelNotConfigured'], { ns: 'workflow' })
|
||||
case KnowledgeBaseValidationIssueCode.embeddingModelConfigureRequired:
|
||||
return t(($) => $['modelProvider.selector.configureRequired'], { ns: 'modelProvider' })
|
||||
case KnowledgeBaseValidationIssueCode.embeddingModelApiKeyUnavailable:
|
||||
return t(($) => $['modelProvider.selector.apiKeyUnavailable'], { ns: 'modelProvider' })
|
||||
case KnowledgeBaseValidationIssueCode.embeddingModelCreditsExhausted:
|
||||
return t(($) => $['modelProvider.selector.creditsExhausted'], { ns: 'modelProvider' })
|
||||
case KnowledgeBaseValidationIssueCode.embeddingModelDisabled:
|
||||
return t(($) => $['modelProvider.selector.disabled'], { ns: 'modelProvider' })
|
||||
case KnowledgeBaseValidationIssueCode.embeddingModelIncompatible:
|
||||
return t(($) => $['modelProvider.selector.incompatible'], { ns: 'modelProvider' })
|
||||
case KnowledgeBaseValidationIssueCode.retrievalSettingRequired:
|
||||
return t(($) => $['nodes.knowledgeBase.retrievalSettingIsRequired'], { ns: 'workflow' })
|
||||
case KnowledgeBaseValidationIssueCode.rerankingModelRequired:
|
||||
return t(($) => $['nodes.knowledgeBase.rerankingModelIsRequired'], { ns: 'workflow' })
|
||||
case KnowledgeBaseValidationIssueCode.rerankingModelInvalid:
|
||||
return t(($) => $['nodes.knowledgeBase.rerankingModelIsInvalid'], { ns: 'workflow' })
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user