Graph Store 提供方(中文译文)
原始 DeepWiki 页面:https://deepwiki.com/mem0ai/mem0/4.2-graph-store-providers
翻译时间:2026-05-27T08:44:57.588Z
翻译模型:deepseek-chat
原文字符数:9845
项目:Mem0 (mem0)
---
图存储提供商
相关源文件
以下文件为本维基页面的生成提供了上下文:
docs/components/vectordbs/dbs/neptune_analytics.mdxexamples/graph-db-demo/neptune-example.ipynbmem0/configs/base.pymem0/configs/enums.pymem0/configs/vector_stores/azure_ai_search.pymem0/configs/vector_stores/neptune.pymem0/configs/vector_stores/supabase.pymem0/configs/vector_stores/vertex_ai_vector_search.pymem0/configs/vector_stores/weaviate.pymem0/proxy/main.pymem0/vector_stores/neptune_analytics.pymem0/vector_stores/supabase.pymem0/vector_stores/vertex_ai_vector_search.pymem0/vector_stores/weaviate.pytests/vector_stores/test_neptune_analytics.pytests/vector_stores/test_vertex_ai_vector_search.py
图存储提供商使 Mem0 能够捕获和查询从对话中提取的实体-关系结构。本文档记录了支持的图数据库提供商、其配置以及用于实例化它们的工厂模式。
有关图记忆操作和搜索的信息,请参阅图搜索与检索。有关相似度阈值配置,请参阅相似度阈值。有关通用图记忆架构,请参阅图记忆概述。
---
支持的提供商
Mem0 通过基于工厂的架构支持多个图数据库提供商。这些提供商使系统能够以实体和关系网络的形式存储和检索知识。
| 提供商 | 后端 | 模块路径 | 主要用例 |
|---|---|---|---|
default (Neo4j) | Neo4j | mem0.memory.graph_memory.MemoryGraph | 具备企业级功能的生产部署 |
memgraph | Memgraph | mem0.memory.memgraph_memory.MemoryGraph | 高性能内存图操作 |
neptune | AWS Neptune Graph | mem0.graphs.neptune.neptunegraph.MemoryGraph | AWS 原生图数据库(无服务器) |
neptunedb | AWS Neptune DB | mem0.graphs.neptune.neptunedb.MemoryGraph | 具备持久化存储的 AWS Neptune |
kuzu | Kuzu | mem0.memory.kuzu_memory.MemoryGraph | 适用于本地部署的嵌入式图数据库 |
来源: mem0/utils/factory.py:214-220
---
工厂架构
GraphStoreFactory 实现
GraphStoreFactory 类负责管理图存储提供商的动态实例化。它将提供商名称映射到各自的类路径和配置要求。
下图展示了配置、工厂以及底层代码实体之间的关系。
图存储代码实体映射
graph TB
subgraph "配置空间"
MemoryConfig["MemoryConfig (mem0.configs.base.MemoryConfig)"]
GraphConfig["graph_store 配置字典"]
end
subgraph "工厂与逻辑空间"
GraphStoreFactory["GraphStoreFactory (mem0.utils.factory.GraphStoreFactory)"]
MemoryInit["Memory.__init__ (mem0.memory.main)"]
end
subgraph "图存储实现"
Neo4j["MemoryGraph (mem0.memory.graph_memory)"]
Memgraph["MemoryGraph (mem0.memory.memgraph_memory)"]
Neptune["MemoryGraph (mem0.graphs.neptune.neptunegraph)"]
NeptuneDB["MemoryGraph (mem0.graphs.neptune.neptunedb)"]
Kuzu["MemoryGraph (mem0.memory.kuzu_memory)"]
end
subgraph "外部依赖"
Neo4jClient["Neo4jGraph (langchain_neo4j.Neo4jGraph)"]
BM25["BM25Okapi (rank_bm25.BM25Okapi)"]
end
MemoryConfig --> GraphConfig
GraphConfig -->|"提供商选择"| GraphStoreFactory
MemoryInit -->|"调用 create()"| GraphStoreFactory
GraphStoreFactory -->|"实例化"| Neo4j
GraphStoreFactory -->|"实例化"| Memgraph
GraphStoreFactory -->|"实例化"| Neptune
GraphStoreFactory -->|"实例化"| NeptuneDB
GraphStoreFactory -->|"实例化"| Kuzu
Neo4j -->|"封装"| Neo4jClient
Neo4j -->|"用于搜索"| BM25
来源: mem0/utils/factory.py:208-230、mem0/configs/base.py:29-58、mem0/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-71、mem0/configs/vector_stores/neptune.py:11-23、mem0/utils/factory.py:216-217
---
Memgraph 和 Kuzu
- Memgraph:一种内存图数据库。配置需要标准连接参数,如
url、username和password。 - Kuzu:一种嵌入式图数据库。配置主要需要本地存储的
path,适用于本地优先的应用场景。
来源: mem0/utils/factory.py:215-218
---
启用图记忆
图记忆在 Memory 类中根据是否存在图存储配置来激活。如果配置中提供了 graph_store,Memory 类会初始化该组件并设置内部标志 enable_graph。
记忆组件初始化流程
graph LR
subgraph "Memory 对象 (mem0.memory.main.Memory)"
MemoryInit["__init__"]
ConfigCheck{"graph_store in config?"}
SetGraph["self.graph = GraphStoreFactory.create()"]
SetFlag["self.enable_graph = True"]
DisableGraph["self.enable_graph = False"]
end
subgraph "工厂 (mem0.utils.factory)"
FactoryCreate["GraphStoreFactory.create()"]
end
MemoryInit --> ConfigCheck
ConfigCheck -->|"是"| FactoryCreate
ConfigCheck -->|"否"| DisableGraph
FactoryCreate --> SetGraph
SetGraph --> SetFlag
来源: 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() 且启用了图记忆时,系统会按照特定顺序更新图。
- 提取:大语言模型从提供的消息中识别实体及其类型。
- 关系构建:大语言模型建立三元组(源 -> 关系 -> 目标)。
- 去重:系统使用向量相似度(由
threshold控制)将新实体与图中已有实体进行匹配。 - 更新:在添加新关系之前,识别并删除过时的关系。
BM25 重排序
在 m.search() 过程中,图存储会检索潜在匹配项。为了提高相关性,系统会应用 BM25 重排序(使用 rank_bm25 库),以确保将最相关的关系作为上下文返回给大语言模型。
来源: mem0/proxy/main.py:182-189
---
配置优先级
图操作可以配置为使用与记忆系统其余部分不同的大语言模型。这对于在复杂提取任务中使用更强大的模型(如 GPT-4 或 Claude 3.7 Sonnet),同时在标准向量记忆中使用更便宜的模型非常有用。
- 图专用大语言模型:
config["graph_store"]["llm"] - 全局大语言模型:
config["llm"](在MemoryConfig中定义) - 默认值:OpenAI
来源: mem0/configs/base.py:29-58、examples/graph-db-demo/neptune-example.ipynb:65-71