2026-04-13 18:11:47 +08:00
package rag
import (
"context"
"fmt"
"log/slog"
2026-08-21 00:41:07 +08:00
"code.tczkiot.com/wlw/ai-agent/internal/ai/rag/vectordb"
2026-04-13 18:11:47 +08:00
)
func ( s * index ) ensureCollection ( ctx context . Context , provider vectordb . Provider , collectionName string , dimension int ) error {
2026-08-28 22:23:13 +08:00
if dimension <= 0 {
return fmt . Errorf ( "invalid embedding dimension: %d" , dimension )
}
2026-04-13 18:11:47 +08:00
collectionInfo , err := provider . GetCollection ( ctx , collectionName )
if err == nil && collectionInfo != nil {
2026-08-28 22:23:13 +08:00
if collectionInfo . Dimension != dimension {
return fmt . Errorf ( "knowledge vector collection dimension is %d, but the current embedding model uses %d; switch back to the original embedding model or recreate the vector collection and rebuild all knowledge base indexes" , collectionInfo . Dimension , dimension )
}
2026-04-13 18:11:47 +08:00
return nil
}
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
}