liveBook · Manning

Embedding

As explained in Build an AI Agent (From Scratch)

A vector is an ordered collection of numerical values representing an object or its properties in a mathematical space.

An embedding is a numerical vector representing text in a space where related meanings tend to be close together. An embedding model converts a word, sentence, or document chunk into an array of hundreds to thousands of numbers. Unlike exact keyword matching, this representation can place “cat” near “kitten” or “sofa” near “couch” despite their different wording. section 5.2.2 section 5.3.1

Embeddings make natural-language retrieval possible. Convert document chunks to vectors and retain their associated text. At search time, embed the query, compare it with the stored vectors, and return the closest chunks. Cosine similarity compares vector angles; a score closer to 1 indicates greater similarity. Euclidean distance is another possible comparison, with smaller distances indicating greater similarity. section 5.2.2 section 5.6

A minimal Python implementation separates embedding generation from retrieval:

  • Accept either one string or a list of strings. Send the list to OpenAI’s text-embedding-3-small model, then collect the returned vectors in a NumPy array. section 5.3.1
  • For long inputs, split text into chunks before embedding it. Keep each chunk’s text and source information alongside its vector so a matching vector can lead back to readable content. section 5.3.5
  • Embed the query, calculate similarity against the chunk vectors, and request the top results rather than sending every source document to the agent. section 5.2.2 section 5.3.5

Check whether the vectors capture meaning

Embed three sentences: a cat sleeping on a couch, a kitten playing with a toy, and a dog running in a park. In the illustrated run, cosine similarity is 0.385 for the cat–kitten pair and 0.200 for the cat–dog pair. The scores offer a quick check that related wording ranks higher; they are not a claim that every cat sentence will receive the same score. section 5.3.1

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

The get_embeddings listing shows the small API boundary: normalize a single string to a list, request embeddings, and return their vectors as a NumPy array. section 5.3.1

Embedding every search result is useful only if retrieval reduces the context the agent must read. In one web-search exercise, 500-character chunks with 50-character overlaps were embedded in a batch. Selecting the three chunks most similar to “quantum computing” reduced an illustrative 37,312-token result set to 304 selected tokens. For stored agent memories, a ChromaDB collection can keep document text, vectors, and metadata together; a configured embedding function generates vectors automatically for both added documents and text queries. section 5.3.5 section 6.4.3

How Build an AI Agent (From Scratch) covers it

How deep it goes
Works through embedding generation and similarity comparison, then shows automatic embedding in a vector database; model training is described, not implemented.
Examples
Compares cat, kitten, and dog sentences, then retrieves quantum-related chunks from web results.
What it assumes
Assumes a Python-based LLM-agent setting while introducing text-to-vector conversion and similarity through small working examples.
What you can do after
Build it from scratch with an embedding API and cosine similarity, or apply it via ChromaDB’s embedding function.
How it connects
Connects embeddings to chunking, cosine similarity, vector search, retrieval-augmented generation, and vector databases.

Also explained in

Compare how 47 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.