关于 faiss
faiss 是面向大规模向量集合的相似性搜索库,适用于需要低延迟、高吞吐检索或 GPU 加速的开发者。它提供精确与近似索引,并支持向量索引的保存和载入。
主要功能
- 01大规模向量相似性搜索
- 02精确与近似索引类型
- 03GPU 与多 GPU 加速
- 04产品量化以节省内存
- 05索引保存与载入
适用场景
- 01搜索百万或十亿级向量
- 02批量处理嵌入向量
- 03构建语义检索系统
- 04在 GPU 上加速向量查询
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ kalarislabs/ research-agent-skills --skill faiss安装后在 Claude Code 中输入/faiss确认已加载。
附带文件references/index_types.md
详细介绍
FAISS - 高效相似性搜索
Facebook AI 用于十亿级向量相似性搜索的库。
何时使用 FAISS
在以下情况下使用 FAISS:
- 需要在大型向量数据集(数百万/数十亿条)上进行快速相似性搜索
- 需要 GPU 加速
- 仅需向量相似性(无需元数据筛选)
- 高吞吐量和低延迟至关重要
- 对嵌入进行离线/批处理
指标:
- Meta/Facebook AI Research
- 可处理数十亿个向量
- 使用 C++,并提供 Python 绑定
改用其他方案:
- Chroma/Pinecone:需要元数据筛选
- Weaviate:需要完整的数据库功能
- Annoy:更简单,功能较少
快速入门
安装
# CPU only
pip install faiss-cpu
# GPU support
pip install faiss-gpu
基本用法
import faiss
import numpy as np
# Create sample data (1000 vectors, 128 dimensions)
d = 128
nb = 1000
vectors = np.random.random((nb, d)).astype('float32')
# Create index
index = faiss.IndexFlatL2(d) # L2 distance
index.add(vectors) # Add vectors
# Search
k = 5 # Find 5 nearest neighbors
query = np.random.random((1, d)).astype('float32')
distances, indices = index.search(query, k)
print(f"Nearest neighbors: {indices}")
print(f"Distances: {distances}")
索引类型
1. Flat(精确搜索)
# L2 (Euclidean) distance
index = faiss.IndexFlatL2(d)
# Inner product (cosine similarity if normalized)
index = faiss.IndexFlatIP(d)
# Slowest, most accurate
2. IVF(倒排文件)- 快速近似搜索
# Create quantizer
quantizer = faiss.IndexFlatL2(d)
# IVF index with 100 clusters
nlist = 100
index = faiss.IndexIVFFlat(quantizer, d, nlist)
# Train on data
index.train(vectors)
# Add vectors
index.add(vectors)
# Search (nprobe = clusters to search)
index.nprobe = 10
distances, indices = index.search(query, k)
3. HNSW(分层 NSW)- 质量/速度最佳
# HNSW index
M = 32 # Number of connections per layer
index = faiss.IndexHNSWFlat(d, M)
# No training needed
index.add(vectors)
# Search
distances, indices = index.search(query, k)
4. Product Quantization - 节省内存
# PQ reduces memory by 16-32×
m = 8 # Number of subquantizers
nbits = 8
index = faiss.IndexPQ(d, m, nbits)
# Train and add
index.train(vectors)
index.add(vectors)
保存和加载
# Save index
faiss.write_index(index, "large.index")
# Load index
index = faiss.read_index("large.index")
# Continue using
distances, indices = index.search(query, k)
GPU 加速
# Single GPU
res = faiss.StandardGpuResources()
index_cpu = faiss.IndexFlatL2(d)
index_gpu = faiss.index_cpu_to_gpu(res, 0, index_cpu) # GPU 0
# Multi-GPU
index_gpu = faiss.index_cpu_to_all_gpus(index_cpu)
# 10-100× faster than CPU
LangChain 集成
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
# Create FAISS vector store
vectorstore = FAISS.from_documents(docs, OpenAIEmbeddings())
# Save
vectorstore.save_local("faiss_index")
# Load
vectorstore = FAISS.load_local(
"faiss_index",
OpenAIEmbeddings(),
allow_dangerous_deserialization=True
)
# Search
results = vectorstore.similarity_search("query", k=5)
LlamaIndex 集成
from llama_index.vector_stores.faiss import FaissVectorStore
import faiss
# Create FAISS index
d = 1536
faiss_index = faiss.IndexFlatL2(d)
vector_store = FaissVectorStore(faiss_index=faiss_index)
最佳实践
- 选择合适的索引类型 - 少于 10K 条使用 Flat,10K-1M 条使用 IVF,注重质量时使用 HNSW
- 为余弦相似度进行归一化 - 将 IndexFlatIP 与归一化向量搭配使用
- 大型数据集使用 GPU - 速度快 10-100 倍
- 保存训练好的索引 - 训练开销很大
- 调整 nprobe/ef_search - 平衡速度与准确度
- 监控内存 - 大型数据集使用 PQ
- 批量查询 - 更充分地利用 GPU
性能
| 索引类型 | 构建时间 | 搜索时间 | 内存 | 准确度 |
|---|---|---|---|---|
| Flat | 快 | 慢 | 高 | 100% |
| IVF | 中等 | 快 | 中等 | 95-99% |
| HNSW | 慢 | 最快 | 高 | 99% |
| PQ | 中等 | 快 | 低 | 90-95% |
资源
- GitHub:https://github.com/facebookresearch/faiss ⭐ 31,700+
- Wiki:https://github.com/facebookresearch/faiss/wiki
- 许可证:MIT
Agent 操作流程
- 检查环境。 确认语料库、嵌入模型和向量存储的版本,以及索引的存储位置。
- 明确输入。 从数据或用户处确认格式、标识符和参数。任何会改变结果的值都应询问,不要猜测。
- 先运行小规模版本。 为一小部分数据建立索引,并使用已知的问题-段落对测试检索。
- 使用以上说明和参考资料 执行完整任务。
- 验证结果。 在带标签的数据集上测量 recall@k;答案只能引用检索到的段落。
- 报告。 说明运行了什么(版本、命令、参数)、检查了什么,以及仍有哪些不确定之处。
| 如果发生这种情况 | 请这样做 |
|---|---|
| 检索结果返回了无关的文本块 | 重新检查分块方式,添加 BM25/混合搜索,或在更改生成器之前进行重排序。 |
| 这些说明中提到的函数、标志或端点在已安装版本中不存在 | 查阅已安装版本自带的文档(help()、--help、官方文档),调整做法并告知用户。切勿编造 API。 |
| 必需的输入、标识符或参数存在歧义 | 询问用户,或在运行前明确说明假设。 |
完整性规则
- 切勿编造结果、参数、标识符、引文或统计数据。如果无法运行或验证某项内容,请直言说明。
- 答案必须来自检索到的文本;如果语料库中没有答案,请说明。
- 此处与特定版本相关的细节可能已过时:请根据已安装版本的官方文档进行确认。
- 对于会产生费用、占用共享 GPU 或云配额、涉及个人或患者数据,或无法撤销的操作,请先征求同意。
相关技能
pinecone:指导通过 Pinecone 的 Python 客户端以及 LangChain 和 LlamaIndex 集成使用 Pinecone(一种托管式无服务器向量数据库)。qdrant-vector-search:用于 RAG 和语义搜索的高性能向量相似度搜索引擎。chroma:面向 AI 应用的开源嵌入数据库。
评论
支持 Markdown