Lesson 3 of 5 Amazon Bedrock

Grounding Answers in Your Data with RAG and Bedrock Knowledge Bases

This lesson explains how retrieval augmented generation (RAG) grounds a model's answers in your own, current data without retraining it, and how Amazon Bedrock Knowledge Bases provides RAG as a managed feature.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the benefits of RAG.
  • Describe the steps of retrieval augmented generation.
  • Identify the AWS services that can serve as vector stores.
  • Distinguish RAG from fine-tuning.

Why RAG

Foundation models do not know your private data, and their knowledge stops at a training cut-off. RAG retrieves relevant information at question time and adds it to the prompt, so answers are:

  • Current: update the documents, not the model.
  • Based on private data.
  • Backed by citations.
  • Less prone to hallucination.

How RAG works

Retrieval-augmented generation supplies a model with relevant information at query time. The process consists of the following steps.

  1. Ingest and chunk

    Ingest documents and split them into chunks.

  2. Embed

    Create an embedding for each chunk.

  3. Store

    Store the vectors in a vector store.

  4. Retrieve

    At query time, embed the question and retrieve the most similar chunks.

  5. Generate

    Add the chunks to the prompt so the model generates an answer based on them.

The following diagram shows the ingestion and query paths of RAG.

How retrieval-augmented generation works. At ingestion, documents are split into chunks, each chunk is embedded, and the vectors are stored in a vector store. At query time, the question is embedded, the most similar chunks are retrieved, and they are added to the prompt so that the model generates an answer based on them, with citations. Amazon Bedrock Knowledge Bases manages these steps.
RAG grounds answers in current documents without training.
Put in order

RAG step by step

Exam skills 3.1.3

Put the steps of Retrieval Augmented Generation in order.

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

Amazon Bedrock Knowledge Bases

Retrieval Augmented Generation (RAG) gives a model relevant information from your own documents at the moment it answers, so its answers are grounded in your data. Amazon Bedrock Knowledge Bases manages the whole process. The following list describes its principal features.

  • Data connectors: S3, web crawler, Confluence, SharePoint, Salesforce and more.
  • Chunking strategies: fixed-size, hierarchical, semantic or none; parsing of complex documents with tables and images, using a foundation model or Bedrock Data Automation.
  • A choice of embedding model, automatic synchronisation, metadata filtering, reranking and source citations.
  • Natural-language queries over structured data in Amazon Redshift, and GraphRAG with Amazon Neptune.

Applications query a knowledge base through two APIs. Retrieve returns only the most relevant chunks of documents, which the application can then pass to a model of its choice in its own prompt. RetrieveAndGenerate does both steps: it retrieves the chunks and asks a model to write an answer from them, with citations to the sources. The following table summarises the two.

APIReturns
RetrieveThe relevant chunks only
RetrieveAndGenerateThe chunks plus a generated answer
Scenario questions

Using a knowledge base

Exam skills 3.1.3 · 2.3.1

Choose the Knowledge Bases feature for each requirement.

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

Vector stores on AWS

A knowledge base turns each chunk of a document into an embedding, a vector that captures its meaning, and stores it in a vector store, which can find the chunks most similar to a question. Several AWS services can be the vector store. Amazon OpenSearch Serverless and OpenSearch Service provide fast vector search at large scale. Aurora PostgreSQL or RDS for PostgreSQL with the pgvector extension keep vectors next to existing relational data. Amazon Neptune Analytics combines graphs with vectors, and Amazon S3 Vectors provides the lowest-cost vector storage. Other options include Amazon DocumentDB, Amazon ElastiCache and partner products such as Pinecone. The following table summarises the stores.

StoreBest for
Amazon OpenSearch Serverless / OpenSearch ServiceVector search at scale
Aurora PostgreSQL or RDS for PostgreSQL with pgvectorVectors alongside existing relational data
Amazon Neptune AnalyticsGraphs combined with vectors
Amazon S3 VectorsLowest-cost vector storage
OthersAmazon DocumentDB, Amazon ElastiCache; partners such as Pinecone, Redis, MongoDB Atlas
Match the pairs

Vector stores

Exam skills 3.1.4

Match each requirement with the vector store that suits it best.

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

RAG or fine-tuning?

RAG and fine-tuning are often presented as alternatives, but they change different things. RAG changes what the model knows when it answers: relevant documents are retrieved and added to the prompt, so the model can use facts it was never trained on, including data that changes often. Updating the knowledge means updating the documents, with no retraining. Fine-tuning changes how the model behaves: training on examples teaches it a style, format or specialised task, such as always replying in a company's tone. It is not a good way to add facts, which would need retraining whenever they change. The following table summarises the distinction.

UseTo change the model'sExample
RAGKnowledgeFacts, documents, frequently changing data
Fine-tuningBehaviourStyle, format, specialisation in a task

Many applications use both.

Sort into groups

RAG or fine-tuning?

Exam skills 3.1.3 · 3.1.5

Decide whether each requirement changes what the model knows or how it behaves.

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

A policy assistant built on a knowledge base

An HR team wants an assistant that answers employees' questions from 300 policy documents and shows where each answer came from. The design uses Bedrock Knowledge Bases:

StepConfiguration
Data sourceAn S3 bucket containing the policy PDFs; re-synced whenever HR updates a policy
ChunkingHierarchical chunking, so answers keep the surrounding section's context
EmbeddingsAmazon Titan Text Embeddings
Vector storeAmazon S3 Vectors (lowest cost) or OpenSearch Serverless
QueryRetrieveAndGenerate with a low temperature and citations
response = bedrock_agent_runtime.retrieve_and_generate(
    input={"text": "How many days can I work from abroad each year?"},
    retrieveAndGenerateConfiguration={
        "type": "KNOWLEDGE_BASE",
        "knowledgeBaseConfiguration": {
            "knowledgeBaseId": "KB12345678",
            "modelArn": "arn:aws:bedrock:us-east-1:111122223333:inference-profile/us.amazon.nova-2-lite-v1:0",
        },
    },
)
print(response["output"]["text"])          # the answer
print(response["citations"])               # which document passages it came from

When the remote-working policy changes, HR uploads the new document and syncs the knowledge base; the next answer reflects it immediately. Fine-tuning a model on the policies would have been slower, more expensive, and out of date at the first policy change.

Check yourself

Why is RAG usually better than fine-tuning for company information that changes often?

Show answer

Updating the documents in the knowledge base keeps answers current at once, while a fine-tuned model would need retraining after every change.

Exam Tips

Remember for the exam

  • A chatbot that answers from company documents with citations points to Bedrock Knowledge Bases (RAG).
  • For frequently changing data, use RAG rather than fine-tuning.
  • RAG reduces hallucinations by grounding answers in retrieved sources.
  • For vector search alongside existing relational data, use Aurora or RDS for PostgreSQL with pgvector.
  • For serverless vector search at scale use OpenSearch Serverless; for the lowest-cost vector storage use S3 Vectors.
  • To return only the relevant chunks use the Retrieve API; to retrieve and answer use RetrieveAndGenerate.