Applications with foundation models · 11 min
RAG with Amazon Bedrock Knowledge Bases
In this lesson you will learn what retrieval-augmented generation (RAG) is, how it prepares documents and answers a question, what Amazon Bedrock Knowledge Bases does and which AWS services store embeddings.
Retrieval-augmented generation (RAG) is the technique that gives a foundation model your organization's documents without retraining it. It is needed because a model's training data has a cutoff date and doesn't include those documents, since they are private to your organization. For example, if an online store's assistant never read the return policy, it may tell a customer that she has 60 days to return a pair of sneakers when the policy says 30, because it answers with the number that seems most likely to it.
What is RAG?

The Amazon Bedrock user guide explains RAG in three steps, which are the ones that give it its name:
- Retrieve, which consists of querying a data source.
- Augment, which consists of adding what was found to the prompt.
- Generate, which consists of getting a better response from the model with that information.
For example, when the store's customer asks how many days she has to return a pair of sneakers, the application first searches its documents and finds the paragraph of the policy that says 30 days. Then it pastes that paragraph into the prompt along with the question, and finally the model answers with the correct figure.
According to AWS, RAG is the cheapest way to give new data to a model, because retraining a foundation model with your organization's information has a high compute and financial cost. Also, with RAG the response can cite which document it came from, for example "according to the return policy", so the customer can check it.
How are the documents prepared?
RAG works in two stages, and the first one happens before anyone asks the first question. In that stage, which is called ingestion, the documents are converted into text and split into chunks (chunking). Then each chunk goes through an embeddings model, which turns it into a vector, that is, a list of numbers that represents its meaning.

Each vector is stored in a vector index, which works like the index of a book: it is used to quickly find where each topic is. Along with the vector, the text of the chunk and a reference to the original document are stored, and that reference is what later makes it possible to cite the source. For example, the store's return policy can end up split into a chunk about deadlines, another about refunds and another about products that can't be returned, each with its own vector.
The index only has what it processed the last time, so if the policy changes and nobody processes it again, the index keeps the old version. That is why AWS recommends updating the documents and their embeddings, in real time, that is, as soon as a document changes, or in batches, several documents together every so often. For example, if the store changes the deadline from 30 to 45 days and doesn't process the policy again, the assistant keeps saying 30.
How is a question answered?
The second stage repeats with every question. First, the question goes through the same embeddings model as the documents and also becomes a vector, so that the two vectors can be compared. Then the index is searched for the vectors most similar to that one, which correspond to the chunks that talk about the same thing even if they use other words.

Those chunks are used to build the augmented prompt, which carries an instruction, the chunks that were found with their source document and the question. With that prompt, the model generates the response and cites it. For example, if the customer writes "how long do I have to send back my sneakers?", the search finds the deadlines chunk even though that text says "returns" and not "send back", because the two vectors represent the same idea.
True or false?
0 of 3With RAG, the model learns the return policy and remembers it even if you turn off the knowledge base.
The user's question goes through the same embeddings model as the documents.
If you change a document and don't sync again, the assistant keeps answering with the old version.
What does Amazon Bedrock Knowledge Bases do?
Amazon Bedrock Knowledge Bases is the service that handles both stages of RAG for you: ingestion, chunking, embeddings, search and, if you ask for it, the response with citations. It has two types, and AWS recommends the first one:
- Managed Knowledge Base, where Bedrock manages ingestion, the index, storage and retrieval, and which comes with connectors, which read your documents where they already are, for Amazon S3, SharePoint, Confluence, Google Drive, OneDrive and a web crawler, which is a program that goes through the pages of a website and copies their text.
- Customer-managed Knowledge Base, where you choose and manage the vector store and control the whole process.
For example, if the store keeps its policies in Google Drive, with the Managed Knowledge Base it connects that folder and Bedrock takes care of the rest.
The Managed Knowledge Base also has two features of its own. The first one filters the results by each document's permissions, except with the web crawler, so an employee only gets answers from the documents they have permission to open. The second one is agentic retrieval, which splits a complex question into several searches. For example, "can I return a pair of sneakers on sale that I bought 40 days ago?" can be split into a search about deadlines and another about products on sale. AWS points out that the third-party connectors, document-level permissions and the integration with AgentCore Gateway are only available in the managed type.
Chunking and reranking
The way you chunk the text changes the quality of the responses. A very small chunk loses the context, and a very large one mixes different topics. By default, Knowledge Bases splits the text into pieces of about 300 tokens while keeping sentences whole, and it also offers fixed-size chunking with overlap, where each piece repeats a little of the end of the previous one, hierarchical chunking, semantic chunking or no chunking. For example, with overlap, the sentence that explains the exception for products on sale isn't cut in half between two chunks.
Reranking also changes that quality. In reranking, a model calculates how relevant each chunk is to the question and orders them again, so, according to AWS, you send the model fewer but more useful chunks, with lower cost and latency. For example, out of ten chunks the search found for the sneakers question, reranking puts the two that talk about return deadlines at the top.
Where are embeddings stored?
Embeddings are stored in a vector database, which is the one that stores vectors and finds the ones most similar to another vector. The exam guide names four AWS services with this function: Amazon OpenSearch Service, Amazon Aurora, Amazon Neptune and Amazon RDS for PostgreSQL.

Each of these services stores vectors in a different way:
- Amazon OpenSearch Service, which does k-NN search (the nearest neighbors) by Euclidean distance or cosine similarity, which are two ways of measuring how similar two vectors are, in Serverless, where AWS takes care of the machines, or in managed clusters, which are groups of machines that you size.
- Amazon Aurora PostgreSQL, which uses the pgvector extension, an add-on that gives it vector search, in version 0.5.0 or later.
- Amazon Neptune Analytics, which stores vectors inside a graph to do GraphRAG, which is RAG that also follows those relationships.
- Amazon S3 Vectors, which is a bucket type for vectors and which AWS recommends for infrequent queries and lower cost.
- Amazon RDS for PostgreSQL, which also has pgvector but is not on the list of Knowledge Bases vector stores.
The choice depends on what the organization already uses and how often it queries the vectors. For example, if the store's orders are already in Aurora PostgreSQL, it can store the vectors for its policies right there with pgvector instead of learning another service.
Match each situation with the vector store
0 of 4 pairsTap a situation and then the service.
0 of 4 pairs.
On the exam
When the scenario says "data that changes", "internal policies", "company documents", "without retraining" or "cite the source", the answer is RAG, and on AWS that is Amazon Bedrock Knowledge Bases. On the other hand, fine-tuning, which trains the model a little more with your examples, is ruled out when the data changes every month, because the model stays with what it knew the day you trained it. For example, if the store changes its promotions every month, with RAG it is enough to update the document, while with fine-tuning it would have to train again. If the question is which service stores embeddings, look for OpenSearch Service, Aurora or RDS for PostgreSQL with pgvector, or Neptune.
cloud path tip: in an account on the new AWS experience, Bedrock, Amazon S3, OpenSearch Serverless, OpenSearch Service and Aurora are on the free plan, and Neptune is only on the paid plan. That account does not support cross-Region inference, so the embeddings model and the one that generates the response have to be within us-east-2. In a full account you choose the Region.
Review question
A human resources team wants an assistant that answers with the internal policies, which change every month, and that says which document each answer came from. They want the option that takes the least effort. What should they use?
Next
- Retrieve data and generate AI responses with Amazon Bedrock Knowledge Bases, in the Amazon Bedrock user guide.
- Take an AI Practitioner practice exam at /examenes.