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


















































