feat:【ele】【ai】知识库的 knowledge 初始化
This commit is contained in:
180
apps/web-ele/src/views/ai/knowledge/document/data.ts
Normal file
180
apps/web-ele/src/views/ai/knowledge/document/data.ts
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
import type { VbenFormSchema } from '#/adapter/form';
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
import type { AiKnowledgeDocumentApi } from '#/api/ai/knowledge/document';
|
||||||
|
|
||||||
|
import { AiModelTypeEnum, CommonStatusEnum, DICT_TYPE } from '@vben/constants';
|
||||||
|
import { getDictOptions } from '@vben/hooks';
|
||||||
|
|
||||||
|
import { z } from '#/adapter/form';
|
||||||
|
import { getModelSimpleList } from '#/api/ai/model/model';
|
||||||
|
|
||||||
|
/** 新增/修改的表单 */
|
||||||
|
export function useFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
component: 'Input',
|
||||||
|
fieldName: 'id',
|
||||||
|
dependencies: {
|
||||||
|
triggerFields: [''],
|
||||||
|
show: () => false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
component: 'Input',
|
||||||
|
fieldName: 'name',
|
||||||
|
label: '知识库名称',
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'description',
|
||||||
|
label: '知识库描述',
|
||||||
|
component: 'Textarea',
|
||||||
|
componentProps: {
|
||||||
|
rows: 3,
|
||||||
|
placeholder: '请输入知识库描述',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
component: 'ApiSelect',
|
||||||
|
fieldName: 'embeddingModelId',
|
||||||
|
label: '向量模型',
|
||||||
|
componentProps: {
|
||||||
|
api: () => getModelSimpleList(AiModelTypeEnum.EMBEDDING),
|
||||||
|
labelField: 'name',
|
||||||
|
valueField: 'id',
|
||||||
|
allowClear: true,
|
||||||
|
placeholder: '请选择向量模型',
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'topK',
|
||||||
|
label: '检索 topK',
|
||||||
|
component: 'InputNumber',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入检索 topK',
|
||||||
|
controlsPosition: 'right',
|
||||||
|
class: '!w-full',
|
||||||
|
min: 0,
|
||||||
|
max: 10,
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'similarityThreshold',
|
||||||
|
label: '检索相似度阈值',
|
||||||
|
component: 'InputNumber',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入检索相似度阈值',
|
||||||
|
controlsPosition: 'right',
|
||||||
|
class: '!w-full',
|
||||||
|
min: 0,
|
||||||
|
max: 1,
|
||||||
|
step: 0.01,
|
||||||
|
precision: 2,
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'status',
|
||||||
|
label: '是否启用',
|
||||||
|
component: 'RadioGroup',
|
||||||
|
componentProps: {
|
||||||
|
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
|
||||||
|
},
|
||||||
|
rules: z.number().default(CommonStatusEnum.ENABLE),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 列表的搜索表单 */
|
||||||
|
export function useGridFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
fieldName: 'name',
|
||||||
|
label: '文件名称',
|
||||||
|
component: 'Input',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入文件名称',
|
||||||
|
allowClear: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'status',
|
||||||
|
label: '是否启用',
|
||||||
|
component: 'Select',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请选择是否启用',
|
||||||
|
allowClear: true,
|
||||||
|
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 列表的字段 */
|
||||||
|
export function useGridColumns(
|
||||||
|
onStatusChange?: (
|
||||||
|
newStatus: number,
|
||||||
|
row: AiKnowledgeDocumentApi.KnowledgeDocument,
|
||||||
|
) => PromiseLike<boolean | undefined>,
|
||||||
|
): VxeTableGridOptions['columns'] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
field: 'id',
|
||||||
|
title: '文档编号',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'name',
|
||||||
|
title: '文件名称',
|
||||||
|
minWidth: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'contentLength',
|
||||||
|
title: '字符数',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'tokens',
|
||||||
|
title: 'Token 数',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'segmentMaxTokens',
|
||||||
|
title: '分片最大 Token 数',
|
||||||
|
minWidth: 150,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'retrievalCount',
|
||||||
|
title: '召回次数',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'status',
|
||||||
|
title: '是否启用',
|
||||||
|
minWidth: 100,
|
||||||
|
align: 'center',
|
||||||
|
cellRender: {
|
||||||
|
attrs: { beforeChange: onStatusChange },
|
||||||
|
name: 'CellSwitch',
|
||||||
|
props: {
|
||||||
|
checkedValue: CommonStatusEnum.ENABLE,
|
||||||
|
unCheckedValue: CommonStatusEnum.DISABLE,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'createTime',
|
||||||
|
title: '上传时间',
|
||||||
|
minWidth: 180,
|
||||||
|
formatter: 'formatDateTime',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: '操作',
|
||||||
|
minWidth: 150,
|
||||||
|
fixed: 'right',
|
||||||
|
slots: { default: 'actions' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
200
apps/web-ele/src/views/ai/knowledge/document/form/index.vue
Normal file
200
apps/web-ele/src/views/ai/knowledge/document/form/index.vue
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import {
|
||||||
|
getCurrentInstance,
|
||||||
|
onBeforeUnmount,
|
||||||
|
onMounted,
|
||||||
|
provide,
|
||||||
|
ref,
|
||||||
|
} from 'vue';
|
||||||
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
|
import { Page } from '@vben/common-ui';
|
||||||
|
import { useTabs } from '@vben/hooks';
|
||||||
|
import { IconifyIcon } from '@vben/icons';
|
||||||
|
|
||||||
|
import { ElCard } from 'element-plus';
|
||||||
|
|
||||||
|
import { getKnowledgeDocument } from '#/api/ai/knowledge/document';
|
||||||
|
|
||||||
|
import ProcessStep from './modules/process-step.vue';
|
||||||
|
import SplitStep from './modules/split-step.vue';
|
||||||
|
import UploadStep from './modules/upload-step.vue';
|
||||||
|
|
||||||
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const uploadDocumentRef = ref();
|
||||||
|
const documentSegmentRef = ref();
|
||||||
|
const processCompleteRef = ref();
|
||||||
|
const currentStep = ref(0); // 步骤控制
|
||||||
|
const steps = [
|
||||||
|
{ title: '上传文档' },
|
||||||
|
{ title: '文档分段' },
|
||||||
|
{ title: '处理并完成' },
|
||||||
|
];
|
||||||
|
const formData = ref({
|
||||||
|
knowledgeId: undefined, // 知识库编号
|
||||||
|
id: undefined, // 编辑的文档编号(documentId)
|
||||||
|
segmentMaxTokens: 500, // 分段最大 token 数
|
||||||
|
list: [] as Array<{
|
||||||
|
count?: number; // 段落数量
|
||||||
|
id: number; // 文档编号
|
||||||
|
name: string; // 文档名称
|
||||||
|
process?: number; // 处理进度
|
||||||
|
segments: Array<{
|
||||||
|
content?: string;
|
||||||
|
contentLength?: number;
|
||||||
|
tokens?: number;
|
||||||
|
}>;
|
||||||
|
url: string; // 文档 URL
|
||||||
|
}>, // 用于存储上传的文件列表
|
||||||
|
}); // 表单数据
|
||||||
|
|
||||||
|
provide('parent', getCurrentInstance()); // 提供 parent 给子组件使用
|
||||||
|
|
||||||
|
const tabs = useTabs();
|
||||||
|
|
||||||
|
/** 返回列表页 */
|
||||||
|
function handleBack() {
|
||||||
|
// 关闭当前页签
|
||||||
|
tabs.closeCurrentTab();
|
||||||
|
// 跳转到列表页
|
||||||
|
router.push({
|
||||||
|
name: 'AiKnowledgeDocument',
|
||||||
|
query: {
|
||||||
|
knowledgeId: route.query.knowledgeId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 初始化数据 */
|
||||||
|
async function initData() {
|
||||||
|
if (route.query.knowledgeId) {
|
||||||
|
formData.value.knowledgeId = route.query.knowledgeId as any;
|
||||||
|
}
|
||||||
|
// 【修改场景】从路由参数中获取文档 ID
|
||||||
|
const documentId = route.query.id;
|
||||||
|
if (documentId) {
|
||||||
|
// 获取文档信息
|
||||||
|
formData.value.id = documentId as any;
|
||||||
|
const document = await getKnowledgeDocument(documentId as any);
|
||||||
|
formData.value.segmentMaxTokens = document.segmentMaxTokens;
|
||||||
|
formData.value.list = [
|
||||||
|
{
|
||||||
|
id: document.id,
|
||||||
|
name: document.name,
|
||||||
|
url: document.url,
|
||||||
|
segments: [],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
// 进入下一步
|
||||||
|
goToNextStep();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 切换到下一步 */
|
||||||
|
function goToNextStep() {
|
||||||
|
if (currentStep.value < steps.length - 1) {
|
||||||
|
currentStep.value++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 切换到上一步 */
|
||||||
|
function goToPrevStep() {
|
||||||
|
if (currentStep.value > 0) {
|
||||||
|
currentStep.value--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 添加组件卸载前的清理代码 */
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
// 清理所有的引用
|
||||||
|
uploadDocumentRef.value = null;
|
||||||
|
documentSegmentRef.value = null;
|
||||||
|
processCompleteRef.value = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 暴露方法给子组件使用 */
|
||||||
|
defineExpose({
|
||||||
|
goToNextStep,
|
||||||
|
goToPrevStep,
|
||||||
|
handleBack,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(async () => {
|
||||||
|
await initData();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Page auto-content-height>
|
||||||
|
<div class="mx-auto">
|
||||||
|
<!-- 头部导航栏 -->
|
||||||
|
<div
|
||||||
|
class="bg-card absolute left-0 right-0 top-0 z-10 flex h-12 items-center border-b px-4"
|
||||||
|
>
|
||||||
|
<!-- 左侧标题 -->
|
||||||
|
<div class="flex w-48 items-center overflow-hidden">
|
||||||
|
<IconifyIcon
|
||||||
|
icon="lucide:arrow-left"
|
||||||
|
class="size-5 flex-shrink-0 cursor-pointer"
|
||||||
|
@click="handleBack"
|
||||||
|
/>
|
||||||
|
<span class="ml-2.5 truncate text-base">
|
||||||
|
{{ formData.id ? '编辑知识库文档' : '创建知识库文档' }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 步骤条 -->
|
||||||
|
<div class="flex h-full flex-1 items-center justify-center">
|
||||||
|
<div class="flex h-full w-96 items-center justify-between">
|
||||||
|
<div
|
||||||
|
v-for="(step, index) in steps"
|
||||||
|
:key="index"
|
||||||
|
class="relative mx-4 flex h-full cursor-pointer items-center"
|
||||||
|
:class="[
|
||||||
|
currentStep === index
|
||||||
|
? 'border-b-2 border-solid border-blue-500 text-blue-500'
|
||||||
|
: 'text-gray-500',
|
||||||
|
]"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="mr-2 flex h-7 w-7 items-center justify-center rounded-full border-2 border-solid text-base"
|
||||||
|
:class="[
|
||||||
|
currentStep === index
|
||||||
|
? 'border-blue-500 bg-blue-500 text-white'
|
||||||
|
: 'border-gray-300 bg-white text-gray-500',
|
||||||
|
]"
|
||||||
|
>
|
||||||
|
{{ index + 1 }}
|
||||||
|
</div>
|
||||||
|
<span class="whitespace-nowrap text-base font-bold">
|
||||||
|
{{ step.title }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 主体内容 -->
|
||||||
|
<ElCard :body-style="{ padding: '10px' }" class="mb-4">
|
||||||
|
<div class="mt-12">
|
||||||
|
<!-- 第一步:上传文档 -->
|
||||||
|
<div v-if="currentStep === 0" class="mx-auto w-[560px]">
|
||||||
|
<UploadStep v-model="formData" ref="uploadDocumentRef" />
|
||||||
|
</div>
|
||||||
|
<!-- 第二步:文档分段 -->
|
||||||
|
<div v-if="currentStep === 1" class="mx-auto w-[560px]">
|
||||||
|
<SplitStep v-model="formData" ref="documentSegmentRef" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 第三步:处理并完成 -->
|
||||||
|
<div v-if="currentStep === 2" class="mx-auto w-[560px]">
|
||||||
|
<ProcessStep v-model="formData" ref="processCompleteRef" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</ElCard>
|
||||||
|
</div>
|
||||||
|
</Page>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, inject, onBeforeUnmount, onMounted, ref } from 'vue';
|
||||||
|
|
||||||
|
import { IconifyIcon } from '@vben/icons';
|
||||||
|
|
||||||
|
import { ElButton, ElProgress } from 'element-plus';
|
||||||
|
|
||||||
|
import { getKnowledgeSegmentProcessList } from '#/api/ai/knowledge/segment';
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
modelValue: {
|
||||||
|
type: Object,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const emit = defineEmits(['update:modelValue']);
|
||||||
|
const parent = inject('parent') as any;
|
||||||
|
const pollingTimer = ref<null | number>(null); // 轮询定时器 ID,用于跟踪和清除轮询进程
|
||||||
|
|
||||||
|
/** 判断文件处理是否完成 */
|
||||||
|
function isProcessComplete(file: any) {
|
||||||
|
return file.progress === 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 判断所有文件是否都处理完成 */
|
||||||
|
const allProcessComplete = computed(() => {
|
||||||
|
return props.modelValue.list.every((file: any) => isProcessComplete(file));
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 完成按钮点击事件处理 */
|
||||||
|
function handleComplete() {
|
||||||
|
if (parent?.exposed?.handleBack) {
|
||||||
|
parent.exposed.handleBack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 获取文件处理进度 */
|
||||||
|
async function getProcessList() {
|
||||||
|
try {
|
||||||
|
// 1. 调用 API 获取处理进度
|
||||||
|
const documentIds = props.modelValue.list
|
||||||
|
.filter((item: any) => item.id)
|
||||||
|
.map((item: any) => item.id);
|
||||||
|
if (documentIds.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await getKnowledgeSegmentProcessList(documentIds);
|
||||||
|
|
||||||
|
// 2.1更新进度
|
||||||
|
const updatedList = props.modelValue.list.map((file: any) => {
|
||||||
|
const processInfo = result.find(
|
||||||
|
(item: any) => item.documentId === file.id,
|
||||||
|
);
|
||||||
|
if (processInfo) {
|
||||||
|
// 计算进度百分比:已嵌入数量 / 总数量 * 100
|
||||||
|
const progress =
|
||||||
|
processInfo.embeddingCount && processInfo.count
|
||||||
|
? Math.floor((processInfo.embeddingCount / processInfo.count) * 100)
|
||||||
|
: 0;
|
||||||
|
return {
|
||||||
|
...file,
|
||||||
|
progress,
|
||||||
|
count: processInfo.count || 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2.2 更新数据
|
||||||
|
emit('update:modelValue', {
|
||||||
|
...props.modelValue,
|
||||||
|
list: updatedList,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3. 如果未完成,继续轮询
|
||||||
|
if (!updatedList.every((file: any) => isProcessComplete(file))) {
|
||||||
|
pollingTimer.value = window.setTimeout(getProcessList, 3000);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// 出错后也继续轮询
|
||||||
|
console.error('获取处理进度失败:', error);
|
||||||
|
pollingTimer.value = window.setTimeout(getProcessList, 5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 组件挂载时开始轮询 */
|
||||||
|
onMounted(() => {
|
||||||
|
// 1. 初始化进度为 0
|
||||||
|
const initialList = props.modelValue.list.map((file: any) => ({
|
||||||
|
...file,
|
||||||
|
progress: 0,
|
||||||
|
}));
|
||||||
|
|
||||||
|
emit('update:modelValue', {
|
||||||
|
...props.modelValue,
|
||||||
|
list: initialList,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. 开始轮询获取进度
|
||||||
|
getProcessList();
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 组件卸载前清除轮询 */
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
// 1. 清除定时器
|
||||||
|
if (pollingTimer.value) {
|
||||||
|
clearTimeout(pollingTimer.value);
|
||||||
|
pollingTimer.value = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<!-- 文件处理列表 -->
|
||||||
|
<div class="mt-4 grid grid-cols-1 gap-2">
|
||||||
|
<div
|
||||||
|
v-for="(file, index) in modelValue.list"
|
||||||
|
:key="index"
|
||||||
|
class="flex items-center rounded-sm border-l-4 border-l-blue-500 px-3 py-1 shadow-sm transition-all duration-300 hover:bg-blue-50"
|
||||||
|
>
|
||||||
|
<!-- 文件图标和名称 -->
|
||||||
|
<div class="mr-2 flex min-w-48 items-center">
|
||||||
|
<IconifyIcon icon="lucide:file-text" class="mr-2 text-blue-500" />
|
||||||
|
<span class="break-all text-sm text-gray-600">
|
||||||
|
{{ file.name }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 处理进度 -->
|
||||||
|
<div class="flex-1">
|
||||||
|
<ElProgress
|
||||||
|
:percentage="file.progress || 0"
|
||||||
|
:stroke-width="10"
|
||||||
|
:status="isProcessComplete(file) ? 'success' : undefined"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 分段数量 -->
|
||||||
|
<div class="ml-2 text-sm text-gray-400">
|
||||||
|
分段数量:{{ file.count ? file.count : '-' }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 底部完成按钮 -->
|
||||||
|
<div class="mt-5 flex justify-end">
|
||||||
|
<ElButton
|
||||||
|
:type="allProcessComplete ? 'primary' : 'default'"
|
||||||
|
:disabled="!allProcessComplete"
|
||||||
|
@click="handleComplete"
|
||||||
|
>
|
||||||
|
完成
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
@@ -0,0 +1,286 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import type { PropType } from 'vue';
|
||||||
|
|
||||||
|
import { computed, getCurrentInstance, inject, onMounted, ref } from 'vue';
|
||||||
|
|
||||||
|
import { IconifyIcon } from '@vben/icons';
|
||||||
|
|
||||||
|
import {
|
||||||
|
ElButton,
|
||||||
|
ElDropdown,
|
||||||
|
ElDropdownItem,
|
||||||
|
ElDropdownMenu,
|
||||||
|
ElEmpty,
|
||||||
|
ElForm,
|
||||||
|
ElFormItem,
|
||||||
|
ElInputNumber,
|
||||||
|
ElMessage,
|
||||||
|
ElTooltip,
|
||||||
|
} from 'element-plus';
|
||||||
|
|
||||||
|
import {
|
||||||
|
createKnowledgeDocumentList,
|
||||||
|
updateKnowledgeDocument,
|
||||||
|
} from '#/api/ai/knowledge/document';
|
||||||
|
import { splitContent } from '#/api/ai/knowledge/segment';
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
modelValue: {
|
||||||
|
type: Object as PropType<any>,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const emit = defineEmits(['update:modelValue']);
|
||||||
|
const parent = inject('parent', null); // 获取父组件实例
|
||||||
|
|
||||||
|
const modelData = computed({
|
||||||
|
get: () => props.modelValue,
|
||||||
|
set: (val) => emit('update:modelValue', val),
|
||||||
|
}); // 表单数据
|
||||||
|
|
||||||
|
const splitLoading = ref(false); // 分段加载状态
|
||||||
|
const currentFile = ref<any>(null); // 当前选中的文件
|
||||||
|
const submitLoading = ref(false); // 提交按钮加载状态
|
||||||
|
|
||||||
|
/** 选择文件 */
|
||||||
|
async function selectFile(index: number) {
|
||||||
|
currentFile.value = modelData.value.list[index];
|
||||||
|
await splitContentFile(currentFile.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 获取文件分段内容 */
|
||||||
|
async function splitContentFile(file: any) {
|
||||||
|
if (!file || !file.url) {
|
||||||
|
ElMessage.warning('文件 URL 不存在');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
splitLoading.value = true;
|
||||||
|
try {
|
||||||
|
// 调用后端分段接口,获取文档的分段内容、字符数和 Token 数
|
||||||
|
file.segments = await splitContent(
|
||||||
|
file.url,
|
||||||
|
modelData.value.segmentMaxTokens,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取分段内容失败:', file, error);
|
||||||
|
} finally {
|
||||||
|
splitLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 处理预览分段 */
|
||||||
|
async function handleAutoSegment() {
|
||||||
|
// 如果没有选中文件,默认选中第一个
|
||||||
|
if (
|
||||||
|
!currentFile.value &&
|
||||||
|
modelData.value.list &&
|
||||||
|
modelData.value.list.length > 0
|
||||||
|
) {
|
||||||
|
currentFile.value = modelData.value.list[0];
|
||||||
|
}
|
||||||
|
// 如果没有选中文件,提示请先选择文件
|
||||||
|
if (!currentFile.value) {
|
||||||
|
ElMessage.warning('请先选择文件');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取分段内容
|
||||||
|
await splitContentFile(currentFile.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 上一步按钮处理 */
|
||||||
|
function handlePrevStep() {
|
||||||
|
const parentEl = parent || getCurrentInstance()?.parent;
|
||||||
|
if (parentEl && typeof parentEl.exposed?.goToPrevStep === 'function') {
|
||||||
|
parentEl.exposed.goToPrevStep();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 保存操作 */
|
||||||
|
async function handleSave() {
|
||||||
|
// 保存前验证
|
||||||
|
if (
|
||||||
|
!currentFile?.value?.segments ||
|
||||||
|
currentFile.value.segments.length === 0
|
||||||
|
) {
|
||||||
|
ElMessage.warning('请先预览分段内容');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置按钮加载状态
|
||||||
|
submitLoading.value = true;
|
||||||
|
try {
|
||||||
|
if (modelData.value.id) {
|
||||||
|
// 修改场景
|
||||||
|
await updateKnowledgeDocument({
|
||||||
|
id: modelData.value.id,
|
||||||
|
segmentMaxTokens: modelData.value.segmentMaxTokens,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 新增场景
|
||||||
|
const data = await createKnowledgeDocumentList({
|
||||||
|
knowledgeId: modelData.value.knowledgeId,
|
||||||
|
segmentMaxTokens: modelData.value.segmentMaxTokens,
|
||||||
|
list: modelData.value.list.map((item: any) => ({
|
||||||
|
name: item.name,
|
||||||
|
url: item.url,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
modelData.value.list.forEach((document: any, index: number) => {
|
||||||
|
document.id = data[index];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 进入下一步
|
||||||
|
const parentEl = parent || getCurrentInstance()?.parent;
|
||||||
|
if (parentEl && typeof parentEl.exposed?.goToNextStep === 'function') {
|
||||||
|
parentEl.exposed.goToNextStep();
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('保存失败:', modelData.value, error);
|
||||||
|
} finally {
|
||||||
|
// 关闭按钮加载状态
|
||||||
|
submitLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(async () => {
|
||||||
|
// 确保 segmentMaxTokens 存在
|
||||||
|
if (!modelData.value.segmentMaxTokens) {
|
||||||
|
modelData.value.segmentMaxTokens = 500;
|
||||||
|
}
|
||||||
|
// 如果没有选中文件,默认选中第一个
|
||||||
|
if (
|
||||||
|
!currentFile.value &&
|
||||||
|
modelData.value.list &&
|
||||||
|
modelData.value.list.length > 0
|
||||||
|
) {
|
||||||
|
currentFile.value = modelData.value.list[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有选中的文件,获取分段内容
|
||||||
|
if (currentFile.value) {
|
||||||
|
await splitContentFile(currentFile.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<!-- 上部分段设置部分 -->
|
||||||
|
<div class="mb-5">
|
||||||
|
<div class="mb-5 flex items-center justify-between">
|
||||||
|
<div class="flex items-center text-base font-bold">
|
||||||
|
分段设置
|
||||||
|
<ElTooltip placement="top">
|
||||||
|
<template #content>
|
||||||
|
系统会自动将文档内容分割成多个段落,您可以根据需要调整分段方式和内容。
|
||||||
|
</template>
|
||||||
|
<IconifyIcon
|
||||||
|
icon="lucide:circle-alert"
|
||||||
|
class="ml-1 text-gray-400"
|
||||||
|
/>
|
||||||
|
</ElTooltip>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<ElButton type="primary" size="small" @click="handleAutoSegment">
|
||||||
|
预览分段
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-5">
|
||||||
|
<ElForm :label-col="{ span: 5 }">
|
||||||
|
<ElFormItem label="最大 Token 数">
|
||||||
|
<ElInputNumber
|
||||||
|
v-model="modelData.segmentMaxTokens"
|
||||||
|
:min="1"
|
||||||
|
:max="2048"
|
||||||
|
controls-position="right"
|
||||||
|
class="!w-full"
|
||||||
|
/>
|
||||||
|
</ElFormItem>
|
||||||
|
</ElForm>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-2.5">
|
||||||
|
<div class="mb-2.5 text-base font-bold">分段预览</div>
|
||||||
|
<!-- 文件选择器 -->
|
||||||
|
<div class="mb-2.5">
|
||||||
|
<ElDropdown
|
||||||
|
v-if="modelData.list && modelData.list.length > 0"
|
||||||
|
trigger="click"
|
||||||
|
>
|
||||||
|
<div class="flex cursor-pointer items-center">
|
||||||
|
<IconifyIcon icon="lucide:file-text" class="mr-1" />
|
||||||
|
<span>{{ currentFile?.name || '请选择文件' }}</span>
|
||||||
|
<span
|
||||||
|
v-if="currentFile?.segments"
|
||||||
|
class="ml-1 text-sm text-gray-500"
|
||||||
|
>
|
||||||
|
({{ currentFile.segments.length }}个分片)
|
||||||
|
</span>
|
||||||
|
<IconifyIcon icon="lucide:chevron-down" class="ml-1" />
|
||||||
|
</div>
|
||||||
|
<template #dropdown>
|
||||||
|
<ElDropdownMenu>
|
||||||
|
<ElDropdownItem
|
||||||
|
v-for="(file, index) in modelData.list"
|
||||||
|
:key="index"
|
||||||
|
@click="selectFile(index)"
|
||||||
|
>
|
||||||
|
{{ file.name }}
|
||||||
|
<span v-if="file.segments" class="ml-1 text-sm text-gray-500">
|
||||||
|
({{ file.segments.length }} 个分片)
|
||||||
|
</span>
|
||||||
|
</ElDropdownItem>
|
||||||
|
</ElDropdownMenu>
|
||||||
|
</template>
|
||||||
|
</ElDropdown>
|
||||||
|
<div v-else class="text-gray-400">暂无上传文件</div>
|
||||||
|
</div>
|
||||||
|
<!-- 文件内容预览 -->
|
||||||
|
<div class="max-h-[600px] overflow-y-auto rounded-md p-4">
|
||||||
|
<div v-if="splitLoading" class="flex items-center justify-center py-5">
|
||||||
|
<IconifyIcon icon="lucide:loader" class="is-loading" />
|
||||||
|
<span class="ml-2.5">正在加载分段内容...</span>
|
||||||
|
</div>
|
||||||
|
<template
|
||||||
|
v-else-if="
|
||||||
|
currentFile &&
|
||||||
|
currentFile.segments &&
|
||||||
|
currentFile.segments.length > 0
|
||||||
|
"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
v-for="(segment, index) in currentFile.segments"
|
||||||
|
:key="index"
|
||||||
|
class="mb-2.5"
|
||||||
|
>
|
||||||
|
<div class="mb-1 text-sm text-gray-500">
|
||||||
|
分片-{{ index + 1 }} · {{ segment.contentLength || 0 }} 字符数 ·
|
||||||
|
{{ segment.tokens || 0 }} Token
|
||||||
|
</div>
|
||||||
|
<div class="bg-card rounded-md p-2">
|
||||||
|
{{ segment.content }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<ElEmpty v-else description="暂无预览内容" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- 添加底部按钮 -->
|
||||||
|
<div class="mt-5 flex justify-between">
|
||||||
|
<div>
|
||||||
|
<ElButton v-if="!modelData.id" @click="handlePrevStep">上一步</ElButton>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<ElButton type="primary" :loading="submitLoading" @click="handleSave">
|
||||||
|
保存并处理
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,270 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import type { UploadProps, UploadRequestOptions } from 'element-plus';
|
||||||
|
|
||||||
|
import type { PropType } from 'vue';
|
||||||
|
|
||||||
|
import type { AxiosProgressEvent } from '#/api/infra/file';
|
||||||
|
|
||||||
|
import { computed, getCurrentInstance, inject, onMounted, ref } from 'vue';
|
||||||
|
|
||||||
|
import { IconifyIcon } from '@vben/icons';
|
||||||
|
import { generateAcceptedFileTypes } from '@vben/utils';
|
||||||
|
|
||||||
|
import {
|
||||||
|
ElButton,
|
||||||
|
ElForm,
|
||||||
|
ElFormItem,
|
||||||
|
ElMessage,
|
||||||
|
ElUpload,
|
||||||
|
} from 'element-plus';
|
||||||
|
|
||||||
|
import { useUpload } from '#/components/upload/use-upload';
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
modelValue: {
|
||||||
|
type: Object as PropType<any>,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const emit = defineEmits(['update:modelValue']);
|
||||||
|
const formRef = ref(); // 表单引用
|
||||||
|
const uploadRef = ref(); // 上传组件引用
|
||||||
|
const parent = inject('parent', null); // 获取父组件实例
|
||||||
|
const { uploadUrl, httpRequest } = useUpload(); // 使用上传组件的钩子
|
||||||
|
const fileList = ref<UploadProps['fileList']>([]); // 文件列表
|
||||||
|
const uploadingCount = ref(0); // 上传中的文件数量
|
||||||
|
|
||||||
|
const supportedFileTypes = [
|
||||||
|
'TXT',
|
||||||
|
'MARKDOWN',
|
||||||
|
'MDX',
|
||||||
|
'PDF',
|
||||||
|
'HTML',
|
||||||
|
'XLSX',
|
||||||
|
'XLS',
|
||||||
|
'DOC',
|
||||||
|
'DOCX',
|
||||||
|
'CSV',
|
||||||
|
'EML',
|
||||||
|
'MSG',
|
||||||
|
'PPTX',
|
||||||
|
'XML',
|
||||||
|
'EPUB',
|
||||||
|
'PPT',
|
||||||
|
'MD',
|
||||||
|
'HTM',
|
||||||
|
]; // 支持的文件类型和大小限制
|
||||||
|
const allowedExtensions = new Set(
|
||||||
|
supportedFileTypes.map((ext) => ext.toLowerCase()),
|
||||||
|
); // 小写的扩展名列表
|
||||||
|
const maxFileSize = 15; // 最大文件大小(MB)
|
||||||
|
const acceptedFileTypes = computed(() =>
|
||||||
|
generateAcceptedFileTypes(supportedFileTypes),
|
||||||
|
); // 构建 accept 属性值,用于限制文件选择对话框中可见的文件类型
|
||||||
|
|
||||||
|
/** 表单数据 */
|
||||||
|
const modelData = computed({
|
||||||
|
get: () => {
|
||||||
|
return props.modelValue;
|
||||||
|
},
|
||||||
|
set: (val) => emit('update:modelValue', val),
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 确保 list 属性存在 */
|
||||||
|
function ensureListExists() {
|
||||||
|
if (!props.modelValue.list) {
|
||||||
|
emit('update:modelValue', {
|
||||||
|
...props.modelValue,
|
||||||
|
list: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 是否所有文件都已上传完成 */
|
||||||
|
const isAllUploaded = computed(() => {
|
||||||
|
return (
|
||||||
|
modelData.value.list &&
|
||||||
|
modelData.value.list.length > 0 &&
|
||||||
|
uploadingCount.value === 0
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传前检查文件类型和大小
|
||||||
|
*
|
||||||
|
* @param file 待上传的文件
|
||||||
|
* @returns 是否允许上传
|
||||||
|
*/
|
||||||
|
function beforeUpload(file: any) {
|
||||||
|
// 1.1 检查文件扩展名
|
||||||
|
const fileName = file.name.toLowerCase();
|
||||||
|
const fileExtension = fileName.slice(
|
||||||
|
Math.max(0, fileName.lastIndexOf('.') + 1),
|
||||||
|
);
|
||||||
|
if (!allowedExtensions.has(fileExtension)) {
|
||||||
|
ElMessage.error('不支持的文件类型!');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 1.2 检查文件大小
|
||||||
|
if (!(file.size / 1024 / 1024 < maxFileSize)) {
|
||||||
|
ElMessage.error(`文件大小不能超过 ${maxFileSize} MB!`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 增加上传中的文件计数
|
||||||
|
uploadingCount.value++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function customRequest(info: UploadRequestOptions) {
|
||||||
|
const file = info.file as File;
|
||||||
|
const name = file?.name;
|
||||||
|
try {
|
||||||
|
// 上传文件
|
||||||
|
const progressEvent: AxiosProgressEvent = (e) => {
|
||||||
|
const percent = Math.trunc((e.loaded / e.total!) * 100);
|
||||||
|
info.onProgress!({ percent });
|
||||||
|
};
|
||||||
|
const res = await httpRequest(info.file as File, progressEvent);
|
||||||
|
info.onSuccess!(res);
|
||||||
|
ElMessage.success('上传成功');
|
||||||
|
ensureListExists();
|
||||||
|
emit('update:modelValue', {
|
||||||
|
...props.modelValue,
|
||||||
|
list: [
|
||||||
|
...props.modelValue.list,
|
||||||
|
{
|
||||||
|
name,
|
||||||
|
url: res,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(error);
|
||||||
|
info.onError!(error);
|
||||||
|
} finally {
|
||||||
|
uploadingCount.value = Math.max(0, uploadingCount.value - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从列表中移除文件
|
||||||
|
*
|
||||||
|
* @param index 要移除的文件索引
|
||||||
|
*/
|
||||||
|
function removeFile(index: number) {
|
||||||
|
// 从列表中移除文件
|
||||||
|
const newList = [...props.modelValue.list];
|
||||||
|
newList.splice(index, 1);
|
||||||
|
// 更新表单数据
|
||||||
|
emit('update:modelValue', {
|
||||||
|
...props.modelValue,
|
||||||
|
list: newList,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 下一步按钮处理 */
|
||||||
|
function handleNextStep() {
|
||||||
|
// 1.1 检查是否有文件上传
|
||||||
|
if (!modelData.value.list || modelData.value.list.length === 0) {
|
||||||
|
ElMessage.warning('请上传至少一个文件');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 1.2 检查是否有文件正在上传
|
||||||
|
if (uploadingCount.value > 0) {
|
||||||
|
ElMessage.warning('请等待所有文件上传完成');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 获取父组件的goToNextStep方法
|
||||||
|
const parentEl = parent || getCurrentInstance()?.parent;
|
||||||
|
if (parentEl && typeof parentEl.exposed?.goToNextStep === 'function') {
|
||||||
|
parentEl.exposed.goToNextStep();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(() => {
|
||||||
|
ensureListExists();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<ElForm ref="formRef" :model="modelData" label-width="0" class="mt-5">
|
||||||
|
<ElFormItem class="mb-5">
|
||||||
|
<div class="w-full">
|
||||||
|
<div
|
||||||
|
class="w-full rounded-md border-2 border-dashed border-gray-200 p-5 text-center hover:border-blue-500"
|
||||||
|
>
|
||||||
|
<ElUpload
|
||||||
|
ref="uploadRef"
|
||||||
|
class="upload-demo"
|
||||||
|
:action="uploadUrl"
|
||||||
|
v-model:file-list="fileList"
|
||||||
|
:accept="acceptedFileTypes"
|
||||||
|
:show-file-list="false"
|
||||||
|
:before-upload="beforeUpload"
|
||||||
|
:http-request="customRequest"
|
||||||
|
:multiple="true"
|
||||||
|
drag
|
||||||
|
>
|
||||||
|
<div class="flex flex-col items-center justify-center py-5">
|
||||||
|
<IconifyIcon
|
||||||
|
icon="ep:upload-filled"
|
||||||
|
class="mb-2.5 text-xs text-gray-400"
|
||||||
|
/>
|
||||||
|
<div class="ant-upload-text text-base text-gray-400">
|
||||||
|
拖拽文件至此,或者
|
||||||
|
<em class="cursor-pointer not-italic text-blue-500">
|
||||||
|
选择文件
|
||||||
|
</em>
|
||||||
|
</div>
|
||||||
|
<div class="mt-2.5 text-sm text-gray-400">
|
||||||
|
已支持 {{ supportedFileTypes.join('、') }},每个文件不超过
|
||||||
|
{{ maxFileSize }} MB。
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</ElUpload>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
v-if="modelData.list && modelData.list.length > 0"
|
||||||
|
class="mt-4 grid grid-cols-1 gap-2"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
v-for="(file, index) in modelData.list"
|
||||||
|
:key="index"
|
||||||
|
class="flex items-center justify-between rounded-sm border-l-4 border-l-blue-500 px-3 py-1 shadow-sm transition-all duration-300 hover:bg-blue-50"
|
||||||
|
>
|
||||||
|
<div class="flex items-center">
|
||||||
|
<IconifyIcon icon="lucide:file-text" class="mr-2 text-blue-500" />
|
||||||
|
<span class="break-all text-sm text-gray-600">
|
||||||
|
{{ file.name }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<ElButton
|
||||||
|
type="danger"
|
||||||
|
text
|
||||||
|
link
|
||||||
|
@click="removeFile(index)"
|
||||||
|
class="ml-2"
|
||||||
|
>
|
||||||
|
<IconifyIcon icon="lucide:trash-2" />
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</ElFormItem>
|
||||||
|
<ElFormItem>
|
||||||
|
<div class="flex w-full justify-end">
|
||||||
|
<ElButton
|
||||||
|
type="primary"
|
||||||
|
@click="handleNextStep"
|
||||||
|
:disabled="!isAllUploaded"
|
||||||
|
>
|
||||||
|
下一步
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</ElFormItem>
|
||||||
|
</ElForm>
|
||||||
|
</template>
|
||||||
192
apps/web-ele/src/views/ai/knowledge/document/index.vue
Normal file
192
apps/web-ele/src/views/ai/knowledge/document/index.vue
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
import type { AiKnowledgeDocumentApi } from '#/api/ai/knowledge/document';
|
||||||
|
|
||||||
|
import { onMounted } from 'vue';
|
||||||
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
|
import { confirm, Page } from '@vben/common-ui';
|
||||||
|
import { DICT_TYPE } from '@vben/constants';
|
||||||
|
import { getDictLabel } from '@vben/hooks';
|
||||||
|
|
||||||
|
import { ElLoading, ElMessage } from 'element-plus';
|
||||||
|
|
||||||
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||||
|
import {
|
||||||
|
deleteKnowledgeDocument,
|
||||||
|
getKnowledgeDocumentPage,
|
||||||
|
updateKnowledgeDocumentStatus,
|
||||||
|
} from '#/api/ai/knowledge/document';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
|
import { useGridColumns, useGridFormSchema } from './data';
|
||||||
|
|
||||||
|
/** AI 知识库文档列表 */
|
||||||
|
defineOptions({ name: 'AiKnowledgeDocument' });
|
||||||
|
|
||||||
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
/** 刷新表格 */
|
||||||
|
function handleRefresh() {
|
||||||
|
gridApi.query();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 创建知识库文档 */
|
||||||
|
function handleCreate() {
|
||||||
|
router.push({
|
||||||
|
name: 'AiKnowledgeDocumentCreate',
|
||||||
|
query: { knowledgeId: route.query.knowledgeId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 编辑知识库文档 */
|
||||||
|
function handleEdit(id: number) {
|
||||||
|
router.push({
|
||||||
|
name: 'AiKnowledgeDocumentUpdate',
|
||||||
|
query: { id, knowledgeId: route.query.knowledgeId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 删除知识库文档 */
|
||||||
|
async function handleDelete(row: AiKnowledgeDocumentApi.KnowledgeDocument) {
|
||||||
|
const loadingInstance = ElLoading.service({
|
||||||
|
text: $t('ui.actionMessage.deleting', [row.name]),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await deleteKnowledgeDocument(row.id as number);
|
||||||
|
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
|
||||||
|
handleRefresh();
|
||||||
|
} finally {
|
||||||
|
loadingInstance.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 跳转到知识库分段页面 */
|
||||||
|
function handleSegment(id: number) {
|
||||||
|
router.push({
|
||||||
|
name: 'AiKnowledgeSegment',
|
||||||
|
query: { documentId: id },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 更新文档状态 */
|
||||||
|
async function handleStatusChange(
|
||||||
|
newStatus: number,
|
||||||
|
row: AiKnowledgeDocumentApi.KnowledgeDocument,
|
||||||
|
): Promise<boolean | undefined> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
confirm({
|
||||||
|
content: `你要将${row.name}的状态切换为【${getDictLabel(DICT_TYPE.COMMON_STATUS, newStatus)}】吗?`,
|
||||||
|
})
|
||||||
|
.then(async () => {
|
||||||
|
// 更新文档状态
|
||||||
|
await updateKnowledgeDocumentStatus({
|
||||||
|
id: row.id,
|
||||||
|
status: newStatus,
|
||||||
|
});
|
||||||
|
// 提示并返回成功
|
||||||
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
||||||
|
resolve(true);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
reject(new Error('取消操作'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
|
formOptions: {
|
||||||
|
schema: useGridFormSchema(),
|
||||||
|
},
|
||||||
|
gridOptions: {
|
||||||
|
columns: useGridColumns(handleStatusChange),
|
||||||
|
height: 'auto',
|
||||||
|
keepSource: true,
|
||||||
|
proxyConfig: {
|
||||||
|
ajax: {
|
||||||
|
query: async ({ page }, formValues) => {
|
||||||
|
return await getKnowledgeDocumentPage({
|
||||||
|
pageNo: page.currentPage,
|
||||||
|
pageSize: page.pageSize,
|
||||||
|
...formValues,
|
||||||
|
knowledgeId: route.query.knowledgeId,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
rowConfig: {
|
||||||
|
keyField: 'id',
|
||||||
|
isHover: true,
|
||||||
|
},
|
||||||
|
toolbarConfig: {
|
||||||
|
refresh: true,
|
||||||
|
search: true,
|
||||||
|
},
|
||||||
|
} as VxeTableGridOptions<AiKnowledgeDocumentApi.KnowledgeDocument>,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(() => {
|
||||||
|
// 如果知识库 ID 不存在,显示错误提示并关闭页面
|
||||||
|
if (!route.query.knowledgeId) {
|
||||||
|
ElMessage.error('知识库 ID 不存在,无法查看文档列表');
|
||||||
|
// 关闭当前路由,返回到知识库列表页面
|
||||||
|
router.back();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Page auto-content-height>
|
||||||
|
<Grid table-title="知识库文档列表">
|
||||||
|
<template #toolbar-tools>
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('ui.actionTitle.create', ['知识库文档']),
|
||||||
|
type: 'primary',
|
||||||
|
icon: ACTION_ICON.ADD,
|
||||||
|
auth: ['ai:knowledge:create'],
|
||||||
|
onClick: handleCreate,
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
<template #actions="{ row }">
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('common.edit'),
|
||||||
|
type: 'primary',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.EDIT,
|
||||||
|
auth: ['ai:knowledge:update'],
|
||||||
|
onClick: handleEdit.bind(null, row.id),
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
:drop-down-actions="[
|
||||||
|
{
|
||||||
|
label: '分段',
|
||||||
|
type: 'primary',
|
||||||
|
link: true,
|
||||||
|
auth: ['ai:knowledge:query'],
|
||||||
|
onClick: handleSegment.bind(null, row.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: $t('common.delete'),
|
||||||
|
type: 'danger',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.DELETE,
|
||||||
|
auth: ['ai:knowledge:delete'],
|
||||||
|
popConfirm: {
|
||||||
|
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
|
||||||
|
confirm: handleDelete.bind(null, row),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</Grid>
|
||||||
|
</Page>
|
||||||
|
</template>
|
||||||
173
apps/web-ele/src/views/ai/knowledge/knowledge/data.ts
Normal file
173
apps/web-ele/src/views/ai/knowledge/knowledge/data.ts
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
import type { VbenFormSchema } from '#/adapter/form';
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
|
||||||
|
import { AiModelTypeEnum, CommonStatusEnum, DICT_TYPE } from '@vben/constants';
|
||||||
|
import { getDictOptions } from '@vben/hooks';
|
||||||
|
|
||||||
|
import { z } from '#/adapter/form';
|
||||||
|
import { getModelSimpleList } from '#/api/ai/model/model';
|
||||||
|
import { getRangePickerDefaultProps } from '#/utils';
|
||||||
|
|
||||||
|
/** 新增/修改的表单 */
|
||||||
|
export function useFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
component: 'Input',
|
||||||
|
fieldName: 'id',
|
||||||
|
dependencies: {
|
||||||
|
triggerFields: [''],
|
||||||
|
show: () => false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
component: 'Input',
|
||||||
|
fieldName: 'name',
|
||||||
|
label: '知识库名称',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入知识库名称',
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'description',
|
||||||
|
label: '知识库描述',
|
||||||
|
component: 'Textarea',
|
||||||
|
componentProps: {
|
||||||
|
rows: 3,
|
||||||
|
placeholder: '请输入知识库描述',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
component: 'ApiSelect',
|
||||||
|
fieldName: 'embeddingModelId',
|
||||||
|
label: '向量模型',
|
||||||
|
componentProps: {
|
||||||
|
api: () => getModelSimpleList(AiModelTypeEnum.EMBEDDING),
|
||||||
|
labelField: 'name',
|
||||||
|
valueField: 'id',
|
||||||
|
allowClear: true,
|
||||||
|
placeholder: '请选择向量模型',
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'topK',
|
||||||
|
label: '检索 topK',
|
||||||
|
component: 'InputNumber',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入检索 topK',
|
||||||
|
controlsPosition: 'right',
|
||||||
|
class: '!w-full',
|
||||||
|
min: 0,
|
||||||
|
max: 10,
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'similarityThreshold',
|
||||||
|
label: '检索相似度阈值',
|
||||||
|
component: 'InputNumber',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入检索相似度阈值',
|
||||||
|
controlsPosition: 'right',
|
||||||
|
class: '!w-full',
|
||||||
|
min: 0,
|
||||||
|
max: 1,
|
||||||
|
step: 0.01,
|
||||||
|
precision: 2,
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'status',
|
||||||
|
label: '是否启用',
|
||||||
|
component: 'RadioGroup',
|
||||||
|
componentProps: {
|
||||||
|
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
|
||||||
|
},
|
||||||
|
rules: z.number().default(CommonStatusEnum.ENABLE),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 列表的搜索表单 */
|
||||||
|
export function useGridFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
fieldName: 'name',
|
||||||
|
label: '知识库名称',
|
||||||
|
component: 'Input',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入知识库名称',
|
||||||
|
allowClear: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'status',
|
||||||
|
label: '是否启用',
|
||||||
|
component: 'Select',
|
||||||
|
componentProps: {
|
||||||
|
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
|
||||||
|
placeholder: '请选择是否启用',
|
||||||
|
allowClear: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'createTime',
|
||||||
|
label: '创建时间',
|
||||||
|
component: 'RangePicker',
|
||||||
|
componentProps: {
|
||||||
|
...getRangePickerDefaultProps(),
|
||||||
|
allowClear: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 列表的字段 */
|
||||||
|
export function useGridColumns(): VxeTableGridOptions['columns'] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
field: 'id',
|
||||||
|
title: '编号',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'name',
|
||||||
|
title: '知识库名称',
|
||||||
|
minWidth: 150,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'description',
|
||||||
|
title: '知识库描述',
|
||||||
|
minWidth: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'embeddingModel',
|
||||||
|
title: '向量化模型',
|
||||||
|
minWidth: 150,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'status',
|
||||||
|
title: '是否启用',
|
||||||
|
minWidth: 100,
|
||||||
|
cellRender: {
|
||||||
|
name: 'CellDict',
|
||||||
|
props: { type: DICT_TYPE.COMMON_STATUS },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'createTime',
|
||||||
|
title: '创建时间',
|
||||||
|
minWidth: 180,
|
||||||
|
formatter: 'formatDateTime',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: '操作',
|
||||||
|
width: 280,
|
||||||
|
fixed: 'right',
|
||||||
|
slots: { default: 'actions' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
166
apps/web-ele/src/views/ai/knowledge/knowledge/index.vue
Normal file
166
apps/web-ele/src/views/ai/knowledge/knowledge/index.vue
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
import type { AiKnowledgeKnowledgeApi } from '#/api/ai/knowledge/knowledge';
|
||||||
|
|
||||||
|
import { useRouter } from 'vue-router';
|
||||||
|
|
||||||
|
import { DocAlert, Page, useVbenModal } from '@vben/common-ui';
|
||||||
|
|
||||||
|
import { ElLoading, ElMessage } from 'element-plus';
|
||||||
|
|
||||||
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||||
|
import {
|
||||||
|
deleteKnowledge,
|
||||||
|
getKnowledgePage,
|
||||||
|
} from '#/api/ai/knowledge/knowledge';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
|
import { useGridColumns, useGridFormSchema } from './data';
|
||||||
|
import Form from './modules/form.vue';
|
||||||
|
|
||||||
|
const [FormModal, formModalApi] = useVbenModal({
|
||||||
|
connectedComponent: Form,
|
||||||
|
destroyOnClose: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 刷新表格 */
|
||||||
|
function handleRefresh() {
|
||||||
|
gridApi.query();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 创建知识库 */
|
||||||
|
function handleCreate() {
|
||||||
|
formModalApi.setData(null).open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 编辑知识库 */
|
||||||
|
function handleEdit(row: AiKnowledgeKnowledgeApi.Knowledge) {
|
||||||
|
formModalApi.setData(row).open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 删除知识库 */
|
||||||
|
async function handleDelete(row: AiKnowledgeKnowledgeApi.Knowledge) {
|
||||||
|
const loadingInstance = ElLoading.service({
|
||||||
|
text: $t('ui.actionMessage.deleting', [row.name]),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await deleteKnowledge(row.id as number);
|
||||||
|
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
|
||||||
|
handleRefresh();
|
||||||
|
} finally {
|
||||||
|
loadingInstance.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 跳转到知识库文档页面 */
|
||||||
|
const router = useRouter();
|
||||||
|
function handleDocument(id: number) {
|
||||||
|
router.push({
|
||||||
|
name: 'AiKnowledgeDocument',
|
||||||
|
query: { knowledgeId: id },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 跳转到文档召回测试页面 */
|
||||||
|
function handleRetrieval(id: number) {
|
||||||
|
router.push({
|
||||||
|
name: 'AiKnowledgeRetrieval',
|
||||||
|
query: { id },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
|
formOptions: {
|
||||||
|
schema: useGridFormSchema(),
|
||||||
|
},
|
||||||
|
gridOptions: {
|
||||||
|
columns: useGridColumns(),
|
||||||
|
height: 'auto',
|
||||||
|
keepSource: true,
|
||||||
|
proxyConfig: {
|
||||||
|
ajax: {
|
||||||
|
query: async ({ page }, formValues) => {
|
||||||
|
return await getKnowledgePage({
|
||||||
|
pageNo: page.currentPage,
|
||||||
|
pageSize: page.pageSize,
|
||||||
|
...formValues,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
rowConfig: {
|
||||||
|
keyField: 'id',
|
||||||
|
isHover: true,
|
||||||
|
},
|
||||||
|
toolbarConfig: {
|
||||||
|
refresh: true,
|
||||||
|
search: true,
|
||||||
|
},
|
||||||
|
} as VxeTableGridOptions<AiKnowledgeKnowledgeApi.Knowledge>,
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Page auto-content-height>
|
||||||
|
<template #doc>
|
||||||
|
<DocAlert title="AI 手册" url="https://doc.iocoder.cn/ai/build/" />
|
||||||
|
</template>
|
||||||
|
<FormModal @success="handleRefresh" />
|
||||||
|
<Grid table-title="AI 知识库列表">
|
||||||
|
<template #toolbar-tools>
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('ui.actionTitle.create', ['AI 知识库']),
|
||||||
|
type: 'primary',
|
||||||
|
icon: ACTION_ICON.ADD,
|
||||||
|
auth: ['ai:knowledge:create'],
|
||||||
|
onClick: handleCreate,
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
<template #actions="{ row }">
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('common.edit'),
|
||||||
|
type: 'primary',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.EDIT,
|
||||||
|
auth: ['ai:knowledge:update'],
|
||||||
|
onClick: handleEdit.bind(null, row),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: $t('ui.widgets.document'),
|
||||||
|
type: 'primary',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.BOOK,
|
||||||
|
auth: ['ai:knowledge:query'],
|
||||||
|
onClick: handleDocument.bind(null, row.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: '召回测试',
|
||||||
|
type: 'primary',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.SEARCH,
|
||||||
|
auth: ['ai:knowledge:query'],
|
||||||
|
onClick: handleRetrieval.bind(null, row.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: $t('common.delete'),
|
||||||
|
type: 'danger',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.DELETE,
|
||||||
|
auth: ['ai:knowledge:delete'],
|
||||||
|
popConfirm: {
|
||||||
|
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
|
||||||
|
confirm: handleDelete.bind(null, row),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</Grid>
|
||||||
|
</Page>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { AiKnowledgeKnowledgeApi } from '#/api/ai/knowledge/knowledge';
|
||||||
|
|
||||||
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
|
import { useVbenModal } from '@vben/common-ui';
|
||||||
|
|
||||||
|
import { ElMessage } from 'element-plus';
|
||||||
|
|
||||||
|
import { useVbenForm } from '#/adapter/form';
|
||||||
|
import {
|
||||||
|
createKnowledge,
|
||||||
|
getKnowledge,
|
||||||
|
updateKnowledge,
|
||||||
|
} from '#/api/ai/knowledge/knowledge';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
|
import { useFormSchema } from '../data';
|
||||||
|
|
||||||
|
const emit = defineEmits(['success']);
|
||||||
|
const formData = ref<AiKnowledgeKnowledgeApi.Knowledge>();
|
||||||
|
const getTitle = computed(() => {
|
||||||
|
return formData.value?.id
|
||||||
|
? $t('ui.actionTitle.edit', ['AI 知识库'])
|
||||||
|
: $t('ui.actionTitle.create', ['AI 知识库']);
|
||||||
|
});
|
||||||
|
|
||||||
|
const [Form, formApi] = useVbenForm({
|
||||||
|
commonConfig: {
|
||||||
|
componentProps: {
|
||||||
|
class: 'w-full',
|
||||||
|
},
|
||||||
|
formItemClass: 'col-span-2',
|
||||||
|
labelWidth: 140,
|
||||||
|
},
|
||||||
|
layout: 'horizontal',
|
||||||
|
schema: useFormSchema(),
|
||||||
|
showDefaultActions: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const [Modal, modalApi] = useVbenModal({
|
||||||
|
async onConfirm() {
|
||||||
|
const { valid } = await formApi.validate();
|
||||||
|
if (!valid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modalApi.lock();
|
||||||
|
// 提交表单
|
||||||
|
const data =
|
||||||
|
(await formApi.getValues()) as AiKnowledgeKnowledgeApi.Knowledge;
|
||||||
|
try {
|
||||||
|
await (formData.value?.id
|
||||||
|
? updateKnowledge(data)
|
||||||
|
: createKnowledge(data));
|
||||||
|
// 关闭并提示
|
||||||
|
await modalApi.close();
|
||||||
|
emit('success');
|
||||||
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async onOpenChange(isOpen: boolean) {
|
||||||
|
if (!isOpen) {
|
||||||
|
formData.value = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 加载数据
|
||||||
|
const data = modalApi.getData<AiKnowledgeKnowledgeApi.Knowledge>();
|
||||||
|
if (!data || !data.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modalApi.lock();
|
||||||
|
try {
|
||||||
|
formData.value = await getKnowledge(data.id);
|
||||||
|
// 设置到 values
|
||||||
|
await formApi.setValues(formData.value);
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Modal :title="getTitle" class="w-2/5">
|
||||||
|
<Form class="mx-4" />
|
||||||
|
</Modal>
|
||||||
|
</template>
|
||||||
|
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { onMounted, reactive, ref } from 'vue';
|
||||||
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
|
import { Page } from '@vben/common-ui';
|
||||||
|
import { IconifyIcon } from '@vben/icons';
|
||||||
|
|
||||||
|
import {
|
||||||
|
ElButton,
|
||||||
|
ElCard,
|
||||||
|
ElEmpty,
|
||||||
|
ElInputNumber,
|
||||||
|
ElInput,
|
||||||
|
ElMessage,
|
||||||
|
} from 'element-plus';
|
||||||
|
|
||||||
|
import { getKnowledge } from '#/api/ai/knowledge/knowledge';
|
||||||
|
import { searchKnowledgeSegment } from '#/api/ai/knowledge/segment';
|
||||||
|
|
||||||
|
/** 知识库文档召回测试 */
|
||||||
|
defineOptions({ name: 'KnowledgeDocumentRetrieval' });
|
||||||
|
|
||||||
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const loading = ref(false); // 加载状态
|
||||||
|
const segments = ref<any[]>([]); // 召回结果
|
||||||
|
const queryParams = reactive({
|
||||||
|
id: undefined,
|
||||||
|
content: '',
|
||||||
|
topK: 10,
|
||||||
|
similarityThreshold: 0.5,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 执行召回测试 */
|
||||||
|
async function getRetrievalResult() {
|
||||||
|
if (!queryParams.content) {
|
||||||
|
ElMessage.warning('请输入查询文本');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
loading.value = true;
|
||||||
|
segments.value = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await searchKnowledgeSegment({
|
||||||
|
knowledgeId: queryParams.id,
|
||||||
|
content: queryParams.content,
|
||||||
|
topK: queryParams.topK,
|
||||||
|
similarityThreshold: queryParams.similarityThreshold,
|
||||||
|
});
|
||||||
|
segments.value = data || [];
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 切换段落展开状态 */
|
||||||
|
function toggleExpand(segment: any) {
|
||||||
|
segment.expanded = !segment.expanded;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 获取知识库配置信息 */
|
||||||
|
async function getKnowledgeInfo(id: number) {
|
||||||
|
try {
|
||||||
|
const knowledge = await getKnowledge(id);
|
||||||
|
if (knowledge) {
|
||||||
|
queryParams.topK = knowledge.topK || queryParams.topK;
|
||||||
|
queryParams.similarityThreshold =
|
||||||
|
knowledge.similarityThreshold || queryParams.similarityThreshold;
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(() => {
|
||||||
|
// 如果知识库 ID 不存在,显示错误提示并关闭页面
|
||||||
|
if (!route.query.id) {
|
||||||
|
ElMessage.error('知识库 ID 不存在,无法进行召回测试');
|
||||||
|
router.back();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
queryParams.id = route.query.id as any;
|
||||||
|
|
||||||
|
// 获取知识库信息并设置默认值
|
||||||
|
getKnowledgeInfo(queryParams.id as any);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<Page auto-content-height>
|
||||||
|
<div class="flex w-full gap-4">
|
||||||
|
<ElCard class="w-3/4 flex-1">
|
||||||
|
<div class="mb-15">
|
||||||
|
<h3 class="m-2 text-lg font-semibold leading-none tracking-tight">
|
||||||
|
召回测试
|
||||||
|
</h3>
|
||||||
|
<div class="m-2 text-sm text-gray-500">
|
||||||
|
根据给定的查询文本测试召回效果。
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div class="relative m-2">
|
||||||
|
<ElInput
|
||||||
|
v-model="queryParams.content"
|
||||||
|
:rows="8"
|
||||||
|
type="textarea"
|
||||||
|
placeholder="请输入文本"
|
||||||
|
/>
|
||||||
|
<div class="absolute bottom-2 right-2 text-sm text-gray-400">
|
||||||
|
{{ queryParams.content?.length }} / 200
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="m-2 flex items-center">
|
||||||
|
<span class="w-16 text-gray-500">topK:</span>
|
||||||
|
<ElInputNumber
|
||||||
|
v-model="queryParams.topK"
|
||||||
|
:min="1"
|
||||||
|
:max="20"
|
||||||
|
controls-position="right"
|
||||||
|
class="!w-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="m-2 flex items-center">
|
||||||
|
<span class="w-16 text-gray-500">相似度:</span>
|
||||||
|
<ElInputNumber
|
||||||
|
v-model="queryParams.similarityThreshold"
|
||||||
|
class="!w-full"
|
||||||
|
controls-position="right"
|
||||||
|
:min="0"
|
||||||
|
:max="1"
|
||||||
|
:precision="2"
|
||||||
|
:step="0.01"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="flex justify-end">
|
||||||
|
<ElButton
|
||||||
|
type="primary"
|
||||||
|
@click="getRetrievalResult"
|
||||||
|
:loading="loading"
|
||||||
|
>
|
||||||
|
测试
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</ElCard>
|
||||||
|
<ElCard class="min-w-300 flex-1">
|
||||||
|
<!-- 加载中状态 -->
|
||||||
|
<template v-if="loading">
|
||||||
|
<div class="flex h-72 items-center justify-center">
|
||||||
|
<ElEmpty description="正在检索中..." />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- 有段落 -->
|
||||||
|
<template v-else-if="segments.length > 0">
|
||||||
|
<div class="mb-15 font-bold">{{ segments.length }} 个召回段落</div>
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
v-for="(segment, index) in segments"
|
||||||
|
:key="index"
|
||||||
|
class="mt-2 rounded border border-solid border-gray-200 px-2 py-2"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="mb-2 flex items-center justify-between gap-8 text-sm text-gray-500"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
分段({{ segment.id }}) · {{ segment.contentLength }} 字符数 ·
|
||||||
|
{{ segment.tokens }} Token
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
class="whitespace-nowrap rounded-full bg-blue-50 px-2 py-1 text-sm text-blue-500"
|
||||||
|
>
|
||||||
|
score: {{ segment.score }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="mb-2 overflow-hidden whitespace-pre-wrap rounded bg-gray-50 text-sm transition-all duration-100"
|
||||||
|
:class="{
|
||||||
|
'line-clamp-2 max-h-40': !segment.expanded,
|
||||||
|
'max-h-[1500px]': segment.expanded,
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
{{ segment.content }}
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center justify-between gap-8">
|
||||||
|
<div class="flex items-center gap-1 text-sm text-gray-500">
|
||||||
|
<IconifyIcon icon="lucide:file-text" />
|
||||||
|
<span>{{ segment.documentName || '未知文档' }}</span>
|
||||||
|
</div>
|
||||||
|
<ElButton size="small" @click="toggleExpand(segment)">
|
||||||
|
{{ segment.expanded ? '收起' : '展开' }}
|
||||||
|
<IconifyIcon
|
||||||
|
:icon="
|
||||||
|
segment.expanded
|
||||||
|
? 'lucide:chevron-up'
|
||||||
|
: 'lucide:chevron-down'
|
||||||
|
"
|
||||||
|
/>
|
||||||
|
</ElButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- 无召回结果 -->
|
||||||
|
<template v-else>
|
||||||
|
<div class="flex h-72 items-center justify-center">
|
||||||
|
<ElEmpty description="暂无召回结果" />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</ElCard>
|
||||||
|
</div>
|
||||||
|
</Page>
|
||||||
|
</template>
|
||||||
131
apps/web-ele/src/views/ai/knowledge/segment/data.ts
Normal file
131
apps/web-ele/src/views/ai/knowledge/segment/data.ts
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
import type { VbenFormSchema } from '#/adapter/form';
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
import type { AiKnowledgeSegmentApi } from '#/api/ai/knowledge/segment';
|
||||||
|
|
||||||
|
import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
|
||||||
|
import { getDictOptions } from '@vben/hooks';
|
||||||
|
|
||||||
|
/** 新增/修改的表单 */
|
||||||
|
export function useFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
component: 'Input',
|
||||||
|
fieldName: 'id',
|
||||||
|
dependencies: {
|
||||||
|
triggerFields: [''],
|
||||||
|
show: () => false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
component: 'Input',
|
||||||
|
fieldName: 'documentId',
|
||||||
|
dependencies: {
|
||||||
|
triggerFields: [''],
|
||||||
|
show: () => false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'content',
|
||||||
|
label: '切片内容',
|
||||||
|
component: 'Textarea',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入切片内容',
|
||||||
|
rows: 6,
|
||||||
|
showCount: true,
|
||||||
|
},
|
||||||
|
rules: 'required',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
/** 列表的搜索表单 */
|
||||||
|
export function useGridFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
fieldName: 'documentId',
|
||||||
|
label: '文档编号',
|
||||||
|
component: 'Input',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入文档编号',
|
||||||
|
allowClear: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'status',
|
||||||
|
label: '是否启用',
|
||||||
|
component: 'Select',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请选择是否启用',
|
||||||
|
allowClear: true,
|
||||||
|
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 列表的字段 */
|
||||||
|
export function useGridColumns(
|
||||||
|
onStatusChange?: (
|
||||||
|
newStatus: number,
|
||||||
|
row: AiKnowledgeSegmentApi.KnowledgeSegment,
|
||||||
|
) => PromiseLike<boolean | undefined>,
|
||||||
|
): VxeTableGridOptions['columns'] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
field: 'id',
|
||||||
|
title: '分段编号',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'expand',
|
||||||
|
width: 40,
|
||||||
|
slots: { content: 'expand_content' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'content',
|
||||||
|
title: '切片内容',
|
||||||
|
minWidth: 250,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'contentLength',
|
||||||
|
title: '字符数',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'tokens',
|
||||||
|
title: 'token 数量',
|
||||||
|
minWidth: 120,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'retrievalCount',
|
||||||
|
title: '召回次数',
|
||||||
|
minWidth: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'status',
|
||||||
|
title: '状态',
|
||||||
|
minWidth: 100,
|
||||||
|
align: 'center',
|
||||||
|
cellRender: {
|
||||||
|
attrs: { beforeChange: onStatusChange },
|
||||||
|
name: 'CellSwitch',
|
||||||
|
props: {
|
||||||
|
checkedValue: CommonStatusEnum.ENABLE,
|
||||||
|
unCheckedValue: CommonStatusEnum.DISABLE,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'createTime',
|
||||||
|
title: '创建时间',
|
||||||
|
minWidth: 180,
|
||||||
|
formatter: 'formatDateTime',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: '操作',
|
||||||
|
width: 150,
|
||||||
|
fixed: 'right',
|
||||||
|
slots: { default: 'actions' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
171
apps/web-ele/src/views/ai/knowledge/segment/index.vue
Normal file
171
apps/web-ele/src/views/ai/knowledge/segment/index.vue
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
import type { AiKnowledgeSegmentApi } from '#/api/ai/knowledge/segment';
|
||||||
|
|
||||||
|
import { onMounted } from 'vue';
|
||||||
|
import { useRoute } from 'vue-router';
|
||||||
|
|
||||||
|
import { confirm, Page, useVbenModal } from '@vben/common-ui';
|
||||||
|
import { DICT_TYPE } from '@vben/constants';
|
||||||
|
import { getDictLabel } from '@vben/hooks';
|
||||||
|
|
||||||
|
import { ElLoading, ElMessage } from 'element-plus';
|
||||||
|
|
||||||
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||||
|
import {
|
||||||
|
deleteKnowledgeSegment,
|
||||||
|
getKnowledgeSegmentPage,
|
||||||
|
updateKnowledgeSegmentStatus,
|
||||||
|
} from '#/api/ai/knowledge/segment';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
|
import { useGridColumns, useGridFormSchema } from './data';
|
||||||
|
import Form from './modules/form.vue';
|
||||||
|
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
|
const [FormModal, formModalApi] = useVbenModal({
|
||||||
|
connectedComponent: Form,
|
||||||
|
destroyOnClose: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 刷新表格 */
|
||||||
|
function handleRefresh() {
|
||||||
|
gridApi.query();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 创建知识库片段 */
|
||||||
|
function handleCreate() {
|
||||||
|
formModalApi.setData({ documentId: route.query.documentId }).open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 编辑知识库片段 */
|
||||||
|
function handleEdit(row: AiKnowledgeSegmentApi.KnowledgeSegment) {
|
||||||
|
formModalApi.setData(row).open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 删除知识库片段 */
|
||||||
|
async function handleDelete(row: AiKnowledgeSegmentApi.KnowledgeSegment) {
|
||||||
|
const loadingInstance = ElLoading.service({
|
||||||
|
text: $t('ui.actionMessage.deleting', [row.id]),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await deleteKnowledgeSegment(row.id as number);
|
||||||
|
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.id]));
|
||||||
|
handleRefresh();
|
||||||
|
} finally {
|
||||||
|
loadingInstance.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 更新知识库片段状态 */
|
||||||
|
async function handleStatusChange(
|
||||||
|
newStatus: number,
|
||||||
|
row: AiKnowledgeSegmentApi.KnowledgeSegment,
|
||||||
|
): Promise<boolean | undefined> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
confirm({
|
||||||
|
content: `你要将片段 ${row.id} 的状态切换为【${getDictLabel(DICT_TYPE.COMMON_STATUS, newStatus)}】吗?`,
|
||||||
|
})
|
||||||
|
.then(async () => {
|
||||||
|
// 更新片段状态
|
||||||
|
await updateKnowledgeSegmentStatus(row.id!, newStatus);
|
||||||
|
// 提示并返回成功
|
||||||
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
||||||
|
resolve(true);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
reject(new Error('取消操作'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
|
formOptions: {
|
||||||
|
schema: useGridFormSchema(),
|
||||||
|
},
|
||||||
|
gridOptions: {
|
||||||
|
columns: useGridColumns(handleStatusChange),
|
||||||
|
height: 'auto',
|
||||||
|
keepSource: true,
|
||||||
|
proxyConfig: {
|
||||||
|
ajax: {
|
||||||
|
query: async ({ page }, formValues) => {
|
||||||
|
return await getKnowledgeSegmentPage({
|
||||||
|
pageNo: page.currentPage,
|
||||||
|
pageSize: page.pageSize,
|
||||||
|
...formValues,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
rowConfig: {
|
||||||
|
keyField: 'id',
|
||||||
|
isHover: true,
|
||||||
|
},
|
||||||
|
toolbarConfig: {
|
||||||
|
refresh: true,
|
||||||
|
search: true,
|
||||||
|
},
|
||||||
|
} as VxeTableGridOptions<AiKnowledgeSegmentApi.KnowledgeSegment>,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(() => {
|
||||||
|
gridApi.formApi.setFieldValue('documentId', route.query.documentId);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Page auto-content-height>
|
||||||
|
<FormModal @success="handleRefresh" />
|
||||||
|
<Grid table-title="分段列表">
|
||||||
|
<template #toolbar-tools>
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('ui.actionTitle.create', ['分段']),
|
||||||
|
type: 'primary',
|
||||||
|
icon: ACTION_ICON.ADD,
|
||||||
|
auth: ['ai:knowledge:create'],
|
||||||
|
onClick: handleCreate,
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
<template #expand_content="{ row }">
|
||||||
|
<div
|
||||||
|
class="whitespace-pre-wrap border-l-4 border-blue-500 px-2.5 py-5 leading-5"
|
||||||
|
>
|
||||||
|
<div class="mb-2 text-sm font-bold text-gray-600">完整内容:</div>
|
||||||
|
{{ row.content }}
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<template #actions="{ row }">
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('common.edit'),
|
||||||
|
type: 'primary',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.EDIT,
|
||||||
|
auth: ['ai:knowledge:update'],
|
||||||
|
onClick: handleEdit.bind(null, row),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: $t('common.delete'),
|
||||||
|
type: 'danger',
|
||||||
|
link: true,
|
||||||
|
icon: ACTION_ICON.DELETE,
|
||||||
|
auth: ['ai:knowledge:delete'],
|
||||||
|
popConfirm: {
|
||||||
|
title: $t('ui.actionMessage.deleteConfirm', [row.id]),
|
||||||
|
confirm: handleDelete.bind(null, row),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</Grid>
|
||||||
|
</Page>
|
||||||
|
</template>
|
||||||
90
apps/web-ele/src/views/ai/knowledge/segment/modules/form.vue
Normal file
90
apps/web-ele/src/views/ai/knowledge/segment/modules/form.vue
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { AiKnowledgeSegmentApi } from '#/api/ai/knowledge/segment';
|
||||||
|
|
||||||
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
|
import { useVbenModal } from '@vben/common-ui';
|
||||||
|
|
||||||
|
import { ElMessage } from 'element-plus';
|
||||||
|
|
||||||
|
import { useVbenForm } from '#/adapter/form';
|
||||||
|
import {
|
||||||
|
createKnowledgeSegment,
|
||||||
|
getKnowledgeSegment,
|
||||||
|
updateKnowledgeSegment,
|
||||||
|
} from '#/api/ai/knowledge/segment';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
|
import { useFormSchema } from '../data';
|
||||||
|
|
||||||
|
const emit = defineEmits(['success']);
|
||||||
|
const formData = ref<AiKnowledgeSegmentApi.KnowledgeSegment>();
|
||||||
|
const getTitle = computed(() => {
|
||||||
|
return formData.value?.id
|
||||||
|
? $t('ui.actionTitle.edit', ['分段'])
|
||||||
|
: $t('ui.actionTitle.create', ['分段']);
|
||||||
|
});
|
||||||
|
|
||||||
|
const [Form, formApi] = useVbenForm({
|
||||||
|
commonConfig: {
|
||||||
|
componentProps: {
|
||||||
|
class: 'w-full',
|
||||||
|
},
|
||||||
|
formItemClass: 'col-span-2',
|
||||||
|
labelWidth: 140,
|
||||||
|
},
|
||||||
|
layout: 'horizontal',
|
||||||
|
schema: useFormSchema(),
|
||||||
|
showDefaultActions: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const [Modal, modalApi] = useVbenModal({
|
||||||
|
async onConfirm() {
|
||||||
|
const { valid } = await formApi.validate();
|
||||||
|
if (!valid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modalApi.lock();
|
||||||
|
// 提交表单
|
||||||
|
const data =
|
||||||
|
(await formApi.getValues()) as AiKnowledgeSegmentApi.KnowledgeSegment;
|
||||||
|
try {
|
||||||
|
await (formData.value?.id
|
||||||
|
? updateKnowledgeSegment(data)
|
||||||
|
: createKnowledgeSegment(data));
|
||||||
|
// 关闭并提示
|
||||||
|
await modalApi.close();
|
||||||
|
emit('success');
|
||||||
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async onOpenChange(isOpen: boolean) {
|
||||||
|
if (!isOpen) {
|
||||||
|
formData.value = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 加载数据
|
||||||
|
const data = modalApi.getData<AiKnowledgeSegmentApi.KnowledgeSegment>();
|
||||||
|
if (!data || !data.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modalApi.lock();
|
||||||
|
try {
|
||||||
|
formData.value = await getKnowledgeSegment(data.id);
|
||||||
|
// 设置到 values
|
||||||
|
await formApi.setValues(formData.value);
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Modal class="w-2/5" :title="getTitle">
|
||||||
|
<Form class="mx-4" />
|
||||||
|
</Modal>
|
||||||
|
</template>
|
||||||
|
|
||||||
Reference in New Issue
Block a user