UnisKB/ui/src/views/application/CreateAndSetting.vue

372 lines
12 KiB
Vue
Raw Normal View History

2023-11-15 09:42:31 +00:00
<template>
2023-12-05 08:30:22 +00:00
<LayoutContainer
:header="id ? '设置' : '创建应用'"
2023-12-05 11:21:13 +00:00
:back-to="id ? '' : '-1'"
2023-12-05 08:30:22 +00:00
class="create-application"
>
2023-11-28 10:53:22 +00:00
<el-row v-loading="loading">
2023-11-22 09:04:47 +00:00
<el-col :span="10">
2023-11-22 10:37:08 +00:00
<div class="p-24 mb-16" style="padding-bottom: 0">
2023-11-22 09:04:47 +00:00
<h4 class="title-decoration-1">应用信息</h4>
</div>
<div class="scrollbar-height-left">
<el-scrollbar>
<el-form
ref="applicationFormRef"
:model="applicationForm"
:rules="rules"
label-position="top"
require-asterisk-position="right"
class="p-24"
2023-11-22 10:37:08 +00:00
style="padding-top: 0"
2023-11-22 09:04:47 +00:00
>
<el-form-item label="应用名称" prop="name">
<el-input
v-model="applicationForm.name"
maxlength="64"
placeholder="请输入应用名称"
show-word-limit
/>
</el-form-item>
<el-form-item label="应用描述">
<el-input
v-model="applicationForm.desc"
type="textarea"
placeholder="描述该应用的应用场景及用途MaxKB 小助手回答用户提出的 MaxKB 产品使用问题"
:rows="3"
maxlength="500"
show-word-limit
/>
</el-form-item>
<el-form-item label="选择模型" prop="model_id">
2023-11-24 06:49:25 +00:00
<el-select
v-model="applicationForm.model_id"
placeholder="请选择模型"
style="width: 100%"
>
2023-11-24 02:40:53 +00:00
<el-option-group
v-for="(value, label) in modelOptions"
:key="value"
2023-11-27 03:21:56 +00:00
:label="realatedObject(providerOptions, label, 'provider')?.name"
2023-11-24 02:40:53 +00:00
>
<el-option
v-for="item in value"
:key="item.id"
:label="item.name"
:value="item.id"
2023-11-24 06:49:25 +00:00
class="flex-between"
2023-11-24 02:40:53 +00:00
>
2023-11-24 06:49:25 +00:00
<div class="flex">
<span
2023-11-27 03:21:56 +00:00
v-html="realatedObject(providerOptions, label, 'provider')?.icon"
2023-11-24 06:49:25 +00:00
class="model-icon mr-8"
></span>
<span>{{ item.name }}</span>
</div>
<el-icon class="check-icon" v-if="item.id === applicationForm.model_id"
><Check
/></el-icon>
2023-11-24 02:40:53 +00:00
</el-option>
</el-option-group>
2023-12-05 11:21:13 +00:00
<template #footer>
2023-12-04 09:24:00 +00:00
<el-button type="primary" link @click="openCreateModel">
2023-11-24 06:49:25 +00:00
<el-icon class="mr-4"><Plus /></el-icon>
</el-button>
2023-12-05 11:21:13 +00:00
</template>
2023-11-24 02:40:53 +00:00
</el-select>
2023-11-22 09:04:47 +00:00
</el-form-item>
2023-11-24 06:49:25 +00:00
<el-form-item label="多轮对话" @click.prevent>
2023-12-05 11:21:13 +00:00
<el-switch
size="small"
v-model="applicationForm.multiple_rounds_dialogue"
></el-switch>
2023-11-22 09:04:47 +00:00
</el-form-item>
2023-12-18 03:32:29 +00:00
<el-form-item label="关联知识库">
2023-11-22 09:04:47 +00:00
<template #label>
<div class="flex-between">
2023-12-18 03:32:29 +00:00
<span>关联知识库</span>
2023-11-24 11:02:52 +00:00
<el-button type="primary" link @click="openDatasetDialog">
2023-11-22 09:04:47 +00:00
<el-icon class="mr-4"><Plus /></el-icon>
</el-button>
</div>
</template>
2023-11-27 03:21:56 +00:00
<div v-if="applicationForm.dataset_id_list.length == 0">
2023-12-18 03:32:29 +00:00
<el-text type="info">关联的知识库展示在这里</el-text>
2023-11-22 09:04:47 +00:00
</div>
2023-11-27 03:21:56 +00:00
<div class="w-full" v-else>
2023-11-22 09:04:47 +00:00
<el-row :gutter="12">
2023-11-27 03:21:56 +00:00
<el-col
:xs="24"
:sm="24"
:md="12"
:lg="12"
:xl="12"
class="mb-8"
v-for="(item, index) in applicationForm.dataset_id_list"
:key="index"
>
2023-11-22 10:37:08 +00:00
<el-card class="relate-dataset-card" shadow="never">
2023-11-22 09:04:47 +00:00
<div class="flex-between">
<div class="flex align-center">
<AppAvatar class="mr-12" shape="square" :size="32">
<img src="@/assets/icon_document.svg" style="width: 58%" alt="" />
</AppAvatar>
2023-11-27 03:21:56 +00:00
<div class="ellipsis-1">
{{ realatedObject(datasetList, item, 'id')?.name }}
</div>
2023-11-22 09:04:47 +00:00
</div>
2023-11-27 03:21:56 +00:00
<el-button text @click="removeDataset(item)">
2023-11-22 09:04:47 +00:00
<el-icon><Close /></el-icon>
</el-button>
</div>
</el-card>
</el-col>
</el-row>
2023-11-27 03:21:56 +00:00
</div>
2023-11-22 09:04:47 +00:00
</el-form-item>
<el-form-item label="开场白">
<el-input
v-model="applicationForm.prologue"
type="textarea"
placeholder="开始对话的欢迎语。您可以这样写:您好,我是 MaxKB 智能小助手,您可以向我提出 MaxKB 产品使用中遇到的任何问题。"
2023-11-24 11:02:52 +00:00
:rows="4"
2023-11-22 09:04:47 +00:00
/>
</el-form-item>
<el-form-item label="示例">
<template v-for="(item, index) in exampleList" :key="index">
<el-input
v-model="exampleList[index]"
:placeholder="`用户提问 示例${index + 1}`"
class="mb-8"
/>
</template>
</el-form-item>
</el-form>
</el-scrollbar>
</div>
<div class="text-right border-t p-16">
2023-11-29 03:25:14 +00:00
<el-button v-if="!id" @click="router.push({ path: `/application` })"> </el-button>
2023-11-28 10:53:22 +00:00
<el-button type="primary" @click="submit(applicationFormRef)" :disabled="loading">
2023-11-29 03:25:14 +00:00
{{ id ? '保存' : '创建' }}
2023-11-28 10:53:22 +00:00
</el-button>
2023-11-22 09:04:47 +00:00
</div>
</el-col>
2023-11-20 10:56:31 +00:00
<el-col :span="14" class="p-24 border-l">
2023-11-21 10:32:51 +00:00
<h4 class="title-decoration-1 mb-16">调试预览</h4>
<div class="dialog-bg">
2023-11-22 09:04:47 +00:00
<h4 class="p-24">{{ applicationForm?.name || '应用名称' }}</h4>
<div class="scrollbar-height">
2023-12-06 09:30:46 +00:00
<AiChat :data="applicationForm"></AiChat>
2023-11-21 10:32:51 +00:00
</div>
2023-11-21 07:22:10 +00:00
</div>
2023-11-20 10:56:31 +00:00
</el-col>
</el-row>
2023-11-30 08:35:52 +00:00
<AddDatasetDialog
ref="AddDatasetDialogRef"
@addData="addDataset"
:data="datasetList"
@refresh="refresh"
:loading="datasetLoading"
/>
2023-12-04 09:30:52 +00:00
<CreateModelDialog
ref="createModelRef"
@submit="getModel"
@change="openCreateModel($event)"
></CreateModelDialog>
<SelectProviderDialog ref="selectProviderRef" @change="openCreateModel($event)" />
2023-11-17 03:36:16 +00:00
</LayoutContainer>
2023-11-15 09:42:31 +00:00
</template>
<script setup lang="ts">
2023-11-23 06:30:28 +00:00
import { reactive, ref, watch, onMounted } from 'vue'
2023-11-24 06:49:25 +00:00
import { useRouter, useRoute } from 'vue-router'
2023-11-24 02:40:53 +00:00
import { groupBy } from 'lodash'
2023-11-24 11:02:52 +00:00
import AddDatasetDialog from './components/AddDatasetDialog.vue'
2023-12-04 09:30:52 +00:00
import CreateModelDialog from '@/views/template/component/CreateModelDialog.vue'
import SelectProviderDialog from '@/views/template/component/SelectProviderDialog.vue'
2023-11-28 10:53:22 +00:00
import applicationApi from '@/api/application'
2023-11-24 02:40:53 +00:00
import type { FormInstance, FormRules } from 'element-plus'
2023-11-23 06:30:28 +00:00
import type { ApplicationFormType } from '@/api/type/application'
2023-11-24 06:49:25 +00:00
import type { Provider } from '@/api/type/model'
2023-11-27 03:21:56 +00:00
import { realatedObject } from '@/utils/utils'
2023-11-28 10:53:22 +00:00
import { MsgSuccess } from '@/utils/message'
2023-11-23 06:30:28 +00:00
import useStore from '@/stores'
2023-12-07 11:28:38 +00:00
2023-11-29 07:42:48 +00:00
const { model, dataset, application, user } = useStore()
2023-11-22 09:04:47 +00:00
2023-11-24 06:49:25 +00:00
const router = useRouter()
const route = useRoute()
const {
2023-11-24 11:02:52 +00:00
params: { id }
2023-11-24 06:49:25 +00:00
} = route as any
2023-12-04 09:30:52 +00:00
const createModelRef = ref<InstanceType<typeof CreateModelDialog>>()
const selectProviderRef = ref<InstanceType<typeof SelectProviderDialog>>()
2023-12-04 09:24:00 +00:00
2023-11-22 09:04:47 +00:00
const applicationFormRef = ref<FormInstance>()
2023-11-24 11:02:52 +00:00
const AddDatasetDialogRef = ref()
2023-11-22 09:04:47 +00:00
const loading = ref(false)
2023-11-30 08:35:52 +00:00
const datasetLoading = ref(false)
2023-11-22 09:04:47 +00:00
const exampleList = ref(['', ''])
2023-11-29 07:42:48 +00:00
const applicationForm = ref<ApplicationFormType>({
2023-11-22 09:04:47 +00:00
name: '',
desc: '',
model_id: '',
multiple_rounds_dialogue: false,
prologue: '',
example: [],
dataset_id_list: []
})
const rules = reactive<FormRules<ApplicationFormType>>({
name: [{ required: true, message: '请输入应用名称', trigger: 'blur' }],
model_id: [
{
required: true,
message: '请选择模型',
trigger: 'change'
}
]
})
2023-11-24 06:49:25 +00:00
const modelOptions = ref<any>(null)
const providerOptions = ref<Array<Provider>>([])
2023-11-27 03:21:56 +00:00
const datasetList = ref([])
2023-11-22 10:37:08 +00:00
2023-12-05 11:21:13 +00:00
watch(
() => exampleList.value,
(val) => {
applicationForm.value.example = val.filter((v) => v)
},
{ deep: true }
)
2023-11-23 06:30:28 +00:00
2023-11-28 10:53:22 +00:00
const submit = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate((valid, fields) => {
if (valid) {
2023-11-29 07:42:48 +00:00
if (id) {
applicationApi.putApplication(id, applicationForm.value, loading).then((res) => {
MsgSuccess('保存成功')
})
} else {
applicationApi.postApplication(applicationForm.value, loading).then((res) => {
2023-11-28 10:53:22 +00:00
MsgSuccess('创建成功')
router.push({ path: `/application` })
})
2023-11-29 07:42:48 +00:00
}
2023-11-28 10:53:22 +00:00
} else {
console.log('error submit!')
}
})
2023-11-24 11:02:52 +00:00
}
2023-11-27 03:21:56 +00:00
2023-12-04 09:30:52 +00:00
const openCreateModel = (provider?: Provider) => {
if (provider && provider.provider) {
createModelRef.value?.open(provider)
} else {
selectProviderRef.value?.open()
}
2023-12-04 09:24:00 +00:00
}
2023-12-04 09:30:52 +00:00
2023-11-29 07:42:48 +00:00
function removeDataset(id: string) {
applicationForm.value.dataset_id_list.splice(applicationForm.value.dataset_id_list.indexOf(id), 1)
2023-11-27 03:21:56 +00:00
}
function addDataset(val: Array<string>) {
2023-11-29 07:42:48 +00:00
applicationForm.value.dataset_id_list = val
2023-11-27 03:21:56 +00:00
}
2023-11-24 11:02:52 +00:00
function openDatasetDialog() {
2023-11-29 07:42:48 +00:00
AddDatasetDialogRef.value.open(applicationForm.value.dataset_id_list)
2023-11-27 03:21:56 +00:00
}
2023-11-29 03:25:14 +00:00
function getDetail() {
2023-11-29 07:42:48 +00:00
application.asyncGetApplicationDetail(id, loading).then((res: any) => {
applicationForm.value = res.data
applicationForm.value.model_id = res.data.model
2023-12-01 10:21:49 +00:00
if (res.data?.example.length === 2) {
exampleList.value = res.data?.example
} else if (res.data?.example.length === 1) {
exampleList.value = [res.data?.example[0], '']
}
2023-11-29 07:42:48 +00:00
})
2023-11-29 03:25:14 +00:00
}
2023-11-27 03:21:56 +00:00
function getDataset() {
2023-11-29 07:42:48 +00:00
if (id) {
2023-12-07 11:28:38 +00:00
application.asyncGetApplicationDataset(id, datasetLoading).then((res: any) => {
2023-11-27 03:21:56 +00:00
datasetList.value = res.data
})
2023-11-29 07:42:48 +00:00
} else {
2023-12-07 11:28:38 +00:00
dataset.asyncGetAllDateset(datasetLoading).then((res: any) => {
datasetList.value = res.data?.filter((v: any) => v.user_id === user.userInfo?.id)
2023-11-30 08:35:52 +00:00
})
2023-11-29 07:42:48 +00:00
}
2023-11-24 11:02:52 +00:00
}
2023-11-23 06:30:28 +00:00
function getModel() {
loading.value = true
2023-11-23 09:20:19 +00:00
model
.asyncGetModel()
2023-11-24 06:49:25 +00:00
.then((res: any) => {
2023-11-24 02:40:53 +00:00
modelOptions.value = groupBy(res?.data, 'provider')
2023-11-23 09:20:19 +00:00
loading.value = false
})
.catch(() => {
loading.value = false
})
}
2023-11-24 02:40:53 +00:00
2023-11-23 09:20:19 +00:00
function getProvider() {
loading.value = true
model
.asyncGetProvider()
2023-11-24 06:49:25 +00:00
.then((res: any) => {
2023-11-24 02:40:53 +00:00
providerOptions.value = res?.data
2023-11-23 06:30:28 +00:00
loading.value = false
})
.catch(() => {
loading.value = false
})
}
2023-11-24 02:40:53 +00:00
2023-11-30 08:35:52 +00:00
function refresh() {
getDataset()
}
2023-11-23 06:30:28 +00:00
onMounted(() => {
2023-11-24 02:40:53 +00:00
getProvider()
getModel()
2023-11-27 03:21:56 +00:00
getDataset()
2023-11-29 07:42:48 +00:00
if (id) {
getDetail()
}
2023-11-23 06:30:28 +00:00
})
2023-11-15 09:42:31 +00:00
</script>
2023-11-21 07:22:10 +00:00
<style lang="scss" scoped>
2023-11-22 09:04:47 +00:00
.create-application {
2023-11-22 10:37:08 +00:00
.relate-dataset-card {
color: var(--app-text-color);
border-radius: 4px;
}
2023-11-22 09:04:47 +00:00
.dialog-bg {
border-radius: 8px;
background: var(--dialog-bg-gradient-color);
overflow: hidden;
box-sizing: border-box;
}
.scrollbar-height-left {
2023-11-24 06:49:25 +00:00
height: calc(var(--app-main-height) - 127px);
2023-11-22 09:04:47 +00:00
}
.scrollbar-height {
2023-11-21 10:32:51 +00:00
height: calc(var(--app-main-height) - 150px);
}
2023-11-21 07:22:10 +00:00
}
2023-11-24 02:40:53 +00:00
.model-icon {
2023-11-24 06:49:25 +00:00
width: 20px;
}
.check-icon {
position: absolute;
right: 10px;
}
2023-11-21 07:22:10 +00:00
</style>