mem0

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Mem0 Platform Integration

Mem0 Platform集成

Mem0 is a managed memory layer for AI applications. It stores, retrieves, and manages user memories via API — no infrastructure to deploy.
Mem0是面向AI应用的托管式内存层,它通过API存储、检索和管理用户记忆——无需部署基础设施。

Step 1: Install and authenticate

步骤1:安装与认证

Python:
bash
pip install mem0ai
export MEM0_API_KEY="m0-your-api-key"
TypeScript/JavaScript:
bash
npm install mem0ai
export MEM0_API_KEY="m0-your-api-key"
Python:
bash
pip install mem0ai
export MEM0_API_KEY="m0-your-api-key"
TypeScript/JavaScript:
bash
npm install mem0ai
export MEM0_API_KEY="m0-your-api-key"

Step 2: Initialize the client

步骤2:初始化客户端

Python:
python
from mem0 import MemoryClient
client = MemoryClient(api_key="m0-xxx")
TypeScript:
typescript
import MemoryClient from 'mem0ai';
const client = new MemoryClient({ apiKey: 'm0-xxx' });
For async Python, use
AsyncMemoryClient
.
Python:
python
from mem0 import MemoryClient
client = MemoryClient(api_key="m0-xxx")
TypeScript:
typescript
import MemoryClient from 'mem0ai';
const client = new MemoryClient({ apiKey: 'm0-xxx' });
对于异步Python,使用
AsyncMemoryClient

Step 3: Core operations

步骤3:核心操作

Every Mem0 integration follows the same pattern: retrieve → generate → store.
所有Mem0集成都遵循相同的模式:检索 → 生成 → 存储

Add memories

添加记忆

python
messages = [
    {"role": "user", "content": "I'm a vegetarian and allergic to nuts."},
    {"role": "assistant", "content": "Got it! I'll remember that."}
]
client.add(messages, user_id="alice")
python
messages = [
    {"role": "user", "content": "I'm a vegetarian and allergic to nuts."},
    {"role": "assistant", "content": "Got it! I'll remember that."}
]
client.add(messages, user_id="alice")

Search memories

搜索记忆

python
results = client.search("dietary preferences", user_id="alice")
for mem in results.get("results", []):
    print(mem["memory"])
python
results = client.search("dietary preferences", user_id="alice")
for mem in results.get("results", []):
    print(mem["memory"])

Get all memories

获取所有记忆

python
all_memories = client.get_all(user_id="alice")
python
all_memories = client.get_all(user_id="alice")

Update a memory

更新记忆

python
client.update("memory-uuid", text="Updated: vegetarian, nut allergy, prefers organic")
python
client.update("memory-uuid", text="Updated: vegetarian, nut allergy, prefers organic")

Delete a memory

删除记忆

python
client.delete("memory-uuid")
client.delete_all(user_id="alice")  # delete all for a user
python
client.delete("memory-uuid")
client.delete_all(user_id="alice")  # delete all for a user

Common integration pattern

常见集成模式

python
from mem0 import MemoryClient
from openai import OpenAI

mem0 = MemoryClient()
openai = OpenAI()

def chat(user_input: str, user_id: str) -> str:
    # 1. Retrieve relevant memories
    memories = mem0.search(user_input, user_id=user_id)
    context = "\n".join([m["memory"] for m in memories.get("results", [])])

    # 2. Generate response with memory context
    response = openai.chat.completions.create(
        model="gpt-4.1-nano-2025-04-14",
        messages=[
            {"role": "system", "content": f"User context:\n{context}"},
            {"role": "user", "content": user_input},
        ]
    )
    reply = response.choices[0].message.content

    # 3. Store interaction for future context
    mem0.add(
        [{"role": "user", "content": user_input}, {"role": "assistant", "content": reply}],
        user_id=user_id
    )
    return reply
python
from mem0 import MemoryClient
from openai import OpenAI

mem0 = MemoryClient()
openai = OpenAI()

def chat(user_input: str, user_id: str) -> str:
    # 1. Retrieve relevant memories
    memories = mem0.search(user_input, user_id=user_id)
    context = "\n".join([m["memory"] for m in memories.get("results", [])])

    # 2. Generate response with memory context
    response = openai.chat.completions.create(
        model="gpt-4.1-nano-2025-04-14",
        messages=[
            {"role": "system", "content": f"User context:\n{context}"},
            {"role": "user", "content": user_input},
        ]
    )
    reply = response.choices[0].message.content

    # 3. Store interaction for future context
    mem0.add(
        [{"role": "user", "content": user_input}, {"role": "assistant", "content": reply}],
        user_id=user_id
    )
    return reply

Common edge cases

常见边缘情况

  • Search returns empty: Memories process asynchronously. Wait 2-3s after
    add()
    before searching. Also verify
    user_id
    matches exactly (case-sensitive).
  • AND filter with user_id + agent_id returns empty: Entities are stored separately. Use
    OR
    instead, or query separately.
  • Duplicate memories: Don't mix
    infer=True
    (default) and
    infer=False
    for the same data. Stick to one mode.
  • Wrong import: Always use
    from mem0 import MemoryClient
    (or
    AsyncMemoryClient
    for async). Do not use
    from mem0 import Memory
    .
  • Immutable memories: Cannot be updated or deleted once created. Use
    client.history(memory_id)
    to track changes over time.
  • 搜索返回空结果: 记忆处理是异步的。在调用
    add()
    后等待2-3秒再进行搜索。同时请确认
    user_id
    完全匹配(区分大小写)。
  • 使用user_id + agent_id的AND过滤器返回空结果: 实体是分开存储的。改用OR过滤器,或分别查询。
  • 重复记忆: 不要对同一数据混合使用
    infer=True
    (默认值)和
    infer=False
    。请坚持使用一种模式。
  • 错误的导入方式: 请始终使用
    from mem0 import MemoryClient
    (异步场景使用
    AsyncMemoryClient
    )。不要使用
    from mem0 import Memory
  • 不可变记忆: 记忆创建后无法更新或删除。使用
    client.history(memory_id)
    跟踪随时间的变更。

Live documentation search

在线文档搜索

For the latest docs beyond what's in the references, use the doc search tool:
bash
python scripts/mem0_doc_search.py --query "topic"
python scripts/mem0_doc_search.py --page "/platform/features/graph-memory"
python scripts/mem0_doc_search.py --index
No API key needed — searches docs.mem0.ai directly.
如需获取参考内容之外的最新文档,可使用文档搜索工具:
bash
python scripts/mem0_doc_search.py --query "topic"
python scripts/mem0_doc_search.py --page "/platform/features/graph-memory"
python scripts/mem0_doc_search.py --index
无需API密钥——直接搜索docs.mem0.ai。

References

参考资料

Load these on demand for deeper detail:
TopicFile
Quickstart (Python, TS, cURL)references/quickstart.md
SDK guide (all methods, both languages)references/sdk-guide.md
API reference (endpoints, filters, object schema)references/api-reference.md
Architecture (pipeline, lifecycle, scoping, performance)references/architecture.md
Platform features (retrieval, graph, categories, MCP, etc.)references/features.md
Framework integrations (LangChain, CrewAI, Vercel AI, etc.)references/integration-patterns.md
Use cases & examples (real-world patterns with code)references/use-cases.md
按需加载以下内容以获取更详细的信息:
主题文件
快速入门(Python、TS、cURL)references/quickstart.md
SDK指南(所有方法、两种语言)references/sdk-guide.md
API参考(端点、过滤器、对象架构)references/api-reference.md
架构(流水线、生命周期、作用域、性能)references/architecture.md
平台功能(检索、图谱、分类、MCP等)references/features.md
框架集成(LangChain、CrewAI、Vercel AI等)references/integration-patterns.md
用例与示例(带代码的真实场景模式)references/use-cases.md