An embedding is a numerical vector that represents text in a high-dimensional space. An embedding model converts words or sentences into arrays of hundreds to thousands of numbers, placing semantically similar text near each other. Context matters: “bank” can receive different vectors when it means a financial institution or a riverbank. section 5.2.2 section 5.3.1
Embeddings let vector search find relevant content without exact word matching. Documents are embedded and stored in advance; a query is then embedded and compared with them. Cosine similarity measures the angle between vectors, with values closer to 1 indicating greater similarity. Euclidean distance is another option, with smaller distances indicating greater similarity. section 5.2.2
Listing 5.1 Implementing the get_embeddings function
from openai import OpenAI
import numpy as np
client = OpenAI()
def get_embeddings(texts, model="text-embedding-3-small"):
"""Convert text to embedding vectors."""
if isinstance(texts, str):
texts = [texts] #1
response = client.embeddings.create(input=texts, model=model) #2
return np.array([item.embedding for item in response.data]) #3
Create embeddings with OpenAI’s text-embedding-3-small model. The helper accepts one string or a list, sends the input to the embeddings API, and returns the vectors as a NumPy array. section 5.3.1
Listing 5.2 Comparing embedding similarity
from sklearn.metrics.pairwise import cosine_similarity
sentences = [
"The cat is sleeping on the couch",
"A kitten is playing with a toy",
"The dog is running in the park"
]
embeddings = get_embeddings(sentences)
cat_kitten = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0] #1
cat_dog = cosine_similarity([embeddings[0]], [embeddings[2]])[0][0] #1
print(f"Cat vs Kitten: {cat_kitten:.3f}") # Cat vs Kitten: 0.385
print(f"Cat vs Dog: {cat_dog:.3f}") # Cat vs Dog: 0.200
Compare the resulting vectors with cosine similarity. In this example, the cat and kitten sentences score 0.385, higher than the cat and dog sentences at 0.200. section 5.3.1
Retrieving relevant web content
To find quantum-computing content in mixed web results, split page text into 500-character chunks with 50-character overlap, embed the chunks in one API call, and search for the three closest results. In one run, the selected chunks used 304 tokens rather than 37,312 for the full results. The reduction is specific to that run; relevance still depends on the retrieved chunks. section 5.3.5
A vector database can manage storage and retrieval. In ChromaDB, a collection stores document text, embedding vectors, and metadata together. Supplying an embedding function lets the collection embed documents on add and queries on search, with similarity calculation handled internally. section 6.4.3
Listing 6.29 ChromaDB basic use
import chromadb
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
# Create client (in-memory)
client = chromadb.Client()
# Set up embedding function
embedding_fn = OpenAIEmbeddingFunction(
model_name="text-embedding-3-small"
)
# Create collection
collection = client.get_or_create_collection(
name="my_memories",
embedding_function=embedding_fn
)
# Store documents - just pass text, embeddings are generated automatically
collection.add(
ids=["mem_001", "mem_002"],
documents=["The cat is sleeping on the sofa", "The dog is playing in the park"],
metadatas=[{"category": "cat"}, {"category": "dog"}]
)
# Search - just pass query text, embeddings are generated automatically
results = collection.query(
query_texts=["Where is the cat resting"],
n_results=1
)
print(results["documents"]) # [['The cat is sleeping on the sofa']]
print(results["metadatas"]) # [[{'category': 'cat'}]]
Create an in-memory ChromaDB collection with an OpenAI embedding function, add documents and metadata, and query using text. section 6.4.3


















































