liveBook · Manning

Embedding

As explained in Data Analysis with LLMs

An embedding is a numerical vector representation of an object that is intended to preserve meaningful properties of that object.

An embedding is a numerical vector that represents the meaning of a piece of text. It lets software compare documents using vector distance: nearby vectors are intended to represent similar content. This makes ordinary vector-based clustering useful for tasks such as grouping related emails. section 4.4.1

Figure 4.3 Clustering emails. We first calculate embedding vectors for all emails. Then we cluster those vectors to assign emails with similar content to the same cluster.
figure

The diagram shows the two-step workflow: calculate a vector for each email, then cluster the vectors. section 4.4.1

To create one, send text to an embedding model rather than a chat-completions model. The response contains the vector, model name, and token usage. Vector size depends on the model; the example model produces a vector with more than 1,000 dimensions. section 4.4.2

Get a text embedding in Python

Use `client = openai.OpenAI()`, then `response = client.embeddings.create(model='text-embedding-ada-002', input=text)`. Read the vector from `response.data[0].embedding`. For a large collection, retry occasional failed requests before giving up. section 4.4.2

Once texts have vectors, compare them for different jobs:

  • Clustering: group documents with similar content. One trial clusters a mixed collection of emails and poems into two groups. section 4.4.1 section 4.4.5
  • Retrieval: embed documents once, embed each new question, and find documents with nearby vectors to help answer it. section 4.4.6
  • Outlier detection: compare a document's vector with the others to find unusually different texts. section 4.4.6

LlamaIndex can handle much of a retrieval pipeline: load files, split long documents into chunks, index them with embeddings, and retrieve relevant chunks for a question. Similar vectors suggest related topics; they do not, by themselves, establish that a retrieved document answers the question. section 10.6.1 section 10.6.3

How Data Analysis with LLMs covers it

How deep it goes
Walks through endpoint calls, vector extraction, retries, and comparisons without explaining how models generate the vectors internally.
Examples
Uses clustered emails and poems, a Transformer-related question and document, and a Python embedding API call.
What it assumes
Introduces document-to-vector mapping before API use; the implementation examples use Python.
What you can do after
Apply it via OpenAI's embedding endpoint to compare texts, or via LlamaIndex to retrieve question-relevant data.
How it connects
Connects embeddings to clustering, vector distance, document retrieval, outlier detection, and question answering.

Also explained in

Compare how 47 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.