Semantic Search with Amazon Bedrock Titan Embeddings in Python (No Vector DB Required)
Everyone jumps straight to a managed RAG stack the moment "search" comes up. Before you provision a vector database, spin up an ingestion pipeline, and wire up a knowledge base, it's worth understanding the primitive underneath all of it: the embedding. Once you can turn text into a vector and compare two vectors, you understand exactly what every RAG system is doing under the hood — and for small corpora you may not need the heavy machinery at all.
This is a copy-pasteable tutorial. In about 40 lines you'll embed a handful of documents with Amazon Bedrock Titan Text Embeddings V2, embed a user query, and rank the documents by cosine similarity. No OpenSearch, no pgvector, no external keys.
Prerequisites
- An AWS account with Amazon Bedrock model access granted for
amazon.titan-embed-text-v2:0 (request it once in the Bedrock console under Model access — approval is usually instant).
- Python 3.9+ and
boto3 installed: pip install boto3 numpy .
- Credentials configured (
aws configure or an IAM role) with permission to call bedrock:InvokeModel . A least-privilege policy:
json
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": "arn:aws:bedrock:*::foundation-model/amazon.titan-embed-text-v2:0"
}]
}
The one function that matters
Titan Text Embeddings V2 takes inputText and returns a dense vector. Two optional parameters are worth knowing: dimensions (256, 512, or 1024) lets you trade a little accuracy for smaller, cheaper-to-store vectors, and normalize (default true ) unit-normalizes the output — which is exactly what you want, because for normalized vectors cosine similarity reduces to a plain dot product.
python
import boto3, json
import numpy as np
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
MODEL_ID = "amazon.titan-embed-text-v2:0"
def embed(text: str, dimensions: int = 1024) -> np.ndarray:
body = json.dumps({
"inputText": text,
"dimensions": dimensions,
"normalize": True,
})
resp = bedrock.invoke_model(modelId=MODEL_ID, body=body)
vec = json.loads(resp["body"].read())["embedding"]
return np.array(vec, dtype=np.float32)
A tiny semantic search engine
python
docs = [
"Amazon Bedrock offers on-demand and provisioned throughput pricing.",
"SageMaker endpoints can autoscale based on invocation metrics.",
"Titan Text Embeddings V2 supports 256, 512, and 1024 dimensions.",
"Guardrails can mask PII inside a tool-using agent loop.",
]
# Embed the corpus once (in production, cache these vectors).
doc_vecs = np.vstack([embed(d) for d in docs])
def search(query: str, k: int = 2):
q = embed(query)
# Vectors are normalized, so dot product == cosine similarity.
scores = doc_vecs @ q
top = np.argsort(scores)[::-1][:k]
return [(float(scores[i]), docs[i]) for i in top]
for score, doc in search("How do I resize model output vectors?"):
print(f"{score:.3f} {doc}")
Run it, and the top hit is the dimensions line — even though the query never says "dimensions" or "embeddings." That's the whole point: you're matching meaning, not keywords. A lexical search would have missed it entirely.
What this costs — and the trap to avoid
Titan Text Embeddings V2 is billed per input token at a small fraction of a cent per thousand tokens (check the current Amazon Bedrock pricing page for the exact rate in your Region). For a few hundred documents, generating embeddings costs pennies. The trap is re-embedding the same documents on every request. Embeddings are deterministic for a given input and model — so embed your corpus once, persist the vectors, and only embed the incoming query at runtime. Re-embedding a static corpus per query is the single most common way builders quietly 10x their bill on this workload.
Cleanup
There's nothing to tear down — embedding is a stateless model call, so no endpoints or jobs are left running. To fully revert, remove the temporary bedrock:InvokeModel IAM permission you granted and delete any local files where you cached vectors. That statelessness is exactly why embeddings are cheap to experiment with.
Why this matters / what I'd do
Reach for a managed knowledge base when you have tens of thousands of documents, need incremental ingestion, or want hybrid search and re-ranking handled for you. But for a config lookup, an internal FAQ, or a docs site with a few hundred pages, an in-memory NumPy dot product over cached Titan vectors is faster to ship, cheaper to run, and far easier to reason about. Start with the primitive; graduate to the platform when the data volume — not the hype — forces your hand.
If you outgrow in-memory, the migration path is short: the same vectors drop straight into pgvector on Aurora or an OpenSearch Serverless vector collection. Nothing about your embedding code changes — only where the vectors live.
The full runnable code — corpus, search function, requirements, and IAM policy — is on GitHub: bedrock-titan-embeddings-semantic-search . Clone it and you're one pip install away from your first semantic search. What's your cutoff — at how many documents do you stop using a flat array and reach for a real vector store? I'd love to hear where you draw the line.
#AWS #Bedrock #GenAI #MachineLearning #RAG #Python
Loading article