使用DSPy ReAct和Mem0构建具备记忆功能的智能体
本教程展示如何利用DSPy的ReAct框架结合Mem0的记忆能力,构建能够跨交互记住信息的智能对话智能体。您将学会创建能够存储、检索并利用上下文信息来提供个性化且连贯响应的智能体。
你将构建什么
在本教程结束时,你将拥有一个具备记忆功能的智能体,它可以:
- 记住用户偏好和过去的对话
- 存储和检索关于用户和主题的事实信息
- 利用记忆来指导决策并提供个性化响应
- 处理复杂的多轮对话,具备上下文感知能力
- 管理不同类型的记忆 (事实、偏好、经验)
先决条件
- 对DSPy和ReAct智能体的基本理解
- 已安装 Python 3.9+
- 您首选LLM提供商的API密钥
安装与设置
步骤1:理解Mem0集成
Mem0提供了一个内存层,可以存储、搜索和检索AI智能体的记忆。让我们从了解如何将其与DSPy集成开始:
import dspy
from mem0 import Memory
import os
from typing import List, Dict, Any, Optional
from datetime import datetime
# Configure environment
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
# Initialize Mem0 memory system
config = {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o-mini",
"temperature": 0.1
}
},
"embedder": {
"provider": "openai",
"config": {
"model": "text-embedding-3-small"
}
}
}
步骤2:创建具备记忆感知的工具
让我们创建能够与记忆系统交互的工具:
import datetime
class MemoryTools:
"""Tools for interacting with the Mem0 memory system."""
def __init__(self, memory: Memory):
self.memory = memory
def store_memory(self, content: str, user_id: str = "default_user") -> str:
"""Store information in memory."""
try:
self.memory.add(content, user_id=user_id)
return f"Stored memory: {content}"
except Exception as e:
return f"Error storing memory: {str(e)}"
def search_memories(self, query: str, user_id: str = "default_user", limit: int = 5) -> str:
"""Search for relevant memories."""
try:
results = self.memory.search(query, user_id=user_id, limit=limit)
if not results:
return "No relevant memories found."
memory_text = "Relevant memories found:\n"
for i, result in enumerate(results["results"]):
memory_text += f"{i}. {result['memory']}\n"
return memory_text
except Exception as e:
return f"Error searching memories: {str(e)}"
def get_all_memories(self, user_id: str = "default_user") -> str:
"""Get all memories for a user."""
try:
results = self.memory.get_all(user_id=user_id)
if not results:
return "No memories found for this user."
memory_text = "All memories for user:\n"
for i, result in enumerate(results["results"]):
memory_text += f"{i}. {result['memory']}\n"
return memory_text
except Exception as e:
return f"Error retrieving memories: {str(e)}"
def update_memory(self, memory_id: str, new_content: str) -> str:
"""Update an existing memory."""
try:
self.memory.update(memory_id, new_content)
return f"Updated memory with new content: {new_content}"
except Exception as e:
return f"Error updating memory: {str(e)}"
def delete_memory(self, memory_id: str) -> str:
"""Delete a specific memory."""
try:
self.memory.delete(memory_id)
return "Memory deleted successfully."
except Exception as e:
return f"Error deleting memory: {str(e)}"
def get_current_time() -> str:
"""Get the current date and time."""
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
步骤3:构建带记忆增强的ReAct智能体
现在让我们创建可以使用记忆的主要ReAct智能体:
class MemoryQA(dspy.Signature):
"""
You're a helpful assistant and have access to memory method.
Whenever you answer a user's input, remember to store the information in memory
so that you can use it later.
"""
user_input: str = dspy.InputField()
response: str = dspy.OutputField()
class MemoryReActAgent(dspy.Module):
"""A ReAct agent enhanced with Mem0 memory capabilities."""
def __init__(self, memory: Memory):
super().__init__()
self.memory_tools = MemoryTools(memory)
# Create tools list for ReAct
self.tools = [
self.memory_tools.store_memory,
self.memory_tools.search_memories,
self.memory_tools.get_all_memories,
get_current_time,
self.set_reminder,
self.get_preferences,
self.update_preferences,
]
# Initialize ReAct with our tools
self.react = dspy.ReAct(
signature=MemoryQA,
tools=self.tools,
max_iters=6
)
def forward(self, user_input: str):
"""Process user input with memory-aware reasoning."""
return self.react(user_input=user_input)
def set_reminder(self, reminder_text: str, date_time: str = None, user_id: str = "default_user") -> str:
"""Set a reminder for the user."""
reminder = f"Reminder set for {date_time}: {reminder_text}"
return self.memory_tools.store_memory(
f"REMINDER: {reminder}",
user_id=user_id
)
def get_preferences(self, category: str = "general", user_id: str = "default_user") -> str:
"""Get user preferences for a specific category."""
query = f"user preferences {category}"
return self.memory_tools.search_memories(
query=query,
user_id=user_id
)
def update_preferences(self, category: str, preference: str, user_id: str = "default_user") -> str:
"""Update user preferences."""
preference_text = f"User preference for {category}: {preference}"
return self.memory_tools.store_memory(
preference_text,
user_id=user_id
)
步骤4:运行记忆增强型智能体
让我们创建一个简单的界面来与我们的支持记忆的智能体进行交互:
import time
def run_memory_agent_demo():
"""Demonstration of memory-enhanced ReAct agent."""
# Configure DSPy
lm = dspy.LM(model='openai/gpt-4o-mini')
dspy.configure(lm=lm)
# Initialize memory system
memory = Memory.from_config(config)
# Create our agent
agent = MemoryReActAgent(memory)
# Sample conversation demonstrating memory capabilities
print("🧠 Memory-Enhanced ReAct Agent Demo")
print("=" * 50)
conversations = [
"Hi, I'm Alice and I love Italian food, especially pasta carbonara.",
"I'm Alice. I prefer to exercise in the morning around 7 AM.",
"I'm Alice. What do you remember about my food preferences?",
"I'm Alice. Set a reminder for me to go grocery shopping tomorrow.",
"I'm Alice. What are my exercise preferences?",
"I'm Alice. I also enjoy hiking on weekends.",
"I'm Alice. What do you know about me so far?"
]
for i, user_input in enumerate(conversations, 1):
print(f"\n📝 User: {user_input}")
try:
response = agent(user_input=user_input)
print(f"🤖 Agent: {response.response}")
time.sleep(1)
except Exception as e:
print(f"❌ Error: {e}")
# Run the demonstration
if __name__ == "__main__":
run_memory_agent_demo()
预期输出
🧠 Memory-Enhanced ReAct Agent Demo
==================================================
📝 User: Hi, I'm Alice and I love Italian food, especially pasta carbonara.
🤖 Agent: Hi Alice! It's great to meet you! Pasta carbonara is a delicious choice. Do you have a favorite place to get it, or do you enjoy making it at home?
📝 User: I'm Alice. I prefer to exercise in the morning around 7 AM.
🤖 Agent: Nice to meet you, Alice! I've noted that you prefer to exercise in the morning around 7 AM. If you have any questions or need tips related to your morning workouts, feel free to ask!
📝 User: I'm Alice. What do you remember about my food preferences?
🤖 Agent: I remember that you love Italian food, and your favorite dish is pasta carbonara!
📝 User: I'm Alice. Set a reminder for me to go grocery shopping tomorrow.
🤖 Agent: I have set a reminder for you, Alice, to go grocery shopping tomorrow.
📝 User: I'm Alice. What are my exercise preferences?
🤖 Agent: Your exercise preference is to work out in the morning around 7 AM.
📝 User: I'm Alice. I also enjoy hiking on weekends.
🤖 Agent: Nice to meet you, Alice! It's great to hear that you enjoy hiking on weekends. If you need any recommendations for trails or tips for your hikes, feel free to ask!
📝 User: I'm Alice. What do you know about me so far?
🤖 Agent: So far, I know that you enjoy hiking on weekends, prefer to exercise in the morning around 7 AM, and love Italian food, especially pasta carbonara. Additionally, you have a reminder set for grocery shopping on October 7, 2023.
后续步骤
- 实现内存持久化 使用数据库 (PostgreSQL, MongoDB)
- 添加内存分类和标记以便更好地组织
- 创建内存过期策略用于数据管理
- 构建多用户内存隔离用于生产应用
- 添加内存分析和洞察
- 与向量数据库集成以增强语义搜索
- 实现记忆压缩以提高长期存储效率
本教程演示了如何通过Mem0的记忆能力增强DSPy的ReAct框架,以创建智能、上下文感知的智能体,这些智能体能够在交互中学习和记忆信息,使其在现实应用中更加实用。