Using Amazon Bedrock Knowledge Bases

Amazon Bedrock Knowledge Bases provides a fully managed RAG (Retrieval Augmented Generation) service for querying uploaded data. By specifying the location of your data in Amazon S3, the service will:

  • Automatically retrieve documents,
  • Chunk them into text blocks,
  • Convert them to embeddings,
  • Store embeddings in a vector database.

There’s also an API for building applications that use the Knowledge Base.

In this module, we’ll create a Knowledge Base using a portion of the AWS Well-Architected Framework documentation.


Exercise 1: Creating a Knowledge Base in the AWS Console

  1. Access the Knowledge Base Console.

Create

  1. Click the Create button (orange) and select Knowledge Base with vector store.

Create

  1. Use the default name or enter a custom name → Click Next.

Create

  1. Click Browse S3, select the bucket with a name containing awsdocsbucket → Click Next.

Create

  1. Select Titan Embeddings V2 as the embedding model → Keep the default Vector store → Click Next.

Create

  1. Scroll down and select Create Knowledge Base.

Create

Creating a Knowledge Base takes a few minutes. Don’t leave this page.

While waiting, you can complete the survey at: https://pulse.aws/survey/UBCJUUZV


Querying a Knowledge Base

When your Knowledge Base is ready, you can test it directly in the console interface:

  1. Click Sync to synchronize the data (takes about 1 minute).

Sync

  1. Click Select Model, choose Amazon Nova Pro → Click Apply.
  2. Enter a question in the Enter your message here box, for example:
  • Can you explain what a VPC is?
  1. Click Run to receive a response from the model and view the source documents in the Show result details section.

Try different questions to explore further!

test


Exercise 2: Using the Knowledge Base API

You can also query via API with 2 methods:

  • retrieve: Returns documents relevant to the question.
  • retrieve_and_generate: Performs the RAG workflow with a model.

Implementation:

  1. Open the rag_examples/kb_rag.py file in your IDE.
  2. Update the KB_ID variable with your Knowledge Base ID (found in the Overview section).

test

  1. Run the command:
python3 rag_examples/kb_rag.py
  1. Try changing the QUERY variable on line 4 to see different responses.

The code is executing the RAG process: converting the question to an embedding, retrieving relevant documents, and answering with the model.

test


Wrap up

After creating a Knowledge Base, the next step will be embedding this Knowledge Base into an Amazon Bedrock Agent to build a more intelligent automated response system.