Details (original message): Add the vector RAG pipeline: paragraph chunker, OpenAI-compatible embeddings and rerank clients, dual-tier vector store with cosine search, and RRF hybrid retrieval
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// dialect 收敛 SQLite / PostgreSQL 差异。全部查询以 ? 占位符书写,PG 执行前 rebind。
|
|
type dialect interface {
|
|
name() string
|
|
rebind(query string) string
|
|
// lockConversation 在事务内锁定会话的事件序列分配(跨实例安全)。
|
|
// PG:事务级咨询锁;SQLite:单写连接天然串行,无需额外锁。
|
|
lockConversation(tx *sql.Tx, conversationID string) error
|
|
// lockScope 锁定设置 scope 的读改写(owner+scope)。
|
|
lockScope(tx *sql.Tx, owner, scope string) error
|
|
}
|
|
|
|
// ---- SQLite(嵌入式档)----
|
|
|
|
type sqliteDialect struct{}
|
|
|
|
func (sqliteDialect) name() string { return "sqlite" }
|
|
func (sqliteDialect) rebind(q string) string { return q }
|
|
func (sqliteDialect) lockConversation(*sql.Tx, string) error { return nil }
|
|
func (sqliteDialect) lockScope(*sql.Tx, string, string) error { return nil }
|
|
|
|
// ---- PostgreSQL(多用户/多实例档)----
|
|
|
|
type postgresDialect struct{}
|
|
|
|
func (postgresDialect) name() string { return "postgres" }
|
|
|
|
func (postgresDialect) rebind(q string) string {
|
|
var b strings.Builder
|
|
n := 0
|
|
for i := 0; i < len(q); i++ {
|
|
if q[i] == '?' {
|
|
n++
|
|
fmt.Fprintf(&b, "$%d", n)
|
|
continue
|
|
}
|
|
b.WriteByte(q[i])
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (postgresDialect) lockConversation(tx *sql.Tx, conversationID string) error {
|
|
_, err := tx.Exec(`SELECT pg_advisory_xact_lock(hashtext($1))`, "conv:"+conversationID)
|
|
return err
|
|
}
|
|
|
|
func (postgresDialect) lockScope(tx *sql.Tx, owner, scope string) error {
|
|
_, err := tx.Exec(`SELECT pg_advisory_xact_lock(hashtext($1))`, "scope:"+owner+"/"+scope)
|
|
return err
|
|
}
|