Skip to main content

4.3 Embeddings & Vector Stores

AI-Generated Content

AI-generated content may contain errors. Always verify against official sources.

4.3 Embeddings & Vector Stores

Key Concepts: Embeddings · Cosine similarity · ChromaDB · FAISS

Official Docs: OpenAI Embeddings · ChromaDB Docs · FAISS GitHub


What is an Embedding?

An embedding is a fixed-size dense vector representing the semantic meaning of a piece of text. Texts with similar meaning produce vectors that are close in vector space.

from openai import OpenAI

client = OpenAI()

response = client.embeddings.create(
model="text-embedding-3-small",
input="The quick brown fox jumps over the lazy dog",
)
vector = response.data[0].embedding
print(len(vector)) # 1536 dimensions

Embedding Models

Consult the provider's official documentation for current models, dimensions, and pricing — these change over time.


The standard similarity metric for embeddings is cosine similarity:

$$\text{sim}(a, b) = \frac{a \cdot b}{|a| \cdot |b|}$$

Returns a value in $[-1, 1]$; higher = more similar.


ChromaDB (Local / Server, Open-Source)

pip install chromadb langchain-chroma langchain-openai
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings

# Build the store from documents
vector_store = Chroma.from_documents(
documents=chunks,
embedding=OpenAIEmbeddings(model="text-embedding-3-small"),
persist_directory="./chroma_db",
)

# Similarity search
results = vector_store.similarity_search(
query="What was Q3 revenue?",
k=4,
)
for doc in results:
print(doc.metadata["source"], doc.page_content[:120])

FAISS (In-Memory, by Meta)

pip install faiss-cpu langchain-community
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings

vector_store = FAISS.from_documents(chunks, OpenAIEmbeddings())

# Save / load to disk
vector_store.save_local("faiss_index")
vector_store = FAISS.load_local(
"faiss_index", OpenAIEmbeddings(),
allow_dangerous_deserialization=True
)

Choosing a Vector Store

StoreBest forNotes
ChromaDBDevelopment, small-mediumOpen-source, disk-backed
FAISSFast local searchIn-memory, no server needed
PineconeManaged productionSaaS
WeaviateSelf-hosted productionOpen-source
pgvectorPostgreSQL + vectorsExtension

Key Takeaways

  • Embeddings convert text → vectors; similar text → close vectors
  • All LangChain vector stores share the same interface — swap them without changing retrieval code
  • ChromaDB is the easiest starting point for local development

Further Reading

Next → 4.4 RAG Pipeline Architecture