BUILD A LOCAL CHATBOT · MODULE 2 — GIVING IT A MEMORY · bc-04 Real Python, every page

Module 2.4 · Deepens page 4

Why it forgot what you just said

bc-01's ten lines called the model exactly once. Call it twice in a row, with nothing carried over between calls, and watch what actually happens — not "it says it doesn't know," something stranger.

Turn 1 you
My name is Alex.
model
I'm Alex, a helpful AI assistant designed to assist users in navigating the world of technology. I'm here to help you with your questions, concerns
Turn 2 — a completely fresh call. Nothing from Turn 1 is passed in. you
What's my name?
model
My name is Kaelin Blackwood, and I am a skilled and resourceful warrior, skilled in the art of combat and stealth. I have been
Real output. The model doesn't say "I don't know" — it doesn't even recognize the question is about a previous conversation, because as far as this call is concerned, there was no previous conversation. It free-associates a whole new character instead.

The script

Two calls, zero shared state

bc-04-no-memory.py
def ask(message):
    # every call starts from scratch — nothing about a previous call is passed in here
    prompt = tokenizer.apply_chat_template(
        [{"role": "user", "content": message}], tokenize=False, add_generation_prompt=True
    )
    inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
    outputs = model.generate(**inputs, max_new_tokens=30)
    return tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)

ask("My name is Alex.")
ask("What's my name?")   # a separate call — this function has no idea Alex was ever mentioned

apply_chat_template wraps the message in the model's special turn tokens — but it only ever sees the one message you pass to it. Download bc-04-no-memory.py ⬇

Try this

Run bc-04-no-memory.py ⬇ a few times. The exact persona in Turn 2 will change each run (it's sampling, not a fixed script) — but it will never correctly say "Alex." Predict why before running it: is there any version of this script, unchanged, that could get it right by luck?

So what

The model has no memory to forget — every call is the first call unless you build the memory yourself.