HOW-TO · RAG

How to Integrate FAISS with LangChain

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

FAISS and LangChain installed

What this does

LangChain provides a vector store abstraction that wraps FAISS, eliminating boilerplate for index creation, persistence, and similarity search. By combining LangChain's document loading and text splitting utilities with a FAISS vector store backed by Ollama embeddings, you can build a complete retrieval pipeline efficiently.

Steps

  1. Configure the embedding model.

    from langchain_ollama import OllamaEmbeddings
    
    embeddings = OllamaEmbeddings(
        model="nomic-embed-text",
        base_url="http://localhost:11434"
    )
    sample = embeddings.embed_query("test embedding")
    print(f"Embedding dimension: {len(sample)}")
    # Expected: integer vector length (e.g., 768)
    
  2. Load and split documents.

    from langchain.text_splitter import RecursiveCharacterTextSplitter
    
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    corpus = "Retrieval-augmented generation combines LLMs with vector databases."
    docs = text_splitter.create_documents([corpus])
    print(f"Created {len(docs)} document chunks")
    
  3. Create the FAISS vector store.

    from langchain_community.vectorstores import FAISS
    
    vectorstore = FAISS.from_documents(documents=docs, embedding=embeddings)
    print(f"FAISS index size: {vectorstore.index.ntotal} vectors")
    
  4. Save and reload the vector store.

    vectorstore.save_local("faiss_index_store")
    loaded_store = FAISS.load_local(
        "faiss_index_store",
        embeddings,
        allow_dangerous_deserialization=True
    )
    print(f"Reloaded index size: {loaded_store.index.ntotal}")
    
  5. Perform similarity search.

    retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
    results = retriever.invoke("How does RAG work?")
    for doc in results:
        print(doc.page_content[:80])
    

Verification

python3 -c "
from langchain_ollama import OllamaEmbeddings
emb = OllamaEmbeddings(model='nomic-embed-text')
print('LangChain OllamaEmbeddings loaded OK')
"
# Expected: LangChain OllamaEmbeddings loaded OK

Common failures

  • Embedding model not found. Run ollama pull nomic-embed-text if missing.
  • Mismatched embedding dimensions on load. Always reload with the identical embedding configuration used during creation.
  • allow_dangerous_deserialization required. The flag is needed when loading FAISS indexes with pickle-serialized metadata.
  • Chunk size too large for embedding context. Set chunk_size below 512 tokens as a safe default.
  • Empty search results after reload. Verify the directory contains index.faiss and index.pkl files.
  • 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