LangChain / RAG / Vector DBs · 课时

智能体 RAG 中的记忆与状态

为您的 RAG 智能体提供短期和长期记忆,让它能够进行对话并跨轮次回忆事实。

第 4 / 4 课13 个步骤

智能体 RAG 中的记忆与状态 是 CoddyKit 上的免费 LangChain / RAG / Vector DBs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LangChain / RAG / Vector DBs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LangChain / RAG / Vector DBs 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Agents Need Memory

A stateless agent forgets everything after each call. Memory lets an agent track the conversation, remember user preferences, and avoid repeating retrievals.

Short-Term vs. Long-Term

Short-term memory holds the current conversation in the context window. Long-term memory persists facts across sessions, often in a vector store or database.

Conversation Buffer

The simplest memory keeps the full message history and replays it each turn so the model has full context.

history = []

def chat(user_msg):
    history.append({"role": "user", "content": user_msg})
    reply = llm.invoke(history)
    history.append({"role": "assistant", "content": reply})
    return reply

The Context Window Limit

Replaying the entire history eventually overflows the model context. You must summarize or trim older turns to stay within the token budget.

Summary Memory

Periodically compress old turns into a running summary, keeping recent turns verbatim. This preserves gist while freeing tokens.

if len(history) > 20:
    summary = llm.invoke("Summarize: " + str(history[:-6]))
    history[:] = [{"role": "system", "content": summary}] + history[-6:]

Long-Term Memory via Vectors

Store durable facts as embeddings. On each turn, retrieve relevant memories by similarity and inject them, just like RAG over a knowledge base.

memory_store.add_texts(["User prefers metric units"])
relevant = memory_store.similarity_search(user_msg, k=3)

Choosing What to Remember

Do not store everything. Extract durable, reusable facts (preferences, decisions, entities) and skip ephemeral chatter to keep long-term memory clean.

State Beyond Chat

Agents also track non-conversational state: which tools ran, intermediate results, and a scratchpad of reasoning steps used to plan the next action.

Thread and Session Keys

In multi-user systems, scope memory by a thread_id or user_id so conversations never leak between people.

def get_history(thread_id):
    return store.get(thread_id, [])

def save(thread_id, messages):
    store[thread_id] = messages

Persistence

For memory to survive restarts, back it with a database or checkpointer rather than an in-process dictionary. LangGraph offers checkpointers for exactly this.

Putting It Together

Combine a trimmed conversation buffer for recency, summary memory for the middle, and vector long-term memory for durable facts, all keyed by session.

Quick Check

Test your understanding of agent memory.

Recap

You added memory to agents:

  • Short-term buffer for the current chat
  • Summary memory to fit the context window
  • Long-term vector memory for durable facts
  • Scope by session and persist for durability
免费开始

用 AI 导师学习 LangChain / RAG / Vector DBs — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「智能体 RAG 中的记忆与状态」课时是免费的吗?

是的 — 「智能体 RAG 中的记忆与状态」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LangChain / RAG / Vector DBs 课程的其余内容,请升级到 CoddyKit PRO。 LangChain / RAG / Vector DBs 课程共包含 4 节课。

「智能体 RAG 中的记忆与状态」这节课中我会学到什么?

为您的 RAG 智能体提供短期和长期记忆,让它能够进行对话并跨轮次回忆事实。 你通过在浏览器中直接运行的动手代码来练习 LangChain / RAG / Vector DBs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LangChain / RAG / Vector DBs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LangChain / RAG / Vector DBs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「智能体 RAG 中的记忆与状态」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LangChain / RAG / Vector DBs 课中编写并运行代码吗?

能。每节 LangChain / RAG / Vector DBs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. LangChain 智能体与工具概念
  2. 构建多智能体 RAG 工作流
  3. 将外部 API 集成为工具
  4. 智能体 RAG 中的记忆与状态
← 返回 LangChain / RAG / Vector DBs