This example shows how to use the Memory class to create a persistent memory.
Every time you run this, the Memory
object will be re-initialized from the DB.
cookbook/agent_concepts/memory/02_persistent_memory.py
from typing import List
from agno.memory.v2.db.schema import MemoryRow
from agno.memory.v2.db.sqlite import SqliteMemoryDb
from agno.memory.v2.memory import Memory
from agno.memory.v2.schema import UserMemory
memory_db = SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db")
memory = Memory(db=memory_db)
john_doe_id = "john_doe@example.com"
# Run 1
memory.add_user_memory(
memory=UserMemory(memory="The user's name is John Doe", topics=["name"]),
user_id=john_doe_id,
)
# Run this the 2nd time
# memory.add_user_memory(
# memory=UserMemory(memory="The user works at a softward company called Agno", topics=["name"]),
# user_id=john_doe_id,
# )
memories: List[MemoryRow] = memory_db.read_memories()
print("All the DB memories:")
for i, m in enumerate(memories):
print(f"{i}: {m.memory['memory']} ({m.last_updated})")
Create a virtual environment
Open the Terminal
and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
Run Example
python cookbook/agent_concepts/memory/02_persistent_memory.py