Project 5: Build a memory system for an agent
Episodic memory (events with time), semantic memory (consolidated facts with source and timestamp), retrieval for a question, handling of contradictions, and a long-term evaluation over scripted sessions showing that the agent gets better thanks to the memory.
Theory
Episodes are raw data about what happened. Semantic facts are distilled from episodes (key → value) and the latest source wins in a conflict. Retrieval returns the most relevant episodes + facts. Long-term evaluation: in session N, ask about things that were said in sessions < N.
Sub-tasks
- episodic memory —
Memory.remember(session, text)stores an episode with a sequence number;Memory.recall(query, k)→ the k most relevant episodes (BM25 or word overlap). - consolidation —
Memory.consolidate()extracts facts from the Swedish patterns 'X är Y' (X is Y) / 'jag gillar Y' (I like Y) / 'mitt namn är Y' (my name is Y) →factswith (value, session, episode id); newer replaces older. - answer from memory —
Memory.answer(question)→ a string from the facts or the best episode; 'vet inte' (don't know) otherwise. - forgetting + privacy —
Memory.forget(session)deletes episodes and facts from a session (the right to be forgotten).
Passes when: accuracy_with_memory >= 0.8
The starter code
runs in an isolated sandbox on the serverimport re
def toks(s):
return set(re.findall(r"[a-zåäö0-9]+", s.lower()))
class Memory:
def __init__(self):
self.episodes = [] # {"id", "session", "text"}
self.facts = {} # nyckel → {"value", "session", "episode"}
def remember(self, session, text):
# TODO: lägg till episod med id = löpnummer
...
def recall(self, query, k=3):
# TODO: rangordna episoder på ordöverlapp med query (toks), returnera k bästa (nyast först vid lika)
...
def consolidate(self):
# TODO: mönster: "mitt namn är X" → namn; "jag gillar X" → gillar; "min favoritfärg är X" → favoritfärg;
# "jag bor i X" → bor; senare episod ersätter tidigare
...
def answer(self, question):
# TODO: mappa frågor ("vad heter jag", "vad gillar jag", "vilken är min favoritfärg", "var bor jag") till fakta; annars bästa episod; annars "vet inte"
...
def forget(self, session):
# TODO: radera episoder och fakta från sessionen
...
You write the code; tests you cannot see decide whether it holds up. Create a free account to run the lab.
Try the diagnosticCreate a free accountExpected results
With memory ≥ 80 % correct on questions about earlier sessions; without memory ≈ 0. A contradiction (the favourite colour changes) is resolved to the latest. forget(2) makes session 2's facts disappear.
Common mistakes
- Consolidation overwrites with older facts (wrong order).
- Retrieval returns episodes from deleted sessions.
- 'vet inte' (don't know) is missing → it makes things up.