How to Intermediate · 3 min read

How to create knowledge base in AWS Bedrock

Quick answer
To create a knowledge base in AWS Bedrock, use the boto3 bedrock-runtime client to upload and index your documents, then query them with the converse API. This involves preparing your documents, storing them in a vector store or database, and invoking Bedrock models for retrieval-augmented generation.

PREREQUISITES

  • Python 3.8+
  • AWS account with Bedrock access
  • AWS CLI configured or environment variables for AWS credentials
  • pip install boto3

Setup

Install the boto3 library and configure AWS credentials with Bedrock access. You can set credentials via ~/.aws/credentials or environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

bash
pip install boto3

Step by step

This example shows how to create a simple knowledge base by uploading documents, embedding them externally (e.g., using OpenAI embeddings), storing vectors in a database, and querying with AWS Bedrock's converse API for retrieval-augmented generation.

python
import os
import boto3
import json

# Initialize Bedrock runtime client
client = boto3.client('bedrock-runtime', region_name='us-east-1')

# Example: documents to add to knowledge base
documents = [
    {"id": "doc1", "text": "AWS Bedrock enables easy access to foundation models."},
    {"id": "doc2", "text": "You can build knowledge bases by combining embeddings and Bedrock models."}
]

# Normally, you would embed these documents with an embedding model and store vectors in a vector DB
# For demo, we simulate a simple retrieval by matching keywords

# User query
query = "How to create a knowledge base in AWS Bedrock?"

# Compose prompt with retrieved context (simulate retrieval)
retrieved_context = "AWS Bedrock enables easy access to foundation models."

prompt = f"Use the following context to answer the question.\nContext: {retrieved_context}\nQuestion: {query}\nAnswer:"

# Call Bedrock converse API with Anthropic Claude model
response = client.converse(
    modelId="anthropic.claude-3-5-haiku-20241022",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": prompt}
    ]
)

answer = response['output']['message']['content'][0]['text']
print("Answer:", answer.strip())
output
Answer: To create a knowledge base in AWS Bedrock, you prepare your documents, embed them for retrieval, and then use Bedrock models like Claude to answer queries based on the retrieved context.

Common variations

  • Use different Bedrock models by changing modelId, e.g., amazon.titan-text-express-v1.
  • Integrate external vector databases like Pinecone or FAISS for scalable retrieval.
  • Use async calls with aiobotocore for concurrency.

Troubleshooting

  • If you get AccessDeniedException, verify your AWS Bedrock permissions and region.
  • For ModelNotFoundException, confirm the modelId is correct and available in your region.
  • Timeouts may require increasing client timeout or retry logic.

Key Takeaways

  • Use boto3 bedrock-runtime client to interact with AWS Bedrock models.
  • Combine document embeddings and vector search with Bedrock's converse API for knowledge base queries.
  • Ensure AWS credentials and Bedrock permissions are correctly configured for API access.
Verified 2026-04 · anthropic.claude-3-5-haiku-20241022, amazon.titan-text-express-v1
Verify ↗