欢迎光临
我们一直在努力

知识图谱增强:RAG 到 GraphRAG 再到 LightRAG

知识图谱增强:RAG 到 GraphRAG 再到 LightRAG

一、概念速查

传统 RAG 的局限

问题表现根因
知识碎片化 问答返回零散段落而非结构化答案 向量检索只做语义匹配,不理解实体间的显式关系
多跳推理弱 "A 和 B 有什么关系"类问题容易答偏 文本块之间无关联索引
可解释性差 无法追溯答案的推理路径 检索过程是黑盒相似度计算
索引更新慢 新增文档需全量重建 所有 chunk 重新向量化

知识碎片化最为致命。向量检索本质是在高维空间中找"语义最像"的文本片段,但它不知道两个片段里的实体是否有关联。比如"苹果发布了新手机"和"库克出席了发布会"都能被"苹果"相关的问题召回,但向量检索不会把它们连接成"库克是苹果CEO"这条关系链——它只返回排名靠前的独立段落。

多跳推理弱的根因在于文本块之间没有关联索引。问"A 和 B 有什么关系"时,系统必须先找到 A 相关的块,再找到 B 相关的块,然后推断两者之间的关系。传统 RAG 的扁平结构让这种链式查找只能依赖 LLM 的"内在知识",等于放弃了检索增强的核心优势。

可解释性差源于检索过程是黑盒相似度计算。用户看到答案后无法追溯"为什么返回这段文本",更无法验证推理路径。这在医疗、金融等合规场景中是致命缺陷。

RAG / GraphRAG / LightRAG 对比

维度传统 RAGGraphRAG (Microsoft)LightRAG
检索方式 向量相似度匹配 向量 + 图谱社区遍历 向量 + 图谱双层级检索
知识表示 扁平文本块 实体 + 关系 + 社区层次 实体 + 关系(去社区层次)
多跳推理 强(社区链式推理) 强(双层级路由)
索引成本 低(一次向量化) 高(LLM 反复调用 + 莱顿聚类) 中(实体抽取 + 去重)
增量更新 不支持,需全量重索引 不支持,需全量重建 支持,增量插入图谱
适用场景 简单事实问答 专业领域深度推理 动态数据、快速迭代
部署复杂度 极低 高(需 Neo4j / Milvus 等) 低(内置 SQLite + NetworkX)

Neo4j 与 NetworkX

特性Neo4jNetworkX
类型 原生图数据库(磁盘持久化) Python 内存图分析库
查询语言 Cypher Python API
规模 千万级节点 百万级以内
部署 需独立服务(Docker 或安装包) pip install 即可
可视化 Neo4j Browser / Bloom 内置 Matplotlib / pyvis
场景 生产环境持久化存储 快速原型、小型实验

二、底层原理

RAG 基础流程

#mermaid-svg-5mDMqtun23d3JPKW{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-5mDMqtun23d3JPKW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-5mDMqtun23d3JPKW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-5mDMqtun23d3JPKW .error-icon{fill:#552222;}#mermaid-svg-5mDMqtun23d3JPKW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-5mDMqtun23d3JPKW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-5mDMqtun23d3JPKW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-5mDMqtun23d3JPKW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-5mDMqtun23d3JPKW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-5mDMqtun23d3JPKW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-5mDMqtun23d3JPKW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-5mDMqtun23d3JPKW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-5mDMqtun23d3JPKW .marker.cross{stroke:#333333;}#mermaid-svg-5mDMqtun23d3JPKW svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-5mDMqtun23d3JPKW p{margin:0;}#mermaid-svg-5mDMqtun23d3JPKW .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-5mDMqtun23d3JPKW .cluster-label text{fill:#333;}#mermaid-svg-5mDMqtun23d3JPKW .cluster-label span{color:#333;}#mermaid-svg-5mDMqtun23d3JPKW .cluster-label span p{background-color:transparent;}#mermaid-svg-5mDMqtun23d3JPKW .label text,#mermaid-svg-5mDMqtun23d3JPKW span{fill:#333;color:#333;}#mermaid-svg-5mDMqtun23d3JPKW .node rect,#mermaid-svg-5mDMqtun23d3JPKW .node circle,#mermaid-svg-5mDMqtun23d3JPKW .node ellipse,#mermaid-svg-5mDMqtun23d3JPKW .node polygon,#mermaid-svg-5mDMqtun23d3JPKW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-5mDMqtun23d3JPKW .rough-node .label text,#mermaid-svg-5mDMqtun23d3JPKW .node .label text,#mermaid-svg-5mDMqtun23d3JPKW .image-shape .label,#mermaid-svg-5mDMqtun23d3JPKW .icon-shape .label{text-anchor:middle;}#mermaid-svg-5mDMqtun23d3JPKW .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-5mDMqtun23d3JPKW .rough-node .label,#mermaid-svg-5mDMqtun23d3JPKW .node .label,#mermaid-svg-5mDMqtun23d3JPKW .image-shape .label,#mermaid-svg-5mDMqtun23d3JPKW .icon-shape .label{text-align:center;}#mermaid-svg-5mDMqtun23d3JPKW .node.clickable{cursor:pointer;}#mermaid-svg-5mDMqtun23d3JPKW .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-5mDMqtun23d3JPKW .arrowheadPath{fill:#333333;}#mermaid-svg-5mDMqtun23d3JPKW .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-5mDMqtun23d3JPKW .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-5mDMqtun23d3JPKW .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-5mDMqtun23d3JPKW .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-5mDMqtun23d3JPKW .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-5mDMqtun23d3JPKW .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-5mDMqtun23d3JPKW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-5mDMqtun23d3JPKW .cluster text{fill:#333;}#mermaid-svg-5mDMqtun23d3JPKW .cluster span{color:#333;}#mermaid-svg-5mDMqtun23d3JPKW div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-5mDMqtun23d3JPKW .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-5mDMqtun23d3JPKW rect.text{fill:none;stroke-width:0;}#mermaid-svg-5mDMqtun23d3JPKW .icon-shape,#mermaid-svg-5mDMqtun23d3JPKW .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-5mDMqtun23d3JPKW .icon-shape p,#mermaid-svg-5mDMqtun23d3JPKW .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-5mDMqtun23d3JPKW .icon-shape .label rect,#mermaid-svg-5mDMqtun23d3JPKW .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-5mDMqtun23d3JPKW .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-5mDMqtun23d3JPKW .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-5mDMqtun23d3JPKW :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

文档输入

文档切分 Chunk

Embedding 向量化

向量库存储

用户查询

查询向量化

ANN 相似度检索

Top-K 文本块

LLM 生成回答

切分策略: chunk_size=300-500,chunk_overlap=50-100。但无论参数怎么调,切分带来的语义断裂无法根除:一个实体被切到两个 chunk 中,两端都丢失了完整上下文。向量检索按余弦相似度排序,返回的 Top-K 片段可能来自文档的不同章节,LLM 需要自己"拼接"它们之间的逻辑关系——这恰恰是 LLM 最容易产生幻觉的环节。

Embedding 的天花板: 即便用最好的 Embedding 模型,语义匹配也无法建模实体间的显式关系。"苹果"和"iPhone"在两个 chunk 中被分别向量化,检索"苹果的供应链"时,系统能找到"苹果"相关的片段,但无法理解"iPhone→供应链"这条路径。

GraphRAG:实体抽取 + 图结构检索

GraphRAG 在 RAG 的基础上引入知识图谱构建层,核心流程如下:

#mermaid-svg-KtkN3znflMSpYLVd{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-KtkN3znflMSpYLVd .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KtkN3znflMSpYLVd .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KtkN3znflMSpYLVd .error-icon{fill:#552222;}#mermaid-svg-KtkN3znflMSpYLVd .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KtkN3znflMSpYLVd .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KtkN3znflMSpYLVd .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KtkN3znflMSpYLVd .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KtkN3znflMSpYLVd .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KtkN3znflMSpYLVd .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KtkN3znflMSpYLVd .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KtkN3znflMSpYLVd .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KtkN3znflMSpYLVd .marker.cross{stroke:#333333;}#mermaid-svg-KtkN3znflMSpYLVd svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KtkN3znflMSpYLVd p{margin:0;}#mermaid-svg-KtkN3znflMSpYLVd .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-KtkN3znflMSpYLVd .cluster-label text{fill:#333;}#mermaid-svg-KtkN3znflMSpYLVd .cluster-label span{color:#333;}#mermaid-svg-KtkN3znflMSpYLVd .cluster-label span p{background-color:transparent;}#mermaid-svg-KtkN3znflMSpYLVd .label text,#mermaid-svg-KtkN3znflMSpYLVd span{fill:#333;color:#333;}#mermaid-svg-KtkN3znflMSpYLVd .node rect,#mermaid-svg-KtkN3znflMSpYLVd .node circle,#mermaid-svg-KtkN3znflMSpYLVd .node ellipse,#mermaid-svg-KtkN3znflMSpYLVd .node polygon,#mermaid-svg-KtkN3znflMSpYLVd .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-KtkN3znflMSpYLVd .rough-node .label text,#mermaid-svg-KtkN3znflMSpYLVd .node .label text,#mermaid-svg-KtkN3znflMSpYLVd .image-shape .label,#mermaid-svg-KtkN3znflMSpYLVd .icon-shape .label{text-anchor:middle;}#mermaid-svg-KtkN3znflMSpYLVd .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-KtkN3znflMSpYLVd .rough-node .label,#mermaid-svg-KtkN3znflMSpYLVd .node .label,#mermaid-svg-KtkN3znflMSpYLVd .image-shape .label,#mermaid-svg-KtkN3znflMSpYLVd .icon-shape .label{text-align:center;}#mermaid-svg-KtkN3znflMSpYLVd .node.clickable{cursor:pointer;}#mermaid-svg-KtkN3znflMSpYLVd .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-KtkN3znflMSpYLVd .arrowheadPath{fill:#333333;}#mermaid-svg-KtkN3znflMSpYLVd .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-KtkN3znflMSpYLVd .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-KtkN3znflMSpYLVd .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-KtkN3znflMSpYLVd .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-KtkN3znflMSpYLVd .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-KtkN3znflMSpYLVd .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-KtkN3znflMSpYLVd .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-KtkN3znflMSpYLVd .cluster text{fill:#333;}#mermaid-svg-KtkN3znflMSpYLVd .cluster span{color:#333;}#mermaid-svg-KtkN3znflMSpYLVd div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-KtkN3znflMSpYLVd .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-KtkN3znflMSpYLVd rect.text{fill:none;stroke-width:0;}#mermaid-svg-KtkN3znflMSpYLVd .icon-shape,#mermaid-svg-KtkN3znflMSpYLVd .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-KtkN3znflMSpYLVd .icon-shape p,#mermaid-svg-KtkN3znflMSpYLVd .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-KtkN3znflMSpYLVd .icon-shape .label rect,#mermaid-svg-KtkN3znflMSpYLVd .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-KtkN3znflMSpYLVd .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-KtkN3znflMSpYLVd .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-KtkN3znflMSpYLVd :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

局部

全局

文档

NER 实体识别

关系抽取

莱顿社区检测

层次化社区摘要

用户查询

查询类型

向量相似度检索

社区摘要检索

结果融合 + Rerank

LLM 生成

关键改进: 用莱顿(Leiden)算法对实体图做社区检测,每个社区自动生成摘要。全局查询时遍历社区摘要链,回答宏观问题效果显著提升。

代价: 索引阶段需反复调用 LLM(GPT-4o 级别),Token 消耗通常是 LightRAG 的 3-5 倍。100 份文档的索引耗时约 3-5 分钟。

实体抽取原理:从文本到三元组

GraphRAG 的实体抽取依赖 LLM 完成两个步骤:命名实体识别(NER)和关系抽取(RE)。

NER 层给 LLM 一段文本,要求它识别出预定义类型的实体。实践中,实体类型体系决定了图谱质量。只分"人名/地名/机构名"三类对技术文档远远不够——“Transformer”"GraphRAG"会被误归为地名。推荐扩展到五类:

类型示例
TECHNOLOGY GraphRAG, PyTorch, FAISS
CONCEPT retrieval-augmented generation
PERSON Yoshua Bengio
ORGANIZATION Microsoft Research
LOCATION San Francisco

RE 层识别实体对之间的关系。有两类策略:一是让 LLM 直接输出 (subject, relation, object) 三元组;二是仅抽实体,同一段落内的实体自动生成 CO_OCCURS_IN 共现边。后者更稳定——LLM 抽关系时经常漏抽或凭空编造,而共现边隐含语义关联,下游图检索通过邻居扩展足以发现相关实体。

def extract_entities(text: str) > list[dict]:
prompt = f"""从以下文本中抽取实体。
实体类型: TECHNOLOGY, CONCEPT, PERSON, ORGANIZATION, LOCATION
输出 JSON 数组,每个元素包含 name 和 type。

文本: {text}
"""

resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
response_format={"type": "json_object"}
)
return json.loads(resp.choices[0].message.content)["entities"]

实体去重是另一个关键环节。"苹果公司"和"Apple Inc."指向同一实体,需用别名映射表或编辑距离做 resolution。不做去重的图谱密度和质量都会严重下降。

Neo4j 查询示例

生产环境中图数据库首选 Neo4j。以下 Cypher 查询覆盖了最常见的图操作:

// 创建实体节点
CREATE (p:Person {name: "姚明", birth: "1980-09-12"})
CREATE (c:Career {name: "篮球运动员"})

// 创建关系
MATCH (p:Person {name: "姚明"})
MATCH (c:Career {name: "篮球运动员"})
CREATE (p)-[:HAS_CAREER]->(c)

// 多跳查询:姚明同事圈
MATCH (p:Person {name: "姚明"})-[:HAS_CAREER]->(:Career)<-[:HAS_CAREER]-(other:Person)
RETURN other.name

// 路径查询:姚明到郭晶晶的关系路径
MATCH path = shortestPath(
(a:Person {name: "姚明"})-[*..6]-(b:Person {name: "郭晶晶"})
)
RETURN path

// 社区检测:Louvain 算法聚类
CALL gds.louvain.stream({
nodeProjection: 'Person',
relationshipProjection: {
CO_OCCURS_IN: {type: 'CO_OCCURS_IN', orientation: 'UNDIRECTED'}
}
})
YIELD nodeId, communityId
RETURN gds.util.asNode(nodeId).name AS person, communityId

Neo4j 自带的 Browser 界面(http://localhost:7474)将查询结果直接渲染为节点-边图,对调试和质量检查非常直观。

LightRAG:增量更新与轻量部署

#mermaid-svg-MI4tuwUOJAtRzxht{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MI4tuwUOJAtRzxht .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MI4tuwUOJAtRzxht .error-icon{fill:#552222;}#mermaid-svg-MI4tuwUOJAtRzxht .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MI4tuwUOJAtRzxht .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MI4tuwUOJAtRzxht .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MI4tuwUOJAtRzxht .marker.cross{stroke:#333333;}#mermaid-svg-MI4tuwUOJAtRzxht svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MI4tuwUOJAtRzxht p{margin:0;}#mermaid-svg-MI4tuwUOJAtRzxht .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-MI4tuwUOJAtRzxht .cluster-label text{fill:#333;}#mermaid-svg-MI4tuwUOJAtRzxht .cluster-label span{color:#333;}#mermaid-svg-MI4tuwUOJAtRzxht .cluster-label span p{background-color:transparent;}#mermaid-svg-MI4tuwUOJAtRzxht .label text,#mermaid-svg-MI4tuwUOJAtRzxht span{fill:#333;color:#333;}#mermaid-svg-MI4tuwUOJAtRzxht .node rect,#mermaid-svg-MI4tuwUOJAtRzxht .node circle,#mermaid-svg-MI4tuwUOJAtRzxht .node ellipse,#mermaid-svg-MI4tuwUOJAtRzxht .node polygon,#mermaid-svg-MI4tuwUOJAtRzxht .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MI4tuwUOJAtRzxht .rough-node .label text,#mermaid-svg-MI4tuwUOJAtRzxht .node .label text,#mermaid-svg-MI4tuwUOJAtRzxht .image-shape .label,#mermaid-svg-MI4tuwUOJAtRzxht .icon-shape .label{text-anchor:middle;}#mermaid-svg-MI4tuwUOJAtRzxht .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MI4tuwUOJAtRzxht .rough-node .label,#mermaid-svg-MI4tuwUOJAtRzxht .node .label,#mermaid-svg-MI4tuwUOJAtRzxht .image-shape .label,#mermaid-svg-MI4tuwUOJAtRzxht .icon-shape .label{text-align:center;}#mermaid-svg-MI4tuwUOJAtRzxht .node.clickable{cursor:pointer;}#mermaid-svg-MI4tuwUOJAtRzxht .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MI4tuwUOJAtRzxht .arrowheadPath{fill:#333333;}#mermaid-svg-MI4tuwUOJAtRzxht .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MI4tuwUOJAtRzxht .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MI4tuwUOJAtRzxht .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MI4tuwUOJAtRzxht .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MI4tuwUOJAtRzxht .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MI4tuwUOJAtRzxht .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MI4tuwUOJAtRzxht .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MI4tuwUOJAtRzxht .cluster text{fill:#333;}#mermaid-svg-MI4tuwUOJAtRzxht .cluster span{color:#333;}#mermaid-svg-MI4tuwUOJAtRzxht div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MI4tuwUOJAtRzxht .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MI4tuwUOJAtRzxht rect.text{fill:none;stroke-width:0;}#mermaid-svg-MI4tuwUOJAtRzxht .icon-shape,#mermaid-svg-MI4tuwUOJAtRzxht .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MI4tuwUOJAtRzxht .icon-shape p,#mermaid-svg-MI4tuwUOJAtRzxht .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MI4tuwUOJAtRzxht .icon-shape .label rect,#mermaid-svg-MI4tuwUOJAtRzxht .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MI4tuwUOJAtRzxht .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MI4tuwUOJAtRzxht .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MI4tuwUOJAtRzxht :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

具体实体

宏观主题

新文档

实体抽取

实体去重+键值索引

增量挂载到图谱

双层级检索

用户查询

问题类型

低层级:相邻节点检索

高层级:全局图遍历

混合排序

LLM 生成

增量更新机制: 新增文档只需抽取新实体和关系,去重后合并到现有图结构中。不需要重建任何已有索引。删除操作同理,仅移除对应节点和边。

# LightRAG 增量插入示例
from lightrag import LightRAG, QueryParam

rag = LightRAG(
working_dir="./rag_storage",
embedding_func=openai_embed,
llm_model_func=gpt_4o_mini_complete,
)

# 首次索引
await rag.ainsert("首次导入的文档内容…")

# 增量更新 — 直接插入新内容,无需重建
await rag.ainsert("新增的文档内容…")

# 增量删除 — 移除文档对应节点和边,已有索引不受影响
# 底层:定位该文档抽取的实体 → 移除出度为 0 的孤立节点 → 更新向量库
await rag.adelete("document_id_123")

# 增量更新 — delete + insert 的原子组合
await rag.aupdate("document_id_456", "更新后的文档内容…")

# 查询(自动路由到合适的检索模式)
result = await rag.aquery(
"某实体的最新信息",
param=QueryParam(mode="hybrid")
)

存储架构: 四层解耦 — KV 存储(SQLite/Redis)、向量存储(Chroma/Faiss)、图存储(NetworkX/Neo4j)、文档状态跟踪。原型阶段用内置 SQLite+NetworkX,生产切 Neo4j+Milvus。

三、架构设计原则

什么时候该用 GraphRAG

  • 你的问题 60% 以上需要多跳推理(“A 和 B 的关系是什么”、“影响链路怎么走”)
  • 知识库是静态的,更新频率很低
  • 你愿意为准确性支付较高的计算成本

什么时候该用 LightRAG

  • 知识库持续增长,需要高频增量更新
  • 资源受限(单机、无 GPU)
  • 需要快速验证知识图谱增强的效果

知识图谱构建质量把控

  • 实体类型体系要覆盖领域专有词: 只分人名/地名/机构名不够,技术文档需要 TECHNOLOGY、CONCEPT 等类型。实体类型太少导致图谱密度过低(>0.3 → <1.8 的差异直接影响召回)
  • 共现边可以替代关系抽取: LLM 直接抽关系不稳定。实测策略"只抽实体,同一页/段内实体自动生成 CO_OCCURS_IN 边"足够实用
  • 必须做实体去重: “苹果公司” vs “Apple Inc.” vs “苹果” 指向同一实体。用别名映射表或编辑距离做 resolution

增量更新策略

  • 时间戳标记: 每个节点和边记录 created_at 和 expires_at
  • 版本化删除: 标记 status: deleted 而非物理删除,支持回滚
  • 阈值触发重索引: 增量增量超过原始图 30% 时,做一次全量优化(compact)
  • 分阶段处理: 大文档集按 batch_size=10 分批次,每批独立抽取后合并,避免 OOM
  • 冲突解决: 增量插入的实体若与已有实体同名,按置信度保留高置信度的一方,expires_at 过期后自动软删除
赞(0)
未经允许不得转载:171主机测评 » 知识图谱增强:RAG 到 GraphRAG 再到 LightRAG
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址