Skip to main content

Documentation Index

Fetch the complete documentation index at: https://agno-v2-shaloo-ai-support-link.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

"""
Standalone SurrealDB Memory Operations
"""

from agno.db.surrealdb import SurrealDb
from agno.memory import MemoryManager, UserMemory
from rich.pretty import pprint

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
SURREALDB_URL = "ws://localhost:8000"
SURREALDB_USER = "root"
SURREALDB_PASSWORD = "root"
SURREALDB_NAMESPACE = "agno"
SURREALDB_DATABASE = "memories"

creds = {"username": SURREALDB_USER, "password": SURREALDB_PASSWORD}
db = SurrealDb(None, SURREALDB_URL, creds, SURREALDB_NAMESPACE, SURREALDB_DATABASE)


# ---------------------------------------------------------------------------
# Create Memory Manager
# ---------------------------------------------------------------------------
memory = MemoryManager(db=db)


# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
def run_example() -> None:
    # Add a memory for the default user
    memory.add_user_memory(
        memory=UserMemory(memory="The user's name is John Doe", topics=["name"]),
    )
    print("Memories:")
    pprint(memory.get_user_memories())

    # Add memories for Jane Doe
    jane_doe_id = "jane_doe@example.com"
    print(f"\nUser: {jane_doe_id}")
    memory_id_1 = memory.add_user_memory(
        memory=UserMemory(memory="The user's name is Jane Doe", topics=["name"]),
        user_id=jane_doe_id,
    )
    memory_id_2 = memory.add_user_memory(
        memory=UserMemory(memory="She likes to play tennis", topics=["hobbies"]),
        user_id=jane_doe_id,
    )
    memories = memory.get_user_memories(user_id=jane_doe_id)
    print("Memories:")
    pprint(memories)

    # Delete a memory
    print("\nDeleting memory")
    assert memory_id_2 is not None
    memory.delete_user_memory(user_id=jane_doe_id, memory_id=memory_id_2)
    print("Memory deleted\n")
    memories = memory.get_user_memories(user_id=jane_doe_id)
    print("Memories:")
    pprint(memories)

    # Replace a memory
    print("\nReplacing memory")
    assert memory_id_1 is not None
    memory.replace_user_memory(
        memory_id=memory_id_1,
        memory=UserMemory(memory="The user's name is Jane Mary Doe", topics=["name"]),
        user_id=jane_doe_id,
    )
    print("Memory replaced")
    memories = memory.get_user_memories(user_id=jane_doe_id)
    print("Memories:")
    pprint(memories)


if __name__ == "__main__":
    run_example()

Run the Example

# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/92_integrations/surrealdb

# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate

python standalone_memory_surreal.py