RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to Set Up FAISS Index for Similarity Search
HOW-TO · RAG

How to Set Up FAISS Index for Similarity Search

intermediate·15 min·By Fredoline Eruo
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

  • choose-configure-faiss-index-types
  • use-faiss-approximate-nearest-neighbor
RELATED GUIDES
RAG
How to Choose and Configure FAISS Index Types (IVF, HNSW)
RAG
How to Use FAISS for Approximate Nearest Neighbor Search
← All how-to guidesCourses →