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

532 lines
18 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>
2024-01-17 08:29:37 +00:00
<el-button type="primary" link @click="promptChange('open')"></el-button>
2024-01-15 07:24:16 +00:00
</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>
2024-01-17 08:29:37 +00:00
<div class="dataset_setting">
<div class="form-item mb-16 p-8">
2024-01-15 07:24:16 +00:00
<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
2024-01-17 08:29:37 +00:00
v-model="applicationForm.dataset_setting.similarity"
2024-01-15 07:24:16 +00:00
:min="0"
:max="1"
:precision="3"
:step="0.1"
controls-position="right"
style="width: 100px"
2024-01-17 08:29:37 +00:00
size="small"
2024-01-15 07:24:16 +00:00
/>
</div>
</div>
2024-01-17 08:29:37 +00:00
<div class="form-item mb-16 p-8">
2024-01-15 07:24:16 +00:00
<div class="title mb-8">引用分段数</div>
<div @click.stop>
TOP
<el-input-number
2024-01-17 08:29:37 +00:00
v-model="applicationForm.dataset_setting.top_n"
2024-01-15 07:24:16 +00:00
:min="1"
:max="10"
controls-position="right"
style="width: 100px"
size="small"
/>
个分段
</div>
</div>
2024-01-17 08:29:37 +00:00
<div class="form-item mb-16 p-8">
2024-01-15 07:24:16 +00:00
<div class="title mb-8">最多引用字符数</div>
<div class="flex align-center">
<el-slider
2024-01-17 08:29:37 +00:00
v-model="applicationForm.dataset_setting.max_paragraph_char_number"
2024-01-15 07:24:16 +00:00
show-input
:show-input-controls="false"
:min="500"
:max="10000"
2024-01-17 08:29:37 +00:00
style="width: 200px"
2024-01-15 07:24:16 +00:00
size="small"
/>
<span class="ml-4">个字符</span>
</div>
</div>
</div>
<div class="text-right">
<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="[]"
/>
2023-11-22 09:04:47 +00:00
</el-form-item>
2024-01-17 09:07:52 +00:00
<el-form-item @click.prevent>
<template #label>
<div class="flex align-center">
<span>问题优化</span>
2024-01-18 07:59:30 +00:00
<el-tooltip
effect="dark"
content="根据历史聊天优化完善当前问题,更利于匹配知识点。"
placement="right"
>
2024-01-17 09:07:52 +00:00
<el-icon style="font-size: 16px">
<Warning />
</el-icon>
</el-tooltip>
</div>
</template>
<el-switch size="small" v-model="applicationForm.problem_optimization"></el-switch>
</el-form-item>
2023-11-22 09:04:47 +00:00
</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>
2024-01-17 08:29:37 +00:00
<el-dialog v-model="dialogFormVisible" title="提示词">
<el-alert type="info" show-icon class="mb-16" :closable="false">
<p>通过调整提示词内容可以引导大模型聊天方向该提示词会被固定在上下文的开头</p>
<p>可以使用变量{data} 是携带知识库中已知信息{question}是用户提出的问题</p>
</el-alert>
2024-01-18 07:59:30 +00:00
<el-input
v-model="model_setting_prompt"
:rows="13"
type="textarea"
:placeholder="defaultPrompt"
/>
2024-01-17 08:29:37 +00:00
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="promptChange('close')"> </el-button>
</span>
</template>
</el-dialog>
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'
2024-01-17 08:29:37 +00:00
import { groupBy, cloneDeep } 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
2024-01-17 08:29:37 +00:00
const defaultPrompt = `已知信息:
{data}
回答要求
- 请简洁和专业的来回答用户的问题
- 如果你不知道答案请回答没有在知识库中查找到相关信息建议咨询相关技术支持或参考官方文档进行操作
- 避免提及你是从已知信息中获得的知识
- 请保证答案与已知信息中描述的一致
- 请使用 Markdown 语法优化答案的格式
- 已知信息中的图片链接地址和脚本语言请直接返回
- 请使用与问题相同的语言来回答
问题
{question}
`
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-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 支持哪些文档类型`,
2024-01-17 08:29:37 +00:00
dataset_id_list: [],
dataset_setting: {
top_n: 3,
similarity: 0.6,
max_paragraph_char_number: 5000
},
model_setting: {
prompt: defaultPrompt
},
2024-01-18 07:59:30 +00:00
problem_optimization: false
2023-11-22 09:04:47 +00:00
})
2024-01-15 07:24:16 +00:00
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([])
2024-01-17 08:29:37 +00:00
const dialogFormVisible = ref(false)
2023-11-22 10:37:08 +00:00
2024-01-17 08:29:37 +00:00
const model_setting_prompt = ref('')
function promptChange(val: string) {
if (val === 'open') {
dialogFormVisible.value = true
model_setting_prompt.value = applicationForm.value.model_setting.prompt
} else if (val === 'close') {
dialogFormVisible.value = false
applicationForm.value.model_setting.prompt = model_setting_prompt.value
}
}
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-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;
}
2024-01-17 08:29:37 +00:00
.dataset_setting {
.form-item {
background: var(--app-layout-bg-color);
}
}
2023-11-21 07:22:10 +00:00
</style>