AWS-113 · Lesson 6 of 12 · 19 min
Vector databases and Amazon Bedrock Knowledge Bases
▶ Video coming soon A video version of this lesson is on the way.
Kwame Asante had built databases for years, and he arrived at the sixth AI hour with a doubtful question. "Why do we need a special kind of database?" he asked. "We already have PostgreSQL. I can search for the word 'cancellation' in a few milliseconds."
Noor typed a staff question from the pilot list into the sandbox: "What do I tell someone who wants to move their dog's operation to next week?" The policy that answers it never uses the words "move", "operation" or "next week". It talks about rescheduling surgical procedures with notice. A keyword search for the question's words would find nothing useful. A search by meaning found the right section first.
"That is what vectors are for," Noor said. "And you are half right: we may well keep them in PostgreSQL." This lesson explains how vector search works, which AWS services can store embeddings, and how Amazon Bedrock Knowledge Bases manages the whole RAG pipeline from lesson 5.
Searching by meaning
An embeddings model turns text into a vector, a list of numbers, often hundreds or about a thousand long. Texts with similar meanings get vectors that point in similar directions, even when they share no words. "Move my dog's operation" and "reschedule a surgical procedure" end up close together; "move my dog's food bowl" ends up somewhere else.
To find relevant passages, the system compares the question's vector with the stored vectors and returns the closest ones. A common measure is cosine similarity, which compares the directions of two vectors: 1 means they point the same way, 0 means unrelated, and negative values mean opposite. Other measures, such as Euclidean distance and dot product, are also used; a vector store is configured with the measure that matches the embeddings model.
The code below uses tiny four-number vectors to show the idea. Real embeddings come from a model such as Amazon Titan Text Embeddings, but the arithmetic is the same.
import math
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
return dot / (math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(y * y for y in b)))
# Toy 4-dimensional embeddings (real ones have hundreds of dimensions).
chunks = {
"Rescheduling surgical procedures needs 48 hours notice": [0.9, 0.8, 0.1, 0.0],
"Late cancellation fees and exceptions": [0.7, 0.9, 0.2, 0.1],
"Payment plans for dental work": [0.1, 0.2, 0.9, 0.3],
"Kennel cleaning between patients": [0.0, 0.1, 0.2, 0.9],
}
question = [0.85, 0.7, 0.15, 0.05] # "move my dog's operation to next week"
ranked = sorted(chunks.items(), key=lambda kv: cosine(question, kv[1]), reverse=True)
for text, vec in ranked:
print(f"{cosine(question, vec):.3f} {text}")0.997 Rescheduling surgical procedures needs 48 hours notice 0.975 Late cancellation fees and exceptions 0.346 Payment plans for dental work 0.141 Kennel cleaning between patients
What this code does
Short passages and a question are each represented by a list of four numbers that captures their meaning. The similarity between the question and each passage is measured by the angle between their number lists, where 1 means pointing the same way. Sorting by that similarity puts the rescheduling and cancellation passages at the top, which is how a search system picks the most relevant text.
The two scheduling chunks score far above the others, so they would be retrieved and placed in the prompt. Searching millions of vectors this way, one by one, would be slow, so vector stores use approximate nearest neighbor indexes that find very close matches quickly without comparing every vector.
Keyword, semantic and hybrid search
Vector search is not always better than keyword search. Keywords excel at exact terms: a drug name, a form number such as SOP-114, or a client account number. Semantic search excels at meaning and paraphrase. Many systems combine them in hybrid search, which scores results on both and merges the lists. Some knowledge bases can also rerank results with a second model that reads the question and each candidate passage together and reorders them by relevance, which often improves the top few results at a small extra cost.
Front Desk Helper used hybrid search, because staff often typed SOP numbers and product names, and they also asked questions in everyday words.
AWS services for storing embeddings
A vector database, or vector store, stores embeddings with their text and metadata and answers similarity queries. AWS offers several services that can do this. The exam guide names four examples.
| Service | How it stores vectors | Good fit when |
|---|---|---|
| Amazon OpenSearch Service (and OpenSearch Serverless) | Vector fields with approximate nearest neighbor search, alongside full-text search | You want hybrid keyword and vector search at scale |
| Amazon Aurora PostgreSQL-Compatible Edition | The pgvector extension adds a vector column type and similarity search | You already run Aurora and want vectors next to relational data |
| Amazon RDS for PostgreSQL | The same pgvector extension on a managed PostgreSQL database | You use RDS PostgreSQL and have modest vector volumes |
| Amazon Neptune (Neptune Analytics) | Vector search over a graph of connected entities | Relationships between items matter, as in GraphRAG |
| Amazon S3 Vectors | Vector buckets and indexes stored in S3 | Large vector sets where low storage cost matters more than the lowest latency |
Kwame's instinct was right: if an application already uses PostgreSQL, the pgvector extension on Aurora or RDS can be the simplest choice, because there is no new database to learn or operate. Other services, such as Amazon DocumentDB and Amazon MemoryDB, also offer vector search. Bedrock Knowledge Bases can use several of these as its vector store, as well as some third-party stores.
Amazon Bedrock Knowledge Bases
Building RAG by hand means writing code for loading, chunking, embedding, storing, retrieving and prompting. Amazon Bedrock Knowledge Bases is a managed feature that does that pipeline for you. You point it at your data, choose an embeddings model and a vector store, and it handles ingestion and retrieval. The exam guide gives it as the example of RAG on AWS.
The main parts:
- Data sources. Amazon S3 is the most common; connectors also exist for sources such as web crawlers and document management and collaboration systems. Cascade Ridge kept its SOPs in an S3 bucket owned by the operations team.
- Sync. When documents change, a sync job ingests the changes: parsing the files, chunking them with the strategy you chose, creating embeddings and updating the vector store.
- Embeddings model and vector store. You choose both when creating the knowledge base. The vector index must use the same number of dimensions as the embeddings model, so changing the embeddings model later means re-embedding everything.
- Retrieval APIs. The Retrieve operation returns the most relevant passages with their sources, so your application can build its own prompt. The RetrieveAndGenerate operation retrieves passages and calls a foundation model to produce an answer with citations in one step.
- Metadata and filters. Each document can carry metadata, such as clinic region, audience or effective date, and queries can filter on it, so managers-only documents are retrieved only for managers.
- Guardrails. A guardrail from lesson 3 can be applied to knowledge base answers, including the contextual grounding check that flags answers not supported by the retrieved passages.
# Illustrative request shape for RetrieveAndGenerate (no credentials shown).
# The application sends the question and names the knowledge base and model.
aws bedrock-agent-runtime retrieve-and-generate \
--input '{"text": "Do we charge a fee if a spay surgery is cancelled the day before?"}' \
--retrieve-and-generate-configuration '{
"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": "KBEXAMPLE01",
"modelArn": "arn:aws:bedrock:REGION::foundation-model/EXAMPLE-MODEL-ID"
}
}'
# The response contains the generated answer and citations pointing to the source chunks.In June 2026 AWS also launched Amazon Bedrock Managed Knowledge Base, a fully managed option that looks after the vector storage itself as well as ingestion, with built-in hybrid search, ranking and agentic retrieval that plans multi-step searches for complex questions. It removes the choice of vector store entirely, at the cost of less control over it. At launch it was offered in a short list of Regions that included US West (Oregon), so the Denver team noted it as a simpler alternative to compare with a standard knowledge base before the pilot. Either way, the RAG ideas in this lesson apply unchanged.
Front Desk Helper used Retrieve rather than RetrieveAndGenerate, because the team wanted its own tested prompt template from Prompt Management, with the refusal rules and short format from lessons 2 and 3. Night Notes did not use a knowledge base at all: everything it needed was in the call note.
Costs and operations
A knowledge base has several cost components, and Lorraine Beaudry wanted each one listed: embedding the documents during ingestion and again whenever they change; the vector store itself, which runs and is billed whether or not anyone asks a question; embedding each question; any reranking; and the foundation model tokens for each answer. For 140 documents, embedding costs were tiny. The vector store was the largest fixed cost, which is why the team compared a serverless option with simply adding pgvector to an Aurora database that already existed for the booking system.
Operations matter too. Someone must own the documents, run or schedule syncs after changes, and check that retired documents are removed. Hannah added a quarterly review: the operations team confirms that every SOP in the bucket is current, and the knowledge base is re-synced afterwards.
Common mistakes
MistakeAssuming keyword search finds everything relevant.
InsteadUse semantic or hybrid search so paraphrased questions find the right passages.
MistakeChanging the embeddings model without re-embedding.
InsteadVectors from different models are not comparable; re-ingest all documents with the new model.
MistakeForgetting the fixed cost of the vector store.
InsteadInclude storage and compute for the vector store, not just per-question costs, in estimates.
MistakeLetting every user retrieve every document.
InsteadAdd metadata and filter queries by what each user may see.
Choosing an embeddings model
The embeddings model matters as much as the vector store. It decides which texts count as similar, so a model that handles the vocabulary of the documents well gives better retrieval. Bedrock offers embeddings models from Amazon and other providers, and the Amazon Titan Text Embeddings models let you choose the vector size; smaller vectors cost less to store and search, larger ones can capture more detail. Multilingual embeddings models place a question and a passage close together even when they are written in different languages, which would matter if clinic staff searched Spanish documents with English questions. The vector store's index must be created with the same number of dimensions as the chosen model.
Jonah tested two embeddings models on the 200-question test set by measuring how often the right SOP section appeared in the top four results. The difference was small, so the team chose the cheaper option and recorded the result so the decision could be revisited if the document collection grew.
Retrieval settings are tuned with evaluation, not instinct. The main ones are how many passages to return, whether to use hybrid search, whether to rerank, and any minimum similarity score below which a passage is ignored. Each change can help one kind of question and hurt another, so the team changed one setting at a time and reran the 200-question test set after each. The results, kept in a simple table, became the record of why the knowledge base was configured the way it was.
Worked problem
Choosing a vector store
Cascade Ridge already runs an Aurora PostgreSQL database for BookVet. The SOP collection is small, about 140 documents and roughly 3,000 chunks. Staff often search by SOP number as well as by meaning. Which vector store options make sense?
Show solution
- Aurora PostgreSQL with pgvector reuses an existing, familiar database with no new service to operate; PostgreSQL text search can cover SOP numbers.
- OpenSearch Serverless offers strong hybrid keyword and vector search and is a common default for knowledge bases, at the cost of another service.
- At 3,000 chunks either is more than enough; scale is not the deciding factor.
- The decision comes down to operations and cost: reuse Aurora if the team prefers fewer services, or choose OpenSearch for richer hybrid search.
Answer: Both are reasonable. Cascade Ridge chose to prototype on OpenSearch Serverless through the knowledge base quick-create option and to compare the monthly cost with pgvector on Aurora before the pilot.
Kwame left the session with a new item on his list: "Try pgvector on the BookVet Aurora cluster in the sandbox." He also admitted that the operation and rescheduling demonstration had been convincing.
Summary and key terms
- Embeddings models turn text into vectors so that similar meanings sit close together.
- Vector search compares a question's vector with stored vectors, often by cosine similarity, using approximate nearest neighbor indexes for speed.
- Hybrid search combines keyword and semantic scores, and reranking can reorder the top results.
- AWS services that store embeddings include Amazon OpenSearch Service, Amazon Aurora and Amazon RDS for PostgreSQL with pgvector, Amazon Neptune, and Amazon S3 Vectors.
- Amazon Bedrock Knowledge Bases manages RAG: it syncs data sources, chunks and embeds documents into a chosen vector store, and offers the Retrieve and RetrieveAndGenerate APIs with citations, metadata filters and guardrails.
- Costs include embedding, the vector store, query embedding, reranking and model tokens.
- Vector database
- A database that stores embeddings and returns the most similar ones to a query.
- Cosine similarity
- A measure of how closely two vectors point in the same direction.
- Approximate nearest neighbor
- An index that finds very close vectors quickly without checking every one.
- Hybrid search
- Search that combines keyword and semantic scores.
- Reranking
- Reordering retrieved passages with a second model for better relevance.
- pgvector
- A PostgreSQL extension that adds vector storage and similarity search.
- Amazon Bedrock Knowledge Bases
- A managed Bedrock feature that builds and runs the RAG pipeline.
- RetrieveAndGenerate
- A Knowledge Bases operation that retrieves passages and generates a cited answer.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Why can semantic search find a passage that shares no words with the question?
Show answer
Because embeddings capture meaning, so paraphrases have similar vectors.
Name three AWS services that can store embeddings.
Show answer
Any three of: Amazon OpenSearch Service, Amazon Aurora, Amazon RDS for PostgreSQL, Amazon Neptune, Amazon S3 Vectors.
What is the difference between Retrieve and RetrieveAndGenerate?
Show answer
Retrieve returns passages only; RetrieveAndGenerate also produces an answer with citations.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.