UnisKB/ui/src/views/document/component/ImportDocumentDialog.vue

199 lines
6.0 KiB
Vue
Raw Normal View History

2024-01-11 06:40:00 +00:00
<template>
<el-dialog
2024-01-19 08:03:17 +00:00
title="导入文档"
2024-01-11 06:40:00 +00:00
v-model="dialogVisible"
:close-on-click-modal="false"
:close-on-press-escape="false"
:destroy-on-close="true"
>
2024-01-19 08:03:17 +00:00
<el-form
label-position="top"
ref="webFormRef"
:rules="rules"
:model="form"
require-asterisk-position="right"
>
<el-form-item label="文档地址" prop="source_url" v-if="isImport">
<el-input
v-model="form.source_url"
placeholder="请输入文档地址,一行一个,地址不正确文档会导入失败。"
:rows="10"
type="textarea"
/>
</el-form-item>
<el-form-item v-else-if="documentType === '1'" label="文档地址" prop="source_url">
2024-01-19 08:03:17 +00:00
<el-input v-model="form.source_url" placeholder="请输入文档地址" />
</el-form-item>
<el-form-item label="选择器" v-if="documentType === '1'">
2024-01-19 08:03:17 +00:00
<el-input
v-model="form.selector"
placeholder="默认为 body可输入 .classname/#idname/tagname"
/>
</el-form-item>
<el-form-item v-if="!isImport">
<template #label>
<div class="flex align-center">
<span class="mr-4">命中处理方式</span>
<el-tooltip
effect="dark"
content="用户提问时,命中文档下的分段时按照设置的方式进行处理。"
placement="right"
>
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
</div>
</template>
<el-radio-group v-model="form.hit_handling_method" class="radio-block mt-4">
<template v-for="(value, key) of hitHandlingMethod" :key="key">
<el-radio :value="key">{{ value }} </el-radio>
</template>
</el-radio-group>
<div
v-if="form.hit_handling_method === 'directly_return'"
class="lighter"
style="margin-left: 21px"
>
<span>相似度高于</span>
<el-input-number
v-model="form.directly_return_similarity"
:min="0"
:max="1"
:precision="3"
:step="0.1"
controls-position="right"
size="small"
class="ml-4 mr-4"
/><span></span>
</div>
</el-form-item>
2024-01-19 08:03:17 +00:00
</el-form>
2024-01-11 06:40:00 +00:00
<template #footer>
<span class="dialog-footer">
<el-button @click.prevent="dialogVisible = false"> 取消 </el-button>
2024-03-04 06:16:25 +00:00
<el-button type="primary" @click="submit(webFormRef)" :loading="loading"> 确定 </el-button>
2024-01-11 06:40:00 +00:00
</span>
</template>
</el-dialog>
</template>
<script setup lang="ts">
2024-01-19 08:03:17 +00:00
import { ref, reactive, watch } from 'vue'
import { useRoute } from 'vue-router'
2024-03-04 06:16:25 +00:00
import type { FormInstance, FormRules } from 'element-plus'
2024-01-19 08:03:17 +00:00
import documentApi from '@/api/document'
2024-01-11 06:40:00 +00:00
import { MsgSuccess } from '@/utils/message'
import { hitHandlingMethod } from '../utils'
2024-01-11 06:40:00 +00:00
2024-01-19 08:03:17 +00:00
const route = useRoute()
const {
params: { id }
} = route as any
2024-01-11 06:40:00 +00:00
const emit = defineEmits(['refresh'])
2024-03-04 06:16:25 +00:00
const webFormRef = ref()
2024-01-11 06:40:00 +00:00
const loading = ref<boolean>(false)
2024-01-19 08:03:17 +00:00
const isImport = ref<boolean>(false)
const form = ref<any>({
source_url: '',
selector: '',
hit_handling_method: 'optimization',
directly_return_similarity: 0.9
2024-01-19 08:03:17 +00:00
})
// 文档设置
2024-01-19 09:51:27 +00:00
const documentId = ref('')
const documentType = ref<string | number>('') //文档类型1: web文档0:普通文档
2024-01-19 08:03:17 +00:00
// 批量设置
const documentList = ref<Array<string>>([])
2024-01-19 08:03:17 +00:00
const rules = reactive({
2024-01-24 02:38:48 +00:00
source_url: [{ required: true, message: '请输入文档地址', trigger: 'blur' }]
2024-01-19 08:03:17 +00:00
})
2024-01-11 06:40:00 +00:00
const dialogVisible = ref<boolean>(false)
watch(dialogVisible, (bool) => {
if (!bool) {
2024-01-19 08:03:17 +00:00
form.value = {
source_url: '',
selector: '',
hit_handling_method: 'optimization',
directly_return_similarity: 0.9
2024-01-19 08:03:17 +00:00
}
isImport.value = false
documentType.value = ''
documentList.value = []
2024-01-11 06:40:00 +00:00
}
})
const open = (row: any, list: Array<string>) => {
2024-01-19 08:03:17 +00:00
if (row) {
documentType.value = row.type
2024-01-19 09:51:27 +00:00
documentId.value = row.id
form.value = {
hit_handling_method: row.hit_handling_method,
directly_return_similarity: row.directly_return_similarity,
...row.meta
}
2024-01-19 08:03:17 +00:00
isImport.value = false
} else if (list) {
// 批量设置
documentList.value = list
2024-01-19 08:03:17 +00:00
} else {
// 导入
2024-01-19 08:03:17 +00:00
isImport.value = true
}
2024-01-11 06:40:00 +00:00
dialogVisible.value = true
}
2024-03-04 06:16:25 +00:00
const submit = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate((valid, fields) => {
if (valid) {
if (isImport.value) {
const obj = {
source_url_list: form.value.source_url.split('\n'),
selector: form.value.selector
}
documentApi.postWebDocument(id, obj, loading).then((res: any) => {
MsgSuccess('导入成功')
emit('refresh')
dialogVisible.value = false
})
} else {
if (documentId.value) {
const obj = {
hit_handling_method: form.value.hit_handling_method,
directly_return_similarity: form.value.directly_return_similarity,
meta: {
source_url: form.value.source_url,
selector: form.value.selector
}
}
documentApi.putDocument(id, documentId.value, obj, loading).then((res) => {
MsgSuccess('设置成功')
emit('refresh')
dialogVisible.value = false
})
} else if (documentList.value.length > 0) {
// 批量设置
const obj = {
hit_handling_method: form.value.hit_handling_method,
directly_return_similarity: form.value.directly_return_similarity,
id_list: documentList.value
}
documentApi.batchEditHitHandling(id, obj, loading).then((res: any) => {
MsgSuccess('设置成功')
emit('refresh')
dialogVisible.value = false
})
2024-03-04 06:16:25 +00:00
}
}
2024-01-19 09:51:27 +00:00
}
2024-03-04 06:16:25 +00:00
})
2024-01-11 06:40:00 +00:00
}
defineExpose({ open })
</script>
2024-01-19 08:03:17 +00:00
<style lang="scss" scoped></style>