mirror of https://github.com/langgenius/dify.git
feat: config conversation role name
This commit is contained in:
parent
9e6940ed3e
commit
cbb298ccb6
|
|
@ -5,11 +5,42 @@ import { useTranslation } from 'react-i18next'
|
|||
import produce from 'immer'
|
||||
import cn from 'classnames'
|
||||
import type { Memory } from '../../../types'
|
||||
import { MemoryRole } from '../../../types'
|
||||
import Field from '@/app/components/workflow/nodes/_base/components/field'
|
||||
import Switch from '@/app/components/base/switch'
|
||||
import Slider from '@/app/components/base/slider'
|
||||
|
||||
const i18nPrefix = 'workflow.nodes.common.memory'
|
||||
const WINDOW_SIZE_MIN = 1
|
||||
const WINDOW_SIZE_MAX = 100
|
||||
const WINDOW_SIZE_DEFAULT = 50
|
||||
type RoleItemProps = {
|
||||
readonly: boolean
|
||||
title: string
|
||||
value: string
|
||||
onChange: (value: string) => void
|
||||
}
|
||||
const RoleItem: FC<RoleItemProps> = ({
|
||||
readonly,
|
||||
title,
|
||||
value,
|
||||
onChange,
|
||||
}) => {
|
||||
const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
onChange(e.target.value)
|
||||
}, [onChange])
|
||||
return (
|
||||
<div className='flex items-center justify-between'>
|
||||
<div className='text-[13px] font-normal text-gray-700'>{title}</div>
|
||||
<input
|
||||
readOnly={readonly}
|
||||
value={value}
|
||||
onChange={handleChange}
|
||||
className='w-[200px] h-8 leading-8 px-2.5 rounded-lg border-0 bg-gray-100 text-gray-900 text-[13px] placeholder:text-gray-400 focus:outline-none focus:ring-1 focus:ring-inset focus:ring-gray-200'
|
||||
type='text' />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
type Props = {
|
||||
className?: string
|
||||
|
|
@ -19,10 +50,6 @@ type Props = {
|
|||
canSetRoleName?: boolean
|
||||
}
|
||||
|
||||
const WINDOW_SIZE_MIN = 1
|
||||
const WINDOW_SIZE_MAX = 100
|
||||
const WINDOW_SIZE_DEFAULT = 50
|
||||
|
||||
const MemoryConfig: FC<Props> = ({
|
||||
className,
|
||||
readonly,
|
||||
|
|
@ -70,6 +97,21 @@ const MemoryConfig: FC<Props> = ({
|
|||
if (payload.window.size === '' || payload.window.size === null)
|
||||
handleWindowSizeChange(WINDOW_SIZE_DEFAULT)
|
||||
}, [handleWindowSizeChange, payload.window?.size])
|
||||
|
||||
const handleRolePrefixChange = useCallback((role: MemoryRole) => {
|
||||
return (value: string) => {
|
||||
const newPayload = produce(payload, (draft) => {
|
||||
if (!draft.role_prefix) {
|
||||
draft.role_prefix = {
|
||||
user: '',
|
||||
assistant: '',
|
||||
}
|
||||
}
|
||||
draft.role_prefix[role] = value
|
||||
})
|
||||
onChange(newPayload)
|
||||
}
|
||||
}, [payload, onChange])
|
||||
return (
|
||||
<div className={cn(className)}>
|
||||
<Field
|
||||
|
|
@ -112,7 +154,24 @@ const MemoryConfig: FC<Props> = ({
|
|||
</div>
|
||||
</div>
|
||||
{canSetRoleName && (
|
||||
<div>Role name</div>
|
||||
<div className='mt-4'>
|
||||
<div className='leading-6 text-xs font-medium text-gray-500 uppercase'>{t(`${i18nPrefix}.conversationRoleName`)}</div>
|
||||
<div className='mt-1 space-y-2'>
|
||||
<RoleItem
|
||||
readonly={readonly}
|
||||
title={t(`${i18nPrefix}.user`)}
|
||||
value={payload.role_prefix?.user || ''}
|
||||
onChange={handleRolePrefixChange(MemoryRole.user)}
|
||||
/>
|
||||
<RoleItem
|
||||
readonly={readonly}
|
||||
title={t(`${i18nPrefix}.assistant`)}
|
||||
value={payload.role_prefix?.assistant || ''}
|
||||
onChange={handleRolePrefixChange(MemoryRole.assistant)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
)}
|
||||
</>
|
||||
</Field>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { MemoryRole } from '../../types'
|
||||
import { BlockEnum } from '../../types'
|
||||
import type { QuestionClassifierNodeType } from './types'
|
||||
|
||||
export const mockData: QuestionClassifierNodeType = {
|
||||
title: 'Test',
|
||||
desc: 'Test',
|
||||
type: 'Test',
|
||||
type: BlockEnum.QuestionClassifier,
|
||||
query_variable_selector: ['aaa', 'name'],
|
||||
model: {
|
||||
provider: 'openai',
|
||||
|
|
@ -28,7 +28,6 @@ export const mockData: QuestionClassifierNodeType = {
|
|||
],
|
||||
instruction: 'You are an entity extraction model that accepts an input',
|
||||
memory: {
|
||||
role_prefix: MemoryRole.assistant,
|
||||
window: {
|
||||
enabled: false,
|
||||
size: 0,
|
||||
|
|
|
|||
|
|
@ -90,8 +90,13 @@ export enum MemoryRole {
|
|||
assistant = 'assistant',
|
||||
}
|
||||
|
||||
export type RolePrefix = {
|
||||
user: string
|
||||
assistant: string
|
||||
}
|
||||
|
||||
export type Memory = {
|
||||
role_prefix?: MemoryRole
|
||||
role_prefix?: RolePrefix
|
||||
window: {
|
||||
enabled: boolean
|
||||
size: number | string | null
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ const translation = {
|
|||
memory: 'Memory',
|
||||
memoryTip: 'Chat memory settings',
|
||||
windowSize: 'Window Size',
|
||||
conversationRoleName: 'Conversation Role Name',
|
||||
user: 'User prefix',
|
||||
assistant: 'Assistant prefix',
|
||||
},
|
||||
},
|
||||
start: {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ const translation = {
|
|||
memory: '记忆',
|
||||
memoryTip: '聊天记忆设置',
|
||||
windowSize: '记忆窗口',
|
||||
conversationRoleName: '对话角色名',
|
||||
user: '用户前缀',
|
||||
assistant: '助手前缀',
|
||||
},
|
||||
},
|
||||
start: {
|
||||
|
|
|
|||
Loading…
Reference in New Issue