mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
feat: Fix prompt template handling for Jinja2 edition type
This commit is contained in:
parent
bc771d9c50
commit
d394adfaf7
@ -6,7 +6,7 @@ import type { PromptItem } from '@/app/components/workflow/types'
|
||||
import { memo, useMemo } from 'react'
|
||||
import WorkflowWithDefaultContext from '@/app/components/workflow'
|
||||
import { WorkflowContextProvider } from '@/app/components/workflow/context'
|
||||
import { BlockEnum, PromptRole } from '@/app/components/workflow/types'
|
||||
import { BlockEnum, EditionType, PromptRole } from '@/app/components/workflow/types'
|
||||
import SubGraphMain from './components/sub-graph-main'
|
||||
import { useSubGraphNodes } from './hooks'
|
||||
import { createSubGraphSlice } from './store'
|
||||
@ -62,21 +62,40 @@ const SubGraph: FC<SubGraphProps> = (props) => {
|
||||
if (!extractorNode)
|
||||
return null
|
||||
|
||||
const nextPromptTemplate = Array.isArray(extractorNode.data.prompt_template)
|
||||
? extractorNode.data.prompt_template.map((item: PromptItem) => {
|
||||
if (item.role === PromptRole.system)
|
||||
return { ...item, text: promptText }
|
||||
return item
|
||||
})
|
||||
: {
|
||||
...extractorNode.data.prompt_template,
|
||||
const updateSystemPrompt = (item: PromptItem) => {
|
||||
if (item.role !== PromptRole.system)
|
||||
return item
|
||||
if (item.edition_type === EditionType.jinja2) {
|
||||
return {
|
||||
...item,
|
||||
text: promptText,
|
||||
jinja2_text: promptText,
|
||||
}
|
||||
}
|
||||
return { ...item, text: promptText }
|
||||
}
|
||||
|
||||
const nextPromptTemplate = Array.isArray(extractorNode.data.prompt_template)
|
||||
? extractorNode.data.prompt_template.map(updateSystemPrompt)
|
||||
: updateSystemPrompt(extractorNode.data.prompt_template as PromptItem)
|
||||
|
||||
const hasSystemPrompt = Array.isArray(nextPromptTemplate)
|
||||
&& nextPromptTemplate.some((item: PromptItem) => item.role === PromptRole.system)
|
||||
const defaultSystemPrompt: PromptItem = (() => {
|
||||
const useJinja = Array.isArray(nextPromptTemplate)
|
||||
&& nextPromptTemplate.some((item: PromptItem) => item.edition_type === EditionType.jinja2)
|
||||
if (useJinja) {
|
||||
return {
|
||||
role: PromptRole.system,
|
||||
text: promptText,
|
||||
jinja2_text: promptText,
|
||||
edition_type: EditionType.jinja2,
|
||||
}
|
||||
}
|
||||
return { role: PromptRole.system, text: promptText }
|
||||
})()
|
||||
const normalizedPromptTemplate = Array.isArray(nextPromptTemplate)
|
||||
? (hasSystemPrompt ? nextPromptTemplate : [{ role: PromptRole.system, text: promptText }, ...nextPromptTemplate])
|
||||
? (hasSystemPrompt ? nextPromptTemplate : [defaultSystemPrompt, ...nextPromptTemplate])
|
||||
: nextPromptTemplate
|
||||
|
||||
return {
|
||||
|
||||
@ -13,7 +13,7 @@ import { useStoreApi } from 'reactflow'
|
||||
import { Agent } from '@/app/components/base/icons/src/vender/workflow'
|
||||
import { useNodesSyncDraft } from '@/app/components/workflow/hooks'
|
||||
import { useStore } from '@/app/components/workflow/store'
|
||||
import { PromptRole } from '@/app/components/workflow/types'
|
||||
import { EditionType, PromptRole } from '@/app/components/workflow/types'
|
||||
import SubGraphCanvas from './sub-graph-canvas'
|
||||
|
||||
const SubGraphModal: FC<SubGraphModalProps> = ({
|
||||
@ -43,11 +43,18 @@ const SubGraphModal: FC<SubGraphModalProps> = ({
|
||||
const getSystemPromptText = useCallback((promptTemplate?: PromptItem[] | PromptItem) => {
|
||||
if (!promptTemplate)
|
||||
return ''
|
||||
const resolveText = (item?: PromptItem) => {
|
||||
if (!item)
|
||||
return ''
|
||||
if (item.edition_type === EditionType.jinja2)
|
||||
return item.jinja2_text || item.text || ''
|
||||
return item.text || ''
|
||||
}
|
||||
if (Array.isArray(promptTemplate)) {
|
||||
const systemPrompt = promptTemplate.find(item => item.role === PromptRole.system)
|
||||
return systemPrompt?.text || ''
|
||||
return resolveText(systemPrompt)
|
||||
}
|
||||
return promptTemplate.text || ''
|
||||
return resolveText(promptTemplate)
|
||||
}, [])
|
||||
|
||||
const handleSave = useCallback((subGraphNodes: any[], _edges: any[]) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user