Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: Implement delete functionality for generated questions in knowledge base, including API and UI updates

wizardchena50f53d20556
diff --git a/frontend/src/api/knowledge-base/index.ts b/frontend/src/api/knowledge-base/index.ts
index 8dd07d69b..2e09ff32a 100644
--- a/frontend/src/api/knowledge-base/index.ts
+++ b/frontend/src/api/knowledge-base/index.ts
@@ -101,6 +101,11 @@ export function getChunkByIdOnly(chunkId: string) {
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(
kbId: string,
params?: { page?: number; page_size?: number; keyword?: string },
diff --git a/frontend/src/components/doc-content.vue b/frontend/src/components/doc-content.vue
index 2671ae51b..6ecac6279 100644
--- a/frontend/src/components/doc-content.vue
+++ b/frontend/src/components/doc-content.vue
@@ -1,8 +1,8 @@
<script setup lang="ts">
import { marked } from "marked";
import { onMounted, ref, nextTick, onUnmounted, onUpdated, watch } from "vue";
-import { downKnowledgeDetails } from "@/api/knowledge-base/index";
-import { MessagePlugin } from "tdesign-vue-next";
+import { downKnowledgeDetails, deleteGeneratedQuestion } from "@/api/knowledge-base/index";
+import { MessagePlugin, DialogPlugin } from "tdesign-vue-next";
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
import { useI18n } from 'vue-i18n';
@@ -52,7 +52,7 @@ renderer.image = function (href, title, text) {
</figure>`;
};
const props = defineProps(["visible", "details", "knowledgeType", "sourceInfo"]);
-const emit = defineEmits(["closeDoc", "getDoc"]);
+const emit = defineEmits(["closeDoc", "getDoc", "questionDeleted"]);
watch(() => props.details.md, (newVal) => {
nextTick(async () => {
const images = mdContentWrap.value.querySelectorAll('img.markdown-image');
@@ -185,12 +185,26 @@ const getChunkMeta = (item: any) => {
return parts.join(' · ');
};
+// 生成的问题类型
+interface GeneratedQuestion {
+ id: string;
+ question: string;
+}
+
// 解析生成的问题
-const getGeneratedQuestions = (item: any): string[] => {
+const getGeneratedQuestions = (item: any): GeneratedQuestion[] => {
if (!item || !item.metadata) return [];
try {
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 {
return [];
}
@@ -211,6 +225,63 @@ const toggleQuestions = (index: number) => {
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 = () => {
downKnowledgeDetails(props.details.id)
.then((result) => {
@@ -327,12 +398,24 @@ const handleDetailsScroll = () => {
</div>
<div v-show="isExpanded(index)" class="questions-list">
<div
- v-for="(question, qIndex) in getGeneratedQuestions(item)"
- :key="qIndex"
+ v-for="question in getGeneratedQuestions(item)"
+ :key="question.id"
class="question-item"
>
<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>
@@ -597,12 +680,37 @@ const handleDetailsScroll = () => {
font-size: 13px;
color: #1d2129;
line-height: 1.5;
+ transition: background-color 0.2s ease;
+
+ &:hover {
+ background: #e6f7ed;
+
+ .delete-question-btn {
+ opacity: 1;
+ }
+ }
.question-icon {
color: #059669;
flex-shrink: 0;
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 {
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index caead9054..29ba40240 100644
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -94,6 +94,8 @@ export default {
segment: 'Segment',
questions: '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',
getInfoFailed: 'Failed to get knowledge base information, file upload is not possible',
missingId: 'Knowledge base ID is missing',
@@ -628,7 +630,9 @@ export default {
on: 'On',
off: 'Off',
resetToDefault: 'Reset to default',
- confirmDelete: 'Confirm Delete'
+ confirmDelete: 'Confirm Delete',
+ deleteSuccess: 'Deleted successfully',
+ deleteFailed: 'Delete failed',
},
file: {
upload: 'Upload File',
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index 8112eb7c4..ebbbb8915 100644
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -94,6 +94,8 @@ export default {
segment: 'Фрагмент',
questions: 'Вопросы',
generatedQuestions: 'Сгенерированные вопросы',
+ confirmDeleteQuestion: 'Вы уверены, что хотите удалить этот вопрос? Соответствующий векторный индекс также будет удален.',
+ legacyQuestionCannotDelete: 'Вопросы в устаревшем формате нельзя удалить. Пожалуйста, сгенерируйте вопросы заново.',
notInitialized: 'База знаний не инициализирована. Пожалуйста, настройте модели в разделе настроек перед загрузкой файлов',
getInfoFailed: 'Не удалось получить информацию о базе знаний, загрузка файла невозможна',
missingId: 'Отсутствует ID базы знаний',
@@ -629,7 +631,9 @@ export default {
on: 'Вкл',
off: 'Выкл',
resetToDefault: 'Сбросить по умолчанию',
- confirmDelete: 'Подтвердить удаление'
+ confirmDelete: 'Подтвердить удаление',
+ deleteSuccess: 'Успешно удалено',
+ deleteFailed: 'Ошибка удаления',
},
file: {
upload: 'Загрузить файл',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index ff5e2bc12..a9de782ee 100644
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -93,6 +93,8 @@ export default {
segment: "片段",
questions: "问题",
generatedQuestions: "生成的问题",
+ confirmDeleteQuestion: "确定要删除这个问题吗?删除后将同时移除对应的向量索引。",
+ legacyQuestionCannotDelete: "旧格式问题无法删除,请重新生成问题",
docActionUnsupported: "当前知识库类型不支持该操作",
notInitialized:
"该知识库尚未完成初始化配置,请先前往设置页面配置模型信息后再上传文件",
@@ -722,6 +724,8 @@ export default {
off: "关闭",
resetToDefault: "恢复默认",
confirmDelete: "确认删除",
+ deleteSuccess: "删除成功",
+ deleteFailed: "删除失败",
},
file: {
upload: "上传文件",
diff --git a/internal/application/repository/retriever/elasticsearch/v7/repository.go b/internal/application/repository/retriever/elasticsearch/v7/repository.go
index 01b848150..5e205a547 100644
--- a/internal/application/repository/retriever/elasticsearch/v7/repository.go
+++ b/internal/application/repository/retriever/elasticsearch/v7/repository.go
@@ -291,6 +291,11 @@ func (e *elasticsearchRepository) DeleteByChunkIDList(ctx context.Context, chunk
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
func (e *elasticsearchRepository) DeleteByKnowledgeIDList(ctx context.Context,
knowledgeIDList []string, dimension int,
diff --git a/internal/application/repository/retriever/elasticsearch/v8/repository.go b/internal/application/repository/retriever/elasticsearch/v8/repository.go
index 378ecb0db..a6c025f5e 100644
--- a/internal/application/repository/retriever/elasticsearch/v8/repository.go
+++ b/internal/application/repository/retriever/elasticsearch/v8/repository.go
@@ -184,6 +184,29 @@ func (e *elasticsearchRepository) DeleteByChunkIDList(ctx context.Context, chunk
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
// Returns an error if the delete operation fails
func (e *elasticsearchRepository) DeleteByKnowledgeIDList(ctx context.Context,
diff --git a/internal/application/repository/retriever/postgres/repository.go b/internal/application/repository/retriever/postgres/repository.go
index adfe42085..ad53480f9 100644
--- a/internal/application/repository/retriever/postgres/repository.go
+++ b/internal/application/repository/retriever/postgres/repository.go
@@ -118,6 +118,21 @@ func (g *pgRepository) DeleteByChunkIDList(ctx context.Context, chunkIDList []st
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
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))
diff --git a/internal/application/service/chat_pipline/rerank.go b/internal/application/service/chat_pipline/rerank.go
index 2d45fde63..7f2769ed7 100644
--- a/internal/application/service/chat_pipline/rerank.go
+++ b/internal/application/service/chat_pipline/rerank.go
@@ -354,8 +354,8 @@ func getEnrichedPassage(ctx context.Context, result *types.SearchResult) string
pipelineWarn(ctx, "Rerank", "chunk_metadata_parse", map[string]interface{}{
"error": err.Error(),
})
- } else if len(docMeta.GeneratedQuestions) > 0 {
- enrichments = append(enrichments, fmt.Sprintf("相关问题: %s", strings.Join(docMeta.GeneratedQuestions, "; ")))
+ } else if questionStrings := docMeta.GetQuestionStrings(); len(questionStrings) > 0 {
+ enrichments = append(enrichments, fmt.Sprintf("相关问题: %s", strings.Join(questionStrings, "; ")))
}
}
diff --git a/internal/application/service/chunk.go b/internal/application/service/chunk.go
index c0b356568..1720d3f40 100644
--- a/internal/application/service/chunk.go
+++ b/internal/application/service/chunk.go
@@ -5,7 +5,9 @@ package service
import (
"context"
+ "fmt"
+ "github.com/Tencent/WeKnora/internal/application/service/retriever"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -18,6 +20,7 @@ type chunkService struct {
chunkRepository interfaces.ChunkRepository // Repository for chunk data persistence
kbRepository interfaces.KnowledgeBaseRepository
modelService interfaces.ModelService
+ retrieveEngine interfaces.RetrieveEngineRegistry
}
// NewChunkService creates a new chunk service
@@ -31,11 +34,13 @@ func NewChunkService(
chunkRepository interfaces.ChunkRepository,
kbRepository interfaces.KnowledgeBaseRepository,
modelService interfaces.ModelService,
+ retrieveEngine interfaces.RetrieveEngineRegistry,
) interfaces.ChunkService {
return &chunkService{
chunkRepository: chunkRepository,
kbRepository: kbRepository,
modelService: modelService,
+ retrieveEngine: retrieveEngine,
}
}
@@ -326,3 +331,107 @@ func (s *chunkService) ListChunkByParentID(
logger.Info(ctx, "Chunk listed successfully")
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
+}
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index 4dfb89805..e4f110b3f 100644
--- a/internal/application/service/knowledge.go
+++ b/internal/application/service/knowledge.go
@@ -1815,9 +1815,17 @@ func (s *knowledgeService) ProcessQuestionGeneration(ctx context.Context, t *asy
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{
- GeneratedQuestions: questions,
+ GeneratedQuestions: generatedQuestions,
}
if err := chunk.SetDocumentMetadata(meta); err != nil {
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
- for j, question := range questions {
- sourceID := fmt.Sprintf("%s-q%d", chunk.ID, j)
+ for _, gq := range generatedQuestions {
+ sourceID := fmt.Sprintf("%s-%s", chunk.ID, gq.ID)
indexInfoList = append(indexInfoList, &types.IndexInfo{
- Content: question,
+ Content: gq.Question,
SourceID: sourceID,
SourceType: types.ChunkSourceType,
ChunkID: chunk.ID,
diff --git a/internal/application/service/retriever/composite.go b/internal/application/service/retriever/composite.go
index a6f828f98..532b1febd 100644
--- a/internal/application/service/retriever/composite.go
+++ b/internal/application/service/retriever/composite.go
@@ -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
func (c *CompositeRetrieveEngine) CopyIndices(
ctx context.Context,
diff --git a/internal/application/service/retriever/keywords_vector_hybrid_indexer.go b/internal/application/service/retriever/keywords_vector_hybrid_indexer.go
index c8ec2da0b..1cbe355f8 100644
--- a/internal/application/service/retriever/keywords_vector_hybrid_indexer.go
+++ b/internal/application/service/retriever/keywords_vector_hybrid_indexer.go
@@ -116,6 +116,13 @@ func (v *KeywordsVectorHybridRetrieveEngineService) DeleteByChunkIDList(ctx cont
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
func (v *KeywordsVectorHybridRetrieveEngineService) DeleteByKnowledgeIDList(ctx context.Context,
knowledgeIDList []string, dimension int,
diff --git a/internal/handler/chunk.go b/internal/handler/chunk.go
index 999c8b58f..46992f4f5 100644
--- a/internal/handler/chunk.go
+++ b/internal/handler/chunk.go
@@ -288,3 +288,66 @@ func (h *ChunkHandler) DeleteChunksByKnowledgeID(c *gin.Context) {
"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",
+ })
+}
diff --git a/internal/router/router.go b/internal/router/router.go
index df9585a09..5758eab05 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -113,6 +113,8 @@ func RegisterChunkRoutes(r *gin.RouterGroup, handler *handler.ChunkHandler) {
chunks.DELETE("/:knowledge_id", handler.DeleteChunksByKnowledgeID)
// 更新分块信息
chunks.PUT("/:knowledge_id/:id", handler.UpdateChunk)
+ // 删除单个生成的问题(通过问题ID)
+ chunks.DELETE("/by-id/:id/questions", handler.DeleteGeneratedQuestion)
}
}
diff --git a/internal/types/faq.go b/internal/types/faq.go
index fb7f5feca..d8d21c71e 100644
--- a/internal/types/faq.go
+++ b/internal/types/faq.go
@@ -19,12 +19,30 @@ type FAQChunkMetadata struct {
Source string `json:"source,omitempty"`
}
+// GeneratedQuestion 表示AI生成的单个问题
+type GeneratedQuestion struct {
+ ID string `json:"id"` // 唯一标识,用于构造 source_id
+ Question string `json:"question"` // 问题内容
+}
+
// DocumentChunkMetadata 定义文档 Chunk 的元数据结构
// 用于存储AI生成的问题等增强信息
type DocumentChunkMetadata struct {
// 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 中的文档元数据
diff --git a/internal/types/interfaces/chunk.go b/internal/types/interfaces/chunk.go
index 871475ba4..1d3e35a12 100644
--- a/internal/types/interfaces/chunk.go
+++ b/internal/types/interfaces/chunk.go
@@ -83,4 +83,7 @@ type ChunkService interface {
ListChunkByParentID(ctx context.Context, tenantID uint64, parentID string) ([]*types.Chunk, error)
// GetRepository gets the chunk repository
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
}
diff --git a/internal/types/interfaces/retriever.go b/internal/types/interfaces/retriever.go
index 41c147b4d..0c8cc3c33 100644
--- a/internal/types/interfaces/retriever.go
+++ b/internal/types/interfaces/retriever.go
@@ -32,6 +32,8 @@ type RetrieveEngineRepository interface {
// DeleteByChunkIDList deletes the index info by chunk id list
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
// sourceToTargetChunkIDMap: 源分块ID到目标分块ID的映射关系
@@ -105,6 +107,9 @@ type RetrieveEngineService interface {
// DeleteByChunkIDList deletes the index info by chunk id list
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(ctx context.Context, knowledgeIDList []string, dimension int) error