5d7c10aeab
- Updated runtime configuration to use __CS_AI_AGENT_WIDGET_CONFIG__ instead of __CS_AGENT_WIDGET_CONFIG__. - Changed message types in support host bridge from "cs-agent" to "cs-ai-agent". - Minified SDK script updated to reflect new AI agent naming conventions. - Adjusted scrollbar styles in main.scss to use .cs-ai-agent-scrollbar instead of .cs-agent-scrollbar.
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package rag
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"cs-ai-agent/internal/ai/rag/vectordb"
|
|
"cs-ai-agent/internal/models"
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
)
|
|
|
|
func (s *index) ensureCollection(ctx context.Context, provider vectordb.Provider, collectionName string, dimension int) error {
|
|
collectionInfo, err := provider.GetCollection(ctx, collectionName)
|
|
if err == nil && collectionInfo != nil {
|
|
return nil
|
|
}
|
|
if dimension <= 0 {
|
|
return fmt.Errorf("invalid embedding dimension: %d", dimension)
|
|
}
|
|
if err := provider.CreateCollection(ctx, collectionName, dimension); err != nil {
|
|
return fmt.Errorf("failed to create collection: %w", err)
|
|
}
|
|
slog.Info("Created collection for knowledge base", "collection", collectionName, "dimension", dimension)
|
|
return nil
|
|
}
|
|
|
|
func (s *index) replaceDocumentChunks(documentID int64, chunkModels []models.KnowledgeChunk) error {
|
|
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
|
|
if err := ctx.Tx.Where("document_id = ?", documentID).Delete(&models.KnowledgeChunk{}).Error; err != nil {
|
|
return err
|
|
}
|
|
for _, chunk := range chunkModels {
|
|
if err := ctx.Tx.Create(&chunk).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *index) replaceFAQChunk(faqID int64, chunkModel *models.KnowledgeChunk) error {
|
|
if chunkModel == nil {
|
|
return nil
|
|
}
|
|
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
|
|
if err := ctx.Tx.Where("faq_id = ?", faqID).Delete(&models.KnowledgeChunk{}).Error; err != nil {
|
|
return err
|
|
}
|
|
return ctx.Tx.Create(chunkModel).Error
|
|
})
|
|
}
|