LLM Agentic Memory Systems

news/2025/10/11 16:34:15/文章来源:https://www.cnblogs.com/lightsong/p/19135366

LLM Agentic Memory Systems

https://kickitlikeshika.github.io/2025/03/22/agentic-memory.html#1-working-memory

 

Introduction

Current AI systems, particularly those built around Large Language Models (LLMs), face a fundamental limitation: they lack true memory. While they can process information provided in their immediate context, they cannot naturally accumulate experiences over time. This creates several problems:

  1. Contextual Amnesia: Agents forget previous interactions with users, forcing repetitive explanations
  2. Inability to Learn: Without recording successes and failures, agents repeat the same mistakes
  3. Personalization Gaps: Agents struggle to adapt to individual users’ preferences and needs over time
  4. Efficiency Barriers: Valuable insights from past interactions are lost, requiring “reinvention of the wheel”

To address these limitations, we need to equip our AI agents with memory systems that capture not just what was said, but what was learned.

Types of Agentic Memory

Agentic memory systems can be categorized into several distinct types, each serving different purposes in enhancing agent capabilities:

1. Working Memory

Working memory represents the short-term, immediate context an agent uses for the current task. It’s analogous to human short-term memory or a computer’s RAM.

Characteristics:

  • Temporarily holds information needed for the current conversation
  • Limited in size due to context window constraints
  • Cleared or reset between different sessions or tasks

Example: When a user asks a series of related questions about a topic, working memory helps the agent maintain coherence throughout that specific conversation without requiring repetition of context.

2. Episodic Memory

Episodic memory stores specific interactions or “episodes” that the agent has experienced. These are concrete, instance-based memories of conversations, including what was discussed and how the interaction unfolded.

Characteristics:

  • Records complete or summarized conversations
  • Includes metadata about the interaction (time, user, topic)
  • Searchable by semantic similarity to current context
  • Contains information about what worked well and what didn’t

Example: An agent remembers that when discussing transformers with a particular user last week, visual explanations were particularly effective, while mathematical formulas caused confusion.

3. Semantic Memory

Semantic memory stores general knowledge extracted from experiences, rather than the experiences themselves. It represents the “lessons learned” across many interactions.

Characteristics:

  • Abstracts patterns across multiple episodes
  • Represents generalized knowledge rather than specific instances
  • Often organized in structured forms (rules, principles, facts)
  • Evolves over time as more experiences accumulate

Example: After numerous interactions explaining technical concepts, an agent develops general principles about how to adapt explanations based on the user’s background.

4. Procedural Memory

Procedural memory captures how to perform actions or processes. For AI agents, this translates to remembering effective strategies for solving problems.

Characteristics:

  • Stores successful action sequences and approaches
  • Focuses on “how” rather than “what”
  • Can be applied across different but similar situations

Example: An agent remembers the effective sequence of steps for debugging code issues, starting with checking syntax, then examining logic, and finally testing edge cases.

 

 

mem0

https://docs.mem0.ai/examples/personal-travel-assistant

https://github.com/mem0ai/mem0/tree/main

import os
from openai import OpenAI
from mem0 import Memory# Set the OpenAI API key
os.environ['OPENAI_API_KEY'] = "sk-xxx"config = {"llm": {"provider": "openai","config": {"model": "gpt-4o","temperature": 0.1,"max_tokens": 2000,}},"embedder": {"provider": "openai","config": {"model": "text-embedding-3-large"}},"vector_store": {"provider": "qdrant","config": {"collection_name": "test","embedding_model_dims": 3072,}},"version": "v1.1",
}class PersonalTravelAssistant:def __init__(self):self.client = OpenAI()self.memory = Memory.from_config(config)self.messages = [{"role": "system", "content": "You are a personal AI Assistant."}]def ask_question(self, question, user_id):# Fetch previous related memoriesprevious_memories = self.search_memories(question, user_id=user_id)# Build the promptsystem_message = "You are a personal AI Assistant."if previous_memories:prompt = f"{system_message}\n\nUser input: {question}\nPrevious memories: {', '.join(previous_memories)}"else:prompt = f"{system_message}\n\nUser input: {question}"# Generate response using Responses APIresponse = self.client.responses.create(model="gpt-4o",input=prompt)# Extract answer from the responseanswer = response.output[0].content[0].text# Store the question in memoryself.memory.add(question, user_id=user_id)return answerdef get_memories(self, user_id):memories = self.memory.get_all(user_id=user_id)return [m['memory'] for m in memories['results']]def search_memories(self, query, user_id):memories = self.memory.search(query, user_id=user_id)return [m['memory'] for m in memories['results']]# Usage example
user_id = "traveler_123"
ai_assistant = PersonalTravelAssistant()def main():while True:question = input("Question: ")if question.lower() in ['q', 'exit']:print("Exiting...")breakanswer = ai_assistant.ask_question(question, user_id=user_id)print(f"Answer: {answer}")memories = ai_assistant.get_memories(user_id=user_id)print("Memories:")for memory in memories:print(f"- {memory}")print("-----")if __name__ == "__main__":main()

 

Implementation

使用向量数据库,并制造记忆prompt或有偏好的记忆信息并存储

https://github.com/KickItLikeShika/agentic-memory/blob/main/agentic-memory.ipynb

 

react prompt属于过程性提示词

https://zhuanlan.zhihu.com/p/1931154686532105460

 

记忆作为工具

https://python.langchain.com/docs/versions/migrating_memory/long_term_memory_agent/

 

summarization

https://github.com/fanqingsong/langgraph-memory-example

from typing import Literal
from langchain_core.messages import SystemMessage, HumanMessage, RemoveMessage
from langchain_openai import ChatOpenAIfrom langgraph.graph import START, StateGraph, MessagesState, END
from langgraph.prebuilt import tools_condition, ToolNodefrom langchain_community.tools.tavily_search import TavilySearchResultstavily_tool = TavilySearchResults(max_results=10)tools = [tavily_tool]# Define LLM with bound tools
llm = ChatOpenAI(model="gpt-4o")
llm_with_tools = llm.bind_tools(tools)
model = llm_with_tools# State class to store messages and summary
class State(MessagesState):summary: str# Define the logic to call the model
def call_model(state: State):# Get summary if it existssummary = state.get("summary", "")# If there is summary, then we add it to messagesif summary:# Add summary to system messagesystem_message = f"Summary of conversation earlier: {summary}"# Append summary to any newer messagesmessages = [SystemMessage(content=system_message)] + state["messages"]else:messages = state["messages"]response = model.invoke(messages)return {"messages": response}# Custom routing function from assistant
def route_assistant(state: State) -> Literal["tools", "summarize_conversation", "__end__"]:"""Route from assistant based on tool calls and message count."""messages = state["messages"]last_message = messages[-1]# Check if the assistant called any toolsif hasattr(last_message, "tool_calls") and len(last_message.tool_calls) > 0:return "tools"# No tools called - check if we should summarizeif len(messages) > 6:return "summarize_conversation"# Otherwise endreturn END# Routing function after tools
def route_after_tools(state: State) -> Literal["summarize_conversation", "assistant"]:"""Route after tools execution."""messages = state["messages"]# If there are more than six messages, summarizeif len(messages) > 6:return "summarize_conversation"# Otherwise go back to assistantreturn "assistant"def summarize_conversation(state: State):# First get the summary if it existssummary = state.get("summary", "")# Create our summarization prompt if summary:# If a summary already exists, add it to the promptsummary_message = (f"This is summary of the conversation to date: {summary}\n\n""Extend the summary by taking into account the new messages above:")else:# If no summary exists, just create a new onesummary_message = "Create a summary of the conversation above:"# Add prompt to our historymessages = state["messages"] + [HumanMessage(content=summary_message)]response = model.invoke(messages)# Delete all but the 2 most recent messages and add our summary to the state delete_messages = [RemoveMessage(id=m.id) for m in state["messages"][:-2]]return {"summary": response.content, "messages": delete_messages}# Build graph
builder = StateGraph(State)  # Note: using State instead of MessagesState
builder.add_node("assistant", call_model)
builder.add_node("tools", ToolNode(tools))
builder.add_node("summarize_conversation", summarize_conversation)# Add edges
builder.add_edge(START, "assistant")# Route from assistant based on tool calls and message count
builder.add_conditional_edges("assistant", route_assistant)# After tools, check if we should summarize or go back to assistant
builder.add_conditional_edges("tools", route_after_tools)# After summarization, go back to assistant
builder.add_edge("summarize_conversation", "assistant")# Compile graph
graph = builder.compile()

 

mem0 集成

import os
from typing import Literal
from langchain_core.messages import SystemMessage, HumanMessage, RemoveMessage
from langchain_openai import ChatOpenAIfrom langgraph.graph import START, StateGraph, MessagesState, END
from langgraph.prebuilt import tools_condition, ToolNodefrom langchain_community.tools.tavily_search import TavilySearchResultsfrom mem0 import MemoryClient
mem0 = MemoryClient(api_key=os.getenv("MEM0_API_KEY"))tavily_tool = TavilySearchResults(max_results=10)tools = [tavily_tool]# Define LLM with bound tools
llm = ChatOpenAI(model="gpt-4o")
llm_with_tools = llm.bind_tools(tools)
model = llm_with_toolsclass State(MessagesState):mem0_user_id: str# Define the logic to call the model
def call_model(state: State):messages = state["messages"]user_id = state.get("mem0_user_id", "default_user_1")# Get only the last message (current user input)current_message = messages[-1]# Retrieve relevant memories based on the current messagememories = mem0.search(current_message.content, user_id=user_id)context = "Relevant information from previous conversations:\n"for memory in memories:context += f"- {memory['memory']}\n"system_message = SystemMessage(content=f"""You are a helpful Assistant. Use the provided context to personalize your responses and remember user preferences and past interactions.
{context}""")# Only send system message + current user message to LLM (no history)full_messages = [system_message, current_message]response = llm_with_tools.invoke(full_messages)# Store the interaction in Mem0 - use a list of messages
    mem0.add(messages=[{"role": "user", "content": current_message.content},{"role": "assistant", "content": response.content}], user_id=user_id)# Clear all previous messages except the current response# so we dont have to store all the messages in the contextmessages_to_delete = [RemoveMessage(id=m.id) for m in messages]return {"messages": messages_to_delete + [response]}# Build graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", call_model)
builder.add_node("tools", ToolNode(tools))# Add edges
builder.add_edge(START, "assistant")# Use tools_condition to route from assistant
builder.add_conditional_edges("assistant",tools_condition,{"tools": "tools",  # If tools are called, go to toolsEND: END,          # If no tools, end
    }
)# After tools execution, go back to assistant
builder.add_edge("tools", "assistant")# Compile graph
graph = builder.compile()

 

https://github.com/fanqingsong/langgraph-mem0-agent

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/934887.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

量化(一)

在神经网络量化过程中,指数平滑法、直方图截断法和KL散度校准法都是用于优化量化过程中缩放因子的计算和选择的技术。这些方法通常旨在尽量减小量化误差,保持模型性能。下面分别解释这三种方法: 1. 指数平滑法(Exp…

2025 年试验箱厂商最新推荐排行榜:涵盖高低温 / 恒温恒湿 / 冷热冲击等设备,精选研发实力强、质量管控严的优质企业

随着工业制造、航空航天、电子通讯等领域飞速发展,试验箱作为检测产品性能稳定性的关键设备,需求日益增长。但当前市场上试验箱制造企业数量繁杂,部分企业存在技术创新不足、设备精度不达标、售后服务滞后等问题,导…

2025 最新化粪池生产厂家推荐排行榜:聚焦老牌标杆与新锐力量,预制 / 玻璃钢品类权威甄选钢筋混凝土/一体/成品/拼装式化粪池厂家推荐

随着城镇化推进与环保政策收紧,化粪池作为市政与民用建筑核心配套,市场需求持续攀升,但行业乱象却愈发凸显:部分厂家工艺粗糙导致产品渗漏率超 30%,使用寿命不足 10 年;多数中小品牌仅能提供单一材质产品,难以适…

MyEMS + 边缘网关:偏远基站如何实现 “无人值守” 下的精准能耗管理?

在通信网络覆盖不断向偏远地区延伸的过程中,偏远基站的运维难题逐渐凸显。这类基站多位于山区、荒漠等地理环境恶劣区域,交通不便、人工运维成本高,且传统 “定期巡检 + 人工调控” 的能耗管理模式,常因数据滞后、…

2025 云栖精选资料:《从云原生到 AI 原生核心技术与最佳实践》PPT 免费下载

AI 原生不再是一个新的概念,而是企业在业务落地实践过程中已经形成的共识。不论是技术团队还是业务部门,都认同 AI 原生已经成为了应用架构的新范式。AI 原生不再是一个新的概念,而是企业在业务落地实践过程中已经形…

Salesforce项目老掉坑?这8个思维陷阱千万别踩

在做Salesforce项目时,你可能遇到过这样的情况: 估算的工作量一拖再拖,本以为的小功能上线后没人用,快速上线却带来长期隐患,客户对新系统抵触甚至不信任……这些问题表面上和需求、技术、流程有关,其实背后往往…

加权图异常检测技术获最具影响力论文奖

卡内基梅隆大学教授Christos Faloutsos因提出加权图异常检测新方法荣获PAKDD最具影响力论文奖,该技术可应用于社交网络欺诈检测、电信网络异常发现等多个领域。Christos Faloutsos荣获PAKDD"最具影响力论文奖&qu…

java基础3-判断和循环

1.顺序结构:顺序结构语句是java程序默认的执行流程,按照代码的先后顺序,从上到下依次执行 2.分支结构:if语句:结构1 if (关系表达式){语句体;  }public class Java01 {public static void main(String[] args…

基于模拟退火的粒子群优化算法的解析

基于模拟退火的粒子群优化算法(Simulated Annealing Particle Swarm Optimization, SAPSO)的解析一、算法原理与创新点 1. 核心思想融合粒子群优化(PSO):通过群体协作搜索最优解,但易陷入局部最优。 模拟退火(S…

总线死锁验证方法

在复杂 SoC 设计中,总线死锁是一类严重影响系统可靠性的问题:多个模块因相互等待资源而陷入永不响应的“僵局”。本文介绍先进的验证策略,结合具体案例,帮你系统掌握如何早期发现并避免总线死锁。 1、什么是总线死…

FPGA MT25QL FLASH

FPGA MT25QL FLASH 基于FPGA读写MT25QL FLASH芯片 https://blog.csdn.net/m0_66360845/article/details/136693637FPGA接口_N25Q128型号的spi flash驱动verilog代码编写 https://blog.csdn.net/weixin_41677362/articl…

C#/.NET/.NET Core优秀项目和框架2025年9月简报

前言 公众号每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架(每周至少会推荐两个优秀的项目和框架当然节假日除外),公众号推文中有项目和框架的详细介绍、功能特点、使用方式以及部分功能截图等(打不开或者打…

论文对比

DG-Mamba: Robust and Efficient Dynamic Graph Structure Learning withSelective State Space Models 论文的创新点 结论 本文提出了一种健壮高效的线性时间复杂度DGSL(动态图结构学习)框架DG-Mamba。在状态离散化…

Alpha稳定分布概率密度函数的MATLAB实现

Alpha稳定分布(Alpha-Stable Distribution)的概率密度函数(PDF)无解析表达式(除高斯分布和柯西分布等特例外),需通过数值方法或近似算法计算。一、核心参数与数学定义 Alpha稳定分布由四个参数定义:稳定性指数…

激光打印机出现黑竖线,清理一下硒鼓即可

激光打印机出现黑竖线,清理一下硒鼓即可如题

关于我心目中的理想课堂构建之法的一些感受

有一说一,选择这门课之前,我曾将其粗略的理解为一门带我们编程的课程。但是老师带我们上了第一节课后,这种感觉似乎烟消云散了。关于理想的课堂,首先,我不是纯cs背景的学生,对我来说,每一次的学习都是一种提高。…

2025 年温控器厂家最新推荐排行榜:涵盖电子式、机械式、双恒温等多类型设备,结合产品性能、创新能力与市场反馈的优质品牌汇总

在工业生产、智能家居、医疗设备等多个领域,温控器都是保障设备稳定运行、提升使用体验的关键部件。当前温控器市场产品种类繁杂,质量与性能差异显著,部分产品存在温控精度不足、稳定性差等问题,难以满足不同场景下…

2025 年工业与民用加热器品牌最新推荐排行榜,深度盘点机柜、柜内、紧凑、PTC 风扇型等多类型加热器优质厂商

当前加热器市场需求持续增长,应用场景覆盖工业生产与日常生活,但大量厂商涌入导致市场产品质量参差不齐。部分产品技术落后,加热效率低、能耗高,不符合节能减排理念,还存在安全与稳定性隐患,给用户选型带来极大困…

Qoj 14436. Robot Construction/Open Your Brain 做题记录

线段树。前置芝士:线段树上二分。 题目大意 你可以制造一个初始高度 \(h\) 在区间 \([0, d]\) 内的机器人。 现在有一条长度为 \(n\) 的路径,上面放置了一些障碍物,用数组 \(a_1, a_2, \ldots, a_n\) 描述。如果 \(…

2025 年最新推荐!国内软件开发厂商排行榜:政企定制开发优选指南 物联网软件开发/运维管理系统软件开发/仓储管理系统软件开发/人力资源管理系统软件开发公司推荐

当前数字化转型浪潮下,政企机构对软件开发服务的需求呈爆发式增长,涵盖 CRM 系统、物联网平台、运维管理系统等多个领域。然而,市场上软件开发厂商资质良莠不齐,部分厂商技术架构落后,无法适配业务长期增长;部分…