AI Model Wiki

    Skip to main content
    28 min
    Lesson 2 of 4

    Knowledge Bases and RAG

    From the course AWS Bedrock Advanced

    Build production-ready RAG systems with Amazon Bedrock Knowledge Bases.

    Bedrock Knowledge Bases

    Setup

    import boto3
    
    bedrock_agent = boto3.client('bedrock-agent', region_name='us-east-1')
    
    # Create knowledge base
    knowledge_base = bedrock_agent.create_knowledge_base(
        name="CompanyDocs",
        description="Internal documentation and policies",
        roleArn="arn:aws:iam::account:role/bedrock-kb-role",
        knowledgeBaseConfiguration={
            "type": "VECTOR",
            "vectorKnowledgeBaseConfiguration": {
                "embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v2:0"
            }
        },
        storageConfiguration={
            "type": "OPENSEARCH_SERVERLESS",
            "opensearchServerlessConfiguration": {
                "collectionArn": "arn:aws:aoss:us-east-1:account:collection/my-collection",
                "vectorIndexName": "bedrock-knowledge-base"
            }
        }
    )
    

    Query Knowledge Base

    bedrock_agent_runtime = boto3.client('bedrock-agent-runtime')
    
    # Retrieve and generate
    response = bedrock_agent_runtime.retrieve_and_generate(
        input={"text": "What is our parental leave policy?"},
        retrieveAndGenerateConfiguration={
            "type": "KNOWLEDGE_BASE",
            "knowledgeBaseConfiguration": {
                "knowledgeBaseId": "YOUR_KB_ID",
                "modelArn": "anthropic.claude-3-5-sonnet-20241022-v2:0",
                "generationConfiguration": {
                    "inferenceConfig": {"textInferenceConfig": {"maxTokens": 500}}
                }
            }
        }
    )
    
    print(response['output']['text'])
    # Access source citations
    for citation in response['citations']:
        print(f"Source: {citation['retrievedReferences'][0]['location']}")
    

    Bedrock Agents

    Building AI Agents

    # Create Bedrock Agent with action groups
    agent = bedrock_agent.create_agent(
        agentName="CustomerSupportAgent",
        agentResourceRoleArn="arn:aws:iam::account:role/bedrock-agent-role",
        foundationModel="anthropic.claude-3-5-sonnet-20241022-v2:0",
        instruction="""You are a helpful customer support agent.
        You can look up orders, check inventory, and process refunds.
        Always be polite and escalate complex issues to humans.""",
        knowledgeBaseIds=["KB_ID"]  # Link to knowledge base
    )
    
    # Add action group for tools
    bedrock_agent.create_agent_action_group(
        agentId=agent['agent']['agentId'],
        agentVersion="DRAFT",
        actionGroupName="OrderManagement",
        apiSchema={
            "payload": open("openapi-schema.json").read(),
            "payloadType": "JSON"
        },
        actionGroupExecutor={
            "lambda": "arn:aws:lambda:us-east-1:account:function:order-handler"
        }
    )
    

    Key Takeaways

    • •Bedrock Knowledge Bases provide fully managed RAG with automatic chunking and embedding
    • •Retrieve and Generate API combines vector search with LLM generation and citation
    • •Bedrock Agents orchestrate multi-step tasks with tool calling and knowledge bases
    • •OpenSearch Serverless provides scalable vector storage with no infrastructure management
    0 of 4 lessons marked complete

    Progress is kept in this browser only.