AI | Amazon Bedrock + Agent + KB

Michael Wahl
2 min readJan 31, 2024

I found that I still needed some help after reading the link below, but I couldn't find any end-to-end code samples to test with.

https://docs.aws.amazon.com/bedrock/latest/userguide/api-agent-invoke.html

Here’s my code sample with added comments explaining each part:

import logging
import boto3
from botocore.exceptions import ClientError

# Set up logging to help with debugging
logger = logging.getLogger(__name__)

class BedrockAgentRuntimeWrapper:
"""
A class to encapsulate interactions with the Amazon Bedrock Agent Runtime.
It simplifies the process of sending prompts and receiving responses.
"""

def __init__(self, agent_runtime_client):
"""
Initialize the wrapper with a Bedrock Agent Runtime client.

:param agent_runtime_client: A Boto3 client for the Bedrock Agent Runtime service.
"""
self.agent_runtime_client = agent_runtime_client
self.agent_id = '' # The unique identifier for your Bedrock agent

def invoke_agent(self, agent_alias_id, session_id, prompt):
"""
Send a prompt to the Bedrock agent and return its response.

:param agent_alias_id: The alias ID of the agent to be invoked.
:param session_id: A unique identifier for the session, used to maintain context in conversations.
:param prompt: The input text to send to the agent.
:return: The agent's response to the input text.
"""
try:
# Make the call to Bedrock to invoke the agent…

--

--

Michael Wahl
Michael Wahl

Written by Michael Wahl

Husband | Dad | VP of IT | MBA | Author | AI | #AWSCommunityBuilder | Opinions expressed here are my own | https://michaelwahl.carrd.co

Responses (1)