Vector index tuning

From findscripter/everything-skills · View interactive version

Vector index tuning is an AI agent skill from the GitHub repository findscripter/everything-skills. 当向量检索延迟高、召回不达标或内存占用过大时使用;做 HNSW 参数(M/efConstruction/efSearch)、量化策略与索引类型选型,产出可基准验证的索引配置与监控指标;不适用于嵌入模型选型、检索结果重排或召回链路的语义优化(属 RAG 上层)。触发词:HNSW、量化、召回率

How to use this skill

Skill file

何时使用

需要在生产环境平衡向量检索的延迟、召回与内存时使用,典型场景:

不该用的边界:

步骤

  1. 按规模选索引类型(先定大方向,再调细节):

| 数据规模 | 推荐索引 |

|---|---|

| < 10K | Flat(暴力精确检索) |

| 10K – 1M | HNSW |

| 1M – 100M | HNSW + 量化 |

| > 100M | IVF + PQ 或 DiskANN |

  1. 从默认值起步,按需调 HNSW 三参数:

| 参数 | 默认 | 作用 |

|---|---|---|

| M | 16 | 每节点连接数,↑ 召回更好、内存更大 |

| efConstruction | 100 | 构建质量,↑ 索引更好、构建更慢 |

| efSearch | 50 | 检索质量,↑ 召回更好、检索更慢 |

经验起点:< 100K 用 M=16/efc=100;< 1M 用 M=32/efc=200;≥ 1M 用 M=48/efc=256。efSearch 按目标召回回退:≥0.99 取 256,≥0.95 取 128,否则 64。

  1. 用真实查询基准,记录 build_time、search_time(ms)、recall@k、memory_mb 四项,网格扫 M × efConstruction × efSearch 取帕累托最优。
  1. 超内存预算时引入量化(按每维字节数估算):FP32=4 / FP16=2 / INT8=1 / PQ≈32–64 字节总量 / Binary=维度/8 字节。
  1. 配置向量库(如 Qdrant,按 recall/speed/memory/balanced 选档),并持续监控 p50/p95/p99 延迟、QPS 与召回,防止数据漂移导致召回衰减。

指令

示例

HNSW 参数网格基准(hnswlib):

import hnswlib, time
index = hnswlib.Index(space='cosine', dim=dim)
index.init_index(max_elements=n, M=m, ef_construction=ef_construction)
index.add_items(vectors)                 # 记录 build_time
index.set_ef(ef_search)                  # 检索前必须设 efSearch
labels, distances = index.knn_query(queries, k=10)
recall = calculate_recall(labels, ground_truth, k=10)

Qdrant 按目标分档建集合 + 检索参数:

hnsw_configs = {
    "recall":   models.HnswConfigDiff(m=32, ef_construct=256),  # 不量化
    "speed":    models.HnswConfigDiff(m=16, ef_construct=64),   # INT8, always_ram
    "balanced": models.HnswConfigDiff(m=16, ef_construct=128),  # INT8
    "memory":   models.HnswConfigDiff(m=8,  ef_construct=64),   # PQ x16
}
# 召回≥0.99:hnsw_ef=256 且 quantization.ignore=True + rescore=True(检索绕过量化精排)
# 召回≥0.95:hnsw_ef=128 + rescore=True + oversampling=2.0

注意事项

该做:

不该做:

互见


采编自 wshobson/agents(MIT 许可)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.