Get startedGet started for free

Building a conversation-manager class

To manage conversation history in a chatbot, you'll create a class that will store all user and assistant messages, so that Claude can use them to generate coherent responses. In the next exercise, you'll build on this to send formatted messages to the Claude model.

The boto3 library has been preloaded.

This exercise is part of the course

Introduction to Amazon Bedrock

View Course

Exercise instructions

  • Create an empty list to store the conversation_history.
  • Add a method add_message() that takes a role and content, and appends a dictionary to the history.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class ConversationManager:
    def __init__(self):
        self.bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
        # Create an empty list for the conversation history
        ____

    # Add the method to append messages    
    def ____(self, ____, ____):
        self.conversation_history.append({"role": role, "content": content})
Edit and Run Code