mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-05 10:10:37 +08:00
feat: llm support new run and types
This commit is contained in:
parent
98e44ca2cc
commit
a8b6062f0e
@ -31,6 +31,7 @@ type NodesExtraData = {
|
||||
getAvailablePrevNodes: (isChatMode: boolean) => BlockEnum[]
|
||||
getAvailableNextNodes: (isChatMode: boolean) => BlockEnum[]
|
||||
checkValid: any
|
||||
defaultRunInputData?: Record<string, any>
|
||||
}
|
||||
export const NODES_EXTRA_DATA: Record<BlockEnum, NodesExtraData> = {
|
||||
[BlockEnum.Start]: {
|
||||
@ -68,6 +69,7 @@ export const NODES_EXTRA_DATA: Record<BlockEnum, NodesExtraData> = {
|
||||
getAvailablePrevNodes: LLMDefault.getAvailablePrevNodes,
|
||||
getAvailableNextNodes: LLMDefault.getAvailableNextNodes,
|
||||
checkValid: LLMDefault.checkValid,
|
||||
defaultRunInputData: LLMDefault.defaultRunInputData,
|
||||
},
|
||||
[BlockEnum.KnowledgeRetrieval]: {
|
||||
author: 'Dify',
|
||||
|
@ -6,10 +6,10 @@ import {
|
||||
cloneElement,
|
||||
memo,
|
||||
useCallback,
|
||||
useRef,
|
||||
useState,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react'
|
||||
import {
|
||||
RiCloseLine,
|
||||
@ -56,6 +56,7 @@ import type { PanelExposedType } from '@/types/workflow'
|
||||
import BeforeRunForm from '../before-run-form'
|
||||
import { sleep } from '@/utils'
|
||||
import { debounce } from 'lodash-es'
|
||||
import { NODES_EXTRA_DATA } from '@/app/components/workflow/constants'
|
||||
|
||||
type BasePanelProps = {
|
||||
children: ReactNode
|
||||
@ -147,13 +148,15 @@ const BasePanel: FC<BasePanelProps> = ({
|
||||
handleRun,
|
||||
handleStop,
|
||||
runInputData,
|
||||
runInputDataRef,
|
||||
setRunInputData: doSetRunInputData,
|
||||
runResult,
|
||||
getInputVars,
|
||||
toVarInputs,
|
||||
} = useOneStepRun<typeof data>({
|
||||
id,
|
||||
data,
|
||||
defaultRunInputData: {},
|
||||
defaultRunInputData: NODES_EXTRA_DATA[data.type]?.defaultRunInputData || {},
|
||||
})
|
||||
const [singleRunParams, setSingleRunParams] = useState<PanelExposedType['singleRunParams'] | undefined>(undefined)
|
||||
|
||||
@ -256,9 +259,11 @@ const BasePanel: FC<BasePanelProps> = ({
|
||||
data,
|
||||
panelProps: {
|
||||
getInputVars,
|
||||
toVarInputs,
|
||||
runInputData,
|
||||
setRunInputData,
|
||||
runResult,
|
||||
runInputDataRef,
|
||||
},
|
||||
ref: childPanelRef,
|
||||
})}
|
||||
|
@ -32,7 +32,7 @@ import LoopDefault from '@/app/components/workflow/nodes/loop/default'
|
||||
import { ssePost } from '@/service/base'
|
||||
import { noop } from 'lodash-es'
|
||||
import { getInputVars as doGetInputVars } from '@/app/components/base/prompt-editor/constants'
|
||||
import type { NodeTracing } from '@/types/workflow'
|
||||
import type { NodeRunResult, NodeTracing } from '@/types/workflow'
|
||||
const { checkValid: checkLLMValid } = LLMDefault
|
||||
const { checkValid: checkKnowledgeRetrievalValid } = KnowledgeRetrievalDefault
|
||||
const { checkValid: checkIfElseValid } = IfElseDefault
|
||||
@ -152,7 +152,7 @@ const useOneStepRun = <T>({
|
||||
}, [])
|
||||
const iterationTimes = iteratorInputKey ? runInputData[iteratorInputKey].length : 0
|
||||
const loopTimes = loopInputKey ? runInputData[loopInputKey].length : 0
|
||||
const [runResult, setRunResult] = useState<any>(null)
|
||||
const [runResult, setRunResult] = useState<NodeRunResult | null>(null)
|
||||
|
||||
const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate()
|
||||
const [canShowSingleRun, setCanShowSingleRun] = useState(false)
|
||||
|
@ -1,8 +1,29 @@
|
||||
// import { RETRIEVAL_OUTPUT_STRUCT } from '../../constants'
|
||||
import { BlockEnum, EditionType } from '../../types'
|
||||
import { type NodeDefault, type PromptItem, PromptRole } from '../../types'
|
||||
import type { LLMNodeType } from './types'
|
||||
import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/blocks'
|
||||
|
||||
const RETRIEVAL_OUTPUT_STRUCT = `{
|
||||
"content": "",
|
||||
"title": "",
|
||||
"url": "",
|
||||
"icon": "",
|
||||
"metadata": {
|
||||
"dataset_id": "",
|
||||
"dataset_name": "",
|
||||
"document_id": [],
|
||||
"document_name": "",
|
||||
"document_data_source_type": "",
|
||||
"segment_id": "",
|
||||
"segment_position": "",
|
||||
"segment_word_count": "",
|
||||
"segment_hit_count": "",
|
||||
"segment_index_node_hash": "",
|
||||
"score": ""
|
||||
}
|
||||
}`
|
||||
|
||||
const i18nPrefix = 'workflow.errorMsg'
|
||||
|
||||
const nodeDefault: NodeDefault<LLMNodeType> = {
|
||||
@ -27,6 +48,10 @@ const nodeDefault: NodeDefault<LLMNodeType> = {
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
defaultRunInputData: {
|
||||
'#context#': [RETRIEVAL_OUTPUT_STRUCT],
|
||||
'#files#': [],
|
||||
},
|
||||
getAvailablePrevNodes(isChatMode: boolean) {
|
||||
const nodes = isChatMode
|
||||
? ALL_CHAT_AVAILABLE_BLOCKS
|
||||
|
@ -1,5 +1,4 @@
|
||||
import type { FC } from 'react'
|
||||
import React, { useCallback } from 'react'
|
||||
import React, { forwardRef, useCallback, useImperativeHandle } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import MemoryConfig from '../_base/components/memory-config'
|
||||
import VarReferencePicker from '../_base/components/variable/var-reference-picker'
|
||||
@ -15,28 +14,28 @@ import Split from '@/app/components/workflow/nodes/_base/components/split'
|
||||
import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
|
||||
import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
|
||||
import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
|
||||
import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
|
||||
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
|
||||
import ResultPanel from '@/app/components/workflow/run/result-panel'
|
||||
import Tooltip from '@/app/components/base/tooltip'
|
||||
import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
|
||||
import useCurrentVars from '../../hooks/use-current-vars'
|
||||
import StructureOutput from './components/structure-output'
|
||||
import Switch from '@/app/components/base/switch'
|
||||
import { RiAlertFill, RiQuestionLine } from '@remixicon/react'
|
||||
import type { PanelExposedType } from '@/types/workflow'
|
||||
|
||||
const i18nPrefix = 'workflow.nodes.llm'
|
||||
|
||||
const Panel: FC<NodePanelProps<LLMNodeType>> = ({
|
||||
const Panel = forwardRef<PanelExposedType, NodePanelProps<LLMNodeType>>(({
|
||||
id,
|
||||
data,
|
||||
}) => {
|
||||
panelProps,
|
||||
}, ref) => {
|
||||
const { t } = useTranslation()
|
||||
const {
|
||||
currentVars,
|
||||
getLastRunInfos,
|
||||
} = useCurrentVars()
|
||||
console.log(currentVars, getLastRunInfos())
|
||||
// console.log(currentVars, getLastRunInfos())
|
||||
const {
|
||||
readOnly,
|
||||
inputs,
|
||||
@ -63,26 +62,20 @@ const Panel: FC<NodePanelProps<LLMNodeType>> = ({
|
||||
handleMemoryChange,
|
||||
handleVisionResolutionEnabledChange,
|
||||
handleVisionResolutionChange,
|
||||
isShowSingleRun,
|
||||
hideSingleRun,
|
||||
inputVarValues,
|
||||
setInputVarValues,
|
||||
visionFiles,
|
||||
setVisionFiles,
|
||||
contexts,
|
||||
setContexts,
|
||||
runningStatus,
|
||||
isModelSupportStructuredOutput,
|
||||
structuredOutputCollapsed,
|
||||
setStructuredOutputCollapsed,
|
||||
handleStructureOutputEnableChange,
|
||||
handleStructureOutputChange,
|
||||
handleRun,
|
||||
handleStop,
|
||||
varInputs,
|
||||
runResult,
|
||||
filterJinjia2InputVar,
|
||||
} = useConfig(id, data)
|
||||
} = useConfig(id, data, panelProps)
|
||||
|
||||
const model = inputs.model
|
||||
|
||||
@ -147,6 +140,12 @@ const Panel: FC<NodePanelProps<LLMNodeType>> = ({
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
singleRunParams: {
|
||||
forms: singleRunForms,
|
||||
},
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className='mt-2'>
|
||||
<div className='space-y-4 px-4 pb-4'>
|
||||
@ -348,20 +347,8 @@ const Panel: FC<NodePanelProps<LLMNodeType>> = ({
|
||||
)}
|
||||
</>
|
||||
</OutputVars>
|
||||
{isShowSingleRun && (
|
||||
<BeforeRunForm
|
||||
nodeName={inputs.title}
|
||||
nodeType={inputs.type}
|
||||
onHide={hideSingleRun}
|
||||
forms={singleRunForms}
|
||||
runningStatus={runningStatus}
|
||||
onRun={handleRun}
|
||||
onStop={handleStop}
|
||||
result={<ResultPanel {...runResult} showSteps={false} />}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
export default React.memo(Panel)
|
||||
|
@ -16,11 +16,16 @@ import {
|
||||
ModelTypeEnum,
|
||||
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
|
||||
import { RETRIEVAL_OUTPUT_STRUCT } from '@/app/components/workflow/constants'
|
||||
import { checkHasContextBlock, checkHasHistoryBlock, checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants'
|
||||
import type { PanelProps } from '@/types/workflow'
|
||||
|
||||
const useConfig = (id: string, payload: LLMNodeType, panelProps?: PanelProps) => {
|
||||
const getVarInputs = panelProps?.getInputVars
|
||||
const toVarInputs = panelProps?.toVarInputs
|
||||
const runInputData = panelProps?.runInputData || {}
|
||||
const runInputDataRef = panelProps?.runInputDataRef || { current: {} }
|
||||
const setRunInputData = panelProps?.setRunInputData
|
||||
|
||||
const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
||||
const isChatMode = useIsChatMode()
|
||||
|
||||
@ -322,28 +327,6 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
filterVar: filterMemoryPromptVar,
|
||||
})
|
||||
|
||||
// single run
|
||||
const {
|
||||
isShowSingleRun,
|
||||
hideSingleRun,
|
||||
getInputVars,
|
||||
runningStatus,
|
||||
handleRun,
|
||||
handleStop,
|
||||
runInputData,
|
||||
runInputDataRef,
|
||||
setRunInputData,
|
||||
runResult,
|
||||
toVarInputs,
|
||||
} = useOneStepRun<LLMNodeType>({
|
||||
id,
|
||||
data: inputs,
|
||||
defaultRunInputData: {
|
||||
'#context#': [RETRIEVAL_OUTPUT_STRUCT],
|
||||
'#files#': [],
|
||||
},
|
||||
})
|
||||
|
||||
const inputVarValues = (() => {
|
||||
const vars: Record<string, any> = {}
|
||||
Object.keys(runInputData)
|
||||
@ -360,12 +343,12 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
'#context#': runInputDataRef.current['#context#'],
|
||||
'#files#': runInputDataRef.current['#files#'],
|
||||
}
|
||||
setRunInputData(newVars)
|
||||
setRunInputData?.(newVars)
|
||||
}, [runInputDataRef, setRunInputData])
|
||||
|
||||
const contexts = runInputData['#context#']
|
||||
const setContexts = useCallback((newContexts: string[]) => {
|
||||
setRunInputData({
|
||||
setRunInputData?.({
|
||||
...runInputDataRef.current,
|
||||
'#context#': newContexts,
|
||||
})
|
||||
@ -373,7 +356,7 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
|
||||
const visionFiles = runInputData['#files#']
|
||||
const setVisionFiles = useCallback((newFiles: any[]) => {
|
||||
setRunInputData({
|
||||
setRunInputData?.({
|
||||
...runInputDataRef.current,
|
||||
'#files#': newFiles,
|
||||
})
|
||||
@ -390,9 +373,9 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
})()
|
||||
|
||||
const varInputs = (() => {
|
||||
const vars = getInputVars(allVarStrArr)
|
||||
const vars = getVarInputs?.(allVarStrArr) || []
|
||||
if (isShowVars)
|
||||
return [...vars, ...toVarInputs(inputs.prompt_config?.jinja2_variables || [])]
|
||||
return [...vars, ...(toVarInputs ? (toVarInputs(inputs.prompt_config?.jinja2_variables || [])) : [])]
|
||||
|
||||
return vars
|
||||
})()
|
||||
@ -423,8 +406,6 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
handleSyeQueryChange,
|
||||
handleVisionResolutionEnabledChange,
|
||||
handleVisionResolutionChange,
|
||||
isShowSingleRun,
|
||||
hideSingleRun,
|
||||
inputVarValues,
|
||||
setInputVarValues,
|
||||
visionFiles,
|
||||
@ -432,15 +413,11 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
||||
contexts,
|
||||
setContexts,
|
||||
varInputs,
|
||||
runningStatus,
|
||||
isModelSupportStructuredOutput,
|
||||
handleStructureOutputChange,
|
||||
structuredOutputCollapsed,
|
||||
setStructuredOutputCollapsed,
|
||||
handleStructureOutputEnableChange,
|
||||
handleRun,
|
||||
handleStop,
|
||||
runResult,
|
||||
filterJinjia2InputVar,
|
||||
}
|
||||
}
|
||||
|
@ -295,6 +295,7 @@ export type Block = {
|
||||
|
||||
export type NodeDefault<T> = {
|
||||
defaultValue: Partial<T>
|
||||
defaultRunInputData: Record<string, any>
|
||||
getAvailablePrevNodes: (isChatMode: boolean) => BlockEnum[]
|
||||
getAvailableNextNodes: (isChatMode: boolean) => BlockEnum[]
|
||||
checkValid: (payload: T, t: any, moreDataForCheckValid?: any) => { isValid: boolean; errorMessage?: string }
|
||||
|
@ -1,9 +1,10 @@
|
||||
import type { Viewport } from 'reactflow'
|
||||
import type { BlockEnum, ConversationVariable, Edge, EnvironmentVariable, InputVar, Node } from '@/app/components/workflow/types'
|
||||
import type { BlockEnum, ConversationVariable, Edge, EnvironmentVariable, InputVar, Node, Variable } from '@/app/components/workflow/types'
|
||||
import type { TransferMethod } from '@/types/app'
|
||||
import type { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types'
|
||||
import type { BeforeRunFormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form'
|
||||
import type { SpecialResultPanelProps } from '@/app/components/workflow/run/special-result-panel'
|
||||
import type { MutableRefObject } from 'react'
|
||||
|
||||
export type AgentLogItem = {
|
||||
node_execution_id: string,
|
||||
@ -361,7 +362,11 @@ export type PanelExposedType = {
|
||||
|
||||
export type PanelProps = {
|
||||
getInputVars: (textList: string[]) => InputVar[]
|
||||
toVarInputs: (variables: Variable[]) => InputVar[]
|
||||
runInputData: Record<string, any>
|
||||
runInputDataRef: MutableRefObject<Record<string, any>>
|
||||
setRunInputData: (data: Record<string, any>) => void
|
||||
runResult: any
|
||||
}
|
||||
|
||||
export type NodeRunResult = NodeTracing
|
||||
|
Loading…
x
Reference in New Issue
Block a user