Skip to content

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

fix(knowledge): bound generated question source IDs

wizardchen95775ae909c9
diff --git a/internal/application/service/chunk.go b/internal/application/service/chunk.go
index 57aed138c..eb8a2f9fb 100644
--- a/internal/application/service/chunk.go
+++ b/internal/application/service/chunk.go
@@ -601,7 +601,7 @@ func (s *chunkService) syncChunkIndex(ctx context.Context, chunk *types.Chunk) e
continue
}
items = append(items, &types.IndexInfo{
- Content: prefix + question.Question, SourceID: fmt.Sprintf("%s-%s", chunk.ID, question.ID),
+ Content: prefix + question.Question, SourceID: types.GeneratedQuestionSourceID(chunk.ID, question.ID),
SourceType: types.ChunkSourceType, ChunkID: chunk.ID,
KnowledgeID: chunk.KnowledgeID, KnowledgeBaseID: chunk.KnowledgeBaseID,
KnowledgeType: kb.Type, IsEnabled: true,
@@ -717,7 +717,7 @@ func (s *chunkService) DeleteGeneratedQuestion(ctx context.Context, chunkID stri
// 5. Delete the vector index for this question
// The source_id format is: {chunk_id}-{question_id}
- sourceID := fmt.Sprintf("%s-%s", chunkID, questionID)
+ sourceID := types.GeneratedQuestionSourceID(chunkID, questionID)
retrieveEngine, err := retriever.CreateRetrieveEngineForKB(
ctx, s.retrieveEngine, s.ownership, tenantID, kb.VectorStoreID)
diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go
index b6db48f5d..d41a10151 100644
--- a/internal/application/service/knowledge_process.go
+++ b/internal/application/service/knowledge_process.go
@@ -1600,7 +1600,7 @@ func (s *knowledgeService) processQuestionGenerationForKnowledge(ctx context.Con
// Create index entries for generated questions
for _, gq := range generatedQuestions {
- sourceID := fmt.Sprintf("%s-%s", chunk.ID, gq.ID)
+ sourceID := types.GeneratedQuestionSourceID(chunk.ID, gq.ID)
indexInfoList = append(indexInfoList, &types.IndexInfo{
Content: gq.Question,
SourceID: sourceID,
@@ -1930,7 +1930,7 @@ func (s *knowledgeService) processQuestionGenerationForChunks(ctx context.Contex
for _, gq := range generatedQuestions {
indexInfoList = append(indexInfoList, &types.IndexInfo{
Content: gq.Question,
- SourceID: fmt.Sprintf("%s-%s", chunk.ID, gq.ID),
+ SourceID: types.GeneratedQuestionSourceID(chunk.ID, gq.ID),
SourceType: types.ChunkSourceType,
ChunkID: chunk.ID,
KnowledgeID: knowledge.ID,
@@ -2688,7 +2688,7 @@ func (s *knowledgeService) updateChunkVector(ctx context.Context, kbID string, c
for _, q := range meta.GeneratedQuestions {
if strings.TrimSpace(q.Question) != "" {
indexInfo = append(indexInfo, &types.IndexInfo{
- Content: prefix + q.Question, SourceID: fmt.Sprintf("%s-%s", chunk.ID, q.ID),
+ Content: prefix + q.Question, SourceID: types.GeneratedQuestionSourceID(chunk.ID, q.ID),
SourceType: types.ChunkSourceType, ChunkID: chunk.ID,
KnowledgeID: chunk.KnowledgeID, KnowledgeBaseID: chunk.KnowledgeBaseID,
KnowledgeType: sourceKB.Type, IsEnabled: true,
diff --git a/internal/types/faq.go b/internal/types/faq.go
index d145db863..57a98a602 100644
--- a/internal/types/faq.go
+++ b/internal/types/faq.go
@@ -30,6 +30,24 @@ type GeneratedQuestion struct {
Question string `json:"question"` // 问题内容
}
+const maxGeneratedQuestionSourceIDLength = 64
+
+// GeneratedQuestionSourceID builds the retrieval source identifier for a
+// generated question. PostgreSQL stores source_id as varchar(64), while a
+// chunk UUID plus a question UUID would be 73 bytes. Preserve the historical
+// representation for short IDs and hash only oversized question IDs so
+// existing index rows remain addressable by delete/reindex operations.
+func GeneratedQuestionSourceID(chunkID, questionID string) string {
+ candidate := chunkID + "-" + questionID
+ if len(candidate) <= maxGeneratedQuestionSourceIDLength {
+ return candidate
+ }
+ digest := sha256.Sum256([]byte(questionID))
+ // UUID chunk IDs use 36 bytes; "-q" plus 24 hex characters keeps the
+ // complete identifier at 62 bytes while retaining ample collision space.
+ return chunkID + "-q" + hex.EncodeToString(digest[:12])
+}
+
// DocumentChunkMetadata 定义文档 Chunk 的元数据结构
// 用于存储AI生成的问题等增强信息
type DocumentChunkMetadata struct {
diff --git a/internal/types/generated_question_source_id_test.go b/internal/types/generated_question_source_id_test.go
new file mode 100644
index 000000000..b546f5f72
--- /dev/null
+++ b/internal/types/generated_question_source_id_test.go
@@ -0,0 +1,26 @@
+package types
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestGeneratedQuestionSourceID(t *testing.T) {
+ chunkID := "135bf11d-c20e-419a-9f3c-5288d6a0516b"
+ shortQuestionID := "q1722147984000000000"
+ if got, want := GeneratedQuestionSourceID(chunkID, shortQuestionID), chunkID+"-"+shortQuestionID; got != want {
+ t.Fatalf("short source ID changed: got %q, want %q", got, want)
+ }
+
+ longQuestionID := "a4fa83a2-0fde-4b85-99bc-4572fa92d51c"
+ got := GeneratedQuestionSourceID(chunkID, longQuestionID)
+ if len(got) > maxGeneratedQuestionSourceIDLength {
+ t.Fatalf("source ID length = %d, want <= %d: %q", len(got), maxGeneratedQuestionSourceIDLength, got)
+ }
+ if !strings.HasPrefix(got, chunkID+"-q") {
+ t.Fatalf("hashed source ID has unexpected format: %q", got)
+ }
+ if got != GeneratedQuestionSourceID(chunkID, longQuestionID) {
+ t.Fatal("source ID hashing is not deterministic")
+ }
+}