faiss について
FAISSを使ったスキルガイドです。Pythonバインディングを備えたC++ライブラリによる大規模ベクトル集合の類似検索を扱い、インデックス選択、GPU高速化、保存、連携方法を説明します。
主な機能
- 01厳密・近似検索インデックス
- 02GPUによる検索高速化
- 03省メモリな積量子化
- 04インデックスの保存と読み込み
- 05LangChain・LlamaIndex連携
活用シーン
- 01大規模な埋め込みベクトル集合の検索
- 02速度と精度に応じたインデックス選択
- 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 stars
- 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
コメント
Markdown 対応