之前介绍了一下概览,今天来看下快速开始

很简单,基本上就是CRUD


安装

pip install mem0ai
  • 1.

基本使用

初始化

基础
from mem0 import Memory
m = Memory()
  • 1.
  • 2.
高级

如果是在生产环境使用,如下

运行 qdrant 服务

docker pull qdrant/qdrant

docker run -p 6333:6333 -p 6334:6334 \
    -v $(pwd)/qdrant_storage:/qdrant/storage:z \
    qdrant/qdrant
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

初始化

from mem0 import Memory

config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "host": "localhost",
            "port": 6333,
        }
    },
}

m = Memory.from_config(config)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

添加

# For a user
result = m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
print(result)
  • 1.
  • 2.
  • 3.

输出

[
  {
    'id': 'm1',
    'event': 'add',
    'data': 'Likes to play cricket on weekends'
  }
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

获取

# Get all memories
all_memories = m.get_all()
print(all_memories)
  • 1.
  • 2.
  • 3.

输出

[
  {
    'id': 'm1',
    'text': 'Likes to play cricket on weekends',
    'metadata': {
      'data': 'Likes to play cricket on weekends',
      'category': 'hobbies'
    }
  },
  # ... other memories ...
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
# Get a single memory by ID
specific_memory = m.get("m1")
print(specific_memory)
  • 1.
  • 2.
  • 3.

输出

{
  'id': 'm1',
  'text': 'Likes to play cricket on weekends',
  'metadata': {
    'data': 'Likes to play cricket on weekends',
    'category': 'hobbies'
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

搜索

related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
print(related_memories)
  • 1.
  • 2.

输出

[
  {
    'id': 'm1',
    'text': 'Likes to play cricket on weekends',
    'metadata': {
      'data': 'Likes to play cricket on weekends',
      'category': 'hobbies'
    },
    'score': 0.85  # Similarity score
  },
  # ... other related memories ...
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

更新

result = m.update(memory_id="m1", data="Likes to play tennis on weekends")
print(result)
  • 1.
  • 2.

输出

{
  'id': 'm1',
  'event': 'update',
  'data': 'Likes to play tennis on weekends'
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

历史

history = m.history(memory_id="m1")
print(history)
  • 1.
  • 2.

输出

[
  {
    'id': 'h1',
    'memory_id': 'm1',
    'prev_value': None,
    'new_value': 'Likes to play cricket on weekends',
    'event': 'add',
    'timestamp': '2024-07-14 10:00:54.466687',
    'is_deleted': 0
  },
  {
    'id': 'h2',
    'memory_id': 'm1',
    'prev_value': 'Likes to play cricket on weekends',
    'new_value': 'Likes to play tennis on weekends',
    'event': 'update',
    'timestamp': '2024-07-14 10:15:17.230943',
    'is_deleted': 0
  }
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

删除

m.delete(memory_id="m1") # Delete a memory

m.delete_all(user_id="alice") # Delete all memories
  • 1.
  • 2.
  • 3.

重置

m.reset() # Reset all memories
  • 1.