feat(knowledge): 优化文档分段功能

- 添加段落编辑和保存功能
- 新增关键词添加和删除功能
- 优化段落显示样式
- 添加分段标识符和重叠长度设置说明
This commit is contained in:
陈昱达
2025-05-13 16:33:41 +08:00
parent e83360aead
commit b08ecfe1fb
6 changed files with 266 additions and 23 deletions

View File

@@ -420,3 +420,11 @@ export function uploadImage(data) {
data data
}) })
} }
export function segmentUpdate(data) {
return request({
url: getUrl(`/datasetDocumentEx/segment/update`),
method: 'post',
data
})
}

View File

@@ -231,3 +231,8 @@ body,
0 0 5px $--color-primary-button-gradient; 0 0 5px $--color-primary-button-gradient;
} }
} }
.resetHtml {
outline: none;
border: none;
}

View File

@@ -36,6 +36,7 @@
:before-close="handleClose" :before-close="handleClose"
> >
<div <div
style="height:calc(100% - 32px);overflow-x: hidden;overflow-y: auto"
v-if=" v-if="
activeSegment !== null && activeSegment !== null &&
descriptions.data && descriptions.data &&
@@ -46,11 +47,15 @@
<div> <div>
<div> <div>
<p>QUESTION</p> <p>QUESTION</p>
<p>{{ descriptions.data[activeSegment].content }}</p> <p contenteditable class="resetHtml" ref="content">
{{ descriptions.data[activeSegment].content }}
</p>
</div> </div>
<div> <div>
<p>ANSWER</p> <p>ANSWER</p>
<p>{{ descriptions.data[activeSegment].answer }}</p> <p contenteditable class="resetHtml" ref="answer">
{{ descriptions.data[activeSegment].answer }}
</p>
</div> </div>
</div> </div>
<div <div
@@ -59,30 +64,48 @@
descriptions.data[activeSegment].keywords && descriptions.data[activeSegment].keywords &&
descriptions.data[activeSegment].keywords.length descriptions.data[activeSegment].keywords.length
" "
style="width: max-content" style="flex-wrap: wrap"
> >
<span>关键词 </span> <span>关键词 </span>
<el-tag <el-tag
v-for="(item, index) in descriptions.data[activeSegment].keywords" v-for="(item, index) in descriptions.data[activeSegment].keywords"
:key="index" :key="index"
class="mr10" class="mr10"
size="mini" size="medium"
type="primary" type="info"
closable
@close="tagClose(item)"
> >
{{ item }} {{ item }}
</el-tag> </el-tag>
<el-input
class="input-new-tag"
v-if="createdTag"
v-model="inputValue"
ref="saveTagInput"
size="small"
@keyup.enter.native="handleInputConfirm"
/>
<el-button
v-else
size="medium"
@click="showInput"
class="fs12"
style="padding: 7px;border-radius: 4px;font-size: 12px"
>添加标签</el-button
>
</div> </div>
</div> </div>
</div> </div>
<span slot="footer" class="dialog-footer"> <div class="dialog-footer text-right">
<el-button @click="dialogVisible = false" size="medium" <el-button @click="saveUS" size="medium" type="primary">保存</el-button>
>关 闭</el-button </div>
>
</span>
</el-drawer> </el-drawer>
</div> </div>
</template> </template>
<script> <script>
import { segmentUpdate } from '@/api/generatedApi'
export default { export default {
name: 'QAModel', name: 'QAModel',
props: { props: {
@@ -90,15 +113,85 @@ export default {
descriptions: { descriptions: {
type: Object, type: Object,
default: () => ({ data: [] }) default: () => ({ data: [] })
},
parentForm: {
type: Object,
default: () => ({})
} }
}, },
data() { data() {
return { return {
createdTag: false,
inputValue: '',
activeSegment: null, activeSegment: null,
dialogVisible: false dialogVisible: false
} }
}, },
methods: { methods: {
saveUS() {
let params = {
keywords: this.descriptions.data[this.activeSegment].keywords,
content: this.$refs.content.innerHTML,
answer: this.$refs.answer.innerHTML
}
this.saveSegment(params, () => {
this.$message.success('保存成功')
})
},
showInput() {
this.createdTag = true
},
tagClose() {
this.descriptions.data[this.activeSegment].keywords.splice(
this.descriptions.data[this.activeSegment].keywords.indexOf(
this.inputValue
),
1
)
let params = {
keywords: this.descriptions.data[this.activeSegment].keywords,
content: this.$refs.content.innerHTML,
answer: this.$refs.answer.innerHTML
}
this.saveSegment(params)
},
handleInputConfirm() {
let params = {
keywords: [
...this.descriptions.data[this.activeSegment].keywords,
this.inputValue
],
content: this.$refs.content.innerHTML,
answer: this.$refs.answer.innerHTML
}
this.saveSegment(params)
},
saveSegment(params, fun) {
params.documentId = this.parentForm.id
params.segmentId = this.descriptions.data[this.activeSegment].id
segmentUpdate(params).then(res => {
if (res) {
this.descriptions.data[this.activeSegment] = JSON.parse(
JSON.stringify(res.content.content)
)
// res.content.content.keywords
// this.descriptions.data[this.activeSegment].content =
// res.content.content.content
// this.descriptions.data[this.activeSegment].answer =
// res.content.content.answer
this.createdTag = false
this.inputValue = ''
if (fun) {
fun()
}
}
})
},
handleSegmentClick(index) { handleSegmentClick(index) {
this.activeSegment = index this.activeSegment = index
this.dialogVisible = true this.dialogVisible = true
@@ -164,4 +257,7 @@ export default {
line-height: 35px; line-height: 35px;
color: #666; color: #666;
} }
.input-new-tag {
width: 100px;
}
</style> </style>

View File

@@ -20,7 +20,7 @@
<span> {{ 0 }} 次召回次数</span> <span> {{ 0 }} 次召回次数</span>
<!-- </el-checkbox>--> <!-- </el-checkbox>-->
<div> <div>
<p class="context">{{ segment.content.slice(0, 20) + '.....' }}</p> <p class="context">{{ segment.content }}</p>
<div <div
class="segment-keywords flex" class="segment-keywords flex"
v-if="segment.keywords && segment.keywords.length" v-if="segment.keywords && segment.keywords.length"
@@ -47,6 +47,7 @@
:before-close="handleClose" :before-close="handleClose"
> >
<div <div
style="height:calc(100% - 32px);overflow-x: hidden;overflow-y: auto"
v-if=" v-if="
activeSegment !== null && activeSegment !== null &&
descriptions.data && descriptions.data &&
@@ -54,7 +55,9 @@
" "
> >
<div class="segment-content"> <div class="segment-content">
{{ descriptions.data[activeSegment].content }} <p contenteditable class="resetHtml" ref="content">
{{ descriptions.data[activeSegment].content }}
</p>
<div <div
class="flex align-items-c mt20" class="flex align-items-c mt20"
v-if=" v-if="
@@ -68,23 +71,41 @@
v-for="(item, index) in descriptions.data[activeSegment].keywords" v-for="(item, index) in descriptions.data[activeSegment].keywords"
:key="index" :key="index"
class="mr10 ellipsis" class="mr10 ellipsis"
size="mini" size="medium"
type="primary" closable
type="info"
@close="tagClose(item)"
> >
{{ item }} {{ item }}
</el-tag> </el-tag>
<el-input
class="input-new-tag"
v-if="createdTag"
v-model="inputValue"
ref="saveTagInput"
size="small"
@keyup.enter.native="handleInputConfirm"
/>
<el-button
v-else
size="medium"
@click="showInput"
class="fs12"
style="padding: 7px;border-radius: 4px;font-size: 12px"
>添加标签</el-button
>
</div> </div>
</div> </div>
</div> </div>
<span slot="footer" class="dialog-footer"> <div class="text-right">
<el-button @click="dialogVisible = false" size="medium" <el-button @click="saveUS" size="medium" type="primary">保存</el-button>
>关 闭</el-button </div>
>
</span>
</el-drawer> </el-drawer>
</div> </div>
</template> </template>
<script> <script>
import { segmentUpdate } from '@/api/generatedApi'
export default { export default {
name: 'TextModel', name: 'TextModel',
props: { props: {
@@ -92,16 +113,86 @@ export default {
descriptions: { descriptions: {
type: Object, type: Object,
default: () => ({ data: [] }) default: () => ({ data: [] })
},
parentForm: {
type: Object,
default: () => ({})
} }
}, },
data() { data() {
return { return {
createdTag: false,
inputValue: '',
activeSegment: null, activeSegment: null,
dialogVisible: false, dialogVisible: false,
selectedSegments: [] selectedSegments: []
} }
}, },
methods: { methods: {
saveUS() {
let params = {
keywords: this.descriptions.data[this.activeSegment].keywords,
content: this.$refs.content.innerHTML,
answer: null
}
this.saveSegment(params, () => {
this.$message.success('保存成功')
})
},
showInput() {
this.createdTag = true
},
tagClose() {
this.descriptions.data[this.activeSegment].keywords.splice(
this.descriptions.data[this.activeSegment].keywords.indexOf(
this.inputValue
),
1
)
let params = {
keywords: this.descriptions.data[this.activeSegment].keywords,
content: this.$refs.content.innerHTML,
answer: null
}
this.saveSegment(params)
},
handleInputConfirm() {
let params = {
keywords: [
...this.descriptions.data[this.activeSegment].keywords,
this.inputValue
],
content: this.$refs.content.innerHTML,
answer: null
}
this.saveSegment(params)
},
saveSegment(params, fun) {
params.documentId = this.parentForm.id
params.segmentId = this.descriptions.data[this.activeSegment].id
segmentUpdate(params).then(res => {
if (res) {
this.descriptions.data[this.activeSegment] = JSON.parse(
JSON.stringify(res.content.content)
)
// res.content.content.keywords
// this.descriptions.data[this.activeSegment].content =
// res.content.content.content
// this.descriptions.data[this.activeSegment].answer =
// res.content.content.answer
this.createdTag = false
this.inputValue = ''
if (fun) {
fun()
}
}
})
},
handleSegmentClick(index) { handleSegmentClick(index) {
this.activeSegment = index this.activeSegment = index
this.dialogVisible = true this.dialogVisible = true
@@ -117,6 +208,12 @@ export default {
display: block; display: block;
} }
.context { .context {
width: auto;
overflow: hidden;
//省略号
text-overflow: ellipsis;
white-space: nowrap;
color: #3a3f4f; color: #3a3f4f;
font-size: 14px; font-size: 14px;
} }
@@ -172,4 +269,7 @@ export default {
line-height: 35px; line-height: 35px;
color: #666; color: #666;
} }
.input-new-tag {
width: 100px;
}
</style> </style>

View File

@@ -29,7 +29,25 @@
<div class="body-content"> <div class="body-content">
<el-form inline label-position="top" :model="form"> <el-form inline label-position="top" :model="form">
<el-form-item label="分段标识符" prop="separator"> <el-form-item label="分段标识符" prop="separator">
<el-input size="mini" v-model="form.separator"></el-input> <template slot="label">
分段标识符
<el-popover
placement="top"
width="400"
trigger="hover"
content="分隔符是用于分隔文本的字符。\n\n和 \n 是常用于分隔段落和行的分隔符。用逗号连接分隔符(n\n,\n),当段落超过最大块长度时,会按行进行分割。你也可以使用自定义的特殊分隔符(例如 ***)。"
>
<el-icon
class="el-icon-question"
slot="reference"
></el-icon>
</el-popover>
</template>
<el-input
size="mini"
v-model="form.separator"
placeholder="\n\n用于分段\n用于分行"
></el-input>
</el-form-item> </el-form-item>
<el-form-item label="分段最大长度(tokens)" prop="maxTokens"> <el-form-item label="分段最大长度(tokens)" prop="maxTokens">
<el-input-number <el-input-number
@@ -43,6 +61,21 @@
></el-input-number> ></el-input-number>
</el-form-item> </el-form-item>
<el-form-item label="分段重叠长度(tokens)" prop="chunkOverlap"> <el-form-item label="分段重叠长度(tokens)" prop="chunkOverlap">
<template slot="label">
分段重叠长度(tokens)
<el-popover
placement="top"
width="400"
trigger="hover"
content="设置分段之间的重叠长度可以保留分段之间的语义关系提升召回效果。建议设置为最大分段长度的1096-25%。"
>
<el-icon
class="el-icon-question"
slot="reference"
></el-icon>
</el-popover>
</template>
<el-input-number <el-input-number
style="width: 100%" style="width: 100%"
controls-position="right" controls-position="right"
@@ -209,7 +242,7 @@ export default {
mode: 'custom', mode: 'custom',
removeExtraSpaces: true, removeExtraSpaces: true,
removeUrlsEmails: false, removeUrlsEmails: false,
separator: '###', separator: '\\n\\n',
maxTokens: 1000, maxTokens: 1000,
chunkOverlap: 50 chunkOverlap: 50
}, },
@@ -218,7 +251,7 @@ export default {
mode: 'custom', mode: 'custom',
removeExtraSpaces: true, removeExtraSpaces: true,
removeUrlsEmails: false, removeUrlsEmails: false,
separator: '###', separator: '\\n\\n',
maxTokens: 1000, maxTokens: 1000,
chunkOverlap: 50 chunkOverlap: 50
}, },
@@ -253,7 +286,6 @@ export default {
api = extractSegmentEstimate api = extractSegmentEstimate
break break
} }
console.log(api)
api({ api({
documentId, documentId,
segmentConfig: { segmentConfig: {

View File

@@ -168,10 +168,12 @@ export default {
<text-model <text-model
v-if="descriptions.doc_form === 'text_model'" v-if="descriptions.doc_form === 'text_model'"
:descriptions="descriptions" :descriptions="descriptions"
:parentForm="form"
/> />
<q-a-model <q-a-model
v-else-if="descriptions.doc_form === 'qa_model'" v-else-if="descriptions.doc_form === 'qa_model'"
:descriptions="descriptions" :descriptions="descriptions"
:parentForm="form"
/> />
</div> </div>
<div v-else class="p20 flex align-items-c"> <div v-else class="p20 flex align-items-c">