Saving Embeddings with FAISS – The Smart Choice

FAISS is a similarity search library developed by Facebook AI. It allows you to index and search embeddings efficiently even at large scale.

It supports fast retrieval using various distance metrics, GPU acceleration, and can handle millions of vectors.

Python Example: Save & Load FAISS Index


import faiss
import numpy as np

# Step 1: Generate some random embeddings (vectors)
dimension = 128  # size of each embedding
num_vectors = 1000
embeddings = np.random.random((num_vectors, dimension)).astype('float32')

# Step 2: Create a FAISS index
index = faiss.IndexFlatL2(dimension)  # L2 distance metric

# Step 3: Add embeddings to the index
index.add(embeddings)

# Step 4: Save the index to a file
faiss.write_index(index, "faiss_index.index")

# Step 5: Load the index back
loaded_index = faiss.read_index("faiss_index.index")

# Step 6: Perform a search
query = np.random.random((1, dimension)).astype('float32')
D, I = loaded_index.search(query, k=5)  # Search for top-5 nearest neighbors

print("Distances:", D)
print("Indices:", I)