Claude With Amazon Bedrock
← All lessons
Lesson 05Claude With Amazon Bedrock

Chat bot exercise

Summary audio

Spoken summary — press play to read along: the line being spoken stays near the top.

Study notes

Chatbot Implementation Study Notes

Objective:

  • Build a simple, conversational chatbot using pre-defined helper functions.
  • The core function of the bot is to maintain conversation context across multiple turns.

Key Concepts & Workflow:

  • Context Maintenance: The chatbot must maintain a list of previous messages (the conversation history) to ensure follow-up prompts (e. g. , "make it shorter") refer to the ongoing topic.
  • Execution Environment: The code can be run in environments like Jupyter Notebook or VS Code.
  • Core Logic (The Loop):
  • Initialize a list of messages (the conversation history).
  • Use a while True loop to allow the chatbot to run indefinitely.
  • Use the built-in input() function to capture user text.

Implementation Steps (The Chat Cycle):

  • Capture Input: Get the user's text input.
  • Add User Message: Call the add_user_message function, passing the current message list and the user's input.
  • Call API: Send the complete, updated list of messages to the chat API.
  • Receive Response: Capture the generated text response from the API.
  • Add Assistant Message: Call the add_assistant_message function, adding the generated text to the message list.
  • Output: Print the generated response to the user.

Environment Specific Note:

  • If running the code in VS Code, explicitly print the user input (e. g. , with a prompt) to display it correctly, as the environment may not automatically show the input.

Takeaways

  • Context maintenance is essential; the chatbot must track a list of previous messages (conversation history) to understand follow-up prompts.
  • The core chatbot logic uses a continuous loop (while True) to capture user input and process responses indefinitely.
  • The chat cycle involves adding the user message, sending the complete message history to the API, and then adding the assistant's response back to the list.
  • If running the code in VS Code, explicitly print the user input to ensure it is displayed correctly.
Flashcards 10 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 5 questions