mcpField
F
faiss

faiss

github.com/orchestra-research数据与数据库14.1k1.1k 次安装19 周前更新
代码仓库

关于 faiss

这是一份 FAISS 技能指南,介绍这款带有 Python 绑定的 C++ 库如何对大型向量集合进行相似度搜索。内容涵盖索引选择、GPU 加速、索引持久化,以及嵌入搜索工作流集成。

主要功能

  1. 01精确与近似索引
  2. 02GPU 加速搜索
  3. 03节省内存的乘积量化
  4. 04保存和加载索引
  5. 05集成 LangChain 与 LlamaIndex

适用场景

  1. 01搜索大型嵌入向量集合
  2. 02按速度和准确度选择索引
  3. 03持久化并复用向量索引

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/orchestra-research/ai-research-skills --skill faiss

安装后在 Claude Code 中输入/faiss确认已加载。

附带文件references/index_types.md

详细介绍

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)

最佳实践

  1. 选择合适的索引类型 - 少于 10K 条用 Flat,10K-1M 条用 IVF,追求质量用 HNSW
  2. 为余弦相似度进行归一化 - 使用归一化后的向量搭配 IndexFlatIP
  3. 大型数据集使用 GPU - 速度快 10-100 倍
  4. 保存训练好的索引 - 训练开销较大
  5. 调整 nprobe/ef_search - 平衡速度和准确度
  6. 监控内存 - 大型数据集使用 PQ
  7. 批量查询 - 更充分地利用 GPU

性能

索引类型构建时间搜索时间内存准确度
Flat快慢高100%
IVF中等快中等95-99%
HNSW慢最快高99%
PQ中等快低90-95%

资源

评论

支持 Markdown