MarkTechPost@AI 11月03日 13:29
构建具备持久记忆与个性化能力的智能代理
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文教程详细介绍了如何构建一个能够记忆、学习并随时间适应用户的智能代理。通过实现一个基于简单规则逻辑的持久记忆与个性化系统,模拟现代Agentic AI框架存储和回忆上下文信息的方式。文章逐步展示了代理如何根据经验演变其响应,记忆衰减如何防止信息过载,以及个性化如何提升其性能。最终目标是理解持久记忆如何将静态聊天机器人转变为具有上下文感知能力的、不断进化的数字伴侣。教程提供了完整的代码示例。

🧠 **持久记忆系统构建**:教程首先定义了`MemoryItem`类来存储信息片段,并构建了`MemoryStore`,其中包含一个指数衰减机制,模拟人类记忆的存储和老化过程,为代理的长期记忆奠定基础。

💡 **记忆管理与检索**:在此基础上,教程扩展了记忆系统,增加了插入、搜索和清理旧记忆的方法。通过实现简单的相似度函数和基于衰减的清理例程,使代理能够记住相关事实,同时自动遗忘薄弱或过时的信息。

🤖 **智能代理设计与响应适应**:教程设计了一个利用记忆来指导响应的智能代理。通过一个模拟的语言模型,代理能够根据存储的偏好和主题调整回答。同时,`perceive`函数使代理能够动态捕获用户的新见解,实现对用户偏好的学习。

📊 **个性化效益评估**:最后,教程实现了代理的行动能力,并加入了一个评估循环,用于比较个性化响应与无记忆基线的差异,量化记忆带来的提升。这证明了持久记忆如何将静态脚本转变为具有学习能力的伴侣。

🚀 **Agentic AI未来展望**:文章强调,添加记忆和个性化使代理更加拟人化,能够记住偏好、调整计划并自然遗忘过时细节。简单的衰减和检索机制显著提高了代理的相关性和响应质量,持久记忆是下一代Agentic AI的核心基础。

In this tutorial, we explore how to build an intelligent agent that remembers, learns, and adapts to us over time. We implement a Persistent Memory & Personalisation system using simple, rule-based logic to simulate how modern Agentic AI frameworks store and recall contextual information. As we progress, we see how the agent’s responses evolve with experience, how memory decay helps prevent overload, and how personalisation improves performance. We aim to understand, step by step, how persistence transforms a static chatbot into a context-aware, evolving digital companion. Check out the FULL CODES here.

import math, time, randomfrom typing import Listclass MemoryItem:   def __init__(self, kind:str, content:str, score:float=1.0):       self.kind = kind       self.content = content       self.score = score       self.t = time.time()class MemoryStore:   def __init__(self, decay_half_life=1800):       self.items: List[MemoryItem] = []       self.decay_half_life = decay_half_life   def _decay_factor(self, item:MemoryItem):       dt = time.time() - item.t       return 0.5 ** (dt / self.decay_half_life)

We established the foundation for our agent’s long-term memory. We define the MemoryItem class to hold each piece of information and build a MemoryStore with an exponential decay mechanism. We begin laying the foundation for storing and aging information just like a human’s memory. Check out the FULL CODES here.

 def add(self, kind:str, content:str, score:float=1.0):       self.items.append(MemoryItem(kind, content, score))   def search(self, query:str, topk=3):       scored = []       for it in self.items:           decay = self._decay_factor(it)           sim = len(set(query.lower().split()) & set(it.content.lower().split()))           final = (it.score * decay) + sim           scored.append((final, it))       scored.sort(key=lambda x: x[0], reverse=True)       return [it for _, it in scored[:topk] if _ > 0]   def cleanup(self, min_score=0.1):       new = []       for it in self.items:           if it.score * self._decay_factor(it) > min_score:               new.append(it)       self.items = new

We expand the memory system by adding methods to insert, search, and clean old memories. We implement a simple similarity function and a decay-based cleanup routine, enabling the agent to remember relevant facts while automatically forgetting weak or outdated ones. Check out the FULL CODES here.

class Agent:   def __init__(self, memory:MemoryStore, name="PersonalAgent"):       self.memory = memory       self.name = name   def _llm_sim(self, prompt:str, context:List[str]):       base = "OK. "       if any("prefers short" in c for c in context):           base = ""       reply = base + f"I considered {len(context)} past notes. "       if "summarize" in prompt.lower():           return reply + "Summary: " + " | ".join(context[:2])       if "recommend" in prompt.lower():           if any("cybersecurity" in c for c in context):               return reply + "Recommended: write more cybersecurity articles."           if any("rag" in c for c in context):               return reply + "Recommended: build an agentic RAG demo next."           return reply + "Recommended: continue with your last topic."       return reply + "Here's my response to: " + prompt   def perceive(self, user_input:str):       ui = user_input.lower()       if "i like" in ui or "i prefer" in ui:           self.memory.add("preference", user_input, 1.5)       if "topic:" in ui:           self.memory.add("topic", user_input, 1.2)       if "project" in ui:           self.memory.add("project", user_input, 1.0)   def act(self, user_input:str):       mems = self.memory.search(user_input, topk=4)       ctx = [m.content for m in mems]       answer = self._llm_sim(user_input, ctx)       self.memory.add("dialog", f"user said: {user_input}", 0.6)       self.memory.cleanup()       return answer, ctx

We design an intelligent agent that utilizes memory to inform its responses. We create a mock language model simulator that adapts replies based on stored preferences and topics. At the same time, the perception function enables the agent to dynamically capture new insights about the user. Check out the FULL CODES here.

def evaluate_personalisation(agent:Agent):   agent.memory.add("preference", "User likes cybersecurity articles", 1.6)   q = "Recommend what to write next"   ans_personal, _ = agent.act(q)   empty_mem = MemoryStore()   cold_agent = Agent(empty_mem)   ans_cold, _ = cold_agent.act(q)   gain = len(ans_personal) - len(ans_cold)   return ans_personal, ans_cold, gain

Now we give our agent the ability to act and evaluate itself. We allow it to recall memories to shape contextual answers and add a small evaluation loop to compare personalised responses versus a memory-less baseline, quantifying how much the memory helps. Check out the FULL CODES here.

mem = MemoryStore(decay_half_life=60)agent = Agent(mem)print("=== Demo: teaching the agent about yourself ===")inputs = [   "I prefer short answers.",   "I like writing about RAG and agentic AI.",   "Topic: cybersecurity, phishing, APTs.",   "My current project is to build an agentic RAG Q&A system."]for inp in inputs:   agent.perceive(inp)print("\n=== Now ask the agent something ===")user_q = "Recommend what to write next in my blog"ans, ctx = agent.act(user_q)print("USER:", user_q)print("AGENT:", ans)print("USED MEMORY:", ctx)print("\n=== Evaluate personalisation benefit ===")p, c, g = evaluate_personalisation(agent)print("With memory :", p)print("Cold start  :", c)print("Personalisation gain (chars):", g)print("\n=== Current memory snapshot ===")for it in agent.memory.items:   print(f"- {it.kind} | {it.content[:60]}... | score~{round(it.score,2)}")

Finally, we run the full demo to see our agent in action. We feed it user inputs, observe how it recommends personalised actions, and check its memory snapshot. We witness the emergence of adaptive behaviour, proof that persistent memory transforms a static script into a learning companion.

In conclusion, we demonstrate how adding memory and personalisation makes our agent more human-like, capable of remembering preferences, adapting plans, and forgetting outdated details naturally. We observe that even simple mechanisms such as decay and retrieval significantly improve the agent’s relevance and response quality. By the end, we realize that persistent memory is the foundation of next-generation Agentic AI, one that learns continuously, tailors experiences intelligently, and maintains context dynamically in a fully local, offline setup.


Check out the FULL CODES here. Feel free to check out our GitHub Page for Tutorials, Codes and Notebooks. Also, feel free to follow us on Twitter and don’t forget to join our 100k+ ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.

The post How to Design a Persistent Memory and Personalized Agentic AI System with Decay and Self-Evaluation? appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Agentic AI 持久记忆 个性化 人工智能 Python 教程 AI Persistent Memory Personalization Tutorial Code
相关文章