feat: Implement delete functionality for generated questions in knowledge base, including API and UI updates
This commit is contained in:
@@ -101,6 +101,11 @@ export function getChunkByIdOnly(chunkId: string) {
|
|||||||
return get(`/api/v1/chunks/by-id/${chunkId}`);
|
return get(`/api/v1/chunks/by-id/${chunkId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete a single generated question from a chunk by question ID
|
||||||
|
export function deleteGeneratedQuestion(chunkId: string, questionId: string) {
|
||||||
|
return del(`/api/v1/chunks/by-id/${chunkId}/questions`, { question_id: questionId });
|
||||||
|
}
|
||||||
|
|
||||||
export function listKnowledgeTags(
|
export function listKnowledgeTags(
|
||||||
kbId: string,
|
kbId: string,
|
||||||
params?: { page?: number; page_size?: number; keyword?: string },
|
params?: { page?: number; page_size?: number; keyword?: string },
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { marked } from "marked";
|
import { marked } from "marked";
|
||||||
import { onMounted, ref, nextTick, onUnmounted, onUpdated, watch } from "vue";
|
import { onMounted, ref, nextTick, onUnmounted, onUpdated, watch } from "vue";
|
||||||
import { downKnowledgeDetails } from "@/api/knowledge-base/index";
|
import { downKnowledgeDetails, deleteGeneratedQuestion } from "@/api/knowledge-base/index";
|
||||||
import { MessagePlugin } from "tdesign-vue-next";
|
import { MessagePlugin, DialogPlugin } from "tdesign-vue-next";
|
||||||
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
|
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ renderer.image = function (href, title, text) {
|
|||||||
</figure>`;
|
</figure>`;
|
||||||
};
|
};
|
||||||
const props = defineProps(["visible", "details", "knowledgeType", "sourceInfo"]);
|
const props = defineProps(["visible", "details", "knowledgeType", "sourceInfo"]);
|
||||||
const emit = defineEmits(["closeDoc", "getDoc"]);
|
const emit = defineEmits(["closeDoc", "getDoc", "questionDeleted"]);
|
||||||
watch(() => props.details.md, (newVal) => {
|
watch(() => props.details.md, (newVal) => {
|
||||||
nextTick(async () => {
|
nextTick(async () => {
|
||||||
const images = mdContentWrap.value.querySelectorAll('img.markdown-image');
|
const images = mdContentWrap.value.querySelectorAll('img.markdown-image');
|
||||||
@@ -185,12 +185,26 @@ const getChunkMeta = (item: any) => {
|
|||||||
return parts.join(' · ');
|
return parts.join(' · ');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 生成的问题类型
|
||||||
|
interface GeneratedQuestion {
|
||||||
|
id: string;
|
||||||
|
question: string;
|
||||||
|
}
|
||||||
|
|
||||||
// 解析生成的问题
|
// 解析生成的问题
|
||||||
const getGeneratedQuestions = (item: any): string[] => {
|
const getGeneratedQuestions = (item: any): GeneratedQuestion[] => {
|
||||||
if (!item || !item.metadata) return [];
|
if (!item || !item.metadata) return [];
|
||||||
try {
|
try {
|
||||||
const metadata = typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata;
|
const metadata = typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata;
|
||||||
return metadata.generated_questions || [];
|
const questions = metadata.generated_questions || [];
|
||||||
|
// 兼容旧格式(字符串数组)和新格式(对象数组)
|
||||||
|
return questions.map((q: string | GeneratedQuestion, index: number) => {
|
||||||
|
if (typeof q === 'string') {
|
||||||
|
// 旧格式:字符串,生成临时ID
|
||||||
|
return { id: `legacy-${index}`, question: q };
|
||||||
|
}
|
||||||
|
return q;
|
||||||
|
});
|
||||||
} catch {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -211,6 +225,63 @@ const toggleQuestions = (index: number) => {
|
|||||||
|
|
||||||
const isExpanded = (index: number) => expandedChunks.value.has(index);
|
const isExpanded = (index: number) => expandedChunks.value.has(index);
|
||||||
|
|
||||||
|
// 删除中的状态
|
||||||
|
const deletingQuestion = ref<{ chunkIndex: number; questionId: string } | null>(null);
|
||||||
|
|
||||||
|
// 删除生成的问题
|
||||||
|
const handleDeleteQuestion = async (item: any, chunkIndex: number, question: GeneratedQuestion) => {
|
||||||
|
if (!item || !item.id) {
|
||||||
|
MessagePlugin.error(t('common.error') || '操作失败');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否是旧格式数据(无法删除)
|
||||||
|
if (question.id.startsWith('legacy-')) {
|
||||||
|
MessagePlugin.warning(t('knowledgeBase.legacyQuestionCannotDelete') || '旧格式问题无法删除,请重新生成问题');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const confirmDialog = DialogPlugin.confirm({
|
||||||
|
header: t('common.confirmDelete') || '确认删除',
|
||||||
|
body: t('knowledgeBase.confirmDeleteQuestion') || '确定要删除这个问题吗?删除后将同时移除对应的向量索引。',
|
||||||
|
confirmBtn: t('common.confirm') || '确认',
|
||||||
|
cancelBtn: t('common.cancel') || '取消',
|
||||||
|
onConfirm: async () => {
|
||||||
|
confirmDialog.hide();
|
||||||
|
deletingQuestion.value = { chunkIndex, questionId: question.id };
|
||||||
|
try {
|
||||||
|
await deleteGeneratedQuestion(item.id, question.id);
|
||||||
|
MessagePlugin.success(t('common.deleteSuccess') || '删除成功');
|
||||||
|
|
||||||
|
// 更新本地数据
|
||||||
|
const metadata = typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata;
|
||||||
|
if (metadata && metadata.generated_questions) {
|
||||||
|
const idx = metadata.generated_questions.findIndex((q: GeneratedQuestion) => q.id === question.id);
|
||||||
|
if (idx > -1) {
|
||||||
|
metadata.generated_questions.splice(idx, 1);
|
||||||
|
}
|
||||||
|
item.metadata = typeof item.metadata === 'string' ? JSON.stringify(metadata) : metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通知父组件刷新数据
|
||||||
|
emit('questionDeleted', { chunkId: item.id, questionId: question.id });
|
||||||
|
} catch (error: any) {
|
||||||
|
MessagePlugin.error(error?.message || t('common.deleteFailed') || '删除失败');
|
||||||
|
} finally {
|
||||||
|
deletingQuestion.value = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onClose: () => {
|
||||||
|
confirmDialog.hide();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 检查是否正在删除某个问题
|
||||||
|
const isDeleting = (chunkIndex: number, questionId: string) => {
|
||||||
|
return deletingQuestion.value?.chunkIndex === chunkIndex && deletingQuestion.value?.questionId === questionId;
|
||||||
|
};
|
||||||
|
|
||||||
const downloadFile = () => {
|
const downloadFile = () => {
|
||||||
downKnowledgeDetails(props.details.id)
|
downKnowledgeDetails(props.details.id)
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
@@ -327,12 +398,24 @@ const handleDetailsScroll = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div v-show="isExpanded(index)" class="questions-list">
|
<div v-show="isExpanded(index)" class="questions-list">
|
||||||
<div
|
<div
|
||||||
v-for="(question, qIndex) in getGeneratedQuestions(item)"
|
v-for="question in getGeneratedQuestions(item)"
|
||||||
:key="qIndex"
|
:key="question.id"
|
||||||
class="question-item"
|
class="question-item"
|
||||||
>
|
>
|
||||||
<t-icon name="help-circle" size="14px" class="question-icon" />
|
<t-icon name="help-circle" size="14px" class="question-icon" />
|
||||||
<span>{{ question }}</span>
|
<span class="question-text">{{ question.question }}</span>
|
||||||
|
<t-button
|
||||||
|
theme="default"
|
||||||
|
variant="text"
|
||||||
|
size="small"
|
||||||
|
class="delete-question-btn"
|
||||||
|
:loading="isDeleting(index, question.id)"
|
||||||
|
@click.stop="handleDeleteQuestion(item, index, question)"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
|
<t-icon name="delete" size="14px" />
|
||||||
|
</template>
|
||||||
|
</t-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -597,12 +680,37 @@ const handleDetailsScroll = () => {
|
|||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: #1d2129;
|
color: #1d2129;
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
|
transition: background-color 0.2s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background: #e6f7ed;
|
||||||
|
|
||||||
|
.delete-question-btn {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.question-icon {
|
.question-icon {
|
||||||
color: #059669;
|
color: #059669;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
margin-top: 2px;
|
margin-top: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.question-text {
|
||||||
|
flex: 1;
|
||||||
|
word-break: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
.delete-question-btn {
|
||||||
|
opacity: 0;
|
||||||
|
flex-shrink: 0;
|
||||||
|
color: #999;
|
||||||
|
transition: opacity 0.2s ease, color 0.2s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
color: #e34d59;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.md-content {
|
.md-content {
|
||||||
|
|||||||
@@ -94,6 +94,8 @@ export default {
|
|||||||
segment: 'Segment',
|
segment: 'Segment',
|
||||||
questions: 'Questions',
|
questions: 'Questions',
|
||||||
generatedQuestions: 'Generated Questions',
|
generatedQuestions: 'Generated Questions',
|
||||||
|
confirmDeleteQuestion: 'Are you sure you want to delete this question? The corresponding vector index will also be removed.',
|
||||||
|
legacyQuestionCannotDelete: 'Legacy format questions cannot be deleted. Please regenerate questions.',
|
||||||
notInitialized: 'Knowledge base is not initialized. Please configure models in settings before uploading files',
|
notInitialized: 'Knowledge base is not initialized. Please configure models in settings before uploading files',
|
||||||
getInfoFailed: 'Failed to get knowledge base information, file upload is not possible',
|
getInfoFailed: 'Failed to get knowledge base information, file upload is not possible',
|
||||||
missingId: 'Knowledge base ID is missing',
|
missingId: 'Knowledge base ID is missing',
|
||||||
@@ -628,7 +630,9 @@ export default {
|
|||||||
on: 'On',
|
on: 'On',
|
||||||
off: 'Off',
|
off: 'Off',
|
||||||
resetToDefault: 'Reset to default',
|
resetToDefault: 'Reset to default',
|
||||||
confirmDelete: 'Confirm Delete'
|
confirmDelete: 'Confirm Delete',
|
||||||
|
deleteSuccess: 'Deleted successfully',
|
||||||
|
deleteFailed: 'Delete failed',
|
||||||
},
|
},
|
||||||
file: {
|
file: {
|
||||||
upload: 'Upload File',
|
upload: 'Upload File',
|
||||||
|
|||||||
@@ -94,6 +94,8 @@ export default {
|
|||||||
segment: 'Фрагмент',
|
segment: 'Фрагмент',
|
||||||
questions: 'Вопросы',
|
questions: 'Вопросы',
|
||||||
generatedQuestions: 'Сгенерированные вопросы',
|
generatedQuestions: 'Сгенерированные вопросы',
|
||||||
|
confirmDeleteQuestion: 'Вы уверены, что хотите удалить этот вопрос? Соответствующий векторный индекс также будет удален.',
|
||||||
|
legacyQuestionCannotDelete: 'Вопросы в устаревшем формате нельзя удалить. Пожалуйста, сгенерируйте вопросы заново.',
|
||||||
notInitialized: 'База знаний не инициализирована. Пожалуйста, настройте модели в разделе настроек перед загрузкой файлов',
|
notInitialized: 'База знаний не инициализирована. Пожалуйста, настройте модели в разделе настроек перед загрузкой файлов',
|
||||||
getInfoFailed: 'Не удалось получить информацию о базе знаний, загрузка файла невозможна',
|
getInfoFailed: 'Не удалось получить информацию о базе знаний, загрузка файла невозможна',
|
||||||
missingId: 'Отсутствует ID базы знаний',
|
missingId: 'Отсутствует ID базы знаний',
|
||||||
@@ -629,7 +631,9 @@ export default {
|
|||||||
on: 'Вкл',
|
on: 'Вкл',
|
||||||
off: 'Выкл',
|
off: 'Выкл',
|
||||||
resetToDefault: 'Сбросить по умолчанию',
|
resetToDefault: 'Сбросить по умолчанию',
|
||||||
confirmDelete: 'Подтвердить удаление'
|
confirmDelete: 'Подтвердить удаление',
|
||||||
|
deleteSuccess: 'Успешно удалено',
|
||||||
|
deleteFailed: 'Ошибка удаления',
|
||||||
},
|
},
|
||||||
file: {
|
file: {
|
||||||
upload: 'Загрузить файл',
|
upload: 'Загрузить файл',
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ export default {
|
|||||||
segment: "片段",
|
segment: "片段",
|
||||||
questions: "问题",
|
questions: "问题",
|
||||||
generatedQuestions: "生成的问题",
|
generatedQuestions: "生成的问题",
|
||||||
|
confirmDeleteQuestion: "确定要删除这个问题吗?删除后将同时移除对应的向量索引。",
|
||||||
|
legacyQuestionCannotDelete: "旧格式问题无法删除,请重新生成问题",
|
||||||
docActionUnsupported: "当前知识库类型不支持该操作",
|
docActionUnsupported: "当前知识库类型不支持该操作",
|
||||||
notInitialized:
|
notInitialized:
|
||||||
"该知识库尚未完成初始化配置,请先前往设置页面配置模型信息后再上传文件",
|
"该知识库尚未完成初始化配置,请先前往设置页面配置模型信息后再上传文件",
|
||||||
@@ -722,6 +724,8 @@ export default {
|
|||||||
off: "关闭",
|
off: "关闭",
|
||||||
resetToDefault: "恢复默认",
|
resetToDefault: "恢复默认",
|
||||||
confirmDelete: "确认删除",
|
confirmDelete: "确认删除",
|
||||||
|
deleteSuccess: "删除成功",
|
||||||
|
deleteFailed: "删除失败",
|
||||||
},
|
},
|
||||||
file: {
|
file: {
|
||||||
upload: "上传文件",
|
upload: "上传文件",
|
||||||
|
|||||||
@@ -291,6 +291,11 @@ func (e *elasticsearchRepository) DeleteByChunkIDList(ctx context.Context, chunk
|
|||||||
return e.deleteByFieldList(ctx, "chunk_id.keyword", chunkIDList)
|
return e.deleteByFieldList(ctx, "chunk_id.keyword", chunkIDList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteBySourceIDList Delete indices by source ID list
|
||||||
|
func (e *elasticsearchRepository) DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int) error {
|
||||||
|
return e.deleteByFieldList(ctx, "source_id.keyword", sourceIDList)
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteByKnowledgeIDList Delete indices by knowledge ID list
|
// DeleteByKnowledgeIDList Delete indices by knowledge ID list
|
||||||
func (e *elasticsearchRepository) DeleteByKnowledgeIDList(ctx context.Context,
|
func (e *elasticsearchRepository) DeleteByKnowledgeIDList(ctx context.Context,
|
||||||
knowledgeIDList []string, dimension int,
|
knowledgeIDList []string, dimension int,
|
||||||
|
|||||||
@@ -184,6 +184,29 @@ func (e *elasticsearchRepository) DeleteByChunkIDList(ctx context.Context, chunk
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteBySourceIDList removes documents from the index based on source IDs
|
||||||
|
// Returns an error if the delete operation fails
|
||||||
|
func (e *elasticsearchRepository) DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int) error {
|
||||||
|
log := logger.GetLogger(ctx)
|
||||||
|
if len(sourceIDList) == 0 {
|
||||||
|
log.Warn("[Elasticsearch] Empty source ID list provided for deletion, skipping")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("[Elasticsearch] Deleting indices by source IDs, count: %d", len(sourceIDList))
|
||||||
|
// Use DeleteByQuery to delete all documents matching the source IDs
|
||||||
|
_, err := e.client.DeleteByQuery(e.index).Query(&types.Query{
|
||||||
|
Terms: &types.TermsQuery{TermsQuery: map[string]types.TermsQueryField{"source_id.keyword": sourceIDList}},
|
||||||
|
}).Do(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("[Elasticsearch] Failed to delete by source IDs: %v", err)
|
||||||
|
return fmt.Errorf("failed to delete by query: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("[Elasticsearch] Successfully deleted documents by source IDs")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteByKnowledgeIDList removes documents from the index based on knowledge IDs
|
// DeleteByKnowledgeIDList removes documents from the index based on knowledge IDs
|
||||||
// Returns an error if the delete operation fails
|
// Returns an error if the delete operation fails
|
||||||
func (e *elasticsearchRepository) DeleteByKnowledgeIDList(ctx context.Context,
|
func (e *elasticsearchRepository) DeleteByKnowledgeIDList(ctx context.Context,
|
||||||
|
|||||||
@@ -118,6 +118,21 @@ func (g *pgRepository) DeleteByChunkIDList(ctx context.Context, chunkIDList []st
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteBySourceIDList deletes indices by source IDs
|
||||||
|
func (g *pgRepository) DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int) error {
|
||||||
|
if len(sourceIDList) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).Infof("[Postgres] Deleting indices by source IDs, count: %d", len(sourceIDList))
|
||||||
|
result := g.db.WithContext(ctx).Where("source_id IN ?", sourceIDList).Delete(&pgVector{})
|
||||||
|
if result.Error != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("[Postgres] Failed to delete indices by source IDs: %v", result.Error)
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).Infof("[Postgres] Successfully deleted %d indices by source IDs", result.RowsAffected)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteByKnowledgeIDList deletes indices by knowledge IDs
|
// DeleteByKnowledgeIDList deletes indices by knowledge IDs
|
||||||
func (g *pgRepository) DeleteByKnowledgeIDList(ctx context.Context, knowledgeIDList []string, dimension int) error {
|
func (g *pgRepository) DeleteByKnowledgeIDList(ctx context.Context, knowledgeIDList []string, dimension int) error {
|
||||||
logger.GetLogger(ctx).Infof("[Postgres] Deleting indices by knowledge IDs, count: %d", len(knowledgeIDList))
|
logger.GetLogger(ctx).Infof("[Postgres] Deleting indices by knowledge IDs, count: %d", len(knowledgeIDList))
|
||||||
|
|||||||
@@ -354,8 +354,8 @@ func getEnrichedPassage(ctx context.Context, result *types.SearchResult) string
|
|||||||
pipelineWarn(ctx, "Rerank", "chunk_metadata_parse", map[string]interface{}{
|
pipelineWarn(ctx, "Rerank", "chunk_metadata_parse", map[string]interface{}{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
} else if len(docMeta.GeneratedQuestions) > 0 {
|
} else if questionStrings := docMeta.GetQuestionStrings(); len(questionStrings) > 0 {
|
||||||
enrichments = append(enrichments, fmt.Sprintf("相关问题: %s", strings.Join(docMeta.GeneratedQuestions, "; ")))
|
enrichments = append(enrichments, fmt.Sprintf("相关问题: %s", strings.Join(questionStrings, "; ")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
@@ -18,6 +20,7 @@ type chunkService struct {
|
|||||||
chunkRepository interfaces.ChunkRepository // Repository for chunk data persistence
|
chunkRepository interfaces.ChunkRepository // Repository for chunk data persistence
|
||||||
kbRepository interfaces.KnowledgeBaseRepository
|
kbRepository interfaces.KnowledgeBaseRepository
|
||||||
modelService interfaces.ModelService
|
modelService interfaces.ModelService
|
||||||
|
retrieveEngine interfaces.RetrieveEngineRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewChunkService creates a new chunk service
|
// NewChunkService creates a new chunk service
|
||||||
@@ -31,11 +34,13 @@ func NewChunkService(
|
|||||||
chunkRepository interfaces.ChunkRepository,
|
chunkRepository interfaces.ChunkRepository,
|
||||||
kbRepository interfaces.KnowledgeBaseRepository,
|
kbRepository interfaces.KnowledgeBaseRepository,
|
||||||
modelService interfaces.ModelService,
|
modelService interfaces.ModelService,
|
||||||
|
retrieveEngine interfaces.RetrieveEngineRegistry,
|
||||||
) interfaces.ChunkService {
|
) interfaces.ChunkService {
|
||||||
return &chunkService{
|
return &chunkService{
|
||||||
chunkRepository: chunkRepository,
|
chunkRepository: chunkRepository,
|
||||||
kbRepository: kbRepository,
|
kbRepository: kbRepository,
|
||||||
modelService: modelService,
|
modelService: modelService,
|
||||||
|
retrieveEngine: retrieveEngine,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,3 +331,107 @@ func (s *chunkService) ListChunkByParentID(
|
|||||||
logger.Info(ctx, "Chunk listed successfully")
|
logger.Info(ctx, "Chunk listed successfully")
|
||||||
return chunks, nil
|
return chunks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteGeneratedQuestion deletes a single generated question from a chunk by question ID
|
||||||
|
// This updates the chunk metadata and removes the corresponding vector index
|
||||||
|
func (s *chunkService) DeleteGeneratedQuestion(ctx context.Context, chunkID string, questionID string) error {
|
||||||
|
logger.Infof(ctx, "Deleting generated question, chunk ID: %s, question ID: %s", chunkID, questionID)
|
||||||
|
|
||||||
|
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||||
|
|
||||||
|
// 1. Get the chunk
|
||||||
|
chunk, err := s.chunkRepository.GetChunkByID(ctx, tenantID, chunkID)
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
"chunk_id": chunkID,
|
||||||
|
"tenant_id": tenantID,
|
||||||
|
})
|
||||||
|
return fmt.Errorf("failed to get chunk: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Parse the metadata
|
||||||
|
meta, err := chunk.DocumentMetadata()
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
"chunk_id": chunkID,
|
||||||
|
})
|
||||||
|
return fmt.Errorf("failed to parse chunk metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if meta == nil || len(meta.GeneratedQuestions) == 0 {
|
||||||
|
return fmt.Errorf("no generated questions found for chunk %s", chunkID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Find the question by ID
|
||||||
|
questionIndex := -1
|
||||||
|
for i, q := range meta.GeneratedQuestions {
|
||||||
|
if q.ID == questionID {
|
||||||
|
questionIndex = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if questionIndex == -1 {
|
||||||
|
return fmt.Errorf("question with ID %s not found in chunk %s", questionID, chunkID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Get knowledge base to get embedding model
|
||||||
|
kb, err := s.kbRepository.GetKnowledgeBaseByID(ctx, chunk.KnowledgeBaseID)
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
"knowledge_base_id": chunk.KnowledgeBaseID,
|
||||||
|
})
|
||||||
|
return fmt.Errorf("failed to get knowledge base: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Delete the vector index for this question
|
||||||
|
// The source_id format is: {chunk_id}-{question_id}
|
||||||
|
sourceID := fmt.Sprintf("%s-%s", chunkID, questionID)
|
||||||
|
|
||||||
|
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||||
|
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.RetrieverEngines.Engines)
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
"chunk_id": chunkID,
|
||||||
|
})
|
||||||
|
return fmt.Errorf("failed to create retrieve engine: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, kb.EmbeddingModelID)
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
"embedding_model_id": kb.EmbeddingModelID,
|
||||||
|
})
|
||||||
|
return fmt.Errorf("failed to get embedding model: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the vector index by source ID
|
||||||
|
if err := retrieveEngine.DeleteBySourceIDList(ctx, []string{sourceID}, embeddingModel.GetDimensions()); err != nil {
|
||||||
|
logger.Warnf(ctx, "Failed to delete vector index for question (may not exist): %v", err)
|
||||||
|
// Continue even if vector deletion fails - the question might not have been indexed
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Remove the question from metadata
|
||||||
|
newQuestions := make([]types.GeneratedQuestion, 0, len(meta.GeneratedQuestions)-1)
|
||||||
|
for i, q := range meta.GeneratedQuestions {
|
||||||
|
if i != questionIndex {
|
||||||
|
newQuestions = append(newQuestions, q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Update chunk metadata
|
||||||
|
meta.GeneratedQuestions = newQuestions
|
||||||
|
if err := chunk.SetDocumentMetadata(meta); err != nil {
|
||||||
|
return fmt.Errorf("failed to set chunk metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.chunkRepository.UpdateChunk(ctx, chunk); err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
"chunk_id": chunkID,
|
||||||
|
})
|
||||||
|
return fmt.Errorf("failed to update chunk: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "Successfully deleted generated question %s from chunk %s", questionID, chunkID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1815,9 +1815,17 @@ func (s *knowledgeService) ProcessQuestionGeneration(ctx context.Context, t *asy
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update chunk metadata
|
// Update chunk metadata with unique IDs for each question
|
||||||
|
generatedQuestions := make([]types.GeneratedQuestion, len(questions))
|
||||||
|
for j, question := range questions {
|
||||||
|
questionID := fmt.Sprintf("q%d", time.Now().UnixNano()+int64(j))
|
||||||
|
generatedQuestions[j] = types.GeneratedQuestion{
|
||||||
|
ID: questionID,
|
||||||
|
Question: question,
|
||||||
|
}
|
||||||
|
}
|
||||||
meta := &types.DocumentChunkMetadata{
|
meta := &types.DocumentChunkMetadata{
|
||||||
GeneratedQuestions: questions,
|
GeneratedQuestions: generatedQuestions,
|
||||||
}
|
}
|
||||||
if err := chunk.SetDocumentMetadata(meta); err != nil {
|
if err := chunk.SetDocumentMetadata(meta); err != nil {
|
||||||
logger.Warnf(ctx, "Failed to set document metadata for chunk %s: %v", chunk.ID, err)
|
logger.Warnf(ctx, "Failed to set document metadata for chunk %s: %v", chunk.ID, err)
|
||||||
@@ -1831,10 +1839,10 @@ func (s *knowledgeService) ProcessQuestionGeneration(ctx context.Context, t *asy
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create index entries for generated questions
|
// Create index entries for generated questions
|
||||||
for j, question := range questions {
|
for _, gq := range generatedQuestions {
|
||||||
sourceID := fmt.Sprintf("%s-q%d", chunk.ID, j)
|
sourceID := fmt.Sprintf("%s-%s", chunk.ID, gq.ID)
|
||||||
indexInfoList = append(indexInfoList, &types.IndexInfo{
|
indexInfoList = append(indexInfoList, &types.IndexInfo{
|
||||||
Content: question,
|
Content: gq.Question,
|
||||||
SourceID: sourceID,
|
SourceID: sourceID,
|
||||||
SourceType: types.ChunkSourceType,
|
SourceType: types.ChunkSourceType,
|
||||||
ChunkID: chunk.ID,
|
ChunkID: chunk.ID,
|
||||||
|
|||||||
@@ -246,6 +246,20 @@ func (c *CompositeRetrieveEngine) DeleteByChunkIDList(ctx context.Context,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteBySourceIDList deletes vector embeddings by source ID list from all registered repositories
|
||||||
|
func (c *CompositeRetrieveEngine) DeleteBySourceIDList(ctx context.Context,
|
||||||
|
sourceIDList []string, dimension int,
|
||||||
|
) error {
|
||||||
|
return c.concurrentExecWithError(ctx, func(ctx context.Context, engineInfo *engineInfo) error {
|
||||||
|
if err := engineInfo.retrieveEngine.DeleteBySourceIDList(ctx, sourceIDList, dimension); err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("Repository %s failed to delete source ID list: %v",
|
||||||
|
engineInfo.retrieveEngine.EngineType(), err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// CopyIndices copies indices from a source knowledge base to a target knowledge base
|
// CopyIndices copies indices from a source knowledge base to a target knowledge base
|
||||||
func (c *CompositeRetrieveEngine) CopyIndices(
|
func (c *CompositeRetrieveEngine) CopyIndices(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
|||||||
@@ -116,6 +116,13 @@ func (v *KeywordsVectorHybridRetrieveEngineService) DeleteByChunkIDList(ctx cont
|
|||||||
return v.indexRepository.DeleteByChunkIDList(ctx, indexIDList, dimension)
|
return v.indexRepository.DeleteByChunkIDList(ctx, indexIDList, dimension)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteBySourceIDList deletes vectors by their source IDs
|
||||||
|
func (v *KeywordsVectorHybridRetrieveEngineService) DeleteBySourceIDList(ctx context.Context,
|
||||||
|
sourceIDList []string, dimension int,
|
||||||
|
) error {
|
||||||
|
return v.indexRepository.DeleteBySourceIDList(ctx, sourceIDList, dimension)
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteByKnowledgeIDList deletes vectors by their knowledge IDs
|
// DeleteByKnowledgeIDList deletes vectors by their knowledge IDs
|
||||||
func (v *KeywordsVectorHybridRetrieveEngineService) DeleteByKnowledgeIDList(ctx context.Context,
|
func (v *KeywordsVectorHybridRetrieveEngineService) DeleteByKnowledgeIDList(ctx context.Context,
|
||||||
knowledgeIDList []string, dimension int,
|
knowledgeIDList []string, dimension int,
|
||||||
|
|||||||
@@ -288,3 +288,66 @@ func (h *ChunkHandler) DeleteChunksByKnowledgeID(c *gin.Context) {
|
|||||||
"message": "All chunks under knowledge deleted",
|
"message": "All chunks under knowledge deleted",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteGeneratedQuestion deletes a generated question by its ID
|
||||||
|
func (h *ChunkHandler) DeleteGeneratedQuestion(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
logger.Info(ctx, "Start deleting generated question from chunk")
|
||||||
|
|
||||||
|
chunkID := secutils.SanitizeForLog(c.Param("id"))
|
||||||
|
if chunkID == "" {
|
||||||
|
logger.Error(ctx, "Chunk ID is empty")
|
||||||
|
c.Error(errors.NewBadRequestError("Chunk ID cannot be empty"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
QuestionID string `json:"question_id" binding:"required"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
logger.Errorf(ctx, "Failed to parse request parameters: %s", secutils.SanitizeForLog(err.Error()))
|
||||||
|
c.Error(errors.NewBadRequestError("Question ID is required"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get tenant ID from context
|
||||||
|
tenantID, exists := c.Get(types.TenantIDContextKey.String())
|
||||||
|
if !exists {
|
||||||
|
logger.Error(ctx, "Failed to get tenant ID")
|
||||||
|
c.Error(errors.NewUnauthorizedError("Unauthorized"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify chunk exists and belongs to tenant
|
||||||
|
chunk, err := h.service.GetChunkByID(ctx, chunkID)
|
||||||
|
if err != nil {
|
||||||
|
if err == service.ErrChunkNotFound {
|
||||||
|
logger.Warnf(ctx, "Chunk not found, chunk ID: %s", chunkID)
|
||||||
|
c.Error(errors.NewNotFoundError("Chunk not found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.ErrorWithFields(ctx, err, nil)
|
||||||
|
c.Error(errors.NewInternalServerError(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if chunk.TenantID != tenantID.(uint64) {
|
||||||
|
logger.Warnf(ctx, "Tenant has no permission to access chunk, chunk ID: %s", chunkID)
|
||||||
|
c.Error(errors.NewForbiddenError("No permission to access this chunk"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the generated question by ID
|
||||||
|
if err := h.service.DeleteGeneratedQuestion(ctx, chunkID, req.QuestionID); err != nil {
|
||||||
|
logger.ErrorWithFields(ctx, err, nil)
|
||||||
|
c.Error(errors.NewBadRequestError(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "Generated question deleted successfully, chunk ID: %s, question ID: %s",
|
||||||
|
secutils.SanitizeForLog(chunkID), secutils.SanitizeForLog(req.QuestionID))
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "Generated question deleted",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -113,6 +113,8 @@ func RegisterChunkRoutes(r *gin.RouterGroup, handler *handler.ChunkHandler) {
|
|||||||
chunks.DELETE("/:knowledge_id", handler.DeleteChunksByKnowledgeID)
|
chunks.DELETE("/:knowledge_id", handler.DeleteChunksByKnowledgeID)
|
||||||
// 更新分块信息
|
// 更新分块信息
|
||||||
chunks.PUT("/:knowledge_id/:id", handler.UpdateChunk)
|
chunks.PUT("/:knowledge_id/:id", handler.UpdateChunk)
|
||||||
|
// 删除单个生成的问题(通过问题ID)
|
||||||
|
chunks.DELETE("/by-id/:id/questions", handler.DeleteGeneratedQuestion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-1
@@ -19,12 +19,30 @@ type FAQChunkMetadata struct {
|
|||||||
Source string `json:"source,omitempty"`
|
Source string `json:"source,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GeneratedQuestion 表示AI生成的单个问题
|
||||||
|
type GeneratedQuestion struct {
|
||||||
|
ID string `json:"id"` // 唯一标识,用于构造 source_id
|
||||||
|
Question string `json:"question"` // 问题内容
|
||||||
|
}
|
||||||
|
|
||||||
// DocumentChunkMetadata 定义文档 Chunk 的元数据结构
|
// DocumentChunkMetadata 定义文档 Chunk 的元数据结构
|
||||||
// 用于存储AI生成的问题等增强信息
|
// 用于存储AI生成的问题等增强信息
|
||||||
type DocumentChunkMetadata struct {
|
type DocumentChunkMetadata struct {
|
||||||
// GeneratedQuestions 存储AI为该Chunk生成的相关问题
|
// GeneratedQuestions 存储AI为该Chunk生成的相关问题
|
||||||
// 这些问题会被独立索引以提高召回率
|
// 这些问题会被独立索引以提高召回率
|
||||||
GeneratedQuestions []string `json:"generated_questions,omitempty"`
|
GeneratedQuestions []GeneratedQuestion `json:"generated_questions,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQuestionStrings 返回问题内容字符串列表(兼容旧代码)
|
||||||
|
func (m *DocumentChunkMetadata) GetQuestionStrings() []string {
|
||||||
|
if m == nil || len(m.GeneratedQuestions) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
result := make([]string, len(m.GeneratedQuestions))
|
||||||
|
for i, q := range m.GeneratedQuestions {
|
||||||
|
result[i] = q.Question
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// DocumentMetadata 解析 Chunk 中的文档元数据
|
// DocumentMetadata 解析 Chunk 中的文档元数据
|
||||||
|
|||||||
@@ -83,4 +83,7 @@ type ChunkService interface {
|
|||||||
ListChunkByParentID(ctx context.Context, tenantID uint64, parentID string) ([]*types.Chunk, error)
|
ListChunkByParentID(ctx context.Context, tenantID uint64, parentID string) ([]*types.Chunk, error)
|
||||||
// GetRepository gets the chunk repository
|
// GetRepository gets the chunk repository
|
||||||
GetRepository() ChunkRepository
|
GetRepository() ChunkRepository
|
||||||
|
// DeleteGeneratedQuestion deletes a single generated question from a chunk by question ID
|
||||||
|
// This updates the chunk metadata and removes the corresponding vector index
|
||||||
|
DeleteGeneratedQuestion(ctx context.Context, chunkID string, questionID string) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ type RetrieveEngineRepository interface {
|
|||||||
|
|
||||||
// DeleteByChunkIDList deletes the index info by chunk id list
|
// DeleteByChunkIDList deletes the index info by chunk id list
|
||||||
DeleteByChunkIDList(ctx context.Context, indexIDList []string, dimension int) error
|
DeleteByChunkIDList(ctx context.Context, indexIDList []string, dimension int) error
|
||||||
|
// DeleteBySourceIDList deletes the index info by source id list
|
||||||
|
DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int) error
|
||||||
// 复制索引数据
|
// 复制索引数据
|
||||||
// sourceKnowledgeBaseID: 源知识库ID
|
// sourceKnowledgeBaseID: 源知识库ID
|
||||||
// sourceToTargetChunkIDMap: 源分块ID到目标分块ID的映射关系
|
// sourceToTargetChunkIDMap: 源分块ID到目标分块ID的映射关系
|
||||||
@@ -105,6 +107,9 @@ type RetrieveEngineService interface {
|
|||||||
// DeleteByChunkIDList deletes the index info by chunk id list
|
// DeleteByChunkIDList deletes the index info by chunk id list
|
||||||
DeleteByChunkIDList(ctx context.Context, indexIDList []string, dimension int) error
|
DeleteByChunkIDList(ctx context.Context, indexIDList []string, dimension int) error
|
||||||
|
|
||||||
|
// DeleteBySourceIDList deletes the index info by source id list
|
||||||
|
DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int) error
|
||||||
|
|
||||||
// DeleteByKnowledgeIDList deletes the index info by knowledge id list
|
// DeleteByKnowledgeIDList deletes the index info by knowledge id list
|
||||||
DeleteByKnowledgeIDList(ctx context.Context, knowledgeIDList []string, dimension int) error
|
DeleteByKnowledgeIDList(ctx context.Context, knowledgeIDList []string, dimension int) error
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user