docs(comments): 注释中文化 · internal/runtime/memory(31 文件,纯替换)
- 只翻注释/文档字符串:820 增 / 820 删 严格 1:1;剥掉整行注释与行尾注释尾巴后, 逐文件与 HEAD 比对「代码部分差异 = 0」;裁决/design 编号计数逐项与 HEAD 一致 - 保留可执行 SQL 样例、SQLite 报错原文、pgvector/HNSW/ANN/BLOB 等技术词原文 - 复核:go build 绿 + memory/projectindex/vector 三包测试绿;gofmt 仅剩 2 个既有未格式化文件(改动前即如此,未顺手修)
This commit is contained in:
1 parent
eb1828ee7a
commit
61cf80d5ee
31 files changed
+820
-820
No files matched your search
@@ -10,25 +10,25 @@ import (
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/embedding"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib" // Postgres driver, registers name "pgx"
|
||||
_ "github.com/jackc/pgx/v5/stdlib" // Postgres 驱动,注册名为 "pgx"
|
||||
)
|
||||
|
||||
// pgMaxOpenConns is the PostgreSQL connection pool size mandated by
|
||||
// DevDocs/02-development-constraints.md §2.2 for the pgvector enhancement.
|
||||
// pgMaxOpenConns 是 DevDocs/02-development-constraints.md §2.2 为
|
||||
// pgvector 增强规定的 PostgreSQL 连接池大小。
|
||||
const pgMaxOpenConns = 25
|
||||
|
||||
// PGVectorIndex stores vectors in a PostgreSQL pgvector vector column and
|
||||
// answers similarity with the cosine-distance operator <=> in SQL. It is the
|
||||
// optional enhancement selected when DB_PG_URL is configured; when used,
|
||||
// semantic search is genuinely backed by pgvector and does not fall back to
|
||||
// keyword matching (DevDocs/design/03-记忆.md §3).
|
||||
// PGVectorIndex 把向量存入 PostgreSQL 的 pgvector vector 列,并
|
||||
// 用 SQL 中的余弦距离运算符 <=> 作答相似度。它是配置了
|
||||
// DB_PG_URL 时选中的可选增强;启用时,
|
||||
// 语义检索真正由 pgvector 支撑,不会退化为
|
||||
// 关键词匹配(DevDocs/design/03-记忆.md §3)。
|
||||
//
|
||||
// The schema is prepared by NewPG (vector extension + vec_items table). Search
|
||||
// issues ORDER BY vector <=> $1 LIMIT n, which returns exact cosine-distance-
|
||||
// ranked results. With no ANN index the query planner does a sequential scan,
|
||||
// which is the linear-scan behavior expected for milestone M4 (small datasets).
|
||||
// An HNSW index (CREATE INDEX ... USING hnsw (vector vector_cosine_ops)) is the
|
||||
// documented ANN upgrade for larger datasets and requires no change to Search.
|
||||
// schema 由 NewPG 准备(vector 扩展 + vec_items 表)。Search
|
||||
// 发出 ORDER BY vector <=> $1 LIMIT n,返回精确的余弦距离
|
||||
// 排名结果。没有 ANN 索引时,查询规划器做顺序扫描,
|
||||
// 这正是 M4 里程碑(小数据集)期望的线性扫描行为。
|
||||
// HNSW 索引(CREATE INDEX ... USING hnsw (vector vector_cosine_ops))是
|
||||
// 大数据集下既定的 ANN 升级方案,且无需改动 Search。
|
||||
type PGVectorIndex struct {
|
||||
db *sql.DB
|
||||
dim int
|
||||
@@ -36,16 +36,16 @@ type PGVectorIndex struct {
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewPG connects to the PostgreSQL+pgvector database at pgURL using the pgx
|
||||
// driver and prepares the schema: the vector extension and a vec_items table
|
||||
// with a fixed-dimension vector column. It deliberately creates no ANN index:
|
||||
// for milestone M4 the <=> query runs as an exact sequential scan, and an HNSW
|
||||
// index is a documented, optional upgrade for larger data. A blank pgURL
|
||||
// returns ErrInvalidPGURL, and a non-positive dim returns ErrInvalidDim, both
|
||||
// before any network I/O. ctx controls the connection ping and schema setup.
|
||||
// NewPG 用 pgx 驱动连接到 pgURL 的 PostgreSQL+pgvector 数据库
|
||||
// 并准备 schema:vector 扩展与一张带固定维度 vector 列的
|
||||
// vec_items 表。它刻意不创建 ANN 索引:
|
||||
// M4 里程碑下 <=> 查询走精确顺序扫描,而 HNSW
|
||||
// 索引是既定的大数据量可选升级。pgURL 为空
|
||||
// 返回 ErrInvalidPGURL,dim 非正返回 ErrInvalidDim,两者都在
|
||||
// 任何网络 I/O 之前。ctx 控制连接 ping 与 schema 准备。
|
||||
//
|
||||
// The returned index has no embedder wired in; use New to build a fully wired
|
||||
// index, or wire one in before calling Embed.
|
||||
// 返回的索引没有接入 embedder;用 New 构建完全接好的
|
||||
// 索引,或在调用 Embed 前接入一个。
|
||||
func NewPG(ctx context.Context, pgURL string, dim int, log *slog.Logger) (*PGVectorIndex, error) {
|
||||
if strings.TrimSpace(pgURL) == "" {
|
||||
return nil, ErrInvalidPGURL
|
||||
@@ -74,16 +74,16 @@ func NewPG(ctx context.Context, pgURL string, dim int, log *slog.Logger) (*PGVec
|
||||
return idx, nil
|
||||
}
|
||||
|
||||
// setup creates the pgvector extension and the vec_items table idempotently.
|
||||
// For milestone M4 no ANN index is created — <->/<=> queries fall back to an
|
||||
// exact sequential scan, matching the linear-scan semantics of the SQLite path.
|
||||
// To scale, add:
|
||||
// setup 幂等地创建 pgvector 扩展与 vec_items 表。
|
||||
// M4 里程碑不创建 ANN 索引 —— <->/<=> 查询退化为
|
||||
// 精确顺序扫描,与 SQLite 路径的线性扫描语义一致。
|
||||
// 要扩容,可添加:
|
||||
//
|
||||
// CREATE INDEX IF NOT EXISTS idx_vec_items_vector
|
||||
// ON vec_items USING hnsw (vector vector_cosine_ops)
|
||||
//
|
||||
// which requires no change to Search. Each statement is parameterized where it
|
||||
// takes input; the table DDL is static after dimension substitution.
|
||||
// 且无需改动 Search。每条语句在取输入处都参数化;
|
||||
// 表 DDL 在维度替换后是静态的。
|
||||
func (p *PGVectorIndex) setup(ctx context.Context) error {
|
||||
statements := []string{
|
||||
"CREATE EXTENSION IF NOT EXISTS vector",
|
||||
@@ -101,16 +101,16 @@ func (p *PGVectorIndex) setup(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// withEmbedder wires the vector-producing embedder into the index. Used by New;
|
||||
// tests may also call it directly.
|
||||
// withEmbedder 把产向量的 embedder 接入索引。New 使用它;
|
||||
// 测试也可直接调用。
|
||||
func (p *PGVectorIndex) withEmbedder(e embedding.Embedder) { p.embedder = e }
|
||||
|
||||
// Dim returns the fixed vector dimension.
|
||||
// Dim 返回固定向量维度。
|
||||
func (p *PGVectorIndex) Dim() int { return p.dim }
|
||||
|
||||
// Embed turns texts into vectors with the wired embedder, returning one Vector
|
||||
// per input text aligned by index. With no embedder wired it returns
|
||||
// ErrNoEmbedder.
|
||||
// Embed 用接入的 embedder 把 texts 转成向量,每段输入文本
|
||||
// 对应一个按索引对齐的 Vector。未接入 embedder 时返回
|
||||
// ErrNoEmbedder。
|
||||
func (p *PGVectorIndex) Embed(ctx context.Context, texts []string) ([]Vector, error) {
|
||||
if p.embedder == nil {
|
||||
return nil, ErrNoEmbedder
|
||||
@@ -126,10 +126,10 @@ func (p *PGVectorIndex) Embed(ctx context.Context, texts []string) ([]Vector, er
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Add persists the (id, vector) pair, upserting on a repeated id. The vector is
|
||||
// passed to Postgres as a vector literal bound to a $n::vector parameter. It
|
||||
// rejects a blank id (ErrMissingID) and a vector whose length differs from the
|
||||
// index dimension (ErrDimMismatch).
|
||||
// Add 持久化 (id, vector) 对,id 重复时 upsert。向量以
|
||||
// 绑定到 $n::vector 参数的向量字面量传给 Postgres。它
|
||||
// 拒绝空 id(ErrMissingID)以及与索引维度不一致的
|
||||
// 向量(ErrDimMismatch)。
|
||||
func (p *PGVectorIndex) Add(ctx context.Context, id string, v Vector) error {
|
||||
if strings.TrimSpace(id) == "" {
|
||||
return ErrMissingID
|
||||
@@ -147,10 +147,10 @@ func (p *PGVectorIndex) Add(ctx context.Context, id string, v Vector) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Search returns up to topK ids most similar to q, ranked by descending cosine
|
||||
// similarity (1 - cosine distance), computed by pgvector in SQL. A non-positive
|
||||
// topK returns an empty result; a query whose length differs from the index
|
||||
// dimension returns ErrDimMismatch.
|
||||
// Search 返回与 q 最相似的最多 topK 个 id,按余弦相似度
|
||||
// (1 - 余弦距离)降序排列,由 pgvector 在 SQL 中计算。topK 非正
|
||||
// 返回空结果;长度与索引维度不一致的查询
|
||||
// 返回 ErrDimMismatch。
|
||||
func (p *PGVectorIndex) Search(ctx context.Context, q Vector, topK int) ([]Hit, error) {
|
||||
if topK <= 0 {
|
||||
return []Hit{}, nil
|
||||
@@ -184,7 +184,7 @@ func (p *PGVectorIndex) Search(ctx context.Context, q Vector, topK int) ([]Hit,
|
||||
return hits, nil
|
||||
}
|
||||
|
||||
// Close releases the underlying PostgreSQL connection pool.
|
||||
// Close 释放底层的 PostgreSQL 连接池。
|
||||
func (p *PGVectorIndex) Close() error {
|
||||
if p.db == nil {
|
||||
return nil
|
||||
@@ -196,9 +196,9 @@ func (p *PGVectorIndex) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// pgVectorLiteral renders v in pgvector's text form "[a,b,c]" so it can be
|
||||
// bound as a vector parameter ($n::vector). Values are formatted with enough
|
||||
// precision to round-trip a float32.
|
||||
// pgVectorLiteral 把 v 渲染成 pgvector 的文本形式 "[a,b,c]",
|
||||
// 以便绑定为向量参数($n::vector)。取值的格式化精度足以
|
||||
// 往返一个 float32。
|
||||
func pgVectorLiteral(v Vector) string {
|
||||
var b strings.Builder
|
||||
b.WriteByte('[')
|
||||
|
||||
Reference in new issue
Block a user