Faiss
Facebook 用于高效相似性搜索和稠密向量聚类的库。支持数十亿级向量、GPU 加速以及各种索引类型(Flat、IVF、HNSW)。适用于快速 k-NN 搜索、大规模向量检索,或需要在无元数据情况下进行纯相似性搜索的场景。最适合高性能应用。
技能元数据
| 来源 | 可选 — 使用 hermes skills install official/mlops/faiss 安装 |
| 路径 | optional-skills/mlops/faiss |
| 版本 | 1.0.0 |
| 作者 | Orchestra Research |
| 许可证 | MIT |
| 依赖项 | faiss-cpu, faiss-gpu, numpy |
| 标签 | RAG, FAISS, Similarity Search, Vector Search, Facebook AI, GPU Acceleration, Billion-Scale, K-NN, HNSW, High Performance, Large Scale |
参考:完整 SKILL.md
信息
以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理所看到的指令。
FAISS - 高效相似性搜索
Facebook AI 用于十亿级向量相似性搜索的库。
何时使用 FAISS
在以下情况使用 FAISS:
- 需要在大型向量数据集(数百万/数十亿)上进行快速相似性搜索
- 需要 GPU 加速
- 纯向量相似性(不需要元数据过滤)
- 高吞吐量、低延迟至关重要
- 嵌入的离线/批处理
指标:
- 31,700+ GitHub 星标
- 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