UnisKB/ui/src/workflow/nodes/ai-chat-node/index.vue

316 lines
10 KiB
Vue
Raw Normal View History

2024-07-01 01:45:59 +00:00
<template>
<NodeContainer :nodeModel="nodeModel">
2025-01-20 03:02:55 +00:00
<h5 class="title-decoration-1 mb-8">{{ $t('views.applicationWorkflow.nodeSetting') }}</h5>
2024-07-01 01:45:59 +00:00
<el-card shadow="never" class="card-never" style="--el-card-padding: 12px">
<el-form
@submit.prevent
:model="chat_data"
label-position="top"
require-asterisk-position="right"
label-width="auto"
ref="aiChatNodeFormRef"
2024-08-19 02:38:55 +00:00
hide-required-asterisk
2024-07-01 01:45:59 +00:00
>
<el-form-item
2025-01-20 03:02:55 +00:00
:label="$t('views.application.applicationForm.form.aiModel.label')"
2024-07-01 01:45:59 +00:00
prop="model_id"
:rules="{
required: true,
2025-01-20 03:02:55 +00:00
message: $t('views.application.applicationForm.form.aiModel.placeholder'),
2024-07-01 01:45:59 +00:00
trigger: 'change'
}"
>
2024-08-19 02:38:55 +00:00
<template #label>
2024-10-25 10:51:05 +00:00
<div class="flex-between w-full">
2024-08-19 02:38:55 +00:00
<div>
2025-01-20 03:02:55 +00:00
<span
>{{ $t('views.application.applicationForm.form.aiModel.label')
}}<span class="danger">*</span></span
>
2024-08-19 02:38:55 +00:00
</div>
2024-08-19 02:38:55 +00:00
<el-button
:disabled="!chat_data.model_id"
2024-08-19 02:38:55 +00:00
type="primary"
link
@click="openAIParamSettingDialog(chat_data.model_id)"
2024-08-19 07:09:39 +00:00
@refreshForm="refreshParam"
2024-08-19 02:38:55 +00:00
>
2025-01-26 04:08:10 +00:00
<el-icon><Setting /></el-icon>
2024-08-19 02:38:55 +00:00
</el-button>
</div>
</template>
2025-01-13 03:15:51 +00:00
<ModelSelect
@change="model_change"
2024-07-01 01:45:59 +00:00
@wheel="wheel"
:teleported="false"
v-model="chat_data.model_id"
2025-01-13 03:15:51 +00:00
:placeholder="$t('views.application.applicationForm.form.aiModel.placeholder')"
:options="modelOptions"
@submitModel="getModel"
showFooter
></ModelSelect>
2024-07-01 01:45:59 +00:00
</el-form-item>
2024-08-19 02:38:55 +00:00
2025-01-20 03:02:55 +00:00
<el-form-item :label="$t('views.application.applicationForm.form.roleSettings.label')">
2024-09-25 02:09:31 +00:00
<MdEditorMagnify
2025-01-20 03:02:55 +00:00
:title="$t('views.application.applicationForm.form.roleSettings.label')"
2024-07-01 01:45:59 +00:00
v-model="chat_data.system"
2024-09-25 02:09:31 +00:00
style="height: 100px"
@submitDialog="submitSystemDialog"
2025-01-20 03:02:55 +00:00
:placeholder="$t('views.application.applicationForm.form.roleSettings.label')"
2024-07-01 01:45:59 +00:00
/>
</el-form-item>
<el-form-item
2025-01-20 03:02:55 +00:00
:label="$t('views.application.applicationForm.form.prompt.label')"
prop="prompt"
:rules="{
required: true,
2025-01-20 03:02:55 +00:00
message: $t('views.application.applicationForm.form.prompt.requiredMessage'),
trigger: 'blur'
}"
>
2024-07-01 01:45:59 +00:00
<template #label>
<div class="flex align-center">
<div class="mr-4">
2025-01-20 03:02:55 +00:00
<span
>{{ $t('views.application.applicationForm.form.prompt.label')
}}<span class="danger">*</span></span
>
2024-07-01 01:45:59 +00:00
</div>
<el-tooltip effect="dark" placement="right" popper-class="max-w-200">
<template #content
2025-01-20 03:02:55 +00:00
>{{ $t('views.application.applicationForm.form.prompt.tooltip') }}
2024-08-19 02:38:55 +00:00
</template>
2024-07-01 01:45:59 +00:00
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
</div>
</template>
2024-09-18 08:03:33 +00:00
<MdEditorMagnify
@wheel="wheel"
2025-01-20 03:02:55 +00:00
:title="$t('views.application.applicationForm.form.prompt.label')"
2024-07-01 01:45:59 +00:00
v-model="chat_data.prompt"
2024-09-18 08:03:33 +00:00
style="height: 150px"
@submitDialog="submitDialog"
/>
2024-07-01 01:45:59 +00:00
</el-form-item>
2025-01-20 03:02:55 +00:00
<el-form-item :label="$t('views.application.applicationForm.form.historyRecord.label')">
<template #label>
<div class="flex-between">
2025-01-20 03:02:55 +00:00
<div>{{ $t('views.application.applicationForm.form.historyRecord.label') }}</div>
<el-select v-model="chat_data.dialogue_type" type="small" style="width: 100px">
2025-01-18 09:15:31 +00:00
<el-option :label="$t('views.applicationWorkflow.node')" value="NODE" />
2025-01-20 03:02:55 +00:00
<el-option :label="$t('views.applicationWorkflow.workflow')" value="WORKFLOW" />
</el-select>
</div>
</template>
2024-07-01 01:45:59 +00:00
<el-input-number
v-model="chat_data.dialogue_number"
:min="0"
:value-on-clear="0"
controls-position="right"
class="w-full"
2024-11-22 08:27:43 +00:00
:step="1"
:step-strictly="true"
2024-07-01 01:45:59 +00:00
/>
</el-form-item>
<el-form-item @click.prevent>
<template #label>
<div class="flex align-center">
<div class="mr-4">
2025-01-20 03:02:55 +00:00
<span
>{{ $t('views.applicationWorkflow.nodes.aiChatNode.returnContent.label')
}}<span class="danger">*</span></span
>
</div>
<el-tooltip effect="dark" placement="right" popper-class="max-w-200">
<template #content>
2025-01-20 03:02:55 +00:00
{{ $t('views.applicationWorkflow.nodes.aiChatNode.returnContent.tooltip') }}
</template>
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
</div>
</template>
<el-switch size="small" v-model="chat_data.is_result" />
</el-form-item>
<el-form-item @click.prevent>
<template #label>
<div class="flex-between w-full">
<div>
<span>{{
$t('views.application.applicationForm.form.reasoningContent.label')
}}</span>
</div>
<el-button
type="primary"
link
@click="openReasoningParamSettingDialog"
@refreshForm="refreshParam"
>
<el-icon><Setting /></el-icon>
</el-button>
</div>
</template>
<el-switch size="small" v-model="chat_data.model_setting.reasoning_content_enable" />
</el-form-item>
2024-07-01 01:45:59 +00:00
</el-form>
</el-card>
2024-09-18 08:03:33 +00:00
2024-08-23 09:46:05 +00:00
<AIModeParamSettingDialog ref="AIModeParamSettingDialogRef" @refresh="refreshParam" />
<ReasoningParamSettingDialog
ref="ReasoningParamSettingDialogRef"
@refresh="submitReasoningDialog"
/>
2024-07-01 01:45:59 +00:00
</NodeContainer>
</template>
<script setup lang="ts">
import { cloneDeep, set, groupBy } from 'lodash'
2024-07-01 01:45:59 +00:00
import { app } from '@/main'
import NodeContainer from '@/workflow/common/NodeContainer.vue'
import type { FormInstance } from 'element-plus'
import { ref, computed, onMounted } from 'vue'
import applicationApi from '@/api/application'
import useStore from '@/stores'
import { isLastNode } from '@/workflow/common/data'
2024-08-19 02:38:55 +00:00
import AIModeParamSettingDialog from '@/views/application/component/AIModeParamSettingDialog.vue'
2025-01-20 03:02:55 +00:00
import { t } from '@/locales'
import ReasoningParamSettingDialog from '@/views/application/component/ReasoningParamSettingDialog.vue'
2024-08-28 07:57:06 +00:00
const { model } = useStore()
2024-07-01 01:45:59 +00:00
const wheel = (e: any) => {
2024-08-28 07:57:06 +00:00
if (e.ctrlKey === true) {
2024-07-01 01:45:59 +00:00
e.preventDefault()
2024-08-28 07:57:06 +00:00
return true
2024-07-01 01:45:59 +00:00
} else {
e.stopPropagation()
return true
}
}
2024-08-19 02:38:55 +00:00
2024-09-25 02:09:31 +00:00
function submitSystemDialog(val: string) {
set(props.nodeModel.properties.node_data, 'system', val)
}
2024-09-18 08:03:33 +00:00
function submitDialog(val: string) {
set(props.nodeModel.properties.node_data, 'prompt', val)
}
2024-08-19 02:38:55 +00:00
const model_change = (model_id?: string) => {
if (model_id) {
AIModeParamSettingDialogRef.value?.reset_default(model_id, id)
} else {
refreshParam({})
}
}
2024-07-01 01:45:59 +00:00
const {
params: { id }
} = app.config.globalProperties.$route as any
// @ts-ignore
2025-01-20 07:42:28 +00:00
const defaultPrompt = `${t('views.applicationWorkflow.nodes.aiChatNode.defaultPrompt')}
{{${t('views.applicationWorkflow.nodes.searchDatasetNode.label')}.data}}
${t('views.problem.title')}
{{${t('views.applicationWorkflow.nodes.startNode.label')}.question}}`
2024-07-01 01:45:59 +00:00
const form = {
model_id: '',
system: '',
prompt: defaultPrompt,
dialogue_number: 1,
2024-08-19 02:38:55 +00:00
is_result: false,
temperature: null,
max_tokens: null,
dialogue_type: 'WORKFLOW',
model_setting: {
reasoning_content_start: '<think>',
reasoning_content_end: '</think>',
reasoning_content_enable: false
}
2024-07-01 01:45:59 +00:00
}
const chat_data = computed({
get: () => {
if (props.nodeModel.properties.node_data) {
if (!props.nodeModel.properties.node_data.model_setting) {
set(props.nodeModel.properties.node_data, 'model_setting', {
reasoning_content_start: '<think>',
reasoning_content_end: '</think>',
reasoning_content_enable: false
})
}
2024-07-01 01:45:59 +00:00
return props.nodeModel.properties.node_data
} else {
set(props.nodeModel.properties, 'node_data', form)
}
2024-07-01 01:45:59 +00:00
return props.nodeModel.properties.node_data
},
set: (value) => {
set(props.nodeModel.properties, 'node_data', value)
}
})
const props = defineProps<{ nodeModel: any }>()
const aiChatNodeFormRef = ref<FormInstance>()
const modelOptions = ref<any>(null)
2024-08-19 02:38:55 +00:00
const AIModeParamSettingDialogRef = ref<InstanceType<typeof AIModeParamSettingDialog>>()
const ReasoningParamSettingDialogRef = ref<InstanceType<typeof ReasoningParamSettingDialog>>()
2024-07-01 01:45:59 +00:00
const validate = () => {
return aiChatNodeFormRef.value?.validate().catch((err) => {
return Promise.reject({ node: props.nodeModel, errMessage: err })
})
}
function getModel() {
if (id) {
applicationApi.getApplicationModel(id).then((res: any) => {
modelOptions.value = groupBy(res?.data, 'provider')
})
} else {
model.asyncGetModel().then((res: any) => {
modelOptions.value = groupBy(res?.data, 'provider')
})
}
}
2024-08-19 02:38:55 +00:00
const openAIParamSettingDialog = (modelId: string) => {
if (modelId) {
AIModeParamSettingDialogRef.value?.open(modelId, id, chat_data.value.model_params_setting)
2024-08-19 02:38:55 +00:00
}
}
const openReasoningParamSettingDialog = () => {
ReasoningParamSettingDialogRef.value?.open(chat_data.value.model_setting)
}
2024-08-19 02:38:55 +00:00
function refreshParam(data: any) {
2024-08-23 09:46:05 +00:00
set(props.nodeModel.properties.node_data, 'model_params_setting', data)
2024-08-19 02:38:55 +00:00
}
function submitReasoningDialog(val: any) {
let model_setting = cloneDeep(props.nodeModel.properties.node_data.model_setting)
model_setting = {
...model_setting,
...val
}
set(props.nodeModel.properties.node_data, 'model_setting', model_setting)
}
2024-07-01 01:45:59 +00:00
onMounted(() => {
getModel()
if (typeof props.nodeModel.properties.node_data?.is_result === 'undefined') {
if (isLastNode(props.nodeModel)) {
set(props.nodeModel.properties.node_data, 'is_result', true)
}
}
2024-07-01 01:45:59 +00:00
set(props.nodeModel, 'validate', validate)
2024-12-10 08:37:57 +00:00
if (!chat_data.value.dialogue_type) {
chat_data.value.dialogue_type = 'WORKFLOW'
}
2024-07-01 01:45:59 +00:00
})
</script>
2024-09-18 08:03:33 +00:00
<style lang="scss" scoped></style>