feat: editor history and query

This commit is contained in:
Joel 2024-03-18 14:49:05 +08:00
parent 6e8ea528c2
commit 25949338cb
4 changed files with 62 additions and 8 deletions

View File

@ -21,6 +21,12 @@ type Props = {
readOnly?: boolean
showRemove?: boolean
onRemove?: () => void
isChatModel: boolean
isChatApp: boolean
hasSetBlockStatus: {
history: boolean
query: boolean
}
}
const Editor: FC<Props> = ({
@ -31,9 +37,15 @@ const Editor: FC<Props> = ({
readOnly,
showRemove,
onRemove,
isChatModel,
isChatApp,
hasSetBlockStatus,
}) => {
const { t } = useTranslation()
const isShowHistory = !isChatModel && isChatApp
const isShowQuery = isShowHistory
const ref = useRef<HTMLDivElement>(null)
const {
wrapClassName,
@ -100,7 +112,7 @@ const Editor: FC<Props> = ({
style={isExpand ? { height: editorExpandHeight - 5 } : {}}
value={value}
contextBlock={{
show: true,
show: false,
selectable: true,
datasets: [],
onAddContext: () => { },
@ -109,22 +121,22 @@ const Editor: FC<Props> = ({
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}

View File

@ -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<Props> = ({
readOnly,
isChatModel,
isChatApp,
payload,
variables,
onChange,
hasSetBlockStatus,
}) => {
const { t } = useTranslation()
@ -122,6 +129,9 @@ const ConfigPrompt: FC<Props> = ({
readOnly={readOnly}
showRemove={(payload as PromptItem[]).length > 1}
onRemove={handleRemove(index)}
isChatModel={isChatModel}
isChatApp={isChatApp}
hasSetBlockStatus={hasSetBlockStatus}
/>
)
})
@ -143,6 +153,9 @@ const ConfigPrompt: FC<Props> = ({
onChange={handleCompletionPromptChange}
variables={variables}
readOnly={readOnly}
isChatModel={isChatModel}
isChatApp={isChatApp}
hasSetBlockStatus={hasSetBlockStatus}
/>
</div>
)}

View File

@ -31,9 +31,11 @@ const Panel: FC<NodePanelProps<LLMNodeType>> = ({
const {
inputs,
isChatModel,
isChatMode,
isCompletionModel,
isShowVisionConfig,
handleModelChanged,
hasSetBlockStatus,
handleCompletionParamsChange,
handleVarListChange,
handleAddVariable,
@ -164,9 +166,11 @@ const Panel: FC<NodePanelProps<LLMNodeType>> = ({
<ConfigPrompt
readOnly={readOnly}
isChatModel={isChatModel}
isChatApp={isChatMode}
payload={inputs.prompt_template}
variables={inputs.variables.map(item => item.variable)}
onChange={handlePromptChange}
hasSetBlockStatus={hasSetBlockStatus}
/>
)}

View File

@ -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,