chatbot1-1 Exercise 1: How Many Messages Does the Provider Get? ================================================================= Predictions (work these out before running the code): (a) Bot, one session, three turns: [1, 3, 5] each turn adds the new question plus the previous answer (b) one_shot, three turns: [1, 1, 1] no memory at all (c) Bot, alice / bob / alice: [1, 1, 3] bob has his own history; alice's second turn carries her first question and answer ex1.py (save in the project root, next to run_terminal.py): import asyncio from bot.core import Bot, one_shot from bot.providers import FakeProvider async def main(): fake = FakeProvider() bot = Bot(fake) for text in ["My name is Ada", "What is my name?", "And my favourite colour?"]: await bot.handle("a", text) print("Bot, one session, 3 turns :", [len(c) for c in fake.calls]) print("roles on the 3rd call :", [m["role"] for m in fake.calls[2]]) fake2 = FakeProvider() for text in ["My name is Ada", "What is my name?", "And my favourite colour?"]: await one_shot(fake2, text) print("one_shot, 3 turns :", [len(c) for c in fake2.calls]) fake3 = FakeProvider() bot3 = Bot(fake3) await bot3.handle("alice", "hi") await bot3.handle("bob", "hi") await bot3.handle("alice", "hi again") print("two sessions interleaved :", [len(c) for c in fake3.calls]) asyncio.run(main()) Run with: py ex1.py Output: Bot, one session, 3 turns : [1, 3, 5] roles on the 3rd call : ['user', 'assistant', 'user', 'assistant', 'user'] one_shot, 3 turns : [1, 1, 1] two sessions interleaved : [1, 1, 3] WHY THIS WORKS AS AN ANSWER --------------------------- All three predictions matched. The bot's history for a session grows by two messages a turn (the user's message and the reply), so the provider is sent 1, then 3, then 5 messages, and the roles alternate user, assistant, user, assistant, user, which is the shape chat APIs expect. one_shot never keeps anything, so it sends 1 every time: that is what a bare model call is. The sessions are kept in a dictionary keyed by session id, so bob's first message does not join alice's history, and alice's second turn carries three messages (her question, the reply, her new question). The numbers describe what your code sent, which is all a fake provider can show; how a real model uses those messages is documented in its provider's API reference.