yangyin
2024-09-06 cf5c3cde7d0dbec44d4c2f7bea87c4d5362accb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<template>
    <div class="flex flex-col h100">
        <div class="mb-[10px] flex flex-shrink-0 items-center">
            <el-button style="margin-left: 8px; width: 40px" link @click="handleExitFlow">
                <el-icon style="font-size: 24px !important">
                    <ArrowLeft />
                </el-icon>
            </el-button>
            <span class="text-[24px] text-[#26244c] font-[700]">{{ state.detailTitle }}</span>
        </div>
        <div class="set-form-height" v-show="!state.showKnowledgeForm">
            <el-form
                :model="state.knowledgeForm"
                label-width="120px"
                label-position="left"
                ref="knowledgeFormRef"
                :rules="knowledgeFormRules"
            >
                <el-divider content-position="left"><span class="text-[18px] font-[500]">知识库基础信息</span></el-divider>
                <el-form-item label="知识库名称:" prop="title">
                    <el-input
                        v-model="state.knowledgeForm.title"
                        style="width: 532px"
                        maxlength="20"
                        placeholder="请输入知识库名称"
                        show-word-limit
                        type="text"
                    />
                </el-form-item>
                <el-form-item label="知识库描述:" prop="prompt">
                    <el-input
                        v-model="state.knowledgeForm.prompt"
                        maxlength="1000"
                        style="width: 532px"
                        placeholder="请输入知识库描述。对知识库包含的内容和数据的用途进行描述。"
                        show-word-limit
                        :rows="5"
                        type="textarea"
                    />
                </el-form-item>
                <el-form-item label="数据类型:">
                    <div v-for="(item, index) in state.dataTypeList" :key="index">
                        <div
                            class="bg-[#fff] border-[1px] border-solid border-[#d8d9e6] py-[12px] w-[215px] mr-[10px] px-[16px] rounded-lg cursor-pointer"
                            :class="{ activeColor: state.knowledgeForm.dataType === item.ID }"
                            @click="activeDataType(item.ID)"
                        >
                            <div class="flex items-center">
                                <div class="data_left">
                                    <img :src="item.ImageURL" class="w-[40px] h-[40px] mr-[10px]" />
                                </div>
                                <div class="data_right">
                                    <el-radio v-model="state.knowledgeForm.dataType" size="large" :label="item.ID">
                                        <span class="font-[500] text[16px]">{{ item.Name }}</span>
                                    </el-radio>
 
                                    <el-tooltip :content="item.DemoDesc" placement="top" effect="light" popper-class="set_tooltip_demo">
                                        <div class="text-[#878aab] text-[12px] mx-0 mt-[2px] mb-0 set-desc">
                                            {{ item.DemoDesc }}
                                        </div>
                                    </el-tooltip>
                                </div>
                            </div>
                        </div>
                    </div>
                </el-form-item>
                <el-divider content-position="left"><span class="text-[18px]">知识库配置</span></el-divider>
                <el-form-item label="配置模式:">
                    <div class="flex-auto flex flex-col">
                        <div class="flex">
                            <div v-for="(item, index) in state.configurationList" :key="index">
                                <div
                                    class="bg-[#fff] border-[1px] border-solid border-[#d8d9e6] py-[12px] w-[215px] mr-[10px] px-[16px] rounded-lg cursor-pointer"
                                    :class="{ activeColor: state.knowledgeForm.configurationMode === item.ID }"
                                    @click="activeConfigurationType(item.ID)"
                                >
                                    <div class="flex items-center">
                                        <div class="data_left">
                                            <img :src="item.ImageURL" class="w-[40px] h-[40px] mr-[10px]" />
                                        </div>
                                        <div class="data_right">
                                            <el-radio v-model="state.knowledgeForm.configurationMode" size="large" :label="item.ID">
                                                <span class="font-[500] text[16px]">{{ item.Name }}</span>
                                            </el-radio>
 
                                            <el-tooltip :content="item.DemoDesc" placement="top" effect="light" popper-class="set_tooltip_demo">
                                                <div class="text-[#878aab] text-[12px] mx-0 mt-[2px] mb-0 set-desc">
                                                    {{ item.DemoDesc }}
                                                </div>
                                            </el-tooltip>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- <div class="set_model_desc">
                            <div v-for="(item, index) in state.modeList" :key="index">
                                <div class="flex items-center mb-[16px]">
                                    <div class="relative w-[148px] text-[14px] text-[#26244c]">{{ item.Name }}</div>
                                    <div class="text-[14px] text-[#26244c]" :class="`min-w-[${item.NameFnWidthStyle}]`">{{ item.NameFn }}</div>
                                    <div v-show="item.ID == 3">
                                        <div class="min-w-[148px] text-[14px] text-[#26244c]">{{ item.NameOrder }}</div>
                                    </div>
                                </div>
                            </div>
                        </div> -->
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <div class="set-form-height" v-show="state.showKnowledgeForm">
            <el-form
                :model="state.knowledgeFile"
                label-width="120px"
                label-position="left"
                class="h100"
                ref="knowledgeFileRef"
                :rules="knowledgeFormRules"
            >
                <el-divider content-position="left"><span class="text-[18px] font-[500]">选择文件</span></el-divider>
                <div class="flex items-start gap-[16px] max-h-[686px] min-h-[360px] set-file-height">
                    <div class="min-h-[360px] h100 set_file_left">
                        <div class="font-[500] mb-2">请选择文档</div>
                        <div class="left_content">
                            <div class="file_menu">
                                <LeftTreeByMgr
                                    title-name="类目"
                                    ref="leftTreeRef"
                                    :treedata="state.knowledgeBaseData"
                                    :current-node-key="currentListID"
                                    :defaultProps="{
                                        children: 'Children',
                                        label: 'title',
                                        id: 'id',
                                    }"
                                    @click="handleClickNode"
                                >
                                </LeftTreeByMgr>
                            </div>
                            <div class="file_table">
                                <el-table
                                    :data="state.fileData"
                                    border
                                    @select="handleSelectItem"
                                    highlight-current-row
                                    ref="multipleTableRef"
                                    @select-all="
                                        (selection) => {
                                            handleSelectAll(selection, state.fileData);
                                        }
                                    "
                                >
                                    <el-table-column type="selection" width="55" />
                                    <el-table-column prop="name" label="文件名称" />
                                </el-table>
                            </div>
                        </div>
                    </div>
                    <div class="set_file_right">
                        <div class="h100">
                            <div class="font-[500] mb-[12px]">已选文档{{ multipleSelection.length }}/50个</div>
                            <div class="selected_file">
                                <div v-for="(item, index) in multipleSelection" :key="index" class="set_file_item">
                                    <span class="set_file_name">{{ item.name }}</span>
                                    <span class="ywifont ywicon-guanbi set_file_close" @click="handleRemoveItem(item)"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <el-divider content-position="left"><span class="text-[18px]">数据处理</span></el-divider>
                <el-form-item label="文档切分chunk:">
                    <div class="flex-auto flex flex-col">
                        <div class="flex">
                            <div v-for="(item, index) in state.segmentationList" :key="index">
                                <div
                                    class="bg-[#fff] border-[1px] border-solid border-[#d8d9e6] py-[12px] w-[215px] mr-[10px] px-[16px] rounded-lg cursor-pointer"
                                    :class="{ activeColor: state.knowledgeForm.segmentationMode === item.ID }"
                                    @click="activeDataProcessType(item.ID)"
                                >
                                    <div class="flex items-center">
                                        <div class="data_left">
                                            <img :src="item.ImageURL" class="w-[40px] h-[40px] mr-[10px]" />
                                        </div>
                                        <div class="data_right">
                                            <el-radio v-model="state.knowledgeForm.segmentationMode" size="large" :label="item.ID">
                                                <span class="font-[500] text[16px]">{{ item.Name }}</span>
                                            </el-radio>
 
                                            <el-tooltip :content="item.DemoDesc" placement="top" effect="light" popper-class="set_tooltip_demo">
                                                <div class="text-[#878aab] text-[12px] mx-0 mt-[2px] mb-0 set-desc">
                                                    {{ item.DemoDesc }}
                                                </div>
                                            </el-tooltip>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <div class="set-form-footer">
            <div v-show="!state.showKnowledgeForm">
                <el-button type="primary" @click="nextKnowledge">下一步</el-button>
            </div>
            <div v-show="state.showKnowledgeForm">
                <el-button @click="importCompleted" type="primary">导入完成</el-button>
                <el-button @click="backKnowledge" v-show="state.showKnowledgeForm">上一步</el-button>
            </div>
            <el-button @click="handleExitFlow">取消</el-button>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import { ElMessage, type FormRules } from 'element-plus';
import moment from 'moment';
import { computed, nextTick, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { add_docvector_file, add_docvector_name } from '/@/api/knowledge/docvector';
import { get_knowledge_group_list, list_knowledge_file } from '/@/api/knowledge/group';
import LeftTreeByMgr from '/@/components/tree/leftTreeByMgr.vue';
import mittBus from '/@/utils/mitt';
import { convertListToTree } from '/@/utils/util';
// 定义变量内容
const state = reactive({
    soliderValue: 0,
    detailTitle: '创建知识库',
    knowledgeForm: {
        title: '',
        prompt: '',
        dataType: 1,
        configurationMode: 1,
        segmentationMode: 1,
    },
    knowledgeFile: {},
    dataTypeList: [
        {
            ID: 1,
            Name: '非结构化数据',
            ImageURL: 'static/images/knowledge/data_type_1.png',
            DemoDesc: '选择数据管理功能中已上传的doc、pdf、md、txt等文件',
        },
        {
            ID: 2,
            Name: '结构化数据',
            ImageURL: 'static/images/knowledge/data_type_2.png',
            DemoDesc: '选择数据管理功能中已创建的数据表',
        },
    ], //数据类型
    configurationList: [
        {
            ID: 1,
            Name: '推荐配置',
            ImageURL: 'static/images/knowledge/data_type_3.png',
            DemoDesc: '百炼推荐配置,在效果、推理成本、检索时延等方面的最佳实践',
        },
        {
            ID: 3,
            Name: '自定义',
            ImageURL: 'static/images/knowledge/data_type_4.png',
            DemoDesc: '完全开放的离线知识库配置,按照检索需求自由配置,获得推理效果和时延方面的不同体验',
        },
    ], //配置模式
    segmentationList: [
        {
            ID: 1,
            Name: '智能切分',
            ImageURL: 'static/images/knowledge/data_type_3.png',
            DemoDesc: '在通用文档上的最优chunk切分方法,经过评测可在多数文档上获得最佳的检索效果',
        },
        {
            ID: 3,
            Name: '自定义切分',
            ImageURL: 'static/images/knowledge/data_type_4.png',
            DemoDesc: '完全开放的chunk切分配置,按照实际文档情况自由配置,通过调试获得更好的检索效果',
        },
    ],
    modeList: [
        {
            ID: 1,
            Name: '多轮对话改写',
            NameFn: '开启',
            NameOrder: null,
            NameFnWidthStyle: '380px',
        },
        {
            ID: 2,
            Name: 'Embedding模型',
            NameFn: '官方向量',
            NameOrder: null,
            NameFnWidthStyle: '380px',
        },
        {
            ID: 3,
            Name: '排序配置',
            NameFn: 'Rank模型',
            NameOrder: '官方排序',
            NameFnWidthStyle: '148px',
        },
        {
            ID: 4,
            Name: '',
            NameFn: '相似度阈值',
            NameOrder: null,
            NameFnWidthStyle: '148px',
        },
    ],
    showKnowledgeForm: false,
    knowledgeBaseData: [], //知识库类目
    fileData: [], //文件数据源
    isCreateIndex: false, //是否调用创建的索引的字段
    knowlg_id: '',
});
const knowledgeFormRules = reactive<FormRules>({
    title: [
        {
            required: true,
            message: '必填项',
            trigger: 'blur',
        },
    ],
    prompt: [
        {
            required: true,
            message: '必填项',
            trigger: 'blur',
        },
    ],
});
const knowledgeFormRef = ref();
const router = useRouter();
 
//#region ====================== 知识库表单操作 ======================
//返回
const handleExitFlow = () => {
    //是否显示返回
    router.back();
    //清空表单
    knowledgeFormRef.value.resetFields();
    state.showKnowledgeForm = false;
};
//选择数据类型
const activeDataType = (id: number) => {
    state.knowledgeForm.dataType = id;
};
//选择配置模式
const activeConfigurationType = (id: number) => {
    state.knowledgeForm.configurationMode = id;
};
//选择数据文档切分
const activeDataProcessType = (id: number) => {
    state.knowledgeForm.segmentationMode = id;
};
//下一步
const nextKnowledge = async () => {
    const valid = await knowledgeFormRef.value.validate().catch(() => {});
    if (!valid) return;
    state.showKnowledgeForm = true;
    if (!state.isCreateIndex) {
        emptyKnowledgeBase();
        state.isCreateIndex = true;
    }
};
const backKnowledge = () => {
    state.showKnowledgeForm = false;
};
//#endregion
//#region ====================== 空知识库的创建 ======================
 
//创建一个空知识库
const emptyKnowledgeBase = async () => {
    const valid = await knowledgeFormRef.value.validate().catch(() => {});
    if (!valid) return;
    const res = await add_docvector_name({ title: state.knowledgeForm.title, prompt: state.knowledgeForm.prompt });
    if (res.json_ok) {
        state.knowlg_id = res.knowlg_id;
        getFileTreeData(true);
    }
};
//导入完成
const importCompleted = async () => {
    var currentTime = moment().format('YYYY-MM-DD HH:mm:ss');
    // 上传文件
    if (multipleSelection.value.length == 0) return ElMessage.warning('请先选择文件');
    const file_id = multipleSelection.value.map((v) => v.id).join(',');
    const res = await add_docvector_file({
        knowlg_id: state.knowlg_id,
        file_id: file_id,
    });
    if (res.json_ok) {
        let obj = {
            id: res.knowlg_id,
            title: state.knowledgeForm.title,
            prompt: state.knowledgeForm.prompt,
            publish: '',
            create_time: currentTime,
            user_name: '',
        };
        mittBus.emit('addKnowledgeBaseObj', obj);
        router.push({ name: 'Knowledge' });
        //清空表单
        knowledgeFormRef.value.resetFields();
        state.showKnowledgeForm = false;
        state.isCreateIndex = false;
    }
};
//#endregion
//#region ====================== 知识库文件上传 ==========
const treeLoading = ref(false);
const currentTreeNode = ref(null);
const leftTreeRef = ref(null);
const currentListID = computed(() => currentTreeNode.value?.id);
//获取文件列表
const getFileTreeData = async (selectFirst = false) => {
    treeLoading.value = true;
    const res = await get_knowledge_group_list().finally(() => {
        treeLoading.value = false;
    });
    if (res?.json_ok) {
        const resData = (res.values || []) as [];
        state.knowledgeBaseData = convertListToTree(resData, {
            ID: 'id',
            Children: 'Children',
            ParentID: 'parent',
        });
        if (selectFirst) {
            const firstListTreeNode = state.knowledgeBaseData[0];
            if (firstListTreeNode) {
                handleClickNode(firstListTreeNode);
            } else {
                state.fileData = [];
                multipleSelection.value = [];
            }
        } else {
            currentTreeNode.value && handleClickNode(currentTreeNode.value);
        }
    }
};
const handleClickNode = (data: any) => {
    multipleSelection.value = [];
    nextTick(() => {
        leftTreeRef.value?.treeRef.setCurrentKey(data.id);
    });
    currentTreeNode.value = data;
    getFileTableData();
};
//获取文件表格列表
const getFileTableData = async () => {
    const res = await list_knowledge_file({
        group_id: currentListID.value,
    }).finally(() => {});
    if (res?.json_ok) {
        const resData = (res.values || []) as [];
        state.fileData = resData;
    } else {
        ElMessage.error('获取文档列表失败' + (res?.json_msg ? `,${JSON.stringify(res.json_msg)}` : ''));
    }
};
let multipleSelection = ref([]);
const multipleTableRef = ref(null);
//表格单选和多选
const handleSelectAll = (selection: any[], pageSelectionData: any[]) => {
    let checked = selection.length ? true : false; // selection为空数组时代表取消全选
    if (checked) {
        let mIds = multipleSelection.value.map((v) => v.id);
        let filterData = pageSelectionData.filter((v) => {
            // 筛选出非重复项
            return !mIds.includes(v.id);
        });
        multipleSelection.value = multipleSelection.value.concat(filterData);
    } else {
        let pIds = pageSelectionData.map((v) => v.id);
        multipleSelection.value = multipleSelection.value.filter((v) => !pIds.includes(v.id));
    }
};
 
const handleSelectItem = (selection: any[], row) => {
    multipleSelection.value = selection;
};
// 移除已选文件
const handleRemoveItem = (item) => {
    multipleSelection.value = multipleSelection.value.filter((v) => v.id !== item.id);
    multipleTableRef.value.toggleRowSelection(item, false);
};
//#endregion
</script>
<style scoped lang="scss">
.set-form-height {
    background: #fff;
    border-radius: 12px 12px 0 0;
    flex: 1;
    margin: 0 24px;
    overflow-y: auto;
    padding: 20px 16px;
    height: 100%;
}
.set-desc {
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
    line-height: 1.5714285714285714;
    font-family: PingFangSC;
    box-sizing: border-box;
}
.set-form-footer {
    align-items: center;
    background: #fff;
    bottom: 0;
    box-shadow: 4px 0 5px 1px rgba(16, 9, 65, 0.06);
    display: flex;
    flex-shrink: 0;
    gap: 8px;
    height: 64px;
    padding-left: 24px;
    // position: fixed;
    width: 100%;
    left: 220px;
}
 
.activeColor {
    border-color: #0062be;
    background-color: #f6f5ff;
}
.set_model_desc {
    background: #f7f8fe;
    border-radius: 8px;
    margin-top: 12px;
    padding: 16px 24px;
}
.set-file-height {
    height: calc(100% - 48px);
}
.set_file_left {
    border: 1px solid #e7e8ee;
    border-radius: 12px;
    flex: 1;
    padding: 12px 16px 16px;
    box-sizing: border-box;
    .left_content {
        align-items: flex-start;
        display: flex;
        gap: 12px;
        height: calc(100% - 29px);
        .file_menu {
            flex-shrink: 0;
            height: 100%;
            overflow-y: auto;
            border: 1px solid #e7e8ee;
            border-radius: 6px;
            max-height: 627px;
            padding: 20px;
            width: 280px;
        }
        .file_table {
            flex-shrink: 0;
            height: 100%;
            overflow-y: auto;
            border-radius: 8px 8px 0 0;
            flex: 1;
        }
    }
}
.set_file_right {
    border: 1px solid #e7e8ee;
    border-radius: 6px;
    flex-shrink: 0;
    height: 100%;
    padding: 12px 16px 16px;
    width: 417px;
    min-height: 360px;
}
.selected_file {
    max-height: calc(100% - 36px);
    overflow-y: auto;
    box-sizing: border-box;
    .set_file_item {
        align-items: center;
        background: linear-gradient(0deg, #f7f8fe, #f7f8fe), #fff;
        border-radius: 8px;
        display: flex;
        font-size: 12px;
        gap: 8px;
        height: 40px;
        justify-content: space-between;
        padding: 0 16px;
        margin-bottom: 9px;
        .set_file_name {
            max-width: calc(100% - 22px);
        }
        .set_file_close {
            color: #878aab;
            cursor: pointer;
            flex-shrink: 0;
            font-size: 14px;
            align-items: center;
            display: inline-flex;
            justify-content: center;
        }
    }
}
</style>
<style>
.set_tooltip_demo {
    max-width: 326px;
    max-height: 150px;
    min-width: 32px;
    min-height: 32px;
    padding: 6px 8px;
    color: #26244c;
    text-align: start;
    text-decoration: none;
    word-wrap: break-word;
    background-color: #fff;
    border-radius: 6px;
    box-shadow: 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
}
</style>