Mem0 and PraisonAI Integration

Guide for integrating Mem0 (formerly EmbedChain) memory management system with PraisonAI, including tools for storing, retrieving, and managing memories

python
from praisonaiagents import Agent

agent = Agent(
    name="Memory Assistant",
    instructions="Remember user preferences across turns.",
    memory=True,
)
agent.start("Remember that I prefer concise bullet answers")

The user shares a preference; the agent stores it with Mem0-backed memory for later sessions.

Diagram

Input

Agent

Tool

Output

Diagram
Mem0 and PraisonAI IntegrationAgentUserMem0 and PraisonAI IntegrationAgentUserRequestProcess requestResultResponse

Mem0 and PaisonAI Integration

bash
pip install "praisonaiagents[memory]"

Mem0 is a tools to store, updated, delete and retrieve memories. https://github.com/mem0ai/mem0

Quick Start

  1. Install
    bash
    pip install praisonaiagents[memory]
  2. Create a memory-enabled agent
    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="MemoryAgent",
        instructions="Remember user preferences across conversations.",
        memory=True,
    )
    
    agent.start("My favorite color is blue")
    agent.start("What is my favorite color?")

Mem0 previously called EmbedChain

python
from mem0 import Memory
from praisonai_tools import BaseTool

class AddMemoryTool(BaseTool):
    name: str = "Add Memory Tool"
    description: str = ("This tool allows storing a new memory with user ID and optional metadata.\n"
                        "Example:\n"
                        "   - Input: text='I am working on improving my tennis skills. Suggest some online courses.', user_id='alice', metadata={'category': 'hobbies'}\n"
                        "   - Output: Memory added with summary 'Improving her tennis skills. Looking for online suggestions.'")

    def _run(self, text: str, user_id: str, metadata: dict = None):
        m = Memory()
        result = m.add(text, user_id=user_id, metadata=metadata)
        return result

class GetAllMemoriesTool(BaseTool):
    name: str = "Get All Memories Tool"
    description: str = ("This tool retrieves all stored memories.\n"
                        "Example:\n"
                        "   - Input: action='get_all'\n"
                        "   - Output: List of all stored memories.")

    def _run(self):
        m = Memory()
        result = m.get_all()
        return result

class SearchMemoryTool(BaseTool):
    name: str = "Search Memory Tool"
    description: str = ("This tool searches for specific memories based on a query and user ID.\n"
                        "Example:\n"
                        "   - Input: query='What are Alice's hobbies?', user_id='alice'\n"
                        "   - Output: Search results related to Alice's hobbies.")

    def _run(self, query: str, user_id: str):
        m = Memory()
        result = m.search(query=query, user_id=user_id)
        return result

class UpdateMemoryTool(BaseTool):
    name: str = "Update Memory Tool"
    description: str = ("This tool updates an existing memory by memory ID and new data.\n"
                        "Example:\n"
                        "   - Input: memory_id='cb032b42-0703-4b9c-954d-77c36abdd660', data='Likes to play tennis on weekends'\n"
                        "   - Output: Memory updated to 'Likes to play tennis on weekends.'")

    def _run(self, memory_id: str, data: str):
        m = Memory()
        result = m.update(memory_id=memory_id, data=data)
        return result

class MemoryHistoryTool(BaseTool):
    name: str = "Memory History Tool"
    description: str = ("This tool gets the history of changes made to a specific memory by memory ID.\n"
                        "Example:\n"
                        "   - Input: memory_id='cb032b42-0703-4b9c-954d-77c36abdd660'\n"
                        "   - Output: History of the specified memory.")

    def _run(self, memory_id: str):
        m = Memory()
        result = m.history(memory_id=memory_id)
        return result

Best Practices

Use a consistent user_id

Always pass the same user_id for the same user across sessions to retrieve their memories correctly.

Add memories after task completion

Store memories at the end of a session to avoid storing intermediate or incorrect information.

Search before adding

Search existing memories before adding new ones to avoid duplicates.

Delete stale memories

Clean up outdated memories periodically to keep retrieval accurate over time.

Related