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>
20 lines
812 B
Python
20 lines
812 B
Python
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."
|