Add user memory feature for cross-conversation recall
Give the LangChain agent a save_user_memory tool so users can ask it to remember preferences and personal facts. Memories are stored per-user in a new user_memories table and injected into the system prompt on each conversation turn. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -22,6 +22,7 @@ from .logic import (
|
||||
get_conversation_by_id,
|
||||
rename_conversation,
|
||||
)
|
||||
from .memory import get_memories_for_user
|
||||
from .models import (
|
||||
Conversation,
|
||||
PydConversation,
|
||||
@@ -36,15 +37,27 @@ conversation_blueprint = Blueprint(
|
||||
_SYSTEM_PROMPT = SIMBA_SYSTEM_PROMPT
|
||||
|
||||
|
||||
async def _build_system_prompt_with_memories(user_id: str) -> str:
|
||||
"""Append user memories to the base system prompt."""
|
||||
memories = await get_memories_for_user(user_id)
|
||||
if not memories:
|
||||
return _SYSTEM_PROMPT
|
||||
memory_block = "\n".join(f"- {m}" for m in memories)
|
||||
return f"{_SYSTEM_PROMPT}\n\nUSER MEMORIES (facts the user has asked you to remember):\n{memory_block}"
|
||||
|
||||
|
||||
def _build_messages_payload(
|
||||
conversation, query_text: str, image_description: str | None = None
|
||||
conversation,
|
||||
query_text: str,
|
||||
image_description: str | None = None,
|
||||
system_prompt: str | None = None,
|
||||
) -> list:
|
||||
recent_messages = (
|
||||
conversation.messages[-10:]
|
||||
if len(conversation.messages) > 10
|
||||
else conversation.messages
|
||||
)
|
||||
messages_payload = [{"role": "system", "content": _SYSTEM_PROMPT}]
|
||||
messages_payload = [{"role": "system", "content": system_prompt or _SYSTEM_PROMPT}]
|
||||
for msg in recent_messages[:-1]: # Exclude the message we just added
|
||||
role = "user" if msg.speaker == "user" else "assistant"
|
||||
text = msg.text
|
||||
@@ -80,10 +93,14 @@ async def query():
|
||||
user=user,
|
||||
)
|
||||
|
||||
messages_payload = _build_messages_payload(conversation, query)
|
||||
system_prompt = await _build_system_prompt_with_memories(str(user.id))
|
||||
messages_payload = _build_messages_payload(
|
||||
conversation, query, system_prompt=system_prompt
|
||||
)
|
||||
payload = {"messages": messages_payload}
|
||||
agent_config = {"configurable": {"user_id": str(user.id)}}
|
||||
|
||||
response = await main_agent.ainvoke(payload)
|
||||
response = await main_agent.ainvoke(payload, config=agent_config)
|
||||
message = response.get("messages", [])[-1].content
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
@@ -163,15 +180,19 @@ async def stream_query():
|
||||
logging.error(f"Failed to analyze image: {e}")
|
||||
image_description = "[Image could not be analyzed]"
|
||||
|
||||
system_prompt = await _build_system_prompt_with_memories(str(user.id))
|
||||
messages_payload = _build_messages_payload(
|
||||
conversation, query_text or "", image_description
|
||||
conversation, query_text or "", image_description, system_prompt=system_prompt
|
||||
)
|
||||
payload = {"messages": messages_payload}
|
||||
agent_config = {"configurable": {"user_id": str(user.id)}}
|
||||
|
||||
async def event_generator():
|
||||
final_message = None
|
||||
try:
|
||||
async for event in main_agent.astream_events(payload, version="v2"):
|
||||
async for event in main_agent.astream_events(
|
||||
payload, version="v2", config=agent_config
|
||||
):
|
||||
event_type = event.get("event")
|
||||
|
||||
if event_type == "on_tool_start":
|
||||
|
||||
@@ -5,9 +5,11 @@ from dotenv import load_dotenv
|
||||
from langchain.agents import create_agent
|
||||
from langchain.chat_models import BaseChatModel
|
||||
from langchain.tools import tool
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_openai import ChatOpenAI
|
||||
from tavily import AsyncTavilyClient
|
||||
|
||||
from blueprints.conversation.memory import save_memory
|
||||
from blueprints.rag.logic import query_vector_store
|
||||
from utils.obsidian_service import ObsidianService
|
||||
from utils.ynab_service import YNABService
|
||||
@@ -589,8 +591,35 @@ async def obsidian_create_task(
|
||||
return f"Error creating task: {str(e)}"
|
||||
|
||||
|
||||
@tool
|
||||
async def save_user_memory(content: str, config: RunnableConfig) -> str:
|
||||
"""Save a fact or preference about the user for future conversations.
|
||||
|
||||
Use this tool when the user:
|
||||
- Explicitly asks you to remember something ("remember that...", "keep in mind...")
|
||||
- Shares a personal preference that would be useful in future conversations
|
||||
(e.g., "I prefer metric units", "my cat's name is Luna")
|
||||
- Tells you a meaningful personal fact (e.g., "I'm allergic to peanuts")
|
||||
|
||||
Do NOT save:
|
||||
- Trivial or ephemeral info (e.g., "I'm tired today")
|
||||
- Information already in the system prompt or documents
|
||||
- Conversation-specific context that won't matter later
|
||||
|
||||
Args:
|
||||
content: A concise statement of the fact or preference to remember.
|
||||
Write it as a standalone sentence (e.g., "User prefers dark mode"
|
||||
rather than "likes dark mode").
|
||||
|
||||
Returns:
|
||||
Confirmation that the memory was saved.
|
||||
"""
|
||||
user_id = config["configurable"]["user_id"]
|
||||
return await save_memory(user_id=user_id, content=content)
|
||||
|
||||
|
||||
# Create tools list based on what's available
|
||||
tools = [get_current_date, simba_search, web_search]
|
||||
tools = [get_current_date, simba_search, web_search, save_user_memory]
|
||||
if ynab_enabled:
|
||||
tools.extend(
|
||||
[
|
||||
|
||||
19
blueprints/conversation/memory.py
Normal file
19
blueprints/conversation/memory.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from .models import UserMemory
|
||||
|
||||
|
||||
async def get_memories_for_user(user_id: str) -> list[str]:
|
||||
"""Return all memory content strings for a user, ordered by most recently updated."""
|
||||
memories = await UserMemory.filter(user_id=user_id).order_by("-updated_at")
|
||||
return [m.content for m in memories]
|
||||
|
||||
|
||||
async def save_memory(user_id: str, content: str) -> str:
|
||||
"""Save a new memory or touch an existing one (exact-match dedup)."""
|
||||
existing = await UserMemory.filter(user_id=user_id, content=content).first()
|
||||
if existing:
|
||||
existing.updated_at = None # auto_now=True will refresh it on save
|
||||
await existing.save(update_fields=["updated_at"])
|
||||
return "Memory already exists (refreshed)."
|
||||
|
||||
await UserMemory.create(user_id=user_id, content=content)
|
||||
return "Memory saved."
|
||||
@@ -47,6 +47,17 @@ class ConversationMessage(Model):
|
||||
table = "conversation_messages"
|
||||
|
||||
|
||||
class UserMemory(Model):
|
||||
id = fields.UUIDField(primary_key=True)
|
||||
user = fields.ForeignKeyField("models.User", related_name="memories")
|
||||
content = fields.TextField()
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
updated_at = fields.DatetimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
table = "user_memories"
|
||||
|
||||
|
||||
PydConversationMessage = pydantic_model_creator(ConversationMessage)
|
||||
PydConversation = pydantic_model_creator(
|
||||
Conversation, name="Conversation", allow_cycles=True, exclude=("user",)
|
||||
|
||||
@@ -54,4 +54,7 @@ You have access to Ryan's daily journal notes. Each note lives at journal/YYYY/Y
|
||||
- Use journal_get_tasks to list tasks (done/pending) for today or a specific date
|
||||
- Use journal_add_task to add a new task to today's (or a given date's) note
|
||||
- Use journal_complete_task to check off a task as done
|
||||
Use these tools when Ryan asks about today's tasks, wants to add something to his list, or wants to mark a task complete."""
|
||||
Use these tools when Ryan asks about today's tasks, wants to add something to his list, or wants to mark a task complete.
|
||||
|
||||
USER MEMORY:
|
||||
You can remember facts about the user across conversations using the save_user_memory tool. When a user explicitly asks you to remember something, or shares a meaningful preference or personal fact, save it. Saved memories will automatically appear at the end of this prompt in future conversations under "USER MEMORIES"."""
|
||||
|
||||
Reference in New Issue
Block a user