Bedrock Knowledge Bases Advanced

Bedrock Knowledge Bases enable Retrieval-Augmented Generation (RAG) by connecting foundation models to your organization's data. Documents are automatically chunked, embedded, and stored in a vector database, allowing the model to retrieve relevant context when answering questions.

Knowledge Base Architecture

Architecture Flow
Ingestion:
  S3 Bucket (documents) → Chunking → Embedding Model → Vector Store

Query:
  User Question → Embedding → Vector Search → Relevant Chunks
  Relevant Chunks + Question → Foundation Model → Grounded Answer

Supported Vector Stores:
  - Amazon OpenSearch Serverless (recommended)
  - Amazon Aurora PostgreSQL (pgvector)
  - Pinecone
  - Redis Enterprise Cloud

Setting Up a Knowledge Base

  1. Prepare data source

    Upload documents (PDF, TXT, HTML, CSV, DOCX) to an S3 bucket.

  2. Create knowledge base

    Select embedding model (Titan Embeddings), configure chunking strategy, and choose vector store.

  3. Sync data

    Trigger ingestion to process documents into vector embeddings. Re-sync when documents change.

  4. Query

    Use the RetrieveAndGenerate API or connect to a Bedrock Agent.

Chunking Strategies

StrategyChunk SizeBest For
Fixed size300-500 tokensUniform documents (articles, docs)
SemanticVariableMixed content (long-form, tables)
HierarchicalParent + childStructured documents (manuals, specs)
No chunkingWhole documentShort documents (<500 tokens each)

Querying the Knowledge Base

Python
client = boto3.client('bedrock-agent-runtime')

response = client.retrieve_and_generate(
    input={'text': 'What is our refund policy?'},
    retrieveAndGenerateConfiguration={
        'type': 'KNOWLEDGE_BASE',
        'knowledgeBaseConfiguration': {
            'knowledgeBaseId': 'KB_ID',
            'modelArn': 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0'
        }
    }
)
print(response['output']['text'])
Quality Tip: The quality of RAG responses depends heavily on chunking strategy and embedding model. Experiment with different chunk sizes and overlap amounts. Use the Retrieve API to inspect which chunks are being retrieved before the model generates a response.

Ready for Best Practices?

The final lesson covers security, cost management, and production patterns for Bedrock.

Next: Best Practices →