fix(knowledge): bound generated question source IDs

main
wizardchen 2026-07-28 14:29:49 +08:00 committed by lyingbug
parent ac3eccddf5
commit 95775ae909
4 changed files with 49 additions and 5 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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 {

View File

@ -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")
}
}