Vector Databases
Specialised stores that index millions of embeddings for fast similarity search.
Comparing a query to a few documents is easy. Comparing it to millions is slow if done naively. A vector database stores embeddings and finds the nearest ones in milliseconds.
What they do
- Store each vector with its original text and metadata.
- Build an index for fast nearest-neighbour search.
- Return the top-k most similar items for a query vector.
Typical workflow
- Index time: embed your documents and upsert them.
- Query time: embed the question and search for the closest vectors.
Example
{
"upsert": [
{"id": "doc-1", "vector": [0.02, -0.18, 0.07, "..."], "metadata": {"title": "Refund policy"}}
],
"query": {"vector": [0.03, -0.15, 0.09, "..."], "top_k": 3}
}When to use it
- A RAG pipeline stores 50k document embeddings in a vector database so each user query retrieves the top-5 matching passages in under 50ms.
- An image search app indexes product photo embeddings in a vector database, letting shoppers upload a photo to find visually similar items.
- A music streaming service stores song embeddings so recommendations are served by nearest-neighbour lookup rather than manual curation.
More examples
Store and query with ChromaDB
Uses ChromaDB (an in-process vector DB) to index three documents and retrieve the top-2 matches.
import chromadb
client = chromadb.Client()
collection = client.create_collection('docs')
# Add documents (ChromaDB auto-embeds if you pass an embed function)
collection.add(
documents=['LLMs predict the next token.', 'Transformers use attention.', 'CNNs process images.'],
ids=['doc1', 'doc2', 'doc3']
)
results = collection.query(query_texts=['how do language models work?'], n_results=2)
for doc in results['documents'][0]:
print(doc)Upsert embeddings to Pinecone index
Generates embeddings and upserts them into a Pinecone index using the standard (id, vector) tuple format.
from pinecone import Pinecone
from openai import OpenAI
pc = Pinecone(api_key='YOUR_PINECONE_API_KEY')
index = pc.Index('my-index')
client = OpenAI()
def embed(text):
return client.embeddings.create(
model='text-embedding-3-small', input=text
).data[0].embedding
vectors = [
('id-1', embed('Photosynthesis converts light to energy.')),
('id-2', embed('Mitosis is cell division.')),
]
index.upsert(vectors=vectors)
print('Upserted', len(vectors), 'vectors')Nearest-neighbour query in Pinecone
Queries the Pinecone index with an embedding and returns the top-3 nearest neighbours with scores.
from pinecone import Pinecone
from openai import OpenAI
pc = Pinecone(api_key='YOUR_PINECONE_API_KEY')
index = pc.Index('my-index')
client = OpenAI()
query_vec = client.embeddings.create(
model='text-embedding-3-small',
input='how do plants make food?'
).data[0].embedding
results = index.query(vector=query_vec, top_k=3, include_metadata=True)
for match in results['matches']:
print(f"id={match['id']} score={match['score']:.4f}")
Discussion