跳转到内容

Day 4 · 上下文与记忆

本章目标:解决"聊久了爆炸"和"下次忘了"两个问题。 产出:my-agent/context.py + my-agent/memory.py

你将学到

  • 为什么上下文会爆炸(token 线性增长)
  • 上下文管理策略:keep_last + 摘要压缩
  • 向量记忆:embedding + 余弦相似度检索
  • Agent v2:接入 Context + Memory

内容结构

内容讲义Demo
18为什么要管上下文18-why-context-mattersdemo_18_context_explosion.py
19上下文管理策略19-context-strategiesdemo_19_context_strategies.py
20Memory 系统20-memory-typesdemo_20_memory.py
21Agent v2(整合)demo_21_agent_v2.py

运行 Demo

bash
cd 04-context-and-memory/lab
python demo_18_context_explosion.py
python demo_19_context_strategies.py
python demo_20_memory.py
python demo_21_agent_v2.py

今日产出

补全 my-agent/context.pymy-agent/memory.py,让测试通过:

bash
cd my-agent
python tests/test_context.py
python tests/test_memory.py

配套练习

exercises/04-context-and-memory/ — 3 道代码填空题

Released under the MIT License.