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

479 lines
16 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
2024-01-15 07:24:16 +00:00
hide-required-asterisk
2023-11-22 09:04:47 +00:00
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
>
2024-01-15 07:24:16 +00:00
<el-form-item prop="name">
<template #label>
<div class="flex-between">
<span>应用名称 <span class="danger">*</span></span>
</div>
</template>
2023-11-22 09:04:47 +00:00
<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>
2024-01-15 07:24:16 +00:00
<el-form-item prop="model_id">
<template #label>
<div class="flex-between">
<span>AI 模型 <span class="danger">*</span></span>
<el-button type="primary" link>提示词</el-button>
</div>
</template>
2023-11-24 06:49:25 +00:00
<el-select
v-model="applicationForm.model_id"
2024-01-15 07:24:16 +00:00
placeholder="请选择 AI 模型"
2023-11-24 06:49:25 +00:00
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>
2024-01-15 07:24:16 +00:00
<el-popover :visible="popoverVisible" :width="300" trigger="click">
<template #reference>
<el-button type="primary" link @click="popoverVisible = !popoverVisible"
>参数设置</el-button
>
</template>
<div class="set-rules__form">
<div class="form-item mb-16">
<div class="title flex align-center mb-8">
<span style="margin-right: 4px">相似度</span>
<el-tooltip
effect="dark"
content="相似度越高相关性越强。"
placement="right"
>
<el-icon style="font-size: 16px">
<Warning />
</el-icon>
</el-tooltip>
</div>
<div @click.stop>
高于
<el-input-number
v-model="formInline.similarity"
:min="0"
:max="1"
:precision="3"
:step="0.1"
controls-position="right"
style="width: 100px"
/>
</div>
</div>
<div class="form-item mb-16">
<div class="title mb-8">引用分段数</div>
<div @click.stop>
TOP
<el-input-number
v-model="formInline.top_number"
:min="1"
:max="10"
controls-position="right"
style="width: 100px"
size="small"
/>
个分段
</div>
</div>
<div class="form-item mb-16">
<div class="title mb-8">最多引用字符数</div>
<div class="flex align-center">
<el-slider
v-model="formInline.top_number"
show-input
:show-input-controls="false"
:min="500"
:max="10000"
style="width: 220px"
size="small"
/>
<span class="ml-4">个字符</span>
</div>
</div>
</div>
<div class="text-right">
<el-button @click="popoverVisible = false" size="small">取消</el-button>
<el-button type="primary" @click="popoverVisible = false" size="small"
>确认</el-button
>
</div>
</el-popover>
2023-11-22 09:04:47 +00:00
</div>
</template>
2024-01-15 07:24:16 +00:00
<div class="w-full">
2023-11-22 09:04:47 +00:00
<el-row :gutter="12">
2024-01-15 07:24:16 +00:00
<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="mb-8">
<CardAdd
title="关联知识库"
@click="openDatasetDialog"
style="min-height: 50px; font-size: 14px"
/>
</el-col>
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>
2024-01-02 09:32:41 +00:00
<div class="ellipsis">
2023-11-27 03:21:56 +00:00
{{ 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="开场白">
2024-01-12 06:55:08 +00:00
<MdEditor
class="prologue-md-editor"
v-model="applicationForm.prologue"
:preview="false"
:toolbars="[]"
:footers="[]"
/>
<!-- <el-input
2023-11-22 09:04:47 +00:00
v-model="applicationForm.prologue"
type="textarea"
placeholder="开始对话的欢迎语。您可以这样写:您好,我是 MaxKB 智能小助手,您可以向我提出 MaxKB 产品使用中遇到的任何问题。"
2023-11-24 11:02:52 +00:00
:rows="4"
2024-01-12 06:55:08 +00:00
/> -->
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'
2024-01-12 06:55:08 +00:00
import { MdEditor } from 'md-editor-v3'
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,
2024-01-12 06:55:08 +00:00
prologue: `您好,我是 MaxKB 小助手,您可以向我提出 MaxKB 使用问题。
- MaxKB 主要功能有什么
- MaxKB 支持哪些大语言模型
- MaxKB 支持哪些文档类型`,
2023-11-22 09:04:47 +00:00
example: [],
dataset_id_list: []
})
2024-01-15 07:24:16 +00:00
const formInline = reactive({
similarity: 0.6,
top_number: 5
})
const popoverVisible = ref(false)
2023-11-22 09:04:47 +00:00
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;
}
2024-01-12 06:55:08 +00:00
.prologue-md-editor {
height: 150px;
}
2023-11-21 07:22:10 +00:00
</style>