AWS Builder Center
How Does AI Understand Our Words?  Amazon Bedrock - Day 3

How Does AI Understand Our Words? Amazon Bedrock - Day 3

Learn how to generate Embeddings with Amazon Bedrock, compare sentence similarity using Python, and understand vector dimensions, storage, and semantic search.

The problem

Ever wondered how AI can find the right answer in your class notes?
AI doesn't automatically know what's inside your personal notes. It needs a way to find the most relevant information first.
But how can a computer find similar meanings in different sentences? That's where embeddings come in — they turn text into numbers that capture meaning.

What is an embedding?

An embedding is a list of numbers that represents the meaning of a piece of text.
Think of GPS coordinates. Two places with nearby coordinates are physically close. Two sentences with nearby embeddings are close in meaning, even if they share no words.
"How do I reset my password?" and "I forgot my login details" use different words, but they land in the same area. That's how search can find the right answer without matching exact keywords.

Try it yourself (15 lines)

We'll use Amazon Titan Text Embeddings V2 on Bedrock and compare three sentences.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import json
import boto3
import numpy as np

client = boto3.client("bedrock-runtime", region_name="us-east-1")

def embed(text):
response = client.invoke_model(
modelId="amazon.titan-embed-text-v2:0",
body=json.dumps({"inputText": text, "dimensions": 512, "normalize": True}),
)
return np.array(json.loads(response["body"].read())["embedding"])

a = embed("How do I reset my password?")
b = embed("I forgot my login details")
c = embed("Best pasta recipe for dinner")

print("Password vs login:", round(float(a @ b), 3))
print("Password vs pasta:", round(float(a @ c), 3))
With normalize on, the dot product (a @ b) works as the similarity score. Higher means closer in meaning.
What to expect: the password vs login score should be clearly higher than password vs pasta. Exact numbers will differ on your run.

Why not just use more numbers?

Titan V2 lets you pick the vector size: 256, 512, or 1024 (default). Bigger vectors cost more to store. The smaller ones lose very little accuracy.

Now, store your notes in S3

Our chatbot (Day 4) will read documents from Amazon S3, AWS's file storage.
  1. Open S3 and click Create bucket.
  2. Give it a unique name
  3. Keep Block all public access ON.
  4. Open the bucket and click Upload. Add 2 or 3 files (PDF, TXT, or Markdown).
Tip: use clear file names like os-unit-3.pdf, not scan0042.pdf. Text-based files work best. Scanned images of pages often don't.

Mistakes I made

----------------------
Error you might seeLikely causeFix
ModuleNotFoundError: numpyNumPy isn't installedRun pip install boto3 numpy
NoCredentialsErrorAWS credentials aren't set upRun aws configure (from Day 1)
AccessDeniedExceptionYour IAM user lacks Bedrock permission, or the model isn't available to your accountCheck your IAM permissions and model availability in your console
ValidationException about the model IDWrong Region, or the model needs an inference profile IDCopy the exact ID from the Bedrock console
"Bucket name already exists"S3 bucket names must be globally uniqueAdd your name or a number to it
Poor results from a PDFThe PDF is a scan (an image of text)Use text-based files

Cost note

Embedding costs depend on how much text you process and the model you use. Amazon S3 storage costs depend on how much data you store. The chart below gives a visual overview, but actual costs may vary. Check the Amazon Bedrock pricing page  and Amazon S3 pricing , and set up an AWS Budgets alert to monitor spending.

Choosing an Embedding Dimension
==============================

Amazon Titan Text Embeddings V2 supports 1024, 512, and 256 dimensions. Smaller vectors can reduce storage requirements, while retrieval quality may vary depending on your data and use case. This chart illustrates the trade-off; check the AWS documentation for benchmark details.

Recap

  • Embeddings = numbers that capture meaning
  • Close numbers = similar meaning
  • Smaller vectors (512) are a smart default for learning

Next: Day 4

We turn this bucket into a Knowledge Base and ask our notes questions. This is the day it all comes together.
What would YOU want a chatbot to answer from your notes? Tell me below.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article