Lab 04 — Give a Bedrock chatbot a memory

Scaffold: 4/5. The table is built and wired; you write the load-and-save logic that turns single calls into a conversation.

The scenario

A support chatbot answers each message perfectly and forgets it instantly. Ask it your name, then ask it back, and it has no idea, because a model call has no memory: every request starts from a blank slate. To hold a conversation you replay the earlier turns on each call, and that transcript has to live somewhere between requests. This lab puts it in DynamoDB, keyed by session.

The requirement

Two messages in the same session behave like a conversation:

You:  My name is Sam and my favourite colour is teal.
Bot:  Nice to meet you, Sam.
You:  What is my name and favourite colour?
Bot:  You are Sam and your favourite colour is teal.

What’s provided

Your task

In src/handler.py:

  1. Load the history for session_id from DynamoDB before the call (the item stores the message list as JSON in messages).
  2. Append the new user turn, call the model with the full list, then append the assistant reply.
  3. Save the updated list back.

Keep every entry in the Converse messages shape: {"role": "user"|"assistant", "content": [{"text": "..."}]}.

Run it

./scripts/deploy.sh
./scripts/test.sh          # states a fact, then asks for it back
./scripts/teardown.sh

What success looks like

Before you wire it, the second answer has no idea what your name is. After, it does, and the turns count climbs across calls in the same session. Start a new session (SESSION=other ./scripts/test.sh) and the memory is separate, because it is keyed by session id.

If it fails

Reveal the solution

SRC=solution ./scripts/deploy.sh && ./scripts/test.sh

What you just learned

Next

Lab 05 — Stand up a vector store and answer from your own documents. The scaffolding drops again: you wire retrieval into the chat, grounding answers in a corpus instead of the model’s own memory.