2026-04-13 18:11:47 +08:00
|
|
|
package rag
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/ai"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/ai/rag/vectordb"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
2026-04-13 18:11:47 +08:00
|
|
|
)
|
|
|
|
|
|
2026-04-19 11:38:05 +08:00
|
|
|
func buildFAQChunkModel(knowledgeBase models.KnowledgeBase, faq models.KnowledgeFAQ, content string) (models.KnowledgeChunk, string) {
|
2026-04-13 18:11:47 +08:00
|
|
|
chunkID := buildKnowledgeFAQChunkVectorID(knowledgeBase.ID, faq.ID, 0)
|
|
|
|
|
now := time.Now()
|
2026-06-02 15:37:50 +08:00
|
|
|
sectionPath := loadKnowledgeDirectoryPath(faq.DirectoryID)
|
2026-04-13 18:11:47 +08:00
|
|
|
return models.KnowledgeChunk{
|
|
|
|
|
KnowledgeBaseID: knowledgeBase.ID,
|
|
|
|
|
FaqID: faq.ID,
|
|
|
|
|
ChunkNo: 0,
|
|
|
|
|
Title: faq.Question,
|
|
|
|
|
Content: content,
|
|
|
|
|
ContentHash: buildChunkContentHash(content),
|
|
|
|
|
CharCount: len([]rune(content)),
|
|
|
|
|
TokenCount: len([]rune(content)) / 2,
|
|
|
|
|
ChunkType: string(enums.KnowledgeChunkTypeFAQ),
|
2026-06-02 15:37:50 +08:00
|
|
|
SectionPath: sectionPath,
|
2026-04-13 18:11:47 +08:00
|
|
|
Provider: string(enums.KnowledgeChunkProviderFAQ),
|
|
|
|
|
VectorID: chunkID,
|
|
|
|
|
Status: enums.StatusOk,
|
|
|
|
|
CreatedAt: now,
|
|
|
|
|
UpdatedAt: now,
|
|
|
|
|
}, chunkID
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 11:38:05 +08:00
|
|
|
func (s *index) prepareFAQVector(ctx context.Context, knowledgeBase models.KnowledgeBase, faq models.KnowledgeFAQ, content string) (vectordb.Vector, models.KnowledgeChunk, int, error) {
|
2026-08-28 22:23:13 +08:00
|
|
|
embeddingBase := fmt.Sprintf("knowledge-index:base:%d:faq:%d:version:%d", knowledgeBase.ID, faq.ID, faq.UpdatedAt.UnixNano())
|
|
|
|
|
embeddingCtx := ai.WithPlatformAIRequestScope(ctx, embeddingBase)
|
|
|
|
|
embeddingCtx = ai.WithPlatformAIRequestPurpose(embeddingCtx, "embedding.faq-index")
|
|
|
|
|
embeddingResult, err := ai.Embedding.GenerateEmbedding(embeddingCtx, content)
|
2026-04-13 18:11:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return vectordb.Vector{}, models.KnowledgeChunk{}, 0, fmt.Errorf("failed to generate embedding for faq %d: %w", faq.ID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chunkModel, chunkID := buildFAQChunkModel(knowledgeBase, faq, content)
|
2026-06-02 15:37:50 +08:00
|
|
|
sectionPath := chunkModel.SectionPath
|
2026-04-13 18:11:47 +08:00
|
|
|
vector := vectordb.Vector{
|
|
|
|
|
ID: chunkID,
|
|
|
|
|
Vector: embeddingResult.Vector,
|
|
|
|
|
Payload: vectordb.ChunkPayload{
|
|
|
|
|
KnowledgeBaseID: knowledgeBase.ID,
|
|
|
|
|
FaqID: faq.ID,
|
|
|
|
|
FaqQuestion: faq.Question,
|
|
|
|
|
ChunkNo: 0,
|
|
|
|
|
ChunkType: string(enums.KnowledgeChunkTypeFAQ),
|
2026-06-02 15:37:50 +08:00
|
|
|
SectionPath: sectionPath,
|
2026-04-13 18:11:47 +08:00
|
|
|
Content: content,
|
|
|
|
|
Title: faq.Question,
|
|
|
|
|
Provider: string(enums.KnowledgeChunkProviderFAQ),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return vector, chunkModel, embeddingResult.Dimension, nil
|
|
|
|
|
}
|