Vector Stores & Embeddings
Vector databases are the specialized memory engines powering Retrieval-Augmented Generation (RAG). Instead of matching brittle keyword tokens, vector stores index high-dimensional geometric representations (embeddings) to retrieve passages based on conceptual and semantic similarity.
1. Vector Embeddings Mechanics
An embedding model maps text into a dense vector space (typically 1,536 or 3,072 float dimensions). Text chunks with similar semantic meaning are mapped geometrically close together:
from langchain_openai import OpenAIEmbeddings
# Initialize embedding model (e.g. text-embedding-3-small)
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
# Generate vector for a single query
query_vector = embeddings.embed_query("What is the refund policy for enterprise accounts?")
print(f"Vector Dimensions: {len(query_vector)}") # 1536 float values
print(f"Sample Floats: {query_vector[:4]}")2. In-Memory & Persistent Vector Search with ChromaDB
ChromaDB is the most popular open-source embedding database for rapid local development and production deployments:
# Install: pip install langchain-chroma chromadb
from langchain_chroma import Chroma
from langchain_core.documents import Document
# Sample segmented knowledge documents
sample_docs = [
Document(page_content="Enterprise customers receive dedicated 24/7 Slack channels.", metadata={"tier": "enterprise"}),
Document(page_content="Standard plan customers have a 48-hour email response SLA.", metadata={"tier": "standard"}),
Document(page_content="Refunds can be requested within 30 days of billing cycle.", metadata={"tier": "all"}),
]
# Create and populate vector database with automatic embeddings
vector_store = Chroma.from_documents(
documents=sample_docs,
embedding=embeddings,
collection_name="support_knowledge_base"
)
# Perform semantic similarity search
results = vector_store.similarity_search("How fast will the support team reply to my ticket?", k=2)
print("Top Matching Chunks:")
for doc in results:
print(f"- {doc.page_content} (Tier: {doc.metadata['tier']})")3. Metadata Filtering & Relevance Scoring
Pure vector search can retrieve irrelevant documents from other departments or unauthorized tiers. Combine semantic search with exact metadata filters:
# Retrieve results strictly matching enterprise tier
filtered_results = vector_store.similarity_search(
"What are my communication options?",
k=2,
filter={"tier": "enterprise"}
)
# Retrieve with normalized relevance scores (0.0 to 1.0)
scored_results = vector_store.similarity_search_with_relevance_scores(
"Can I get my subscription fee refunded?",
k=2
)
for doc, score in scored_results:
print(f"Score: {score:.3f} | Content: {doc.page_content}")4. Enterprise Production with Pinecone
For multi-tenant applications managing hundreds of millions of vectors with sub-10ms query latency, switch seamlessly to Pinecone:
# Install: pip install langchain-pinecone pinecone-client
from langchain_pinecone import PineconeVectorStore
pinecone_store = PineconeVectorStore.from_existing_index(
index_name="enterprise-docs-index",
embedding=embeddings
)
retriever = pinecone_store.as_retriever(search_kwargs={"k": 5})Explore Related Tools
Hand-picked utilities and calculators related to this tool.
