similarity-search-patterns
它能做什么
这个工具可以在海量数据中查找相似的内容,就像在庞大的商品目录里搜索“相似商品”一样。它采用先进技术,快速而高效地完成这项工作。
适用场景
- ✓当你想搭建一个能理解查询含义的搜索系统时。
- ✓当你要搭建基于文档回答问题的系统时。
- ✓当你想为商品或内容生成个性化推荐时。
技能链接
https://github.com/wshobson/agents/blob/main/plugins/llm-application-dev/skills/similarity-search-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“帮我找出与这段关于纯素烹饪的文字相似的文档。”
每周 5 个新技能,1 分钟读完。
工作原理
在内部,它会把数据转换成数字(向量),再用数学方法计算哪些数字彼此最接近。随后,它把这些数字整理成特殊的索引,从而实现非常快速的搜索。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Similarity Search Patterns
Patterns for implementing efficient similarity search in production systems.
When to Use This Skill
- Building semantic search systems
- Implementing RAG retrieval
- Creating recommendation engines
- Optimizing search latency
- Scaling to millions of vectors
- Combining semantic and keyword search
Core Concepts
1. Distance Metrics
| Metric | Formula | Best For | | ------------------ | ------------------ | --------------------- | --- | -------------- | | Cosine | 1 - (A·B)/(‖A‖‖B‖) | Normalized embeddings | | Euclidean (L2) | √Σ(a-b)² | Raw embeddings | | Dot Product | A·B | Magnitude matters | | Manhattan (L1) | Σ | a-b | | Sparse vectors |
2. Index Types
┌─────────────────────────────────────────────────┐
│ Index Types │
├─────────────┬───────────────┬───────────────────┤
│ Flat │ HNSW │ IVF+PQ │
│ (Exact) │ (Graph-based) │ (Quantized) │
├─────────────┼───────────────┼───────────────────┤
│ O(n) search │ O(log n) │ O(√n) │
│ 100% recall │ ~95-99% │ ~90-95% │
│ Small data │ Medium-Large │ Very Large │
└─────────────┴───────────────┴───────────────────┘
Templates and detailed worked examples
Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.
Best Practices
Do's
- Use appropriate index - HNSW for most cases
- Tune parameters - ef_search, nprobe for recall/speed
- Implement hybrid search - Combine with keyword search
- Monitor recall - Measure search quality
- Pre-filter when possible - Reduce search space
Don'ts
- Don't skip evaluation - Measure before optimizing
- Don't over-index - Start with flat, scale up
- Don't ignore latency - P99 matters for UX
- Don't forget costs - Vector storage adds up
常见问题
什么是“距离度量”(distance metrics)?
这些是用来衡量两组数字(向量)之间“远近”的数学公式。它有助于找到相似的信息。
什么是“索引类型”(index types)?
这是几种不同的数据组织方式,用来加快搜索速度。有的更精确,有的更快,每种方式适用于不同规模的数据。
什么是“混合搜索”(hybrid search)?
它是把基于语义的搜索和精确的关键词搜索结合起来。这样得到的结果既与语义相关,又包含你输入的词语。
详情
作者的原始描述
Implement efficient similarity search with vector databases. Use when building semantic search, implementing nearest neighbor queries, or optimizing retrieval performance.
repo: wshobson/agents
热度
- GitHub 星标
- 41k
- 排名
- #6726
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …