feat: question classify node support use var in instruction (#4710)

This commit is contained in:
Joel 2024-06-04 14:01:40 +08:00 committed by GitHub
parent 86e7c7321f
commit a38dfc006e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 87 additions and 20 deletions

View File

@ -511,7 +511,10 @@ export const getNodeUsedVars = (node: Node): ValueSelector[] => {
break break
} }
case BlockEnum.QuestionClassifier: { case BlockEnum.QuestionClassifier: {
res = [(data as QuestionClassifierNodeType).query_variable_selector] const payload = (data as QuestionClassifierNodeType)
res = [payload.query_variable_selector]
const varInInstructions = matchNotSystemVars([payload.instruction || ''])
res.push(...varInInstructions)
break break
} }
case BlockEnum.HttpRequest: { case BlockEnum.HttpRequest: {
@ -726,6 +729,7 @@ export const updateNodeVars = (oldNode: Node, oldVarSelector: ValueSelector, new
const payload = data as QuestionClassifierNodeType const payload = data as QuestionClassifierNodeType
if (payload.query_variable_selector.join('.') === oldVarSelector.join('.')) if (payload.query_variable_selector.join('.') === oldVarSelector.join('.'))
payload.query_variable_selector = newVarSelector payload.query_variable_selector = newVarSelector
payload.instruction = replaceOldVarInText(payload.instruction, oldVarSelector, newVarSelector)
break break
} }
case BlockEnum.HttpRequest: { case BlockEnum.HttpRequest: {

View File

@ -2,9 +2,9 @@
import type { FC } from 'react' import type { FC } from 'react'
import React from 'react' import React from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import TextEditor from '../../_base/components/editor/text-editor'
import MemoryConfig from '../../_base/components/memory-config' import MemoryConfig from '../../_base/components/memory-config'
import type { Memory } from '@/app/components/workflow/types' import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
import type { Memory, Node, NodeOutPutVar } from '@/app/components/workflow/types'
import TooltipPlus from '@/app/components/base/tooltip-plus' import TooltipPlus from '@/app/components/base/tooltip-plus'
import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general' import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
const i18nPrefix = 'workflow.nodes.questionClassifiers' const i18nPrefix = 'workflow.nodes.questionClassifiers'
@ -16,6 +16,15 @@ type Props = {
memory?: Memory memory?: Memory
onMemoryChange: (memory?: Memory) => void onMemoryChange: (memory?: Memory) => void
readonly?: boolean readonly?: boolean
isChatModel: boolean
isChatApp: boolean
hasSetBlockStatus?: {
context: boolean
history: boolean
query: boolean
}
nodesOutputVars: NodeOutPutVar[]
availableNodes: Node[]
} }
const AdvancedSetting: FC<Props> = ({ const AdvancedSetting: FC<Props> = ({
@ -25,13 +34,17 @@ const AdvancedSetting: FC<Props> = ({
memory, memory,
onMemoryChange, onMemoryChange,
readonly, readonly,
isChatModel,
isChatApp,
hasSetBlockStatus,
nodesOutputVars,
availableNodes,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
return ( return (
<> <>
<TextEditor <Editor
isInNode
title={ title={
<div className='flex items-center space-x-1'> <div className='flex items-center space-x-1'>
<span className='uppercase'>{t(`${i18nPrefix}.instruction`)}</span> <span className='uppercase'>{t(`${i18nPrefix}.instruction`)}</span>
@ -45,15 +58,13 @@ const AdvancedSetting: FC<Props> = ({
} }
value={instruction} value={instruction}
onChange={onInstructionChange} onChange={onInstructionChange}
minHeight={160} readOnly={readonly}
placeholder={t(`${i18nPrefix}.instructionPlaceholder`)!} isChatModel={isChatModel}
headerRight={( isChatApp={isChatApp}
<div className='flex items-center h-full'> isShowContext={false}
<div className='text-xs font-medium text-gray-500'>{instruction?.length || 0}</div> hasSetBlockStatus={hasSetBlockStatus}
<div className='mx-3 h-3 w-px bg-gray-200'></div> nodesOutputVars={nodesOutputVars}
</div> availableNodes={availableNodes}
)}
readonly={readonly}
/> />
{!hideMemorySetting && ( {!hideMemorySetting && (
<MemoryConfig <MemoryConfig

View File

@ -27,18 +27,23 @@ const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
inputs, inputs,
handleModelChanged, handleModelChanged,
isChatMode, isChatMode,
isChatModel,
handleCompletionParamsChange, handleCompletionParamsChange,
handleQueryVarChange, handleQueryVarChange,
handleTopicsChange, handleTopicsChange,
hasSetBlockStatus,
availableVars,
availableNodesWithParent,
handleInstructionChange, handleInstructionChange,
inputVarValues,
varInputs,
setInputVarValues,
handleMemoryChange, handleMemoryChange,
isShowSingleRun, isShowSingleRun,
hideSingleRun, hideSingleRun,
runningStatus, runningStatus,
handleRun, handleRun,
handleStop, handleStop,
query,
setQuery,
runResult, runResult,
filterVar, filterVar,
} = useConfig(id, data) } = useConfig(id, data)
@ -99,6 +104,11 @@ const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
memory={inputs.memory} memory={inputs.memory}
onMemoryChange={handleMemoryChange} onMemoryChange={handleMemoryChange}
readonly={readOnly} readonly={readOnly}
isChatApp={isChatMode}
isChatModel={isChatModel}
hasSetBlockStatus={hasSetBlockStatus}
nodesOutputVars={availableVars}
availableNodes={availableNodesWithParent}
/> />
</Field> </Field>
</div> </div>
@ -125,9 +135,9 @@ const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
variable: 'query', variable: 'query',
type: InputVarType.paragraph, type: InputVarType.paragraph,
required: true, required: true,
}], }, ...varInputs],
values: { query }, values: inputVarValues,
onChange: keyValue => setQuery((keyValue as any).query), onChange: setInputVarValues,
}, },
]} ]}
runningStatus={runningStatus} runningStatus={runningStatus}

View File

@ -7,11 +7,13 @@ import {
useWorkflow, useWorkflow,
} from '../../hooks' } from '../../hooks'
import { useStore } from '../../store' import { useStore } from '../../store'
import useAvailableVarList from '../_base/hooks/use-available-var-list'
import type { QuestionClassifierNodeType } from './types' import type { QuestionClassifierNodeType } from './types'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' 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 useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks' import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants'
const useConfig = (id: string, payload: QuestionClassifierNodeType) => { const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
const { nodesReadOnly: readOnly } = useNodesReadOnly() const { nodesReadOnly: readOnly } = useNodesReadOnly()
@ -67,7 +69,6 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
draft.query_variable_selector = newVar as ValueSelector draft.query_variable_selector = newVar as ValueSelector
}) })
setInputs(newInputs) setInputs(newInputs)
// console.log(newInputs.query_variable_selector)
}, [inputs, setInputs]) }, [inputs, setInputs])
useEffect(() => { useEffect(() => {
@ -93,6 +94,24 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs]) }, [inputs, setInputs])
const filterInputVar = useCallback((varPayload: Var) => {
return [VarType.number, VarType.string].includes(varPayload.type)
}, [])
const {
availableVars,
availableNodesWithParent,
} = useAvailableVarList(id, {
onlyLeafNodeVar: false,
filterVar: filterInputVar,
})
const hasSetBlockStatus = {
history: false,
query: isChatMode ? checkHasQueryBlock(inputs.instruction) : false,
context: false,
}
const handleInstructionChange = useCallback((instruction: string) => { const handleInstructionChange = useCallback((instruction: string) => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {
draft.instruction = instruction draft.instruction = instruction
@ -111,6 +130,7 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
const { const {
isShowSingleRun, isShowSingleRun,
hideSingleRun, hideSingleRun,
getInputVars,
runningStatus, runningStatus,
handleRun, handleRun,
handleStop, handleStop,
@ -133,6 +153,22 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
}) })
}, [runInputData, setRunInputData]) }, [runInputData, setRunInputData])
const varInputs = getInputVars([inputs.instruction])
const inputVarValues = (() => {
const vars: Record<string, any> = {
query,
}
Object.keys(runInputData)
.forEach((key) => {
vars[key] = runInputData[key]
})
return vars
})()
const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
setRunInputData(newPayload)
}, [setRunInputData])
const filterVar = useCallback((varPayload: Var) => { const filterVar = useCallback((varPayload: Var) => {
return varPayload.type === VarType.string return varPayload.type === VarType.string
}, []) }, [])
@ -147,7 +183,13 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
handleQueryVarChange, handleQueryVarChange,
filterVar, filterVar,
handleTopicsChange: handleClassesChange, handleTopicsChange: handleClassesChange,
hasSetBlockStatus,
availableVars,
availableNodesWithParent,
handleInstructionChange, handleInstructionChange,
varInputs,
inputVarValues,
setInputVarValues,
handleMemoryChange, handleMemoryChange,
isShowSingleRun, isShowSingleRun,
hideSingleRun, hideSingleRun,