Memory Systems
Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.
Setup
- Open a fresh terminal in a new folder, e.g.
mkdir lab-3 && cd lab-3. - Create a Python 3.11 venv:
python -m venv .venv && source .venv/bin/activate. - Install the dependencies noted on each starter snippet (the small pip install … line above the code).
- Copy each starter into a file with the suggested name, then run it and capture the output.
Starter code
import psycopg, openai
db = psycopg.connect("postgresql://localhost/agent")
db.execute("CREATE EXTENSION IF NOT EXISTS vector;")
db.execute("""CREATE TABLE IF NOT EXISTS mem(
id bigserial primary key, text text, embedding vector(1536))""")
def embed(text):
return openai.embeddings.create(model="text-embedding-3-small", input=text).data[0].embedding
def remember(text):
db.execute("INSERT INTO mem(text, embedding) VALUES(%s, %s)", (text, embed(text)))
db.commit()
def recall(query, k=3):
return db.execute(
"SELECT text FROM mem ORDER BY embedding <-> %s LIMIT %s",
(embed(query), k)).fetchall()python 03_01_lab.pyclass ChatMemory:
def __init__(self, llm, window=8):
self.llm, self.window, self.msgs, self.summary = llm, window, [], ""
def add(self, role, content):
self.msgs.append({"role": role, "content": content})
if len(self.msgs) > self.window:
chunk = self.msgs[:-self.window]
self.summary = self.llm.summarise(self.summary + "\n" + str(chunk))
self.msgs = self.msgs[-self.window:]
def context(self):
return [{"role":"system","content": f"Summary: {self.summary}"}] + self.msgspython 03_02_lab.pyGuided steps
Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.
- 1Pgvector memory
Do — Stand up Postgres+pgvector locally. Ingest 100 personal notes and query top-3 by embedding distance.
Expected artifact — Query 'what did I learn about LoRA' returns notes that actually mention LoRA in rank 1.
Run free: - 2Sliding window + summary
Do — Implement ChatMemory with window=6. Run a 20-turn conversation and dump the rolling summary.
Expected artifact — memory_dump.json with 6 recent messages and a summary field referencing earlier topics.
Run free: - 3Memory eval
Do — On turn 25, ask 'what was the user's name they gave on turn 2?'. Test with and without long-term memory.
Expected artifact — With memory: correct. Without: incorrect/unsure. Documented in memory_eval.md.
Run free:
Authoritative sources
Independent primary sources used to check and corroborate the guidance on this page.
- NIST National Initiative for Cybersecurity Education (NICE)
US national framework for cybersecurity work roles and competencies.
- Financial Action Task Force (FATF)
Global AML/CFT standards referenced by financial-crime curricula.
- FBI Internet Crime Complaint Center — Annual Reports
Primary loss and typology data used in fraud-investigation training.
- US Federal Trade Commission — Consumer Sentinel Data Book
Official complaint statistics for teaching fraud trend analysis.
