mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 12:37:20 +08:00
rag pipeline initial template
This commit is contained in:
parent
e1a2755e3b
commit
ff33d42c55
@ -1,48 +1,14 @@
|
|||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { generateNewNode } from '@/app/components/workflow/utils'
|
import { generateNewNode } from '@/app/components/workflow/utils'
|
||||||
import {
|
import {
|
||||||
NODE_WIDTH_X_OFFSET,
|
|
||||||
START_INITIAL_POSITION,
|
START_INITIAL_POSITION,
|
||||||
} from '@/app/components/workflow/constants'
|
} from '@/app/components/workflow/constants'
|
||||||
import type { KnowledgeBaseNodeType } from '@/app/components/workflow/nodes/knowledge-base/types'
|
import type { KnowledgeBaseNodeType } from '@/app/components/workflow/nodes/knowledge-base/types'
|
||||||
import knowledgeBaseDefault from '@/app/components/workflow/nodes/knowledge-base/default'
|
import knowledgeBaseDefault from '@/app/components/workflow/nodes/knowledge-base/default'
|
||||||
import ToolNodeDefault from '@/app/components/workflow/nodes/tool/default'
|
|
||||||
|
|
||||||
export const usePipelineTemplate = () => {
|
export const usePipelineTemplate = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
const { newNode: difyExtractorNode } = generateNewNode({
|
|
||||||
id: 'difyExtractor',
|
|
||||||
data: {
|
|
||||||
...ToolNodeDefault.defaultValue,
|
|
||||||
type: ToolNodeDefault.metaData.type,
|
|
||||||
title: '',
|
|
||||||
provider_id: 'langgenius/dify_extractor/dify_extractor',
|
|
||||||
tool_name: 'dify_extractor',
|
|
||||||
_notInitialized: true,
|
|
||||||
},
|
|
||||||
position: {
|
|
||||||
x: START_INITIAL_POSITION.x + NODE_WIDTH_X_OFFSET,
|
|
||||||
y: START_INITIAL_POSITION.y,
|
|
||||||
},
|
|
||||||
} as any)
|
|
||||||
|
|
||||||
const { newNode: generalChunkNode } = generateNewNode({
|
|
||||||
id: 'generalChunk',
|
|
||||||
data: {
|
|
||||||
...ToolNodeDefault.defaultValue,
|
|
||||||
type: ToolNodeDefault.metaData.type,
|
|
||||||
title: '',
|
|
||||||
provider_id: 'langgenius/general_chunker/general_chunker',
|
|
||||||
tool_name: 'general_chunker',
|
|
||||||
_notInitialized: true,
|
|
||||||
},
|
|
||||||
position: {
|
|
||||||
x: START_INITIAL_POSITION.x + NODE_WIDTH_X_OFFSET * 2,
|
|
||||||
y: START_INITIAL_POSITION.y,
|
|
||||||
},
|
|
||||||
} as any)
|
|
||||||
|
|
||||||
const { newNode: knowledgeBaseNode } = generateNewNode({
|
const { newNode: knowledgeBaseNode } = generateNewNode({
|
||||||
id: 'knowledgeBase',
|
id: 'knowledgeBase',
|
||||||
data: {
|
data: {
|
||||||
@ -51,29 +17,13 @@ export const usePipelineTemplate = () => {
|
|||||||
title: t(`workflow.blocks.${knowledgeBaseDefault.metaData.type}`),
|
title: t(`workflow.blocks.${knowledgeBaseDefault.metaData.type}`),
|
||||||
},
|
},
|
||||||
position: {
|
position: {
|
||||||
x: START_INITIAL_POSITION.x + NODE_WIDTH_X_OFFSET * 3,
|
x: START_INITIAL_POSITION.x + 500,
|
||||||
y: START_INITIAL_POSITION.y,
|
y: START_INITIAL_POSITION.y,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const difyExtractorToGeneralChunkEdge = {
|
|
||||||
id: `${difyExtractorNode.id}-${generalChunkNode.id}`,
|
|
||||||
source: difyExtractorNode.id,
|
|
||||||
sourceHandle: 'source',
|
|
||||||
target: generalChunkNode.id,
|
|
||||||
targetHandle: 'target',
|
|
||||||
}
|
|
||||||
|
|
||||||
const generalChunkToKnowledgeBaseEdge = {
|
|
||||||
id: `${generalChunkNode.id}-${knowledgeBaseNode.id}`,
|
|
||||||
source: generalChunkNode.id,
|
|
||||||
sourceHandle: 'source',
|
|
||||||
target: knowledgeBaseNode.id,
|
|
||||||
targetHandle: 'target',
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
nodes: [difyExtractorNode, generalChunkNode, knowledgeBaseNode],
|
nodes: [knowledgeBaseNode],
|
||||||
edges: [difyExtractorToGeneralChunkEdge, generalChunkToKnowledgeBaseEdge],
|
edges: [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,15 +3,12 @@ import React from 'react'
|
|||||||
import type { ToolNodeType } from './types'
|
import type { ToolNodeType } from './types'
|
||||||
import type { NodeProps } from '@/app/components/workflow/types'
|
import type { NodeProps } from '@/app/components/workflow/types'
|
||||||
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import { useInitial } from './use-initial'
|
|
||||||
|
|
||||||
const Node: FC<NodeProps<ToolNodeType>> = ({
|
const Node: FC<NodeProps<ToolNodeType>> = ({
|
||||||
id,
|
|
||||||
data,
|
data,
|
||||||
}) => {
|
}) => {
|
||||||
const { tool_configurations, paramSchemas } = data
|
const { tool_configurations, paramSchemas } = data
|
||||||
const toolConfigs = Object.keys(tool_configurations || {})
|
const toolConfigs = Object.keys(tool_configurations || {})
|
||||||
useInitial(id)
|
|
||||||
|
|
||||||
if (!toolConfigs.length)
|
if (!toolConfigs.length)
|
||||||
return null
|
return null
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/compo
|
|||||||
import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show'
|
import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show'
|
||||||
import { useStore } from '@/app/components/workflow/store'
|
import { useStore } from '@/app/components/workflow/store'
|
||||||
import { wrapStructuredVarItem } from '@/app/components/workflow/utils/tool'
|
import { wrapStructuredVarItem } from '@/app/components/workflow/utils/tool'
|
||||||
import { useInitial } from './use-initial'
|
|
||||||
import useMatchSchemaType from '../_base/components/variable/use-match-schema-type'
|
import useMatchSchemaType from '../_base/components/variable/use-match-schema-type'
|
||||||
|
|
||||||
const i18nPrefix = 'workflow.nodes.tool'
|
const i18nPrefix = 'workflow.nodes.tool'
|
||||||
@ -22,7 +21,6 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
|
|||||||
data,
|
data,
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
useInitial(id)
|
|
||||||
const {
|
const {
|
||||||
readOnly,
|
readOnly,
|
||||||
inputs,
|
inputs,
|
||||||
|
|||||||
@ -26,5 +26,4 @@ export type ToolNodeType = CommonNodeType & {
|
|||||||
tool_description?: string
|
tool_description?: string
|
||||||
is_team_authorization?: boolean
|
is_team_authorization?: boolean
|
||||||
params?: Record<string, any>
|
params?: Record<string, any>
|
||||||
_notInitialized?: boolean
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,67 +0,0 @@
|
|||||||
import {
|
|
||||||
useCallback,
|
|
||||||
useEffect,
|
|
||||||
} from 'react'
|
|
||||||
import { useStoreApi } from 'reactflow'
|
|
||||||
import { useNodeDataUpdate } from '@/app/components/workflow/hooks/use-node-data-update'
|
|
||||||
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
|
||||||
import { useStore } from '../../store'
|
|
||||||
import type { ToolNodeType } from './types'
|
|
||||||
import { canFindTool } from '@/utils'
|
|
||||||
|
|
||||||
export const useInitial = (id: string) => {
|
|
||||||
const store = useStoreApi()
|
|
||||||
const buildInTools = useStore(s => s.buildInTools)
|
|
||||||
const language = useLanguage()
|
|
||||||
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<ToolNodeType>) => {
|
|
||||||
handleNodeDataUpdateWithSyncDraft({
|
|
||||||
id,
|
|
||||||
data,
|
|
||||||
})
|
|
||||||
}, [id, handleNodeDataUpdateWithSyncDraft])
|
|
||||||
|
|
||||||
const handleInitial = useCallback(() => {
|
|
||||||
const nodeData = getNodeData()
|
|
||||||
const { provider_id, tool_name, _notInitialized } = nodeData?.data || {}
|
|
||||||
const currCollection = buildInTools.find(item => canFindTool(item.id, provider_id))
|
|
||||||
const currTool = currCollection?.tools.find(tool => tool.name === tool_name)
|
|
||||||
|
|
||||||
if (!currTool)
|
|
||||||
return
|
|
||||||
|
|
||||||
if (_notInitialized && currCollection) {
|
|
||||||
const params: Record<string, string> = {}
|
|
||||||
if (currTool.parameters) {
|
|
||||||
currTool.parameters.forEach((item) => {
|
|
||||||
params[item.name] = ''
|
|
||||||
})
|
|
||||||
}
|
|
||||||
handleNodeDataUpdate({
|
|
||||||
provider_id: currCollection.id,
|
|
||||||
provider_type: currCollection.type as any,
|
|
||||||
provider_name: currCollection.name,
|
|
||||||
tool_name: currTool.name,
|
|
||||||
tool_label: currTool.label[language],
|
|
||||||
tool_description: currTool.description[language],
|
|
||||||
title: currTool.label[language],
|
|
||||||
is_team_authorization: currCollection.is_team_authorization,
|
|
||||||
paramSchemas: currTool.parameters,
|
|
||||||
params,
|
|
||||||
_notInitialized: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}, [handleNodeDataUpdate, language, buildInTools, getNodeData])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
handleInitial()
|
|
||||||
}, [handleInitial])
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user