Text Embeddings
Go deeper into how text is represented as vectors. Learn how documents and queries are embedded, how similarity search works, and how to build a practical text-embedding workflow with Python.
By the end of this lesson, you will be able to:
- Explain how text is converted into an embedding vector.
- Distinguish document embeddings from query embeddings.
- Understand similarity search at a practical level.
- Prepare text before embedding and avoid common mistakes.
- Build a small text-embedding search workflow with Python.
Text Becomes a Numerical Representation
A text embedding model takes text as input and returns a vector of numbers. The vector is a representation that applications can use for comparison and retrieval.
Document Embeddings vs Query Embeddings
A semantic search system usually embeds two sides: the content you want to search and the user's query. Both need compatible representations so they can be compared.
Stored content
βTo reset your password, open Account Settings and select Security.β
β stored as a vectorUser question
βWhere can I change my password?β
β compared with document vectorsThe search system can rank stored documents according to their similarity to the query vector.
The Text Embedding Pipeline
At query time, the application embeds the user's question, searches the stored vectors, and returns the most relevant text.
Prepare Text Before Embedding
The embedding model can only represent the text you give it. Unnecessary navigation, repeated headers, broken extraction, or irrelevant boilerplate can make retrieval less useful.
HOME | PRODUCTS | LOGIN | SUPPORT Refund Policy Refunds may be requested within 30 days... FOOTER Copyright 2026...
Refund Policy Refunds may be requested within 30 days of purchase.
Why Chunking Matters
Long documents are often divided into smaller chunks before embedding. A query can then retrieve the specific passages that are most relevant.
Very small chunks can lose context, while very large chunks can mix unrelated information and increase retrieval payload.
Similarity Search
After creating a query embedding, the system compares it with stored document embeddings and ranks the closest matches.
Password reset instructions
0.94Account login troubleshooting
0.86Update account profile
0.62Shipping address changes
0.21Scores here are examples, not universal thresholds. Your model, data, and evaluation method determine what scores are useful.
Python: Create Text Embeddings
An embedding API converts text into a vector that your application can store, compare, or send to a vector database.
from openai import OpenAI
client = OpenAI()
text = "Refunds are available within 30 days."
response = client.embeddings.create(
model="YOUR_EMBEDDING_MODEL",
input=text,
)
embedding = response.data[0].embedding
print("Vector length:", len(embedding))
print("First 5 values:", embedding[:5])Python: Embed Multiple Texts
Embedding multiple inputs is useful when preparing a document collection for search.
from openai import OpenAI
client = OpenAI()
texts = [
"How do I reset my password?",
"How can I request a refund?",
"Where is my order?",
]
response = client.embeddings.create(
model="YOUR_EMBEDDING_MODEL",
input=texts,
)
embeddings = [
item.embedding
for item in response.data
]
for text, vector in zip(texts, embeddings):
print(text)
print("Dimensions:", len(vector))Python: Calculate Cosine Similarity
Cosine similarity compares the direction of two vectors and is commonly used for semantic similarity.
import numpy as np
def cosine_similarity(a, b):
a = np.array(a)
b = np.array(b)
return np.dot(a, b) / (
np.linalg.norm(a) * np.linalg.norm(b)
)
score = cosine_similarity(
query_embedding,
document_embedding,
)
print("Similarity:", score)Build a Small Semantic Search
A basic semantic search implementation embeds a query, scores stored vectors, and sorts the results.
documents = [
{"text": "Reset your password from Account Settings.",
"embedding": password_vector},
{"text": "Refunds can be requested within 30 days.",
"embedding": refund_vector},
{"text": "Track your order from the Orders page.",
"embedding": shipping_vector},
]
query_vector = embed("I forgot my password.")
results = []
for document in documents:
score = cosine_similarity(
query_vector,
document["embedding"],
)
results.append({
"text": document["text"],
"score": score,
})
results.sort(key=lambda item: item["score"], reverse=True)
for result in results:
print(result)Embeddings Need Metadata Too
A vector by itself is rarely enough for a useful application. Store metadata that helps you identify, filter, and reconstruct the source of each result.
Metadata can support filters such as product, language, customer type, date, or access permissions.
Normalization and Consistent Comparison
Vector search systems may normalize vectors or use distance measures where normalization is useful. The exact approach depends on the model and database.
Text Embeddings Across Languages
Multilingual applications should evaluate how well the selected embedding model represents the languages used by their users and documents.
Evaluate multilingual retrieval with real examples instead of assuming identical performance across languages.
Common Text Embedding Mistakes
Compare vectors only when they are designed to share a compatible representation space.
Repeated navigation and irrelevant boilerplate can reduce retrieval usefulness.
Chunk boundaries affect whether useful context is retrieved.
A similarity score is a signal. Evaluate whether results are actually useful.
Build a Mini FAQ Semantic Search
Create a small FAQ collection and use text embeddings to retrieve the most relevant answer.
- Create 10 FAQ entries for a fictional online store.
- Clean the text and give each FAQ a stable ID.
- Create embeddings for all 10 entries.
- Write five user questions that do not exactly repeat the FAQ wording.
- Embed each query and rank the FAQ entries by similarity.
- Check whether the top result is actually useful.
Test Your Understanding
A numerical vector representation of the supplied text.
So the query can be compared with stored document representations in the same compatible vector space.
It determines the units that can be retrieved and influences how much relevant context each result contains.
No. Similarity is a retrieval signal and should be evaluated against real queries and expected results.
Text Embeddings in One Flow
Key Takeaways
- Text embeddings convert text into numerical vectors.
- Document and query embeddings enable semantic search.
- Cleaning and chunking can have a major effect on retrieval quality.
- Cosine similarity is one common way to compare text vectors.
- Useful systems store vectors together with document IDs and metadata.
- Evaluate retrieved results with realistic queries instead of trusting similarity scores alone.