HOW-TO · RAG

How to Set Up FAISS Index for Similarity Search

intermediate15 minBy Eruo Fredoline
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

FAISS installed, embedding model for generating vector embeddings

What this does

FAISS (Facebook AI Similarity Search) enables fast approximate nearest neighbor search over dense vectors. This guide walks through creating a basic index, adding vectors, and performing similarity searches using Ollama embeddings.

Steps

  1. Install FAISS and prepare embeddings.

    import faiss
    import ollama
    import numpy as np
    
    corpus = [
        "Retrieval-augmented generation combines LLMs with external knowledge.",
        "Vector databases store embeddings for semantic similarity search.",
        "FAISS provides fast approximate nearest neighbor search algorithms."
    ]
    
    def embed_texts(texts):
        embeddings = []
        for text in texts:
            resp = ollama.embeddings(model="nomic-embed-text", prompt=text)
            embeddings.append(resp["embedding"])
        return np.array(embeddings).astype("float32")
    
    vectors = embed_texts(corpus)
    faiss.normalize_L2(vectors)
    
  2. Create the FAISS index.

    d = vectors.shape[1]
    index = faiss.IndexFlatL2(d)
    print(f"Index is trained: {index.is_trained}")
    # Expected: Index is trained: True
    
  3. Add vectors to the index.

    index.add(vectors)
    print(f"Vectors after add: {index.ntotal}")
    # Expected: Vectors after add: 3
    
  4. Search the index.

    query_text = "How does vector search work?"
    query_resp = ollama.embeddings(model="nomic-embed-text", prompt=query_text)
    query_vec = np.array([query_resp["embedding"]]).astype("float32")
    faiss.normalize_L2(query_vec)
    
    distances, labels = index.search(query_vec, k=2)
    print(f"Labels: {labels}")
    print(f"Distances: {distances}")
    # Expected: integer label array and float distance array
    

Verification

python3 -c "import faiss; print(f'FAISS version: {faiss.__version__}')"
# Expected: FAISS version: <installed-version>

Common failures

  • FAISS import fails. Install with pip install faiss-cpu or pip install faiss-gpu matching the platform. Verify with python3 -c "import faiss".
  • Embedding dimension mismatch. Every vector must have the same dimension as the index's d parameter. Mismatched dimensions cause runtime exceptions.
  • Ollama service not running. Requests fail with connection errors if the Ollama daemon is stopped. Start with ollama serve.
  • Normalization not applied. L2-normalized indexes require query vectors to be normalized too. Use faiss.normalize_L2 on both index and query vectors.
  • Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
  • Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.

Related guides

RELATED GUIDES