揭秘GraphRAG:深入解析prompt每一步逻辑

张开发
2026/4/13 8:58:32 15 分钟阅读

分享文章

揭秘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 Apples 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 Apples 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 Apples product philosophy and innovation culture. Although he is no longer alive, his vision continues to influence Apples design and marketing strategies. The relationship between Jobs and Apple Inc. is historical but remains central to the companys identity and reputation. [Data: Entities (4); Relationships (4-1)]},{summary: Cupertino as the geographic anchor,explanation: Cupertino is the location of Apples headquarters (Apple Park), symbolizing the companys corporate identity and operational base. This geographic entity is directly tied to Apple Inc. and represents the regions economic dependence on Apples 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 Apples 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 companys 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 Microsofts 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 Microsofts strategic decisions affect billions of users worldwide. The companys 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 Microsofts primary operating system, used on over a billion devices. It is tightly integrated with Office (e.g., default installation on many PCs) and represents Microsofts historical dominance in the PC market. The relationship between Windows and Microsoft is direct and critical to the companys 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 Microsofts 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的详细讲解有什么问题欢迎评论区交流或者私信~

更多文章