---
type: study
status: verified
created: 2026-08-11
updated: 2026-08-11
sensitivity: standard
tags:
  - study
  - langchain
  - python
  - integrations
  - graph-database
  - cypher
  - sparql
topic: LangChain Python 图数据库查询生成与最小权限
sources:
  - https://docs.langchain.com/oss/python/integrations/graphs/amazon_neptune_open_cypher
  - https://docs.langchain.com/oss/python/integrations/graphs/kuzu_db
  - https://docs.langchain.com/oss/python/integrations/graphs/memgraph
  - https://docs.langchain.com/oss/python/integrations/graphs/neo4j_cypher
  - https://docs.langchain.com/oss/python/integrations/graphs/sap_hana_rdf_graph
  - https://docs.langchain.com/oss/python/integrations/graphs/timbr
last_verified: 2026-08-11
---
# Graph Integrations：查询生成与最小权限

## 路由地图

| 路由 | 包/接口 | 查询模式 |
|---|---|---|
| `amazon_neptune_open_cypher` | `langchain-aws` / `NeptuneGraph`、`NeptuneAnalyticsGraph`、`create_neptune_opencypher_qa_chain` | Neptune Database/Analytics 上生成 openCypher、执行并组织答案；可用 `RunnableWithMessageHistory` 加会话历史。 |
| `kuzu_db` | `langchain-kuzu` / `KuzuGraph`、`KuzuQAChain` | 嵌入式 Kùzu、Cypher、schema refresh，可分开 query LLM 与 answer LLM；示例依赖不再维护的 experimental graph transformer。 |
| `memgraph` | `langchain-memgraph` / `MemgraphLangChain`、`MemgraphQAChain` | Cypher QA、prompt refinement、return direct/intermediate、result limit，以及从文本构建图。 |
| `neo4j_cypher` | `langchain-neo4j` / `Neo4jGraph`、`GraphCypherQAChain` | schema/enhanced schema、Cypher generation、direct/intermediate、limit、few-shot、类型排除和方向校验。 |
| `sap_hana_rdf_graph` | `langchain-hana` / `HanaRdfGraph` | RDF/SPARQL，支持 default/URI graph、远程 ontology、自定义 ontology query、本地 RDF 和自动 ontology 抽取。 |
| `timbr` | `langchain-timbr` | ontology semantic layer 上的 SQL agent/chains/nodes：识别概念、生成/校验 SQL、执行和回答。 |

## 安全边界

Kuzu、Memgraph、Neo4j 示例显式要求 `allow_dangerous_requests=True`，这表示调用方承认模型生成查询可能有危险，不表示框架已经把它变安全。`validate_cypher=True` 当前只用于校验/修正 relationship direction，不能阻止写查询、笛卡尔积、资源耗尽或数据越权。

生产要求：

- 使用只读数据库账号、限定 database/graph/schema/view，并在服务端拒绝 mutation、DDL、procedure、文件/网络访问。
- 对 query 长度、AST、node/edge type、traversal depth、result limit、statement timeout 和并发做确定性限制。
- schema/ontology 可能暴露敏感实体和字段；只向模型提供当前租户、当前角色需要的最小视图。
- query result 先做 ACL/DLP/size gate，再交给回答模型；verbose/intermediate steps 不进入用户响应或普通日志。
- Memgraph 的 LLM graph extraction 页面明确说明非确定性；写图时使用 staging、stable IDs、审核和可回滚 upsert。

## 正确性与运营

schema refresh 需要和 migration/deploy 协调，否则模型会按旧 schema 生成查询。生成 query、执行 query、答案组织要分别记录耗时和错误类型。回答必须基于实际 result，并保留 query/result count/provenance；空结果不能让模型靠常识补齐。

这些页面主要展示同步 query/invoke，没有建立统一 async 或 streaming 契约。大图查询不应靠 async 包装规避资源限制；应由数据库超时、cost guard、分页和取消机制控制。

## 验证边界

已逐页核对 6 个官方 Markdown 路由。未连接真实图数据库验证查询计划、权限、方言和性能；`allow_dangerous_requests` 场景必须在隔离数据与只读身份下做专项安全测试。

