liveBook · Manning

Embedding

As explained in Grokking Software Architecture

A numerical representation that maps an input such as text or an image into a vector space so semantic relationships can be measured.

An embedding turns text or an image into a list of numbers that represents meaning. Similar concepts land near each other in this mathematical space, letting a vector database find related items even when their words differ. section 8.2.3

From “Comfort Food” to lasagna

An AI service converts “Lasagna” and a “Comfort Food” query into vectors. A vector database can return Lasagna because the two vectors are close, even though the phrases do not match exactly. section 8.2.4

Listing 8.3 The Vector Way (Pinecone/Weaviate)
# STEP 1: Generate the Embedding (using an AI service) 
import openai 
 
# The AI converts "Lasagna" into 1536 numbers 
embedding_response = openai.Embedding.create( 
    input="Lasagna", 
    model="text-embedding-ada-002" 
) 
 
vector = embedding_response['data'][0]['embedding'] #A
# STEP 2: Store it in the Vector Database 
vector_db.upsert( 
    id="recipe_1", 
    values=vector, # The 1536-dimensional vector from OpenAI 
    metadata={"name": "Lasagna"} 
) 
 
# STEP 3: Query 
# We convert the USER'S query into a vector first 
query_vector = openai.Embedding.create( #B
    input="Comfort Food", 
    model="text-embedding-ada-002" 
)['data'][0]['embedding'] 
 
results = vector_db.query( 
    vector=query_vector, 
    top_k=1 
) 
# RETURNS: "Lasagna" (because its vector is closest to "Comfort Food")

See the steps for generating, storing, and querying recipe embeddings with an AI service and vector database. section 8.2.4

How Grokking Software Architecture covers it

How deep it goes
Defines embeddings and explains vector similarity with examples, while deferring vector generation to AI services rather than manual calculation.
Examples
Contrasts “Red Shirt” with “Crimson Top,” compares dog, puppy, and car vectors, and retrieves lasagna for “Comfort Food.”
What you can do after
Apply it via an AI embedding service and vector database to index text and retrieve semantically similar items.
How it connects
Links embeddings to vector databases, semantic search, nearest-neighbor search, and comparisons with SQL and NoSQL.

Also explained in

Compare how 72 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.