Lesson 3 of 5 Text Preparation, Embeddings and Vector Databases

Generating Embeddings for Text and Images

This lesson explains embeddings, dense vectors in which similar meanings lie close together, which power semantic search, RAG, clustering, recommendations and classification features.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how embedding models map inputs to vectors.
  • Use embedding models on Amazon Bedrock, including multimodal embeddings.
  • Choose consistent embedding configurations.

How embeddings work

An embedding model maps an input, such as a sentence, a document or an image, to a fixed-length vector of numbers, for example 1,024 of them. The model is trained so that inputs with similar meaning produce vectors that are close together. Similarity is measured with cosine similarity, the angle between vectors, the dot product, or Euclidean distance. When vectors are normalised to unit length, cosine similarity and dot product give the same ranking.

Unlike one-hot or TF-IDF vectors, which treat each word as an independent symbol, embeddings capture synonyms and context, so "car" and "automobile" are close, and "bank" in a financial sentence differs from "bank" in a sentence about a river. This makes embeddings the basis of semantic search, retrieval for RAG, clustering and recommendation.

The following diagram sketches how embeddings place inputs by meaning.

A two-dimensional sketch of an embedding space. Inputs with similar meaning map to nearby vectors, so car and automobile are close; the same word in different contexts maps to different places, so bank in a sentence about a loan sits near mortgage while bank in a sentence about a river sits near shore. Similarity is measured by the angle (cosine) or distance between vectors.
Real embeddings have hundreds or thousands of dimensions; the projection is schematic.
True or false

What an embedding is

Exam skills 1.2.5

Check the properties of embeddings that retrieval depends on.

Log in to try this activity Interactive activities are free with an account.

Embedding models on AWS

Amazon Bedrock provides managed embedding models through the same runtime API as text models. Amazon Titan Text Embeddings V2 accepts up to 8,192 tokens, outputs 1,024, 512 or 256 dimensions and can normalise vectors. Amazon Titan Multimodal Embeddings embeds images and text into the same space, so text queries can find images. Cohere Embed models provide English and multilingual embeddings, with input types that distinguish documents from queries. Open-source embedding models can be hosted on SageMaker AI endpoints through JumpStart.

import boto3, json
brt = boto3.client("bedrock-runtime")
resp = brt.invoke_model(
    modelId="amazon.titan-embed-text-v2:0",
    body=json.dumps({"inputText": "How do I reset my router?", "dimensions": 512, "normalize": True}),
)
vector = json.loads(resp["body"].read())["embedding"]   # 512 floats

8,192tokens

Maximum input to Titan Text Embeddings V2

1,024dimensions

Largest Titan Text Embeddings V2 output (also 512 or 256)

Scenario questions

Pick the embedding model

Exam skills 1.2.5

Choose an embedding model for each search requirement.

Log in to try this activity Interactive activities are free with an account.

Configuration choices

Use the same model, version and settings for documents and queries; vectors from different models are not comparable, so changing the model means re-embedding the whole corpus. Dimensions trade accuracy against storage and search cost. Inputs longer than the model's limit are truncated, so long documents are chunked first. Batch embedding through Bedrock batch inference reduces cost for large corpora. The following table summarises the choices.

ChoiceEffect
ModelLanguage coverage, domain quality, modality; documents and queries must match
Dimensions (for example 1,024 vs 256)Higher: more accurate, larger index; lower: cheaper, faster
NormalizationMakes cosine similarity equal to dot product
Maximum input tokensDetermines maximum chunk size
Multimodal modelSearch images with text and vice versa

The following diagram shows the indexing and query paths of a multimodal product search that matches text queries to product images.

Multimodal product search. Every product image and title is embedded with Titan Multimodal Embeddings into one vector space and stored with category, price and stock. A shopper's text query is embedded with the same model, so it lands near matching photos, and the search is filtered to in-stock items.
The scenario is illustrative.
Fill in the value

Vector storage

Exam skills 1.2.5 · 4.2.10

Calculate how embedding dimensions affect storage.

Log in to try this activity Interactive activities are free with an account.
Worked example

Product search across text and images

An online retailer wants shoppers to type "red leather ankle boots" and see matching products, including products whose descriptions are sparse but whose photos match.

  • Embed every product image (and its title) with Titan Multimodal Embeddings into one vector space.
  • Store vectors with product metadata (category, price, stock) in a vector index.
  • At query time, embed the text query with the same model and search for the nearest image vectors, filtered by in-stock items.

A text-only embedding model would only match products with good descriptions. Because the multimodal model places text and images in the same space, the photo of a red leather boot is found even when its description says only "autumn collection".

Check yourself

What must happen when the embedding model is changed?

Show answer

All content must be re-embedded, because vectors from different models are not comparable.

Exam Tips

Remember for the exam

  • Documents and queries must use the same embedding model and settings.
  • Changing the embedding model requires re-embedding all content.
  • Titan Text Embeddings V2: 1,024, 512 or 256 dimensions with optional normalisation.
  • Search images with text: Titan Multimodal Embeddings.
  • Fewer dimensions reduce storage and search cost with a small accuracy loss.