Managing AI conversations

Introduction to Amazon Bedrock

Nikhil Rangarajan

Data Scientist

Conversation state management in Bedrock

  • Managing conversations: essential for natural, coherent AI interactions
  • Conversation context: includes history and metadata
  • Claude models can account for context

An illustration showing circles with one element of the conversation each: the message, the history, and the metadata.

Introduction to Amazon Bedrock

Implementing conversation management

  • ConversationManager class handles core conversation functionality
  • Message history stores both user and assistant messages with roles
  • Structured approach ensures scalability and maintainability
class ConversationManager:

def __init__(self): self.bedrock = boto3.client('bedrock-runtime')
self.conversation_history = [] self.max_tokens = 8000
def add_message(self, role, content): self.conversation_history .append({"role": role,
"content": content
})
Introduction to Amazon Bedrock

Using ConversationManager in practice

  • Add new messages to the conversation log
  • Ensure the user's input is logged
  • Help the model maintain context

 

conversation_manager = ConversationManager()
conversation_manager.add_message("user", "What's the weather like?")
Introduction to Amazon Bedrock

Using ConversationManager in practice

  • Format as message
  • Limit the amount of text sent
  • Send formatted conversation to the model
# Send the latest context to the model
conversation_history = conversation_manager.conversation_history
messages = "\n\n".join(
    f"{msg['role']}: {msg['content']}" 
    for msg in conversation_history[-2:]) # Only most recent exchanges


# Send the full message to the model response = conversation_manager.bedrock.invoke_model( modelId="amazon.nova-micro-v1:0", body=messages)
Introduction to Amazon Bedrock

Let's practice!

Introduction to Amazon Bedrock

Preparing Video For Download...