Skip to main content

Vector Store with FAISS

FAISS, or Facebook AI Similarity Search, is a library used for efficient similarity search and clustering of high-dimensional vectors. It is commonly used in AI applications such as semantic search, Retrieval-Augmented Generation (RAG), recommendation systems, and document retrieval.

Topics Covered

  • Indexing vectors with FAISS
  • Persisting a FAISS index
  • Loading and querying an index

1. What is a Vector Store?

A vector store stores numerical representations called embeddings. For example, a sentence embedding model converts text into a vector:
Similar sentences produce vectors that are close together in vector space.
FAISS allows us to efficiently store these vectors and find the most similar vectors to a query.

2. Installing FAISS

Install the required libraries:
For NVIDIA GPU support, FAISS also provides GPU builds, but faiss-cpu is sufficient for learning and small projects.

3. Indexing Vectors with FAISS

The basic workflow is:

Important FAISS Concepts

Vector dimension

Every embedding has a fixed number of dimensions. For example:
This model produces embeddings with:
FAISS needs to know this dimension when creating an index.

IndexFlatL2

A simple FAISS index is:
IndexFlatL2 performs similarity search using Euclidean distance. The lower the distance:

4. Complete Example: Create and Query a Vector Store

Create a file named:
Example output:

5. How FAISS Search Works

When you execute:
FAISS returns two values.

distances

Contains the similarity distances:
Example:
With IndexFlatL2, smaller values indicate more similar vectors.

indices

Contains the positions of matching vectors:
Example:
These positions correspond to the original document list:

6. Persisting a FAISS Index

Creating embeddings can take time for large datasets. Instead of recreating the FAISS index every time, we can save it to disk. FAISS provides:

Save the Index

This creates:
You should also save the original documents because FAISS stores vectors but does not automatically store your document text or metadata.

7. Complete Example: Save Documents and FAISS Index

After running the program:

8. Loading and Querying the Index

Create a file named:

9. FAISS Vector Store Workflow

A key rule is:
Use the same embedding model for both document embeddings and query embeddings.
If you create document vectors using one model and query vectors using another incompatible model, the similarity search results may not be meaningful.

10. Using Cosine Similarity with FAISS

Sentence embeddings are often compared using cosine similarity. To use cosine similarity in FAISS:
  1. Normalize the vectors.
  2. Use IndexFlatIP.
With normalized vectors:
Higher score means greater similarity.

11. IndexFlatL2 vs IndexFlatIP

For semantic search with sentence embeddings, normalized embeddings with IndexFlatIP are a common and intuitive approach.

12. Key FAISS Methods

Create an index

Add vectors

Search vectors

Number of stored vectors

Save index

Load index


13. Important Points

  • FAISS stores and searches numerical vectors efficiently.
  • Text must first be converted into embeddings.
  • The vector dimension must match the FAISS index dimension.
  • index.add() stores vectors in the index.
  • index.search() finds the nearest vectors.
  • k determines how many results are returned.
  • faiss.write_index() saves the index.
  • faiss.read_index() loads a previously saved index.
  • Store documents and metadata separately alongside the FAISS index.
  • Use the same embedding model for indexing and querying.
  • For cosine similarity, normalize embeddings and use IndexFlatIP.