Vector Stores: Chroma and FAISS
Creating and persisting vector stores, similarity search, MMR retrieval, hybrid search.
Vector Stores: Chroma and FAISS is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Vector Store?
A vector store holds your embedded chunks and finds the most similar ones to a query vector. It is the search engine at the heart of RAG. Two popular choices are Chroma and FAISS.
pip install langchain-chroma faiss-cpuCreating a Chroma Store
Chroma.from_documents embeds your chunks and indexes them in one call. Pass the documents and an embeddings object. Chroma returns a store you can immediately search.
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
db = Chroma.from_documents(chunks, embeddings)Persisting Chroma to Disk
Pass persist_directory so the index is saved to disk and survives restarts. Reload it later by constructing Chroma with the same directory and embeddings, avoiding re-embedding cost.
db = Chroma.from_documents(
chunks, embeddings,
persist_directory="./chroma_db"
)
# Reload later
db = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)Similarity Search
Query with similarity_search(query, k=4). It embeds the query, finds the k nearest chunks, and returns them as Documents. k controls how many passages you retrieve.
results = db.similarity_search("How do I reset my password?", k=4)
for doc in results:
print(doc.page_content[:80])Scores with Search
Use similarity_search_with_score to also get a distance score per result. Lower distance means more similar. Scores help you filter out weak matches before sending them to the LLM.
pairs = db.similarity_search_with_score("password reset", k=4)
for doc, score in pairs:
print(round(score, 3), doc.page_content[:60])Introducing FAISS
FAISS (Facebook AI Similarity Search) is a high-performance library for vector search. It is extremely fast and memory-efficient, ideal for large local indexes without a separate server.
Creating a FAISS Store
The API mirrors Chroma: FAISS.from_documents builds the index. FAISS keeps the index in memory, so it is great for read-heavy, in-process search.
from langchain_community.vectorstores import FAISS
db = FAISS.from_documents(chunks, embeddings)
results = db.similarity_search("refund policy", k=3)Saving and Loading FAISS
Persist a FAISS index with save_local and reload with load_local. Reloading requires allow_dangerous_deserialization=True because the index uses pickle.
db.save_local("faiss_index")
db = FAISS.load_local(
"faiss_index", embeddings,
allow_dangerous_deserialization=True
)Relevance vs Diversity
Plain similarity search can return four nearly identical chunks. Sometimes you want diverse results that cover different aspects of the answer. That is where MMR helps.
MMR Retrieval
Maximal Marginal Relevance (MMR) balances relevance against diversity, picking chunks that are relevant but not redundant. Use search_type="mmr" via the retriever interface.
retriever = db.as_retriever(
search_type="mmr",
search_kwargs={"k": 4, "fetch_k": 20}
)
docs = retriever.invoke("password reset")Choosing Chroma vs FAISS
Chroma is friendly, persistent, and good for prototypes and small apps. FAISS is faster at scale and ideal for in-memory search. Both expose similarity_search and as_retriever, so switching is easy.
Quick Check
Test your vector store knowledge.
Recap: Chroma and FAISS
You built vector stores with Chroma.from_documents (using persist_directory) and FAISS.from_documents, searched with similarity_search(query, k=4), and saved/loaded both. You used as_retriever with MMR to get diverse results, and learned when to pick each store.
Frequently asked questions
Is the “Vector Stores: Chroma and FAISS” lesson free?
Yes — the full text of “Vector Stores: Chroma and FAISS” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Vector Stores: Chroma and FAISS”?
Creating and persisting vector stores, similarity search, MMR retrieval, hybrid search. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Vector Stores: Chroma and FAISS” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- LangChain Architecture and LCEL
- Document Loading, Splitting, and Embedding
- Vector Stores: Chroma and FAISS
- Building a RAG Q&A System End-to-End