1、GraphRAG概述
1.1、概念回顾
传统RAG (向量检索式): 核心是“语义相似度”。将文档切块、嵌入成向量,存储在向量数据库中。查询时,将问题也嵌入成向量,寻找最相似的文本块,交给大模型生成答案。
1.2、问题分析
传统RAG(检索增强生成)本质上是一种"检索-拼接"模式,这种模式虽然能够快速响应用户的特定问题,但缺乏对整个知识体系的宏观拓扑结构认知,很难做出真正的"群体洞察"。
不针对某个具体问题,而是希望对整个文档集进行高层次、综合性的总结,发现潜在主题、趋势或矛盾。
对于这类问题,不是简单、直接的问答为主,而是需要深度分析、关系推理和全局洞察
1.3 、GraphRAG的应用场景
GraphRAG的核心价值在于处理复杂、需要深度推理和关系挖掘的查询。它的优势场景与传统RAG的短板场景高度互补。
是一种将知识图谱与检索增强生成相结合的方法。它在传统 RAG 的基础上,引入了图结构和社区检测等方法,以提高对复杂数据的理解和生成能力。
1.4、知识图谱
1.4.1、概念
GraphRAG的核心价值在于处理复杂、需要深度推理和关系挖掘的查询。它的优势场景与传统RAG的短板场景高度互补。
是一种将知识图谱与检索增强生成相结合的方法。它在传统 RAG 的基础上,引入了图结构和社区检测等方法,以提高对复杂数据的理解和生成能力。
1.4.2、知识图谱的构成情况
都由节点(Node)和边(Edge)组成。KG 的每个节点代表一个概念,每条边则表示两个概念之间的关系。

1.4.3、实际应用情况
1、谷歌搜索每天处理数十亿次查询,其核心依赖知识图谱来提供精准结果。
2、Facebook则通过知识图谱分析用户关系链,实现好友推荐功能。
2、GraphRAG原理
2.1、概念
GraphRAG 是一种将知识图谱与检索增强生成相结合的方法。它在传统 RAG 的基础上,引入了图结构和社区检测等方法,以提高对复杂数据的理解和生成能力。GraphRAG 利用大语言模型从非结构化文本中提取实体、关系和摘要,构建知识图谱,然后利用图结构进行高效的检索和问答。
GraphRAG 的理念跟我们人类很像,当别人问我们一个问题时,其实我们是基于自己整个已有的知识库做提炼,然后去回答
2.2、GraphRAG的发展情况

2.3、GraphRAG工作流
GraphRAG 分为两个主要阶段:索引阶段(Indexing Phase) 和 查询阶段(Query Phase)。
1、索引阶段
索引过程利用LLM提取出节点(实体)、边(关系)和协变量(如 claim),然后利用社区检测技术对整个知识图谱进行划分,再利用LLM进一步总结。最终针对特定的查询,可以汇总所有与之相关的社区摘要生成一个全局性的答案。
2、查询阶段
社区(Community)是在知识图谱中具有紧密联系的一组实体。这些实体之间的连接(关系)比较密集,代表了在某个主题或情节下高度相关的内容。
在 GraphRAG 中,社区代表了文档中的一个主题、故事线或概念集合。
社区层级编号(C0、C1、C2、C3 等):编号中的数字越小,表示社区的层级越高,内容越概括;数字越大,表示社区的层级越低,内容越具体。

2.3.1、第一阶段: 生成知识图谱
- 第 1 步:文本切分
将源文档分割成较小的子文档(如300个token),并设置一定的重叠部分(如100个token),确保在分块过程中尽可能保留上下文信息。 - 第 2 步:提取实体和关系
在此阶段,LLM 会提取每个块中的实体和关系。此外,LLM还会生成有关所有实体及其关系的简短描述。
每个提取的实体都分配有一个唯一的 ID 以实现可追溯性。
为清楚起见,将解析块中的代词或不明确的引用。 - 第 3 步:构建知识图谱 (KG)
将所有抽取的实体作为节点,关系作为边,构建一个全局的知识图谱。这个图谱展示了文档中所有实体和它们之间的复杂关系。 - 第 4 步: 社区划分和层级结构
构建 KG 后,使用 Leiden 社区检测算法在 KG 中识别出不同的社区。每个社区代表一组紧密相关的实体和关系,反映了文档中的特定主题或情节。
该算法遵循分层方法,递归检测子群落,直到达到叶群落,而叶群落不能再被分区。它还确保每个节点仅属于一个社区(互斥),并且不会遗漏任何节点被分配到社区。
节点的大小与其重要性或相关性成正比。 - 第 5 步: 社区总结
第一级摘要(细节级): 针对每个社区,生成包含详细信息的摘要。
第二级摘要(中等粒度): 将相关社区组合,生成更广泛主题的摘要。
第三级摘要(全局级): 生成整个文档的整体摘要,提供高层次的概览。
2.3.2、第一阶段: 查询
1、本地搜索
本地搜索方法通过将 AI 提取的知识图谱中的相关数据与原始文档的文本块相结合来生成答案。这种方法适用于需要了解文档中提到的特定实体的问题。例如,洋甘菊的治疗特性是什么?这种情况下系统会利用 C2 或 C3(低级别) 等更细粒度的社区来检索和生成答案。

2、全局搜索
全局搜索方法通过以 map-reduce 方式搜索所有 AI 生成的社区报告来生成答案。例如,本文的核心主题是什么?这种情况下系统会利用 C0 或 C1 的社区和摘要来生成回答。

-
Map Reduce 方法
Map 阶段:每个社区报告都经过并行处理,以生成多个中间部分响应以及重要性分数 (0 – 100),指示该点在回答给定查询中的重要性。
Reduce 阶段:然后,根据重要性分数对所有部分摘要进行降序排序,并将高分响应组合在一起以生成全局答案。
Map Reduce-评分中间响应部分
3、DRIFT 搜索
DRIFT 搜索通过整合社区信息,使本地搜索的起点更为宽泛,能够检索更多类型的事实,从而提升查询的详细性
DRIFT 在 GraphRAG 的查询引擎基础上进一步扩展,为本地搜索提供了更精细的分层查询方式,使其能够处理与预定义模板不完全匹配的复杂查询。
DRIFT搜索
3、项目实战
以 《圣诞颂歌》 为例
3.1、环境配置
1、安装环境
# python 3.11
pip install graphrag
2、使用模型
qwen-coder-plus
3.2、开发步骤
1、创建.env环境配置文件
graphrag init --root D:\graghrag
2、settings.yaml项目配置文件
models:
default_chat_model:
# encoding_type: cl100k_base
encoding_model : cl100k_base
type: openai_chat
model_provider: openai
auth_type: api_key # or azure_managed_identity
api_key: ${GRAPHRAG_API_KEY} # set this in the generated .env file, or remove if managed identity
model: qwen-coder-plus
api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
# api_version: 2024-05-01-preview
model_supports_json: true # recommended if this is available for your model.
concurrent_requests: 25
async_mode: threaded # or asyncio
retry_strategy: exponential_backoff
max_retries: 10
tokens_per_minute: null
requests_per_minute: null
default_embedding_model:
encoding_model: cl100k_base
type: openai_embedding
model_provider: openai
auth_type: api_key
api_key: ${GRAPHRAG_API_KEY}
model: text-embedding-v1
api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
# api_version: 2024-05-01-preview
concurrent_requests: 25
async_mode: threaded # or asyncio
retry_strategy: exponential_backoff
max_retries: 10
tokens_per_minute: null
requests_per_minute: null
### Input settings ###
input:
storage:
type: file # or blob
base_dir: "input"
file_type: text # [csv, text, json]
chunks:
size: 1200
overlap: 100
group_by_columns: [id]
### Output/storage settings ###
## If blob storage is specified in the following four sections,
## connection_string and container_name must be provided
output:
type: file # [file, blob, cosmosdb]
base_dir: "output"
cache:
type: file # [file, blob, cosmosdb]
base_dir: "cache"
reporting:
type: file # [file, blob]
base_dir: "logs"
vector_store:
default_vector_store:
type: lancedb
db_uri: output\lancedb
container_name: default
### Workflow settings ###
embed_text:
model_id: default_embedding_model
vector_store_id: default_vector_store
extract_graph:
model_id: default_chat_model
prompt: "prompts/extract_graph.txt"
entity_types: [organization,person,geo,event]
max_gleanings: 1
summarize_descriptions:
model_id: default_chat_model
prompt: "prompts/summarize_descriptions.txt"
max_length: 500
extract_graph_nlp:
text_analyzer:
extractor_type: regex_english # [regex_english, syntactic_parser, cfg]
async_mode: threaded # or asyncio
cluster_graph:
max_cluster_size: 10
extract_claims:
enabled: false
model_id: default_chat_model
prompt: "prompts/extract_claims.txt"
description: "Any claims or facts that could be relevant to information discovery."
max_gleanings: 1
community_reports:
model_id: default_chat_model
graph_prompt: "prompts/community_report_graph.txt"
text_prompt: "prompts/community_report_text.txt"
max_length: 2000
max_input_length: 8000
embed_graph:
enabled: false # if true, will generate node2vec embeddings for nodes
umap:
enabled: false # if true, will generate UMAP embeddings for nodes (embed_graph must also be enabled)
snapshots:
graphml: true
embeddings: false
### Query settings ###
## The prompt locations are required here, but each search method has a number of optional knobs that can be tuned.
## See the config docs: https://microsoft.github.io/graphrag/config/yaml/#query
local_search:
chat_model_id: default_chat_model
embedding_model_id: default_embedding_model
prompt: "prompts/local_search_system_prompt.txt"
global_search:
chat_model_id: default_chat_model
map_prompt: "prompts/global_search_map_system_prompt.txt"
reduce_prompt: "prompts/global_search_reduce_system_prompt.txt"
knowledge_prompt: "prompts/global_search_knowledge_system_prompt.txt"
drift_search:
chat_model_id: default_chat_model
embedding_model_id: default_embedding_model
prompt: "prompts/drift_search_system_prompt.txt"
reduce_prompt: "prompts/drift_search_reduce_prompt.txt"
basic_search:
chat_model_id: default_chat_model
embedding_model_id: default_embedding_model
prompt: "prompts/basic_search_system_prompt.txt"
3、构建索引(10分钟左右)
graphrag index --root D:\graghrag
4、搜索
- 本地搜索:
graphrag query --root D:\graghrag --method local --query "这个故事的主题是什么?"
- 全局搜索
graphrag query --root D:\graghrag --method global --query "这个故事主题是什么?"
- DRIFT搜索
graphrag query --root D:\graghrag --method drift --query "这个故事主题是什么?"
3.3、可视化图谱
安装Gephi

