Add Mem0’s Powerful Memory in just Four Lines of Code

Add Mem0’s Powerful Memory in just Four Lines of Code

For Large Language Model (LLM) powered agents and applications, efficient memory management can make or break your application. Implementing a memory management systems from scratch is complex and doesn’t directly make the core product valuable.

But what if you could add and search memory with just a few lines of code? Enter Mem0, the lightweight solution changing how we approach AI memory.

The Power of Simplicity

Mem0 stands out because of its simplicity and ease of use. Unlike traditional memory management systems that require extensive setup and maintenance, Mem0 offers a streamlined approach that can be implemented in minutes. To get started, you need to obtain an API key from the Mem0 dashboard at https://app.mem0.ai/dashboard/api-keys. If you don't have an account yet, you'll need to register first.

Initialization

To begin using Mem0 in your project, you'll need to import the MemoryClient and initialize it with your API key. This process is straightforward and requires minimal code:

from mem0 import MemoryClient

client = MemoryClient(api_key="your-api-key")

Adding Memory

One of Mem0's core strengths is its simplicity in adding new information to your AI's memory. With just a single line of code, you can store important data in 2 ways:

  1. Adding a single piece of information:
client.add("Tokyo is the most populous metropolitan area in the world.", user_id="sarah", output_format="v1.0")
  1. Adding a conversation history:
messages = [
    {"role": "user", "content": "Hi, I'm Michael. I work as a software engineer and I prefer to code in Python and TypeScript."},
    {"role": "assistant", "content": "Nice to meet you Michael! I understand you're a software engineer who works primarily with Python and TypeScript. I'll keep your programming language preferences in mind for any coding discussions."}
]

client.add(messages, user_id="michael", output_format="v1.0")

Searching Memory

When it comes to retrieving information, Mem0 keeps it just as simple:

# searching memory
client.search("What programming languages does Michael use?", user_id="michael")

# example output of the memory search
{
    "results": [
        {
            "id": "3d982a1c-6f45-4b2d-ae19-f87654c31b9d",
            "memory": "Software engineer. Programs in Python and TypeScript.",
            "input": [
                {
                    "role": "user",
                    "content": "Hi, I'm Michael. I work as a software engineer and I prefer to code in Python and TypeScript."
                },
                {
                    "role": "assistant",
                    "content": "Nice to meet you Michael! I understand you're a software engineer who works primarily with Python and TypeScript. I'll keep your programming language preferences in mind for any coding discussions."
                }
            ],
            "user_id": "michael",
            "hash": "8bc4f2367a91e2d5c899fd3582eb14cd",
            "metadata": null,
            "created_at": "2024-07-20T01:45:22.183567-07:00",
            "updated_at": "2024-07-20T01:45:22.183589-07:00"
        }
    ]
}

This single line of code initiates a search query against the stored memories. The search function is designed to understand natural language queries and return relevant results based on the question asked. The user_id parameter ensures that the search is limited to the specific user's memory, while the output_format parameter allows you to specify how you want the results structured.

The simplicity of Mem0 isn't just about writing less code—it's about making your development process smoother, faster, and intuitive. By reducing the complexity of memory management, Mem0 allows you to focus on what really matters: building innovative AI applications.

It's important to note that the four lines of code we've discussed here represent just the most basic functionality of Mem0. The platform also supports a range of advanced features, including memory customization and custom categories. To explore these advanced capabilities and learn how to integrate them into your projects, we encourage you to refer to our documentation. You can also reach out to us at founders@mem0.ai.