feat: llm support jinja fe (#4260)
This commit is contained in:
@@ -3,7 +3,8 @@ import type { FC } from 'react'
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { uniqueId } from 'lodash-es'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import type { PromptItem } from '../../../types'
|
||||
import type { PromptItem, Variable } from '../../../types'
|
||||
import { EditionType } from '../../../types'
|
||||
import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
|
||||
import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
|
||||
import TooltipPlus from '@/app/components/base/tooltip-plus'
|
||||
@@ -24,6 +25,7 @@ type Props = {
|
||||
payload: PromptItem
|
||||
handleChatModeMessageRoleChange: (role: PromptRole) => void
|
||||
onPromptChange: (p: string) => void
|
||||
onEditionTypeChange: (editionType: EditionType) => void
|
||||
onRemove: () => void
|
||||
isShowContext: boolean
|
||||
hasSetBlockStatus: {
|
||||
@@ -33,6 +35,8 @@ type Props = {
|
||||
}
|
||||
availableVars: any
|
||||
availableNodes: any
|
||||
varList: Variable[]
|
||||
handleAddVariable: (payload: any) => void
|
||||
}
|
||||
|
||||
const roleOptions = [
|
||||
@@ -64,17 +68,21 @@ const ConfigPromptItem: FC<Props> = ({
|
||||
isChatApp,
|
||||
payload,
|
||||
onPromptChange,
|
||||
onEditionTypeChange,
|
||||
onRemove,
|
||||
isShowContext,
|
||||
hasSetBlockStatus,
|
||||
availableVars,
|
||||
availableNodes,
|
||||
varList,
|
||||
handleAddVariable,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const [instanceId, setInstanceId] = useState(uniqueId())
|
||||
useEffect(() => {
|
||||
setInstanceId(`${id}-${uniqueId()}`)
|
||||
}, [id])
|
||||
|
||||
return (
|
||||
<Editor
|
||||
className={className}
|
||||
@@ -107,7 +115,7 @@ const ConfigPromptItem: FC<Props> = ({
|
||||
</TooltipPlus>
|
||||
</div>
|
||||
}
|
||||
value={payload.text}
|
||||
value={payload.edition_type === EditionType.jinja2 ? (payload.jinja2_text || '') : payload.text}
|
||||
onChange={onPromptChange}
|
||||
readOnly={readOnly}
|
||||
showRemove={canRemove}
|
||||
@@ -118,6 +126,11 @@ const ConfigPromptItem: FC<Props> = ({
|
||||
hasSetBlockStatus={hasSetBlockStatus}
|
||||
nodesOutputVars={availableVars}
|
||||
availableNodes={availableNodes}
|
||||
isSupportJinja
|
||||
editionType={payload.edition_type}
|
||||
onEditionTypeChange={onEditionTypeChange}
|
||||
varList={varList}
|
||||
handleAddVariable={handleAddVariable}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
@@ -6,8 +6,8 @@ import produce from 'immer'
|
||||
import { ReactSortable } from 'react-sortablejs'
|
||||
import { v4 as uuid4 } from 'uuid'
|
||||
import cn from 'classnames'
|
||||
import type { PromptItem, ValueSelector, Var } from '../../../types'
|
||||
import { PromptRole } from '../../../types'
|
||||
import type { PromptItem, ValueSelector, Var, Variable } from '../../../types'
|
||||
import { EditionType, PromptRole } from '../../../types'
|
||||
import useAvailableVarList from '../../_base/hooks/use-available-var-list'
|
||||
import ConfigPromptItem from './config-prompt-item'
|
||||
import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
|
||||
@@ -30,6 +30,8 @@ type Props = {
|
||||
history: boolean
|
||||
query: boolean
|
||||
}
|
||||
varList?: Variable[]
|
||||
handleAddVariable: (payload: any) => void
|
||||
}
|
||||
|
||||
const ConfigPrompt: FC<Props> = ({
|
||||
@@ -42,10 +44,12 @@ const ConfigPrompt: FC<Props> = ({
|
||||
onChange,
|
||||
isShowContext,
|
||||
hasSetBlockStatus,
|
||||
varList = [],
|
||||
handleAddVariable,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const payloadWithIds = (isChatModel && Array.isArray(payload))
|
||||
? payload.map((item, i) => {
|
||||
? payload.map((item) => {
|
||||
const id = uuid4()
|
||||
return {
|
||||
id: item.id || id,
|
||||
@@ -67,7 +71,16 @@ const ConfigPrompt: FC<Props> = ({
|
||||
const handleChatModePromptChange = useCallback((index: number) => {
|
||||
return (prompt: string) => {
|
||||
const newPrompt = produce(payload as PromptItem[], (draft) => {
|
||||
draft[index].text = prompt
|
||||
draft[index][draft[index].edition_type === EditionType.jinja2 ? 'jinja2_text' : 'text'] = prompt
|
||||
})
|
||||
onChange(newPrompt)
|
||||
}
|
||||
}, [onChange, payload])
|
||||
|
||||
const handleChatModeEditionTypeChange = useCallback((index: number) => {
|
||||
return (editionType: EditionType) => {
|
||||
const newPrompt = produce(payload as PromptItem[], (draft) => {
|
||||
draft[index].edition_type = editionType
|
||||
})
|
||||
onChange(newPrompt)
|
||||
}
|
||||
@@ -106,7 +119,14 @@ const ConfigPrompt: FC<Props> = ({
|
||||
|
||||
const handleCompletionPromptChange = useCallback((prompt: string) => {
|
||||
const newPrompt = produce(payload as PromptItem, (draft) => {
|
||||
draft.text = prompt
|
||||
draft[draft.edition_type === EditionType.jinja2 ? 'jinja2_text' : 'text'] = prompt
|
||||
})
|
||||
onChange(newPrompt)
|
||||
}, [onChange, payload])
|
||||
|
||||
const handleCompletionEditionTypeChange = useCallback((editionType: EditionType) => {
|
||||
const newPrompt = produce(payload as PromptItem, (draft) => {
|
||||
draft.edition_type = editionType
|
||||
})
|
||||
onChange(newPrompt)
|
||||
}, [onChange, payload])
|
||||
@@ -161,11 +181,14 @@ const ConfigPrompt: FC<Props> = ({
|
||||
isChatApp={isChatApp}
|
||||
payload={item}
|
||||
onPromptChange={handleChatModePromptChange(index)}
|
||||
onEditionTypeChange={handleChatModeEditionTypeChange(index)}
|
||||
onRemove={handleRemove(index)}
|
||||
isShowContext={isShowContext}
|
||||
hasSetBlockStatus={hasSetBlockStatus}
|
||||
availableVars={availableVars}
|
||||
availableNodes={availableNodes}
|
||||
varList={varList}
|
||||
handleAddVariable={handleAddVariable}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -187,7 +210,7 @@ const ConfigPrompt: FC<Props> = ({
|
||||
<Editor
|
||||
instanceId={`${nodeId}-chat-workflow-llm-prompt-editor`}
|
||||
title={<span className='capitalize'>{t(`${i18nPrefix}.prompt`)}</span>}
|
||||
value={(payload as PromptItem).text}
|
||||
value={(payload as PromptItem).edition_type === EditionType.basic ? (payload as PromptItem).text : ((payload as PromptItem).jinja2_text || '')}
|
||||
onChange={handleCompletionPromptChange}
|
||||
readOnly={readOnly}
|
||||
isChatModel={isChatModel}
|
||||
@@ -196,6 +219,11 @@ const ConfigPrompt: FC<Props> = ({
|
||||
hasSetBlockStatus={hasSetBlockStatus}
|
||||
nodesOutputVars={availableVars}
|
||||
availableNodes={availableNodes}
|
||||
isSupportJinja
|
||||
editionType={(payload as PromptItem).edition_type}
|
||||
varList={varList}
|
||||
onEditionTypeChange={handleCompletionEditionTypeChange}
|
||||
handleAddVariable={handleAddVariable}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
Reference in New Issue
Block a user