49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package initialize
|
|
|
|
import (
|
|
"git.echol.cn/loser/st/server/global"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// InitPgSQLExtension 初始化 PostgreSQL 扩展(仅创建 pgvector 扩展)
|
|
// 必须在 AutoMigrate 之前调用
|
|
func InitPgSQLExtension() {
|
|
if global.GVA_CONFIG.System.DbType != "pgsql" {
|
|
return
|
|
}
|
|
|
|
db := global.GVA_DB
|
|
|
|
// 安装 pgvector 扩展(用于向量存储)
|
|
if err := db.Exec("CREATE EXTENSION IF NOT EXISTS vector").Error; err != nil {
|
|
global.GVA_LOG.Error("failed to create pgvector extension", zap.Error(err))
|
|
global.GVA_LOG.Warn("请确保 PostgreSQL 已安装 pgvector 扩展")
|
|
} else {
|
|
global.GVA_LOG.Info("pgvector extension is ready")
|
|
}
|
|
}
|
|
|
|
// CreateVectorIndexes 创建向量索引
|
|
// 必须在 AutoMigrate 之后调用(确保表已存在)
|
|
func CreateVectorIndexes() {
|
|
if global.GVA_CONFIG.System.DbType != "pgsql" {
|
|
return
|
|
}
|
|
|
|
db := global.GVA_DB
|
|
|
|
// 为 ai_memory_vectors 表创建 HNSW 索引(余弦相似度)
|
|
sql := `
|
|
CREATE INDEX IF NOT EXISTS idx_memory_vectors_embedding
|
|
ON ai_memory_vectors
|
|
USING hnsw (embedding vector_cosine_ops)
|
|
`
|
|
|
|
if err := db.Exec(sql).Error; err != nil {
|
|
global.GVA_LOG.Error("failed to create vector indexes", zap.Error(err))
|
|
return
|
|
}
|
|
|
|
global.GVA_LOG.Info("vector indexes created successfully")
|
|
}
|