feat: add knowledge directory management for documents and FAQs
- Implemented directory validation in Create and Update knowledge document services. - Added directory selection in document and FAQ edit dialogs with a new KnowledgeDirectoryPanel component. - Enhanced API to support fetching, creating, updating, and deleting knowledge directories. - Updated data models to include directory information for knowledge documents and FAQs. - Added translations for directory-related messages in English and Chinese.
This commit is contained in:
@@ -47,6 +47,7 @@ func (s *index) prepareDocumentVectors(ctx context.Context, knowledgeBase models
|
||||
vectors := make([]vectordb.Vector, 0, len(chunks))
|
||||
chunkModels := make([]models.KnowledgeChunk, 0, len(chunks))
|
||||
dimension := 0
|
||||
directoryPath := loadKnowledgeDirectoryPath(document.DirectoryID)
|
||||
|
||||
for i, chunk := range chunks {
|
||||
embeddingResult, err := ai.Embedding.GenerateEmbedding(ctx, chunk.Content)
|
||||
@@ -65,6 +66,7 @@ func (s *index) prepareDocumentVectors(ctx context.Context, knowledgeBase models
|
||||
providerName = value
|
||||
}
|
||||
}
|
||||
sectionPath := joinKnowledgeSectionPath(directoryPath, chunk.SectionPath)
|
||||
now := time.Now()
|
||||
chunkModels = append(chunkModels, models.KnowledgeChunk{
|
||||
KnowledgeBaseID: knowledgeBase.ID,
|
||||
@@ -76,7 +78,7 @@ func (s *index) prepareDocumentVectors(ctx context.Context, knowledgeBase models
|
||||
CharCount: chunk.CharCount,
|
||||
TokenCount: chunk.TokenCount,
|
||||
ChunkType: string(chunk.ChunkType),
|
||||
SectionPath: chunk.SectionPath,
|
||||
SectionPath: sectionPath,
|
||||
Provider: providerName,
|
||||
VectorID: chunkID,
|
||||
Status: enums.StatusOk,
|
||||
@@ -93,7 +95,7 @@ func (s *index) prepareDocumentVectors(ctx context.Context, knowledgeBase models
|
||||
DocumentTitle: document.Title,
|
||||
ChunkNo: chunk.ChunkNo,
|
||||
ChunkType: string(chunk.ChunkType),
|
||||
SectionPath: chunk.SectionPath,
|
||||
SectionPath: sectionPath,
|
||||
Content: chunk.Content,
|
||||
Title: chunk.Title,
|
||||
Provider: providerName,
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
func buildFAQChunkModel(knowledgeBase models.KnowledgeBase, faq models.KnowledgeFAQ, content string) (models.KnowledgeChunk, string) {
|
||||
chunkID := buildKnowledgeFAQChunkVectorID(knowledgeBase.ID, faq.ID, 0)
|
||||
now := time.Now()
|
||||
sectionPath := loadKnowledgeDirectoryPath(faq.DirectoryID)
|
||||
return models.KnowledgeChunk{
|
||||
KnowledgeBaseID: knowledgeBase.ID,
|
||||
FaqID: faq.ID,
|
||||
@@ -24,6 +25,7 @@ func buildFAQChunkModel(knowledgeBase models.KnowledgeBase, faq models.Knowledge
|
||||
CharCount: len([]rune(content)),
|
||||
TokenCount: len([]rune(content)) / 2,
|
||||
ChunkType: string(enums.KnowledgeChunkTypeFAQ),
|
||||
SectionPath: sectionPath,
|
||||
Provider: string(enums.KnowledgeChunkProviderFAQ),
|
||||
VectorID: chunkID,
|
||||
Status: enums.StatusOk,
|
||||
@@ -39,6 +41,7 @@ func (s *index) prepareFAQVector(ctx context.Context, knowledgeBase models.Knowl
|
||||
}
|
||||
|
||||
chunkModel, chunkID := buildFAQChunkModel(knowledgeBase, faq, content)
|
||||
sectionPath := chunkModel.SectionPath
|
||||
vector := vectordb.Vector{
|
||||
ID: chunkID,
|
||||
Vector: embeddingResult.Vector,
|
||||
@@ -48,6 +51,7 @@ func (s *index) prepareFAQVector(ctx context.Context, knowledgeBase models.Knowl
|
||||
FaqQuestion: faq.Question,
|
||||
ChunkNo: 0,
|
||||
ChunkType: string(enums.KnowledgeChunkTypeFAQ),
|
||||
SectionPath: sectionPath,
|
||||
Content: content,
|
||||
Title: faq.Question,
|
||||
Provider: string(enums.KnowledgeChunkProviderFAQ),
|
||||
|
||||
@@ -2,6 +2,7 @@ package rag
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/internal/pkg/enums"
|
||||
@@ -18,6 +19,35 @@ func (s *index) loadDocumentByID(documentID int64) (*models.KnowledgeDocument, e
|
||||
return document, nil
|
||||
}
|
||||
|
||||
func loadKnowledgeDirectoryPath(directoryID int64) string {
|
||||
if directoryID <= 0 {
|
||||
return ""
|
||||
}
|
||||
item := repositories.KnowledgeDirectoryRepository.Get(sqls.DB(), directoryID)
|
||||
if item == nil {
|
||||
return ""
|
||||
}
|
||||
if item.ParentID <= 0 {
|
||||
return item.Name
|
||||
}
|
||||
parent := repositories.KnowledgeDirectoryRepository.Get(sqls.DB(), item.ParentID)
|
||||
if parent == nil {
|
||||
return item.Name
|
||||
}
|
||||
return parent.Name + " / " + item.Name
|
||||
}
|
||||
|
||||
func joinKnowledgeSectionPath(parts ...string) string {
|
||||
ret := make([]string, 0, len(parts))
|
||||
for _, item := range parts {
|
||||
item = strings.TrimSpace(item)
|
||||
if item != "" {
|
||||
ret = append(ret, item)
|
||||
}
|
||||
}
|
||||
return strings.Join(ret, " / ")
|
||||
}
|
||||
|
||||
func (s *index) loadFAQByID(faqID int64) (*models.KnowledgeFAQ, error) {
|
||||
faq := repositories.KnowledgeFAQRepository.Get(sqls.DB(), faqID)
|
||||
if faq == nil {
|
||||
|
||||
Reference in New Issue
Block a user