FAISS
Abstract
FAISS — a library from Meta for fast vector similarity search. Not a database, but a search engine.
What It Does
- Fast search over millions of vectors;
- Runs on CPU and GPU;
- Supports various indexing algorithms;
- Integrates with LangChain.
Use Cases
- Local vector search;
- Accelerating RAG on your machine;
- Research tasks without a separate database.
FAISS vs ChromaDB
| FAISS | ChromaDB | |
|---|---|---|
| Type | Library | Database |
| Storage | No metadata | Yes |
| Suitable For | Pure search | RAG applications |
Getting Started
Install via pip:
pip install faiss-cpu
For NVIDIA GPUs — faiss-gpu. Minimal example:
import faiss
import numpy as np
# 1000 vectors of dimension 128
vectors = np.random.random((1000, 128)).astype("float32")
index = faiss.IndexFlatL2(128) # index using Euclidean distance
index.add(vectors)
# Search for 5 nearest vectors to the query
query = np.random.random((1, 128)).astype("float32")
distances, ids = index.search(query, k=5)
print(ids) # indices of nearest vectors
IndexFlatL2 is the simplest and most accurate index. For larger volumes, use compressed indexes (IndexIVFFlat, IndexHNSW), which are faster but with slight accuracy loss.