How to use FAISS with LangChain
Quick answer
Use
FAISS from langchain_community.vectorstores to create a vector store with embeddings generated by OpenAIEmbeddings. Load documents, embed them, and index with FAISS for efficient similarity search within LangChain.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install langchain_openai langchain_community faiss-cpu openai
Setup
Install the required packages and set your OpenAI API key as an environment variable.
pip install langchain_openai langchain_community faiss-cpu openai Step by step
This example loads text documents, creates embeddings with OpenAI, indexes them with FAISS, and performs a similarity search.
import os
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
# Set your OpenAI API key in environment variable before running
# export OPENAI_API_KEY='your_api_key'
# Load documents from text files
loader = TextLoader("example_docs/doc1.txt")
docs = loader.load()
# Initialize OpenAI embeddings
embeddings = OpenAIEmbeddings(openai_api_key=os.environ["OPENAI_API_KEY"])
# Create FAISS vector store from documents
vectorstore = FAISS.from_documents(docs, embeddings)
# Query vector store
query = "What is LangChain?"
results = vectorstore.similarity_search(query, k=3)
for i, doc in enumerate(results, 1):
print(f"Result {i}: {doc.page_content}") output
Result 1: LangChain is a framework for developing applications powered by language models. Result 2: LangChain helps you build chatbots, question answering systems, and more. Result 3: It integrates with vector stores like FAISS for semantic search.
Common variations
- Use different embedding models by swapping
OpenAIEmbeddingswith other LangChain embeddings. - Use
FAISS.load_local()andsave_local()to persist the index. - Integrate with async LangChain chains for asynchronous workflows.
from langchain_community.vectorstores import FAISS
# Save FAISS index locally
vectorstore.save_local("faiss_index")
# Load FAISS index later
loaded_vectorstore = FAISS.load_local("faiss_index", embeddings) Troubleshooting
- If you get import errors for
faiss, ensure you installedfaiss-cpuorfaiss-gpucorrectly. - Check your OpenAI API key is set in
os.environ["OPENAI_API_KEY"]. - For large document sets, increase FAISS index parameters or use approximate nearest neighbor settings.
Key Takeaways
- Use
FAISS.from_documents()withOpenAIEmbeddingsto build a vector store in LangChain. - Persist your FAISS index locally with
save_local()andload_local()for reuse. - Ensure
faiss-cpuis installed to avoid import errors. - Customize embeddings or indexing parameters for different use cases.
- LangChain's modular design makes it easy to swap vector stores or embedding models.