0%
⭐ 0 XPMap
Chapter 2 · Quest 5 of 5 · 15 XP

Chat Loop

💬 Models don't remember anything on their own. To hold a conversation, your bot keeps a message history: a list of every message so far, sent in full each time.

messages = []
messages.append({"role": "user", "content": "Hi!"})
reply = pip.chat(messages)
messages.append({"role": "assistant", "content": reply})

Each message is a dict with a role (who said it) and content (what they said). pip.chat() reads the whole list and answers the last user message. That's memory, the simple way. Finish this one for your second badge: First Contact.

Instructions

  1. Make an empty list: messages = []
  2. Loop over talk. Each time, add {"role": "user", "content": text} to messages.
  3. In the loop, store pip.chat(messages) in reply, print it, and add it as {"role": "assistant", "content": reply}.
  4. At the end, messages holds 6 messages and Pip says Your name is Sam.
💡 Hint

for text in talk: append the user message, then reply = pip.chat(messages), print(reply), then append the assistant message. All 4 lines indented inside the loop.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 2: Talk to the Model

The full quest map

Talk it out

  1. Read the instruction with the ✗ out loud.
  2. Say what your code does, line by line, as if to a friend.
  3. Find the line where what it does and what you want split apart.
  4. Press Run and read the terminal. Is that what you expected?
  5. Still stuck? Press Ask Pip why, or peek at the solution.
  6. Or ask on the community help board: say which quest, and paste your code.
New badge!

Your bot just got a new part. See it on the quest map.

Name your bot

It levels up as you play, and gets a new part with every chapter.