Skip to main content

LlamaIndex Memory Integration: Mem0

Installation

To install the required package, run:

%pip install llama-index-memory-mem0

Setup with Mem0 Platform

  1. Set your Mem0 Platform API key as an environment variable. You can replace <your-mem0-api-key> with your actual API key:

Note: You can obtain your Mem0 Platform API key from the Mem0 Platform.

os.environ["MEM0_API_KEY"] = "<your-mem0-api-key>"
  1. Import the necessary modules and create a Mem0Memory instance:
from llama_index.memory.mem0 import Mem0Memory

context = {"user_id": "user_1"}
memory = Mem0Memory.from_client(
    context=context,
    api_key="<your-mem0-api-key>",
    search_msg_limit=4,  # optional, default is 5
)

Mem0 Context is used to identify the user, agent or the conversation in the Mem0. It is required to be passed in the at least one of the fields in the Mem0Memory constructor. It can be any of the following:

context = {
    "user_id": "user_1",
    "agent_id": "agent_1",
    "run_id": "run_1",
}

search_msg_limit is optional, default is 5. It is the number of messages from the chat history to be used for memory retrieval from Mem0. More number of messages will result in more context being used for retrieval but will also increase the retrieval time and might result in some unwanted results.

Setup with Mem0 OSS

  1. Set your Mem0 OSS by providing configuration details:

Note: To know more about Mem0 OSS, read Mem0 OSS Quickstart.

config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "collection_name": "test_9",
            "host": "localhost",
            "port": 6333,
            "embedding_model_dims": 1536,  # Change this according to your local model's dimensions
        },
    },
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-4o",
            "temperature": 0.2,
            "max_tokens": 1500,
        },
    },
    "embedder": {
        "provider": "openai",
        "config": {"model": "text-embedding-3-small"},
    },
    "version": "v1.1",
}
  1. Create a Mem0Memory instance:
memory = Mem0Memory.from_config(
    context=context,
    config=config,
    search_msg_limit=4,  # optional, default is 5
)

Basic Usage

Currently, Mem0 Memory is supported in agents and chat engines.

Initialize the LLM

import os
from llama_index.llms.openai import OpenAI

os.environ["OPENAI_API_KEY"] = "<your-openai-api-key>"
llm = OpenAI(model="gpt-4o")

SimpleChatEngine

from llama_index.core import SimpleChatEngine

chat_engine = SimpleChatEngine.from_defaults(
    llm=llm, memory=memory  # set you memory here
)

# Start the chat
response = chat_engine.chat("Hi, My name is Mayank")
print(response)

Initialize the tools

from llama_index.core.tools import FunctionTool


def call_fn(name: str):
    """Call the provided name.
    Args:
        name: str (Name of the person)
    """
    print(f"Calling... {name}")


def email_fn(name: str):
    """Email the provided name.
    Args:
        name: str (Name of the person)
    """
    print(f"Emailing... {name}")


call_tool = FunctionTool.from_defaults(fn=call_fn)
email_tool = FunctionTool.from_defaults(fn=email_fn)

FunctionAgent

from llama_index.core.agent.workflow import FunctionAgent

agent = FunctionAgent(
    tools=[call_tool, email_tool],
    llm=llm,
)

# Start the chat
response = await agent.run("Hi, My name is Mayank", memory=memory)
print(response)

ReActAgent

from llama_index.core.agent.workflow import ReActAgent

agent = ReActAgent(
    tools=[call_tool, email_tool],
    llm=llm,
)

# Start the chat
response = await agent.run("Hi, My name is Mayank", memory=memory)
print(response)

Note: For more examples refer to: Notebooks

References

Metadata

Release files for llama-index-memory-mem0 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llama-index-memory-mem0 2.1.0
File Size Uploaded Provenance Actions
llama_index_memory_mem0-2.1.0.tar.gz 7.1 kB Without provenance Details for llama_index_memory_mem0-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-memory-mem0 2.1.0
File Interpreter ABI Platform Actions
llama_index_memory_mem0-2.1.0-py3-none-any.whl Python 3 none any Details for llama_index_memory_mem0-2.1.0-py3-none-any.whl

Total release size: 14.0 kB

Release files / llama_index_memory_mem0-2.1.0.tar.gz

Download URL llama_index_memory_mem0-2.1.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
dca389e938575e259dae24f59d40f6a439505bfdad3b7e5cef970710528c9999
BLAKE2b-256 checksum
How to use checksums
89f5a9f3be30010b0612f2838381bb116462a21bbc77a4918331554c49ad0f46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / llama_index_memory_mem0-2.1.0-py3-none-any.whl

Download URL llama_index_memory_mem0-2.1.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b57873c153b3c15049a6664813954add1ee3fdc36c489dd6f7615e5d1954363
BLAKE2b-256 checksum
How to use checksums
a1c2afebd92d72db00c3b4b5347f7401546bdb8ff75d736e733476ee98c430fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for llama-index-memory-mem0 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page