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 floating-point numbers. Learned from context in training data, embeddings place semantically related words or phrases near one another in a vector space, making them useful for comparing text. section 2.8.4

Smaller distances between vectors suggest greater relatedness; larger distances suggest less. Different embedding approaches serve different tasks, including semantic similarity, document search, and code search. Embeddings produced by different providers should not be assumed interchangeable. section 2.8.4

Representing words as vectors

A simplified embedding might represent “cat” as [0.2, 0.3, -0.1] and “dog” as [0.4, 0.1, 0.2]. Models can use such vectors as inputs for classification, sentiment analysis, or translation. 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])

This Python example requests an embedding from OpenAI and prints its length and first five values. The hard-coded API key is an illustrative shortcut, not a production security practice. section 2.8.4

For enterprise question answering, embeddings can support retrieval-augmented generation (RAG). Split proprietary documents into chunks, create embeddings, and store them in a vector database. At question time, embed the query, search for similar chunks, and add the results to the prompt sent to the language model. section 8.1.2 section 8.6

Creating embeddings and indexing documents ahead of time avoids repeating that work for every question. Production use still requires planning for data engineering, performance, cost, and security; the right ingestion and chunking approach depends on the data. section 8.1.2 section 8.9

How Generative AI in Action covers it

How deep it goes
Explains learned vector representations and retrieval workflows, while deferring similarity-measure details.
Examples
Uses illustrative cat and dog vectors, an OpenAI embedding request, and a blog-question-answering application.
What you can do after
Apply embeddings via the OpenAI API and use them in a Redis-backed retrieval workflow.
How it connects
Connects embeddings to semantic search, vector databases, vector indexes, and retrieval-augmented generation.

Also explained in

Compare how 51 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.