mirror of
https://git.boos.lat/aibo/BossChat.git
synced 2026-10-31 15:46:12 +00:00
0f40930a41
- Rename Go module: cs-agent → bosschat - Rename all import paths across 200+ Go files - Rename SDK: cs-ai-agent-sdk → bosschat-sdk - Rename message protocol: cs-agent:* → bosschat:* - Rename JS globals: CSAgentConfig → BossChatConfig - Rename CSS classes: cs-agent-* → bosschat-* - Update Makefile APP_NAME and ARCHIVE_NAME - Update logo assets - Fix display name: BoosChat → BossChat
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package rag
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"bosschat/internal/ai/rag/vectordb"
|
|
"bosschat/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
|
|
})
|
|
}
|