chatbot1-8 Exercise 2: What Buffering Costs ============================================== ex2.py (save in the project root): import asyncio, time from bot.core import Bot from bot.persona import Persona from bot.providers import FakeProvider PERSONA = Persona("Rowan", "a library bot.", "brief", "library questions", "I can only help with library questions.") REPLY = " ".join(f"word{i}" for i in range(1, 21)) # 20 words, 0.05 s between pieces async def run(live): bot = Bot(FakeProvider([REPLY], chunk_delay=0.05), persona=PERSONA, stream_live=live) start = time.perf_counter() first = None count = 0 async for event in bot.stream_message("s", "hello"): count += 1 if first is None: first = time.perf_counter() - start total = time.perf_counter() - start return first, total, count async def main(): print(f"{'mode':<9} {'first text on screen':>21} {'reply complete':>15} {'events':>7}") for live in (True, False): first, total, count = await run(live) print(f"{'live' if live else 'buffered':<9} {first:>20.2f}s {total:>14.2f}s {count:>7}") asyncio.run(main()) Run with: py ex2.py Output (timings vary by a few hundredths of a second between runs): mode first text on screen reply complete events live 0.06s 1.24s 21 buffered 1.25s 1.25s 1 Which mode for a children's library bot (judgement): buffered. If a model says something unsuitable, the guards and moderation can only act once they have the whole reply; in live mode the child's screen would already have shown it, even if it is then replaced. Waiting a second or two for a reply is a small price for never showing unchecked text to that audience. For a low-stakes bot where speed matters more, live is the better experience. WHY THIS WORKS AS AN ANSWER --------------------------- Live mode sends one event per word (20 deltas plus the final: 21 events), so the first word is visible after one pause of 0.05 s and the reply completes after 20 pauses, about 1.2 s. Buffered mode must wait for the complete reply before it can run the checks, so it sends a single event at about the same total time but nothing is visible before then. The checks, the stored text and the final text are identical in both modes; only what the user can see while waiting differs.