FAISS: Fast Vector Similarity Search
Use Facebook AI's FAISS library to run fast vector similarity search on large embedding datasets, from a few vectors up to billions.
How to use
FAISS suits fast similarity search on large vector datasets with millions or billions of vectors.
It fits when GPU acceleration is required.
It covers pure vector similarity, without metadata filtering.
It is aimed at cases where high throughput and low latency are critical.
It also handles offline or batch processing of embeddings.
If you need metadata filtering, use Chroma or Pinecone instead.
If you need full database features, use Weaviate instead.
Annoy is a simpler alternative with fewer features.
Choose the right index type: Flat for fewer than 10K vectors, IVF for 10K–1M, and HNSW for quality.
Use the GPU for large datasets, which is 10–100× faster.
Monitor memory, and use PQ for large datasets.
Index types differ in trade-offs: Flat builds fast but searches slowly with high memory and 100% accuracy; IVF has medium build time, fast search, medium memory and 95–99% accuracy; HNSW builds slowly but searches fastest with high memory and 99% accuracy; PQ has medium build time, fast search, low memory and 90–95% accuracy.
Install FAISS with the CPU-only package or the GPU-supported package.
# CPU only
pip install faiss-cpu
# GPU support
pip install faiss-gpu
Run the basic usage example to create an index, add vectors, and search for nearest neighbors.
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}")
Use a flat index for exact search, with L2 distance or inner product; it is the slowest but most accurate option.
# L2 (Euclidean) distance
index = faiss.IndexFlatL2(d)
# Inner product (cosine similarity if normalized)
index = faiss.IndexFlatIP(d)
# Slowest, most accurate
Use an IVF (inverted file) index for fast approximate search: create a quantizer, build the index with clusters, train on data, add vectors, and set the number of clusters to search.
# 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)
Use an HNSW (Hierarchical NSW) index for the best quality/speed trade-off; it needs no training, so you can add vectors and search directly.
# 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)
Use Product Quantization for a memory-efficient index, reducing memory by 16–32×; train and add vectors as with other indexes.
# 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 a trained index to a file and load it back to continue searching.
# Save index
faiss.write_index(index, "large.index")
# Load index
index = faiss.read_index("large.index")
# Continue using
distances, indices = index.search(query, k)
Enable GPU acceleration by moving a CPU index to a single GPU or to all GPUs, which is 10–100× faster than CPU.
# 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
Integrate FAISS with LangChain to build a vector store from documents, save it locally, load it back, and run similarity searches.
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)
Integrate FAISS with LlamaIndex by creating a FAISS index and wrapping it in the FaissVectorStore.
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)
For cosine similarity, use IndexFlatIP with normalized vectors.
Save trained indices, since training is expensive.
Tune nprobe or ef_search to balance speed and accuracy.
Batch queries for better GPU utilization.
FAISS is licensed under MIT.
Source: nousresearch/hermes-agent and its contributors. License: MIT.
Copyright (c) 2025 Nous Research
Translation and editorial adaptation by ClawCases.
>Resources
>Related Use Cases
Agent Merge-Conflict Arbiter
Resolve a git merge conflict between two agents' branches as an impartial third party: a neutral arbiter that takes both diffs and their stated intents and produces a merged result without overwriting either side.
Autonomous Project Management with Subagents
Coordinate projects autonomously: a main agent delegates work to PM subagents that track tasks in a shared STATE.yaml, update it as they progress, pick up unblocked work in parallel, and keep all task state in version-controlled files.
Control Sonos speakers with sonoscli
Use sonoscli to control Sonos speakers on the local network.