揭秘GraphRAG:深入解析prompt每一步逻辑
最近在学习GraphRAG,我感觉GraphRAG调用的prompt很多,对于我来说如果想搞清楚逻辑,就得清楚每一步都做了什么,每一步输出什么,简单学习记录下。
首先GraphRAG整体分为构建索引和查询两个步骤,我将一次对于各个阶段详细调用进行解析。
构建索引阶段
文本分块
这一部分和Naive RAG没什么区别,只是切块,并没有调用LLM,没有用到Prompt。这篇文章不记录具体讲解的分块方式,之后会有一篇新的文章来详细介绍这一部分。并且Embedding这个模块应该是存在于多个步骤的,这篇文章也不做讲解。
实体 & 关系抽取(extract_graph.txt)
这个prompt就是给定一段话,然后要求大模型提取出实体以及各个实体对应的关系,看起来指令很长,但是它做的事情很简单。可以举出一个例子说明一下。
假设我们有两个实体类型:PERSON, ORGANIZATION,给定一段文本:Apple Inc. was founded by Steve Jobs.
输出示例(英文)
("entity"<:>APPLE INC.<:>ORGANIZATION<:>Apple Inc. is a technology company founded by Steve Jobs)上述输出中APPLE INC是提取到的实体,ORGANIZATION是对应实体的类型,Apple Inc. is a technology company founded by Steve Jobs是解释。{record_delimiter} ("entity"<:>STEVE JOBS<:>PERSON<:>Steve Jobs is the founder of Apple Inc.) {record_delimiter} ("relationship"<:>STEVE JOBS<:>APPLE INC.<:>Steve Jobs founded Apple Inc.{tuple_delimiter}10) {completion_delimiter}上述输出中STEVE JOBS和APPLE INC是两个实体名称,Steve Jobs founded Apple Inc.是两者的关系,10是关系强度。说明:
<:>代表{tuple_delimiter}
{record_delimiter}在这里只是一个占位符文本,实际使用时可以替换成例如"\n---\n"或"<RECORD_END>"
{completion_delimiter}同理,表示输出结束关系强度打了 10 分(因为直接创始人关系很强)
实体/关系描述总结(summarize_descriptions.txt)
这段prompt就是将同一个是实体在多个分块中出现的描述或者关系进行合并,本质上就是将多个随便信息进行整体成这个实体和关系的全部信息。
例如
上面的步骤输出的实体和关系可以整理成
{
"entities": [
{
"name": "APPLE INC.",
"type": "ORGANIZATION",
"description": "Apple Inc. is a technology company founded by Steve Jobs"
},
{
"name": "STEVE JOBS",
"type": "PERSON",
"description": "Steve Jobs is the founder of Apple Inc."
}
],
"relationships": [
{
"source": "STEVE JOBS",
"target": "APPLE INC.",
"description": "Steve Jobs founded Apple Inc.",
"weight": 10
}
]
}
经过这一阶段的prompt可以整理成
{
"entities": [
{
"id": "E1",
"name": "APPLE INC.",
"type": "ORGANIZATION",
"description": "Apple Inc. is a technology company founded by Steve Jobs."
},
{
"id": "E2",
"name": "STEVE JOBS",
"type": "PERSON",
"description": "Steve Jobs is an entrepreneur and the founder of Apple Inc."
}
],
"relationships": [
{
"id": "R1",
"source": "E2",
"target": "E1",
"relation": "founded",
"description": "Steve Jobs founded Apple Inc.",
"weight": 10
}
]
}
社区发现
这个步骤就是把一张大的知识图拆解成几个主题连接紧密的子图,假设有一个“大实体中心图”
算法不会看名字,它只看:谁和谁连得多?谁更像一伙?
苹果这一团:
苹果公司 ↔ iPhone ↔ iOS
苹果公司 ↔ 乔布斯
苹果公司 ↔ 库比蒂诺
微软这一团:
微软 ↔ Windows ↔ Office
微软 ↔ 比尔盖茨
如果图里有这种:苹果公司 —— Windows(偶尔提到), 会被算法“忽略掉”或者影响很小,最终拆分结果:
社区1(苹果生态):
- 苹果公司
- iPhone
- iOS
- 乔布斯
- 库比蒂诺
社区2(微软生态):
- 微软公司
- Windows
- Office
- 比尔盖茨
社区报告生成(community_report_graph.txt)
这个步骤就是将上一步骤得到的社区进行总结,提示词中有说明都输出哪些字段,例如包括整体的题目和摘要等等。
上个输出的步骤经过这部分的prompt以后会生成多个社区,以下是对应的格式
{
"title": "Apple Ecosystem: Apple Inc., iPhone, iOS, Steve Jobs, Cupertino",
"summary": "This community centers around Apple Inc., a technology company headquartered in Cupertino. Its key products include iPhone and iOS, with foundational influence from co-founder Steve Jobs. The relationships among these entities form a tightly integrated ecosystem, where hardware (iPhone), software (iOS), leadership (Jobs), and corporate location (Cupertino) are all directly linked to Apple Inc.",
"rating": 7.5,
"rating_explanation": "The impact severity is high due to Apple's global market dominance, technological influence, and the lasting legacy of Steve Jobs on consumer electronics and software ecosystems.",
"findings": [
{
"summary": "Apple Inc. as the central hub of the ecosystem",
"explanation": "Apple Inc. is the core entity connecting all others: it develops iPhone hardware, creates the iOS operating system, is headquartered in Cupertino, and was co-founded by Steve Jobs. This central position indicates that any disruption to Apple Inc. would directly affect its products, location, and brand legacy. [Data: Entities (1), Relationships (1-2, 1-3, 1-4, 1-5)]"
},
{
"summary": "Integration between iPhone and iOS",
"explanation": "iPhone and iOS are tightly coupled: iOS is the exclusive operating system for iPhone, enabling a seamless user experience and strong vendor lock-in. This integration has driven Apple's success in the smartphone market but also creates a single point of failure if either component faces security or regulatory issues. [Data: Entities (2, 3); Relationships (2-3)]"
},
{
"summary": "Steve Jobs' foundational role",
"explanation": "Steve Jobs, as co-founder and former CEO, shaped Apple's product philosophy and innovation culture. Although he is no longer alive, his vision continues to influence Apple's design and marketing strategies. The relationship between Jobs and Apple Inc. is historical but remains central to the company's identity and reputation. [Data: Entities (4); Relationships (4-1)]"
},
{
"summary": "Cupertino as the geographic anchor",
"explanation": "Cupertino is the location of Apple's headquarters (Apple Park), symbolizing the company's corporate identity and operational base. This geographic entity is directly tied to Apple Inc. and represents the region's economic dependence on Apple's success. [Data: Entities (5); Relationships (5-1)]"
},
{
"summary": "Potential risks from ecosystem lock-in",
"explanation": "The closed nature of the Apple ecosystem (iPhone + iOS) creates high customer loyalty but also invites antitrust scrutiny and interoperability challenges. Rivals and regulators may target the exclusivity of iOS on iPhone, which could impact Apple's business model. This risk is not represented by weak or occasional links (e.g., to Windows) as those are negligible in this community. [Data: Entities (1,2,3); Relationships (1-2, 1-3, 2-3)]"
}
]
}
{
"title": "Microsoft Ecosystem: Microsoft Corp., Windows, Office, Bill Gates",
"summary": "This community revolves around Microsoft Corporation, a global software giant. Its flagship products are Windows operating system and Office productivity suite, both foundational to personal and enterprise computing. Bill Gates, as co-founder, represents the company's origins and ongoing influence. The relationships show a strong internal coherence among these entities.",
"rating": 7.0,
"rating_explanation": "The impact severity is high because Microsoft's Windows and Office dominate global productivity software, and Bill Gates' philanthropic and technological legacy continues to shape the industry.",
"findings": [
{
"summary": "Microsoft Corporation as the central entity",
"explanation": "Microsoft Corp. directly owns and develops Windows and Office, and was co-founded by Bill Gates. This central role means that Microsoft's strategic decisions affect billions of users worldwide. The company's headquarters in Redmond (though not listed here) further anchors its operations. [Data: Entities (1); Relationships (1-2, 1-3, 1-4)]"
},
{
"summary": "Windows as the flagship operating system",
"explanation": "Windows is Microsoft's primary operating system, used on over a billion devices. It is tightly integrated with Office (e.g., default installation on many PCs) and represents Microsoft's historical dominance in the PC market. The relationship between Windows and Microsoft is direct and critical to the company's revenue. [Data: Entities (2); Relationships (2-1)]"
},
{
"summary": "Office as the productivity backbone",
"explanation": "Microsoft Office (including Word, Excel, PowerPoint) is a suite that has become the industry standard for document creation and data analysis. Its integration with Windows and the rise of Microsoft 365 (cloud subscription) shows ongoing evolution. The relationship between Office and Microsoft is strong and mutually reinforcing. [Data: Entities (3); Relationships (3-1)]"
},
{
"summary": "Bill Gates' enduring influence",
"explanation": "Bill Gates co-founded Microsoft and served as its CEO, shaping its early culture and product direction. Though he has stepped back from day-to-day operations, his legacy through the Bill & Melinda Gates Foundation and his continued role as a technology advisor keeps him associated with Microsoft's identity. [Data: Entities (4); Relationships (4-1)]"
},
{
"summary": "Cohesion and lack of cross-community contamination",
"explanation": "The Microsoft community is internally cohesive with no strong links to the Apple community (e.g., occasional mentions of Windows at Apple are ignored). This separation allows each ecosystem to be analyzed independently, highlighting their distinct product strategies and market positions. [Data: Relationships (implicit from input)]"
}
]
}
整体构建索引阶段就是分块->实体和关系抽取->将混乱一整个信息按照实体和关系整理->利用算法划分社区->并且对每个社区进行总结方便后续查找
查询阶段
查询阶段我放在了这一篇博客GraphRAG中各个Search的区别以及GraphRAG中settings.yaml文件详解,这里面会有详细的介绍。
上述就是关于GraphRAG的详细讲解,有什么问题欢迎评论区交流或者私信~
