跳到主要内容

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
平台linux, macos
标签RAG, FAISS, 相似性搜索, 向量搜索, Facebook AI, GPU 加速, 十亿规模, K-NN, HNSW, 高性能, 大规模

参考:完整 SKILL.md

信息

以下是当此技能被触发时,Hermes 加载的完整技能定义。这是智能体在技能激活时看到的指令。

FAISS - 高效相似性搜索

Facebook AI 的十亿规模向量相似性搜索库。

何时使用 FAISS

在以下情况下使用 FAISS:

  • 需要在大规模向量数据集上进行快速相似性搜索(数百万/数十亿)
  • 需要 GPU 加速
  • 纯向量相似性(无需元数据过滤)
  • 高吞吐量、低延迟至关重要
  • 对嵌入进行离线/批处理

指标

  • 31,700+ GitHub 星标
  • Meta/Facebook AI 研究
  • 可处理数十亿向量
  • C++ 实现,提供 Python 绑定

改用替代方案

  • Chroma/Pinecone:需要元数据过滤
  • Weaviate:需要完整的数据库功能
  • Annoy:更简单,功能较少

快速入门

安装

# 仅 CPU
pip install faiss-cpu

# GPU 支持
pip install faiss-gpu

基本用法

import faiss
import numpy as np

# 创建样本数据 (1000 个向量, 128 维)
d = 128
nb = 1000
vectors = np.random.random((nb, d)).astype('float32')

# 创建索引
index = faiss.IndexFlatL2(d) # L2 距离
index.add(vectors) # 添加向量

# 搜索
k = 5 # 寻找 5 个最近邻
query = np.random.random((1, d)).astype('float32')
distances, indices = index.search(query, k)

print(f"最近邻: {indices}")
print(f"距离: {distances}")

索引类型

1. Flat (精确搜索)

# L2 (欧氏) 距离
index = faiss.IndexFlatL2(d)

# 内积 (如果向量已归一化,则为余弦相似性)
index = faiss.IndexFlatIP(d)

# 最慢,最精确

2. IVF (倒排文件) - 快速近似搜索

# 创建量化器
quantizer = faiss.IndexFlatL2(d)

# IVF 索引,包含 100 个聚类
nlist = 100
index = faiss.IndexIVFFlat(quantizer, d, nlist)

# 在数据上训练
index.train(vectors)

# 添加向量
index.add(vectors)

# 搜索 (nprobe = 要搜索的聚类数)
index.nprobe = 10
distances, indices = index.search(query, k)

3. HNSW (分层 NSW) - 最佳质量/速度比

# HNSW 索引
M = 32 # 每层连接数
index = faiss.IndexHNSWFlat(d, M)

# 无需训练
index.add(vectors)

# 搜索
distances, indices = index.search(query, k)

4. 乘积量化 (PQ) - 内存高效

# PQ 可将内存减少 16-32 倍
m = 8 # 子量化器数量
nbits = 8
index = faiss.IndexPQ(d, m, nbits)

# 训练并添加
index.train(vectors)
index.add(vectors)

保存和加载

# 保存索引
faiss.write_index(index, "large.index")

# 加载索引
index = faiss.read_index("large.index")

# 继续使用
distances, indices = index.search(query, k)

GPU 加速

# 单 GPU
res = faiss.StandardGpuResources()
index_cpu = faiss.IndexFlatL2(d)
index_gpu = faiss.index_cpu_to_gpu(res, 0, index_cpu) # GPU 0

# 多 GPU
index_gpu = faiss.index_cpu_to_all_gpus(index_cpu)

# 比 CPU 快 10-100 倍

LangChain 集成

from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings

# 创建 FAISS 向量存储
vectorstore = FAISS.from_documents(docs, OpenAIEmbeddings())

# 保存
vectorstore.save_local("faiss_index")

# 加载
vectorstore = FAISS.load_local(
"faiss_index",
OpenAIEmbeddings(),
allow_dangerous_deserialization=True
)

# 搜索
results = vectorstore.similarity_search("query", k=5)

LlamaIndex 集成

from llama_index.vector_stores.faiss import FaissVectorStore
import faiss

# 创建 FAISS 索引
d = 1536
faiss_index = faiss.IndexFlatL2(d)

vector_store = FaissVectorStore(faiss_index=faiss_index)

最佳实践

  1. 选择正确的索引类型 - <10K 用 Flat,10K-1M 用 IVF,追求质量用 HNSW
  2. 余弦相似性需归一化 - 对归一化向量使用 IndexFlatIP
  3. 大数据集使用 GPU - 快 10-100 倍
  4. 保存训练好的索引 - 训练开销大
  5. 调优 nprobe/ef_search - 平衡速度/准确率
  6. 监控内存 - 大数据集使用 PQ
  7. 批量查询 - 更好地利用 GPU

性能

索引类型构建时间搜索时间内存占用准确率
Flat100%
IVF中等中等95-99%
HNSW最快99%
PQ中等90-95%

资源