Improving User Experiences with Memory Export

Introduction

Memories are the backbone of personalized user experiences. They enable systems to understand user behavior, and interactions, creating tailored experiences that users love. We at Mem0 recently shipped Memory export. A feature to export memories in pre-defined format. By exporting memories, developers can analyze user data, train machine learning models, generate business insights, and migrate data seamlessly.

In this blog, we’ll explore the importance of memory export, walk through a practical implementation, and highlight best practices.

Why Memory Export?

Memory export refers to the process of extracting and structuring user interaction data for further analysis or integration. It is a critical tool for personalization, enabling developers to leverage user data effectively.

Use Cases:

  • Data Analysis: Understand user behavior and preferences.
  • Machine Learning: Training models using labelled data for supervised training.
  • Business Insights: Generate detailed reports for decision-making.
  • Data Migration: Scale systems by transferring data to new platforms.

Implementing Memory Export: A Step-by-Step Guide

Let’s dive into a practical example using the mem0 library.

Step 1: Install and Import the Library

%pip install mem0ai 

from mem0 import MemoryClient

Step 2: Create Sample Memories

We’ll start by creating a list of user-assistant conversation:

messages = [
    {
        "content": "Hi there! I'm AI assistant. How can I help you today?",
        "timestamp": "2023-10-01T09:55:00Z",
        "role": "assistant"
    },
    {
        "content": "Hi! I'm Tom from San Francisco. I'm really into photography, especially capturing candid family moments. It's one of my biggest passions.",
        "timestamp": "2023-10-01T10:00:00Z",
        "role": "user"
    },
    {
        "content": "That's wonderful Tom! What's the best way to reach you if I find some interesting photography events in the area?",
        "timestamp": "2023-10-02T08:30:00Z",
        "role": "assistant"
    },
    {
        "content": "You can reach me at tom.photography@email.com. I'm also really into cooking - I love making comfort food, especially my signature mushroom carbonara. Always trying to sneak in healthy vegetables for the kids!",
        "timestamp": "2023-10-03T15:45:00Z",
        "role": "user"
    },
    {
        "content": "Great! Tell me more about your other interests and favorite places to visit!",
        "timestamp": "2023-10-07T18:15:00Z",
        "role": "assistant"
    },
    {
        "content": "I'm a huge 80s rock fan - love Queen and Journey! For travel, I prefer small, cozy mountain towns over beaches. We have this family tradition of visiting a little place in Colorado every winter. I also love hiking trails and scenic drives whenever I can find them.",
        "timestamp": "2023-10-08T11:30:00Z",
        "role": "user"
    }
]

Step 3: Initialize the Memory Client

Pass the org_id and project_id along with api_key when initializing the client.

client = MemoryClient(
    api_key="<mem0_api_key>",
    org_id="<mem0_org_id>",
    project_id="<mem0_project_id>"
)
client.add(messages, user_id="Tom")

Note: To get your project id and organization id, click on the i button on the navigation bar and copy the credentials.

Step 4: Define the JSON Schema

A well-defined schema ensures data integrity and clarity:

json_schema={
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "User's full name"
    },
    "travel_preferences": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "List of user's travel preferences and interests"
    },
    "location": {
      "type": "string",
      "description": "User's current city or location"
    },
    "email": {
      "type": "string",
      "format": "email",
      "description": "User's email address"
    },
    "interests": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "List of user's general interests and hobbies"
    }
  },
  "required": ["name", "travel_preferences", "location", "email"],
  "additionalProperties": False
}

Step 5: Create and Retrieve Memory Export

Memory export entails two calls: POST and GET call. POST call with the desired schema for memories and the GET call to receive the memories once they are processed in the desired schema and ready.

response = client.create_memory_export(
    schema=json_schema,
    user_id="Tom",
    categories="user_preferences"
)
print(response)

Output:

{
    "message": "Memory export request received. The export will be ready in a few seconds.",
    "id": "b182dd27-62d8-4800-8f12-25b0dd42db69"
}

Retrieve the exported memory:

response = client.get_memory_export(user_id="Tom")
print(response)

Exported memory

{
  "name": "Tom",
  "email": "tom.photography@email.com",
  "location": "San Francisco",
  "interests": [
    "Loves hiking trails and scenic drives",
    "Huge 80s rock fan, loves Queen and Journey",
    "Enjoys cooking, loves making comfort food, especially mushroom carbonara",
    "Passionate about photography, especially capturing candid family moments",
    "Tries to sneak in healthy vegetables for the kids"
  ],
  "travel_preferences": [
    "Has a family tradition of visiting a little place in Colorado every winter",
    "Prefers small, cozy mountain towns over beaches for travel"
  ]
}

Best Practices for Memory Export

  1. Use Explicit JSON Schemas

Avoid vague schemas like:

json_schema = {"fields": ["content", "role"]}

Instead, define clear schemas:

json_schema = {
    "type": "object",
    "properties": {
        "content": {"type": "string"},
        "role": {"type": "string"}
    },
    "required": ["content", "role"]
}

This ensures proper data validation and clarity.

  1. Include Organization and Project IDsAlways pass org_id and project_id when initializing the MemoryClient . This ensures data is correctly organized and traceable.
client = MemoryClient(
    api_key="<mem0_api_key>",
    org_id="<mem0_org_id>",
    project_id="<mem0_project_id>"
)

Conclusion

Memory export unlocks powerful opportunities for personalization, data analysis, and scaling applications. It provides a simple yet effective way to transform raw data into actionable insights while maintaining granular control over your data.

Start using memory export today to transform your workflows and improve your customer’s experience.

View Mem0's documentation to learn more about integrating our memory features. You can also reach out to us at founders@mem0.ai.