Similarity search finds items whose numerical representations are close to a query’s representation. The vectors act like coordinates on a map: related meanings can be neighbors even when their words differ. section 8.2.3
This approach supports semantic search, recommendations, and retrieval-augmented generation. For example, a search for something to wear to a wedding can return suits and dresses without matching the word “wedding.” section 8.2.3
Find comfort food
An AI service converts “Lasagna” and “Comfort Food” into vectors. A vector database returns Lasagna when it is the closest match to the query. 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")The example shows the sequence: generate an embedding, store it with metadata, then query for the nearest result. section 8.2.4




































