Aan de slagGa gratis aan de slag

Making a conversation memory-aware

Continuing work on ChargeBot at ChargeNet, you’ll store each turn of the dialogue and send only the most recent messages to Claude, to maintain conversation context with Amazon Bedrock's Claude model. The bot will manage message history and format the conversation in a way Claude can understand, using only the most recent context.

The boto3 and json libraries, and the ConversationManager class you defined in the previous exercise, have been preloaded.

Deze oefening maakt deel uit van de cursus

Introduction to Amazon Bedrock

Cursus bekijken

Oefeninstructies

  • Use the add_message() method to store the user's input.
  • Send only the last two messages from the history to Claude using the correct message format.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

conversation = ConversationManager()

user_input = "What is the charging speed of your Highway Max station?"

# Add the user input
conversation.____
# Send only the last two messages from conversation history
messages = ____

request_body = json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 200, "temperature": 0.2, "messages": messages})
response = conversation.bedrock.invoke_model(modelId="anthropic.claude-3-5-sonnet-20240620-v1:0", body=request_body)
completion = json.loads(response['body'].read().decode())["content"][0]["text"]

conversation.add_message("assistant", completion)
print(completion)
Code bewerken en uitvoeren