liveBook · Manning

Embedding

As explained in Generative AI in Action

A vector is an ordered sequence of numerical values representing a point or quantity in a multidimensional space.

An embedding represents text as a vector: an ordered sequence of numbers that software can compare. Words, phrases, or documents with related meanings can have nearby representations in a vector space. This makes embeddings useful when an enterprise application needs to find relevant information by meaning rather than merely reuse the wording of a query. section 2.8.4 section 10.4.2

From text to numbers

Illustrative vectors might represent “cat” as [0.2, 0.3, -0.1] and “dog” as [0.4, 0.1, 0.2]. Smaller distances suggest stronger relatedness; a similarity score makes that relationship usable in search. Embeddings are learned from text during model training, so these numbers are representations produced by a model, not labels assigned by hand. section 2.8.4

Choose an embedding approach for the task: similarity embeddings compare pieces of text, text-search embeddings relate a short query to longer documents, and code-search embeddings relate code snippets to natural-language queries. Keep vectors from one provider’s embedding method with that method; another provider’s method cannot interpret them interchangeably. section 2.8.4

Listing 2.3 Getting an embedding in OpenAI
import os
from openai import OpenAI

client = OpenAI(api_key=’your-API-key’)

def get_embedding(text):
    response = client.embeddings.create(
        model="text-embedding-ada-002",
        input=text)
    return response.data[0].embedding

embeddings = get_embedding("I have a white dog named Champ.")
print("Embedding Length:", len(embeddings))
print("Embedding:", embeddings[:5])

A short Python listing calls OpenAI’s embeddings API with text-embedding-ada-002, then prints the returned vector’s length and first five values. Its hard-coded API-key placeholder is illustrative, not a production credential-handling pattern. section 2.8.4

For retrieval-augmented generation (RAG), process source documents into chunks, create their embeddings, and save them in a vector database. When a user asks a question, embed the question, search for similar stored chunks, and add the retrieved text to the LLM prompt. A chat application over years of blog posts uses Redis and a hybrid search index to retrieve matching chunks before generating an answer. section 8.1.2 section 8.6

Avoid recreating embeddings for every question when the source can be processed and indexed ahead of time: repeated work adds latency and cost. In production, plan chunking around the data, and use a vector index and database to manage performance, scale, and security. Building the full retrieval-and-chat workflow still requires substantial data engineering and integration work. section 8.1.2 section 8.9

How Generative AI in Action covers it

How deep it goes
Defines vector similarity and demonstrates API creation and indexed retrieval, without deriving the mathematics of embedding models.
Examples
Shows sample word vectors, an OpenAI embedding API call, and Redis retrieval for chat over blog posts.
What it assumes
Defines embeddings and similarity from plain-language examples; assumes basic Python and API-call familiarity for the executable listing.
What you can do after
Apply it via OpenAI’s API and Redis to create embeddings and retrieve relevant chunks for a RAG chat application.
How it connects
Connects embeddings to semantic search, vector databases, document chunking, retrieval-augmented generation, and LLM prompts.

Also explained in

Compare how 47 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.