From 25949338cb46e3381201f4269457985b0fc1131f Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 18 Mar 2024 14:49:05 +0800 Subject: [PATCH] feat: editor history and query --- .../nodes/_base/components/prompt/editor.tsx | 28 +++++++++++++------ .../nodes/llm/components/config-prompt.tsx | 13 +++++++++ .../components/workflow/nodes/llm/panel.tsx | 4 +++ .../workflow/nodes/llm/use-config.ts | 25 +++++++++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx b/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx index b0e5a0d600..480f2ed924 100644 --- a/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx +++ b/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx @@ -21,6 +21,12 @@ type Props = { readOnly?: boolean showRemove?: boolean onRemove?: () => void + isChatModel: boolean + isChatApp: boolean + hasSetBlockStatus: { + history: boolean + query: boolean + } } const Editor: FC = ({ @@ -31,9 +37,15 @@ const Editor: FC = ({ readOnly, showRemove, onRemove, + isChatModel, + isChatApp, + hasSetBlockStatus, }) => { const { t } = useTranslation() + const isShowHistory = !isChatModel && isChatApp + const isShowQuery = isShowHistory + const ref = useRef(null) const { wrapClassName, @@ -100,7 +112,7 @@ const Editor: FC = ({ style={isExpand ? { height: editorExpandHeight - 5 } : {}} value={value} contextBlock={{ - show: true, + show: false, selectable: true, datasets: [], onAddContext: () => { }, @@ -109,22 +121,22 @@ const Editor: FC = ({ variables: variables.map(item => ({ name: item, value: item, - })), + })), // add context externalTools: [], onAddExternalTool: () => { }, }} historyBlock={{ - show: true, - selectable: true, + show: isShowHistory, + selectable: !hasSetBlockStatus?.history, history: { - user: 'user', - assistant: 'xxx', + user: 'Human', + assistant: 'Assistant', }, onEditRole: () => { }, }} queryBlock={{ - show: true, - selectable: true, + show: isShowQuery, + selectable: !hasSetBlockStatus?.query, }} onChange={onChange} onBlur={setBlur} diff --git a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx index 40e5d67a53..5ab70100b9 100644 --- a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx +++ b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx @@ -16,17 +16,24 @@ const i18nPrefix = 'workflow.nodes.llm' type Props = { readOnly: boolean isChatModel: boolean + isChatApp: boolean payload: PromptItem | PromptItem[] variables: string[] onChange: (payload: PromptItem | PromptItem[]) => void + hasSetBlockStatus: { + history: boolean + query: boolean + } } const ConfigPrompt: FC = ({ readOnly, isChatModel, + isChatApp, payload, variables, onChange, + hasSetBlockStatus, }) => { const { t } = useTranslation() @@ -122,6 +129,9 @@ const ConfigPrompt: FC = ({ readOnly={readOnly} showRemove={(payload as PromptItem[]).length > 1} onRemove={handleRemove(index)} + isChatModel={isChatModel} + isChatApp={isChatApp} + hasSetBlockStatus={hasSetBlockStatus} /> ) }) @@ -143,6 +153,9 @@ const ConfigPrompt: FC = ({ onChange={handleCompletionPromptChange} variables={variables} readOnly={readOnly} + isChatModel={isChatModel} + isChatApp={isChatApp} + hasSetBlockStatus={hasSetBlockStatus} /> )} diff --git a/web/app/components/workflow/nodes/llm/panel.tsx b/web/app/components/workflow/nodes/llm/panel.tsx index c293fd85dd..8e859b7ef4 100644 --- a/web/app/components/workflow/nodes/llm/panel.tsx +++ b/web/app/components/workflow/nodes/llm/panel.tsx @@ -31,9 +31,11 @@ const Panel: FC> = ({ const { inputs, isChatModel, + isChatMode, isCompletionModel, isShowVisionConfig, handleModelChanged, + hasSetBlockStatus, handleCompletionParamsChange, handleVarListChange, handleAddVariable, @@ -164,9 +166,11 @@ const Panel: FC> = ({ item.variable)} onChange={handlePromptChange} + hasSetBlockStatus={hasSetBlockStatus} /> )} diff --git a/web/app/components/workflow/nodes/llm/use-config.ts b/web/app/components/workflow/nodes/llm/use-config.ts index e6e66c6c26..6a65588ee5 100644 --- a/web/app/components/workflow/nodes/llm/use-config.ts +++ b/web/app/components/workflow/nodes/llm/use-config.ts @@ -13,6 +13,7 @@ import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-cr import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run' import type { PromptItem } from '@/models/debug' import { RETRIEVAL_OUTPUT_STRUCT } from '@/app/components/workflow/constants' +import { checkHasHistoryBlock, checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants' const useConfig = (id: string, payload: LLMNodeType) => { const isChatMode = useIsChatMode() @@ -27,8 +28,31 @@ const useConfig = (id: string, payload: LLMNodeType) => { const model = inputs.model const modelMode = inputs.model?.mode const isChatModel = modelMode === 'chat' + const isCompletionModel = !isChatModel + const hasSetBlockStatus = (() => { + const promptTemplate = inputs.prompt_template + if (!isChatMode) { + return { + history: false, + query: false, + } + } + if (isChatModel) { + return { + history: false, + query: (promptTemplate as PromptItem[]).some(item => checkHasQueryBlock(item.text)), + } + } + else { + return { + history: checkHasHistoryBlock((promptTemplate as PromptItem).text), + query: checkHasQueryBlock((promptTemplate as PromptItem).text), + } + } + })() + const appendDefaultPromptConfig = useCallback((draft: LLMNodeType, defaultConfig: any, passInIsChatMode?: boolean) => { const promptTemplates = defaultConfig.prompt_templates if (passInIsChatMode === undefined ? isChatModel : passInIsChatMode) { @@ -216,6 +240,7 @@ const useConfig = (id: string, payload: LLMNodeType) => { inputs, isChatModel, isCompletionModel, + hasSetBlockStatus, isShowVisionConfig, handleModelChanged, handleCompletionParamsChange,