agentic_huge_data_base / wiki
页面 Mem0 · 4.2 图存储提供方·DeepWiki 中文全文译文

4.2 · 图存储提供方

长期记忆与上下文管理 · 聚焦本章的模块关系、源码依据与实现要点。

项目Mem0 章节4.2 状态全文译文 模块检索、召回与索引、测试、发布与运维、配置治理、入库与解析
源码线索
  • docs/components/vectordbs/dbs/neptune_analytics.mdx
  • examples/graph-db-demo/neptune-example.ipynb
  • mem0/configs/base.py
  • mem0/configs/enums.py
  • mem0/configs/vector_stores/azure_ai_search.py
  • mem0/configs/vector_stores/neptune.py
  • mem0/configs/vector_stores/supabase.py
  • mem0/configs/vector_stores/vertex_ai_vector_search.py
  • mem0/configs/vector_stores/weaviate.py
  • mem0/proxy/main.py
模块标签
  • 检索、召回与索引
  • 测试、发布与运维
  • 配置治理
  • 入库与解析
  • 图谱与关系

章节正文

图存储提供方

图存储提供商

相关源文件

以下文件为本维基页面的生成提供了上下文:

  • docs/components/vectordbs/dbs/neptune_analytics.mdx
  • examples/graph-db-demo/neptune-example.ipynb
  • mem0/configs/base.py
  • mem0/configs/enums.py
  • mem0/configs/vector_stores/azure_ai_search.py
  • mem0/configs/vector_stores/neptune.py
  • mem0/configs/vector_stores/supabase.py
  • mem0/configs/vector_stores/vertex_ai_vector_search.py
  • mem0/configs/vector_stores/weaviate.py
  • mem0/proxy/main.py
  • mem0/vector_stores/neptune_analytics.py
  • mem0/vector_stores/supabase.py
  • mem0/vector_stores/vertex_ai_vector_search.py
  • mem0/vector_stores/weaviate.py
  • tests/vector_stores/test_neptune_analytics.py
  • tests/vector_stores/test_vertex_ai_vector_search.py

图存储提供商使 Mem0 能够捕获和查询从对话中提取的实体-关系结构。本文档记录了支持的图数据库提供商、其配置以及用于实例化它们的工厂模式。

有关图记忆操作和搜索的信息,请参阅图搜索与检索。有关相似度阈值配置,请参阅相似度阈值。有关通用图记忆架构,请参阅图记忆概述

支持的提供商

Mem0 通过基于工厂的架构支持多个图数据库提供商。这些提供商使系统能够以实体和关系网络的形式存储和检索知识。

提供商后端模块路径主要用例
default (Neo4j)Neo4jmem0.memory.graph_memory.MemoryGraph具备企业级功能的生产部署
memgraphMemgraphmem0.memory.memgraph_memory.MemoryGraph高性能内存图操作
neptuneAWS Neptune Graphmem0.graphs.neptune.neptunegraph.MemoryGraphAWS 原生图数据库(无服务器)
neptunedbAWS Neptune DBmem0.graphs.neptune.neptunedb.MemoryGraph具备持久化存储的 AWS Neptune
kuzuKuzumem0.memory.kuzu_memory.MemoryGraph适用于本地部署的嵌入式图数据库

来源: mem0/utils/factory.py:214-220

工厂架构

GraphStoreFactory 实现

GraphStoreFactory 类负责管理图存储提供商的动态实例化。它将提供商名称映射到各自的类路径和配置要求。

下图展示了配置、工厂以及底层代码实体之间的关系。

图存储代码实体映射

Mem0 · GraphStoreFactory 实现 · 图 1
Mem0 · GraphStoreFactory 实现 · 图 1

来源: mem0/utils/factory.py:208-230mem0/configs/base.py:29-58mem0/memory/main.py:199-206

提供商配置

Neo4j(默认)

Neo4j 是默认的图存储提供商。它使用 langchain_neo4j.Neo4jGraph 客户端进行数据库交互,并使用 rank_bm25 对搜索结果进行重排序。

配置参数:

config = {
    "graph_store": {
        "provider": "default",
        "config": {
            "url": "bolt://localhost:7687",
            "username": "neo4j",
            "password": "your-password",
            "database": "neo4j"
        },
        "threshold": 0.7
    }
}

索引管理: Neo4j 实现会创建特定的索引以提升性能:

  • entity_single:针对 user_id 的单一属性索引。
  • entity_composite:针对 (name, user_id) 的复合索引(需要 Neo4j 企业版)。

来源: mem0/utils/factory.py:214

AWS Neptune

Mem0 提供了与 AWS Neptune 的深度集成,同时支持 Analytics(无服务器)和 Database(持久化)两种引擎。

Neptune Analytics(无服务器): 通过 neptune 提供商使用。通常与 AWS Bedrock 配合使用以进行嵌入向量和大语言模型操作。对于 Analytics,endpoint 必须遵循 neptune-graph:// 协议。

config = {
    "embedder": {
        "provider": "aws_bedrock",
        "config": {
            "model": "amazon.titan-embed-text-v2:0",
            "embedding_dims": 1024
        }
    },
    "graph_store": {
        "provider": "neptune",
        "config": {
            "endpoint": "neptune-graph://my-graph-identifier",
            "region": "us-east-1"
        }
    }
}

来源: examples/graph-db-demo/neptune-example.ipynb:65-71mem0/configs/vector_stores/neptune.py:11-23mem0/utils/factory.py:216-217

Memgraph 和 Kuzu
  • Memgraph:一种内存图数据库。配置需要标准连接参数,如 urlusernamepassword
  • Kuzu:一种嵌入式图数据库。配置主要需要本地存储的 path,适用于本地优先的应用场景。

来源: mem0/utils/factory.py:215-218

启用图记忆

图记忆在 Memory 类中根据是否存在图存储配置来激活。如果配置中提供了 graph_storeMemory 类会初始化该组件并设置内部标志 enable_graph

记忆组件初始化流程

Mem0 · 启用图记忆 · 图 2
Mem0 · 启用图记忆 · 图 2

来源: mem0/memory/main.py:199-206

安装

图存储依赖项是可选的,以保持核心包的轻量。可以通过 graph 额外选项进行安装。对于像 Neptune 这样的 AWS 特定图存储,也建议使用 extras 标签。

# 安装所有图依赖
pip install "mem0ai[graph]"

# 安装图和 AWS 依赖(用于 Neptune/Bedrock)
pip install "mem0ai[graph,extras]"

来源: examples/graph-db-demo/neptune-example.ipynb:27-30

实体提取与搜索

提取管线

当调用 m.add() 且启用了图记忆时,系统会按照特定顺序更新图。

  1. 提取:大语言模型从提供的消息中识别实体及其类型。
  2. 关系构建:大语言模型建立三元组(源 -> 关系 -> 目标)。
  3. 去重:系统使用向量相似度(由 threshold 控制)将新实体与图中已有实体进行匹配。
  4. 更新:在添加新关系之前,识别并删除过时的关系。
BM25 重排序

m.search() 过程中,图存储会检索潜在匹配项。为了提高相关性,系统会应用 BM25 重排序(使用 rank_bm25 库),以确保将最相关的关系作为上下文返回给大语言模型。

来源: mem0/proxy/main.py:182-189

配置优先级

图操作可以配置为使用与记忆系统其余部分不同的大语言模型。这对于在复杂提取任务中使用更强大的模型(如 GPT-4 或 Claude 3.7 Sonnet),同时在标准向量记忆中使用更便宜的模型非常有用。

  1. 图专用大语言模型config["graph_store"]["llm"]
  2. 全局大语言模型config["llm"](在 MemoryConfig 中定义)
  3. 默认值:OpenAI

来源: mem0/configs/base.py:29-58examples/graph-db-demo/neptune-example.ipynb:65-71