
Faiss
Pick the right FAISS index type and Python snippets when you add fast vector search to embeddings, RAG, or recommendation backends.
Install
npx skills add https://github.com/orchestra-research/ai-research-skills --skill faissWhat is this skill?
- Dataset-size matrix: Flat (<10K), IVF (10K–1M), HNSW (1M–10M), IVF+PQ (>10M)
- IndexFlatL2 and IndexFlatIP recipes with normalize_L2 for cosine similarity
- IVF cluster setup with quantizer, nlist, and training requirements spelled out
- Accuracy vs speed vs memory tradeoffs in one comparison table
Adoption & trust: 1 installs on skills.sh; 9.4k GitHub stars; 2/3 security scanners passed (skills.sh audits).
Recommended Skills
Paper Context Resolverlllllllama/ai-paper-reproduction-skill
Repo Intake And Planlllllllama/ai-paper-reproduction-skill
Env And Assets Bootstraplllllllama/ai-paper-reproduction-skill
Minimal Run And Auditlllllllama/ai-paper-reproduction-skill
Analyze Projectlllllllama/rigorpilot-skills
Ai Research Reproductionlllllllama/rigorpilot-skills
Journey fit
Common Questions / FAQ
Is Faiss safe to install?
skills.sh reports 2 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Faiss
# FAISS Index Types Guide Complete guide to choosing and using FAISS index types. ## Index selection guide | Dataset Size | Index Type | Training | Accuracy | Speed | |--------------|------------|----------|----------|-------| | < 10K | Flat | No | 100% | Slow | | 10K-1M | IVF | Yes | 95-99% | Fast | | 1M-10M | HNSW | No | 99% | Fastest | | > 10M | IVF+PQ | Yes | 90-95% | Fast, low memory | ## Flat indices (exact search) ### IndexFlatL2 - L2 (Euclidean) distance ```python import faiss import numpy as np d = 128 # Dimension index = faiss.IndexFlatL2(d) # Add vectors vectors = np.random.random((1000, d)).astype('float32') index.add(vectors) # Search k = 5 query = np.random.random((1, d)).astype('float32') distances, indices = index.search(query, k) ``` **Use when:** - Dataset < 10,000 vectors - Need 100% accuracy - Serving as baseline ### IndexFlatIP - Inner product (cosine similarity) ```python # For cosine similarity, normalize vectors first import faiss d = 128 index = faiss.IndexFlatIP(d) # Normalize vectors (required for cosine similarity) faiss.normalize_L2(vectors) index.add(vectors) # Search faiss.normalize_L2(query) distances, indices = index.search(query, k) ``` **Use when:** - Need cosine similarity - Recommendation systems - Text embeddings ## IVF indices (inverted file) ### IndexIVFFlat - Cluster-based search ```python # Create quantizer quantizer = faiss.IndexFlatL2(d) # Create IVF index with 100 clusters nlist = 100 # Number of clusters index = faiss.IndexIVFFlat(quantizer, d, nlist) # Train on data (required!) index.train(vectors) # Add vectors index.add(vectors) # Search (nprobe = clusters to search) index.nprobe = 10 # Search 10 closest clusters distances, indices = index.search(query, k) ``` **Parameters:** - `nlist`: Number of clusters (√N to 4√N recommended) - `nprobe`: Clusters to search (1-nlist, higher = more accurate) **Use when:** - Dataset 10K-1M vectors - Need fast approximate search - Can afford training time ### Tuning nprobe ```python # Test different nprobe values for nprobe in [1, 5, 10, 20, 50]: index.nprobe = nprobe distances, indices = index.search(query, k) # Measure recall/speed trade-off ``` **Guidelines:** - `nprobe=1`: Fastest, ~50% recall - `nprobe=10`: Good balance, ~95% recall - `nprobe=nlist`: Exact search (same as Flat) ## HNSW indices (graph-based) ### IndexHNSWFlat - Hierarchical NSW ```python # HNSW index M = 32 # Number of connections per layer (16-64) index = faiss.IndexHNSWFlat(d, M) # Optional: Set ef_construction (build time parameter) index.hnsw.efConstruction = 40 # Higher = better quality, slower build # Add vectors (no training needed!) index.add(vectors) # Search index.hnsw.efSearch = 16 # Search time parameter distances, indices = index.search(query, k) ``` **Parameters:** - `M`: Connections per layer (16-64, default 32) - `efConstruction`: Build quality (40-200, higher = better) - `efSearch`: Search quality (16-512, higher = more accurate) **Use when:** - Need best quality approximate search - Can afford higher memory (more connections) - Dataset 1M-10M vectors ## PQ indices (product quantization) ### IndexPQ - Memory-efficient ```python # PQ reduces memory by 16-32× m = 8 # Number of subquantizers (divides d) nbits = 8 # Bits per subquantizer index = faiss.IndexPQ(d, m, nbits) # Train (required!) index.train(vectors) # Add vectors index.add(vectors) # Search distances, indices = index.search(query, k) ``` **Parameters:** - `m`: Subquantizers (d must be divisible by m) - `nbits`: Bits per code (8 or 16) **Memory savings:** - Original: d × 4 bytes (float32) - PQ: m bytes - Compression ratio: 4d/m **Use when:** - Limited memory - Large datasets (> 10M vectors) - Can accept ~90-95% accuracy ### IndexIVFPQ - IVF + PQ combined ```python # Best for very large datasets nlist = 4096 m = 8 nbits = 8 quantizer = faiss.IndexFlatL2(d) index = faiss.IndexIVFPQ(quantizer, d, nlist, m, nbits) # Train index.train(v