Files
simbarag/blueprints/conversation/memory.py
T
ryan af33463f98 Put today's date (ET) in system prompt; stop hardcoding Simba's age
The model had no idea of the current date unless it remembered to call
the get_current_date tool, so relative/yearless dates (e.g. 'October')
got filled in from training data. build_system_prompt_with_memories now
prepends today's date (America/New_York) to every prompt, covering all
channels (web chat, iMessage, WhatsApp) and all date-related questions.

Simba's age is now computed from his birth date instead of a stale
hardcoded 'approximately 9 years 5 months'.
2026-10-12 18:25:32 -04:00

42 lines
1.6 KiB
Python

from datetime import datetime
from zoneinfo import ZoneInfo
from .models import UserMemory
from .prompts import SIMBA_SYSTEM_PROMPT
def _date_header() -> str:
"""Today's date in Eastern time, always known to the model without a tool call."""
now = datetime.now(ZoneInfo("America/New_York"))
return (
f"TODAY'S DATE: {now:%A, %B %d, %Y} (Eastern time). "
f"Interpret any relative or yearless dates (e.g. 'October', 'last month') relative to today's date.\n\n"
)
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 build_system_prompt_with_memories(user_id: str) -> str:
memories = await get_memories_for_user(user_id)
prompt = f"{_date_header()}{SIMBA_SYSTEM_PROMPT}"
if not memories:
return prompt
block = "\n".join(f"- {m}" for m in memories)
return f"{prompt}\n\nUSER MEMORIES (facts the user has asked you to remember):\n{block}"
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."