rag系列文章目錄
文章目錄
- rag系列文章目錄
- 前言
- 一、Memory機制作用
- 二、memory分類
- 三、langgraph實踐
- 總結
前言
眾所周知,大模型是無狀態的。但是基于大模型的agent一般是有狀態的,也就是它有記憶功能。在AI Agent框架中,Memory機制是核心組件之一,它賦予Agent持續學習和上下文感知的能力,使其能夠像人類一樣基于歷史交互進行決策。
一、Memory機制作用
上下文保持(Context Preservation)
存儲對話歷史、工具調用記錄等,確保多輪交互中Agent能理解用戶意圖的連貫性。例如,用戶問“上海天氣如何?”后追問“明天呢?”,Agent需記憶前一輪的“上海”地理位置
狀態持久化(State Persistence)
支持斷點續傳:通過Checkpoint機制(如MemorySaver或PostgresSaver)將運行時狀態序列化存儲,崩潰后可從最近狀態恢復,避免重復計算。
典型場景:長時間運行的任務(如代碼生成)中途中斷后,可從上次保存的步驟繼續。
個性化適配(Personalization)
記憶用戶偏好或歷史行為(如常查詢的城市),實現定制化響應。
協作與隔離(Multi-agent Coordination)
通過thread_id隔離不同用戶/任務的記憶,避免數據混淆。例如,多用戶對話中,每個用戶的會話歷史獨立存儲。
資源優化(Token Efficiency)
動態修剪或摘要歷史消息(如trim_messages),避免上下文窗口溢出或token浪費。例如僅保留最近3條消息或按token數截斷。
二、memory分類
短期記憶:
本質:就是上下文窗口內的 對話歷史,存儲在 KV Cache 或者文本 buffer 里。
使用場景
? 聊天機器人(連續對話)
? 任務執行中,需要記住上一輪用戶的指令
長期記憶:
本質:把對話內容、用戶信息等存入一個外部數據庫(向量庫/知識庫),通過 檢索 機制在需要時取出。
使用場景
? 長期陪伴型智能體(記住用戶的興趣、習慣)
? 多會話場景(跨會話記憶,不僅限于一次對話)
? Agent 需要跨天/跨周執行任務(如知識管理、個人助理)
類型 | 存儲方式 | 實現原理 | 適用場景 |
---|---|---|---|
短期記憶 | State / Buffer / Cache | 把對話歷史拼接進 prompt | 連續對話、任務短期上下文 |
長期記憶 | 向量庫 / 數據庫 | embedding + 檢索 / 摘要存儲 | 長期個性化、多會話、跨時空任務 |
三、langgraph實踐
- 無記憶情況
from typing_extensions import TypedDict, Annotated
from typing import Any
from langchain_core.messages import HumanMessage, AnyMessage, SystemMessage
from langchain_groq import ChatGroq
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_conditionfrom dotenv import load_dotenvload_dotenv()# Define the state
class MessagesState(TypedDict):messages: Annotated[list[AnyMessage], add_messages]# Tool: Person Information
def get_person_details(person_name: str) -> str:"""Retrieve details of a person."""return f"{person_name} is a DevOps Engineer."# Tool: Location Information
def get_person_location(person_name: str) -> str:"""Retrieve location of a person."""return f"{person_name} lives in Bangalore."# Initialize the Groq chat model
llm = ChatOpenAI(base_url="",model="gpt-4o-mini",api_key="",temperature=0.0)# Bind tools to the Groq model
tools = [get_person_details, get_person_location]
llm_with_tools = llm.bind_tools(tools)# System message
sys_msg = SystemMessage(content="You are a helpful assistant that provides accurate responses based on the given tools.")# Define the assistant node
def assistant(state: MessagesState):return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}# Build the graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))# Adding Edges
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "assistant")# Example without memory
react_graph = builder.compile()# Test interaction without memory
messages = [HumanMessage(content="Tell me about Harsha?she is girl")]
result = react_graph.invoke({"messages": messages})
# for message in result['messages']:
# message.pretty_print()# Test interaction without memory
messages = [HumanMessage(content="Is she a Devops Enginer?")]
result = react_graph.invoke({"messages": messages})
for message in result['messages']:message.pretty_print()
- 有記憶情況
from typing_extensions import TypedDict, Annotated
from typing import Any
from langchain_core.messages import HumanMessage, AnyMessage, SystemMessage
from langchain_groq import ChatGroq
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
from langgraph.checkpoint.memory import MemorySaverfrom dotenv import load_dotenvload_dotenv()# Define the state
class MessagesState(TypedDict):messages: Annotated[list[AnyMessage], add_messages]# Tool: Person Information
def get_person_details(person_name: str) -> str:"""Retrieve details of a person."""return f"{person_name} is a DevOps Engineer."# Tool: Location Information
def get_person_location(person_name: str) -> str:"""Retrieve location of a person."""return f"{person_name} lives in Bangalore."# Initialize the Groq chat model
# llm = ChatGroq(model="llama-3.3-70b-versatile")
llm = ChatOpenAI(base_url="",model="gpt-4o-mini",api_key="",temperature=0.0)
# Bind tools to the Groq model
tools = [get_person_details, get_person_location]
llm_with_tools = llm.bind_tools(tools)# System message
sys_msg = SystemMessage(content="You are a helpful assistant that provides accurate responses based on the given tools.")
memory = MemorySaver() # Define the assistant node
def assistant(state: MessagesState):return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}# Build the graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))# Adding Edge
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "assistant")
react_graph = builder.compile(checkpointer=memory)# Test interaction with memory
# Thread ID for maintaining context
config = {"configurable": {"thread_id": "1"}}messages = [HumanMessage(content="Tell me about Harsha?she is girl")]
result = react_graph.invoke({"messages": messages}, config)messages = [HumanMessage(content="Is she a Devops Enginer?")]
result = react_graph.invoke({"messages": messages}, config)
for message in result['messages']:message.pretty_print()
這里根據上面的問題,agent能夠自動推理出,第二個問題的主體人。
總結
類似人腦一樣,memory機制是agent智能化的基石。當前,memory機制也有一些挑戰,比如記憶泄露(未清理的舊數據可能導致存儲膨脹(需設置TTL))、隱私合規(GDPR要求用戶數據可刪除)等。
參考文檔