微信扫码
添加专属顾问
微软正式开源GraphRAG后重磅 - 微软官宣正式在GitHub开源GraphRAG,引起了业界热烈的讨论,公众号文章一天阅读量达到一万以上,后续也分享了进一步的解析开源GraphRAG解读:微软的人工智能驱动知识发现方法 和 揭示微软开源的RAG策略:GraphRAG。今天摘取分享实践介绍,共同学习。我们也在医药和工业领域利用GraphRAG落地,解决通用RAG缺乏行业上下文语义理解、精准问答和溯源等问题,期待一起交流合作。
摘要:
尝试使用Microsoft的Graph RAG – baeke.info
本文讨论了Microsoft的GraphRAG方法,将其与基准RAG进行对比,以实现更有效的面向查询的总结,利用知识图谱和社区摘要来回答全局问题。
要点:
- Graph RAG通过知识图和社区摘要增强全局查询响应能力。
- Graph RAG的处理过程涉及实体提取、图形创建和社区摘要生成。
- 基准RAG在缺乏数据集描述性主题的全局问题上遇到困难。
- 该实现包括基于Python的工具进行索引和查询。
- Graph RAG涉及更高的计算成本,但提供细致的查询响应。
- 优化结果关键在于关键提示的修改。
- 该系统支持本地和全局搜索功能。
来源:
https://blog.baeke.info/2024/07/07/trying-out-graph-rag/
正文:
当我们在诸如OpenAI的gpt-4o等LLM之上构建应用程序时,我们经常使用RAG模式。RAG代表检索增强生成。您可以使用它让LLM回答有关它从未见过的数据的问题。为了回答问题,您检索相关信息并将其交给LLM以生成答案。
下面的图示从高层次描绘了数据摄取和查询部分,使用gtp-4和Azure中的向量数据库,Azure AI搜索。
以上,我们的文档被切分并进行了向量化。这些向量存储在 Azure AI Search 中。向量使我们能够找到与用户查询相似的文本片段。当用户输入问题时,我们将问题进行向量化,找到相似的向量,然后将前 n 个匹配项交给 LLM 处理。找到的文本片段会与原始问题一起放在提示中。查看此页面以了解更多关于向量的信息。
请注意,以上是其最简单形式下的基本情况。您可以在索引和检索阶段以多种方式优化此过程。查看 YouTube 上的“RAG From Scratch”系列以深入了解更多信息。
https://youtube.com/playlist?list=PLfaIDFEXuae2LXbO1_PKyVJiQ23ZztA0x&si=ZQdvbm-_9mUYWq8D
尽管您可以用基本的 RAG 取得一定进展,但对于整个数据集的全局问题,它并不十分擅长。如果您问“数据集中的主题是什么?”除非数据集本身描述了主题,否则要找到与问题相关的文本块将会很困难。实质上,这是一个以查询为焦点的总结任务与一个显式检索任务之间的对比。
在这篇论文中重磅-微软发表GraphRAG论文并即将开源项目,从本地到全局:一种基于GraphRAG 方法的以查询为焦点的总结,微软提出了一种基于知识图谱和中间社区摘要的解决方案,以更有效地回答这些全局问题。
如果您不太清楚基本 RAG 和GraphRAG 之间的区别,可以观看这段 YouTube 上的视频,在该视频中更详细地解释了它们的区别。
创建索引开始
微软具有基于Python的开源Graph RAG实现,可用于本地和全局查询。我们稍后会讨论本地查询,并暂时专注于全局查询。请查看GitHub存储库以获取更多信息。https://github.com/microsoft/graphrag
如果您的本地计算机上有Python,则很容易尝试:
在其中创建一个文件夹并在其中创建一个Python虚拟环境
确保Python环境是活动的,并运行pip install graphrag
在文件夹中创建一个名为“input”的文件夹,并在其中放入一些带有您内容的文本文件
从包含输入文件夹的文件夹中运行以下命令:python -m graphrag.index --init --root .
这会创建一个.env文件和一个settings.yaml文件。
在.env文件中输入您的OpenAI密钥。这也可以是Azure OpenAI密钥。Azure OpenAI在settings.yaml文件中需要额外的设置:api_base,api_version,deployment_name。
我直接使用了OpenAI并在settings.yaml文件中修改了模型。找到“model”设置,并将其设置为gpt-4o。
你现在可以开始运行索引管道。在运行之前,请注意,根据您放入索引文件夹中的数据量,这将会产生大量的LLM调用。在我的测试中,使用800KB的文本数据,索引的成本在10到15欧元之间。以下是命令:
1 | python -m graphrag.index --root . |
展示发生的情况,请看下面的图表:
上面,让我们从上到下看一下,暂时不包括用户查询部分:
输入文件夹中的源文档被分割成300个令牌的片段,重叠100个令牌。微软使用cl100k_base令牌化程序,这是gpt-4使用的程序,而不是gpt-4o。这不应该产生影响。您可以调整令牌大小和重叠。使用更大的令牌大小,在后续步骤中进行的LLM调用更少,但元素提取可能不够精确。
在gpt-4o的帮助下,从每个块中提取元素。这些元素是正在构建的图中的实体和实体之间的关系。此外,有关这些实体的声明也被提取。上述文献和图表使用了术语协变量。如果输入文件夹中有大量数据,则这是一项昂贵的操作。
生成了元素的文本描述。
在这些步骤之后,建立了一个图,其中包含 gpt-4o 能够找到的所有实体、关系、声明和元素描述。但是流程并不止步于此。为了支持全球查询,会发生以下情况:
在图中检测社区。社区是密切相关实体的群体。使用Leiden算法进行检测。在我的小数据集中,大约检测到250个社区。
为每个社区创建使用gpt-4o并存储的社区摘要。这些摘要以后可以用于全局查询。
为了使以上所有工作正常运行,必须进行大量的LLM调用。使用的提示可以在prompts文件夹中找到:
您可以且可能应该修改这些提示以匹配您文档的领域。实体提取提示包含示例,以教示大语言模型应该提取的实体。默认情况下,会检测到人物、地点、组织等实体。但如果您主要处理建筑项目、建筑物、桥梁、建筑材料等内容,那么提示应相应进行调整。答案的质量将在很大程度上取决于这些调整。
除了图表之外,解决方案还使用开源项目 LanceDB 来存储每个文本块的嵌入。数据库中只有一个表,包含四个字段:
id: unique id for the chunk
text: the text in the chunk
vector: the vector of the chunk; by default the text-embedding-3-small model is used
attributes: e.g., {“title”: “\”title here\””}
图表和相关数据存储在另一个带有时间戳的文件夹中的 artifacts 文件夹中的 Parquet 文件中。例如:
包含图结构的Parquet文件
如果您有一个镶木地板查看器,您可以检查create_final_entities.parquet文件以查看检测到的实体。您将找到诸如ORGANIZATION、PERSON、GEO、EVENT、CONCEPT等的实体类型... 每个实体都有一个描述并链接回文本单元的标识符。文本单元是块。
请注意,如果您希望以GraphML格式获得图谱,请在settings.yaml中将snapshots.graphml设置为true。然后,您的工件文件夹将包含.graphml文件。您可以在类似Gephi的查看器中加载这些文件:
如果您在不设置此值的情况下已经运行了索引器,您可以再次运行它。Graph RAG具有一个缓存层,因此当您再次运行索引器以生成.graphml文件时,您不会产生费用。
现在让我们进行全局查询。全局查询使用生成的社区摘要来回答问题。中间答案用于生成最终答案。
全局查询不仅仅是一个LLM调用,而是几个调用。总令牌成本相对较高,与使用3到5个检索到的块的典型相似度搜索相比。看到有200K令牌的> 10个LLM调用并不罕见。您可以在settings.yaml中控制令牌使用情况。请参阅底部的global_search设置。
要进行全局查询,请运行下面的命令。我在语料库中有一本《1984》的书,所以让我们询问主题:
1 2 3 4 | python -m graphrag.query \ --root . \ --method global \ "What are the top themes in 1984?" |
请注意,由于它知道关于1984年的情况,LLM可以独自回答这个问题。但这并不是重点。您得到的答案将来自索引:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ### Top Themes in *1984*
**1. Totalitarianism and Absolute Control** The Party in Oceania exercises absolute control over its citizens, manipulating historical records, maintaining a state of perpetual conflict, and enforcing oppressive policies through purges, arrests, tortures, imprisonments, and vaporizations [Data: Reports (211)]. The regime's control extends to every aspect of life, including personal relationships, sexual behavior, and even thoughts, enforced by the Thought Police and the omnipresent Telescreen [Data: Reports (141, 183, 223, 185, 191, 181, 151, 116, 186)]. **2. Surveillance and Psychological Manipulation** Surveillance is a pervasive theme, with the Telescreen and the Thought Police constantly monitoring individuals to ensure compliance and suppress dissent [Data: Reports (52, 141, 183, 223, 185, 191, 181, 151, 116, 186)]. The Party uses psychological manipulation techniques such as doublethink, Newspeak, and the alteration of historical records to control and limit the range of thought among its citizens [Data: Reports (141, 175, 183, 181, 215, 149, 179, 177, 178, 165, 58)]. **3. Manipulation of Truth and Reality** The Party manipulates historical records and information to maintain its narrative and control over reality. This is evident in Winston's work at the Ministry of Truth and the concept of doublethink, which allows individuals to accept two contradictory beliefs simultaneously [Data: Reports (141, 175, 183, 181, 215, 149, 179, 177, 178, 165, 58)]. The Memory Hole is used to destroy documents, ensuring that information is permanently erased, highlighting the Party's control over historical narratives [Data: Reports (52)]. **4. Continuous Warfare** Oceania is engaged in a perpetual state of war with Eastasia and Eurasia. This continuous warfare serves to maintain the Party's control over the population and justify its oppressive policies [Data: Reports (211, 178, 214)]. The war consumes surplus goods, maintains a hierarchical structure, and impacts the mental states of individuals, ensuring that the populace remains divided and distracted [Data: Reports (211, 178, 214, 208)]. **5. Rebellion and Resistance** Despite the oppressive regime, themes of rebellion and resistance are present, as seen through characters like Winston and Julia. Winston's clandestine activities, such as his relationship with Julia and his hope in the Proles, reflect his desire to rebel against the Party's oppressive regime [Data: Reports (141, 183, 151, 182, 101, 186)]. The Brotherhood represents an underground network aiming to overthrow the state of Oceania, highlighting the existence of resistance within the oppressive regime [Data: Reports (211, 64, 152)]. **6. Class Struggle and Social Hierarchy** The distinction between the Inner Party, Outer Party, and Proles highlights the rigid social hierarchy within Oceania. The Party's control over the Proles and the limited privileges of the Outer Party members emphasize the theme of class struggle [Data: Reports (141, 66, 185, 211, 182, 124)]. The Proles, despite their subjugation, are seen as a potential source of hope and rebellion [Data: Reports (182, 64)]. **7. Language as a Tool of Control** Newspeak, a language created by The Party, is designed to limit the range of thought and eliminate any possibility of rebellious thoughts. By controlling language, The Party effectively controls thought, making it impossible for individuals to conceptualize or express subversive ideas [Data: Reports (177, 179, 181, 149, 114)]. **8. Fear and Psychological Torture** The Party uses fear and psychological torture to maintain control, exemplified by the use of Room 101, where individuals are subjected to their worst fears. Winston's torture with the cage containing rats demonstrates the regime's ability to break individuals' spirits [Data: Reports (169, 145, 224, 220)].
These themes collectively illustrate the oppressive nature of the regime in *1984* and its impact on personal freedom, truth, and reality. The Party's extensive use of surveillance, psychological manipulation, and control over information ensures its dominance and the suppression of any potential dissent. |
Local Search本地搜索
本地搜索通过将知识图谱中的数据与原始文档的文本块结合起来生成答案。这种方法适用于需要理解文档中特定实体的问题(例如,Winston与谁互动了?)。以下笔记本包含有关使用Python进行本地搜索的更多信息https://github.com/microsoft/graphrag/blob/a22003c302bf4ffeefec76a09533acaf114ae7bb/examples_notebooks/local_search.ipynb#L43
要从CLI执行本地搜索,请使用以下命令:
1 2 3 4 | python -m graphrag.query \ --root . \ --method local \ "Who is Winston? Who does he interact with and why?" |
本地搜索使用相似性搜索在图中查找实体作为入口点。一旦找到这些入口点,就会使用图来找到进一步感兴趣的实体。本地搜索需要LanceDB嵌入存储,而全局搜索则不需要。上述问题的回答:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ### Who is Winston?
Winston is a complex character from George Orwell's novel "Nineteen Eighty-Four." He is a member of the Outer Party and works at the Ministry of Truth, where his job involves altering historical records to fit the Party's propaganda. Winston is deeply resentful of the Party and Big Brother, harboring a sense of intellectual inferiority and mental domination by figures like O'Brien. His life is marked by intense fear, paranoia, and physical and emotional suffering, particularly during his interactions with O'Brien, who tortures and interrogates him [Data: Entities (8, 3)].
Winston's reflections on reality, power, and the nature of suffering are central to his character. He contemplates the symbolic meaning of chess, the principles of Ingsoc, and the dominion of The Party. His dreams and memories, including the tragic death of his mother and sister, the bells of St. Clement's, and his early childhood, provide a poignant backdrop to his internal struggles [Data: Entities (8)].
### Interactions and Relationships
#### Julia Winston's most significant relationship is with Julia, a fellow Party member. Despite the risks, they engage in a love affair, renting a room from Mr. Charrington for their secret meetings. Julia represents a source of intimacy and rebellion for Winston, as they navigate their dangerous liaison under the watchful eyes of The Party [Data: Entities (8)].
#### O'Brien O'Brien is another crucial figure in Winston's life. Initially, Winston feels a sense of connection and admiration towards O'Brien, hoping that his political orthodoxy is not perfect. However, O'Brien ultimately becomes his torturer, subjecting Winston to severe psychological and physical pain. Despite this, Winston experiences moments of connection and even a peculiar intimacy with O'Brien [Data: Entities (8)].
#### Mr. Charrington Mr. Charrington is the shop owner who rents a room to Winston and Julia for their secret meetings. Initially, he appears discreet and non-judgmental, but later reveals a more authoritative and alert persona, indicating his role in the Party's surveillance [Data: Entities (317)].
#### Other Characters Winston also interacts with various other characters, such as Syme, Parsons, and the old man in the pub. These interactions reveal his curiosity about the past and the changes brought about by The Party. For instance, Syme is a colleague who discusses the principles of Newspeak with Winston, while Parsons is a fellow employee at the Ministry of Truth [Data: Entities (8, 83)].
### Conclusion
Winston is a deeply reflective and observant character, constantly grappling with the oppressive nature of The Party and his own internal conflicts. His interactions with Julia, O'Brien, Mr. Charrington, and others provide a multifaceted view of his struggles and the dystopian world he inhabits. Through these relationships, Winston's character is fleshed out, revealing the complexities of life under totalitarian rule. |
请注意,输出中包含对找到的实体的引用。例如,关于查林顿先生的部分指定实体317。在Gephi数据实验室中,我们可以使用human_readable_id轻松找到该实体:
在构建应用程序时,用户界面可以提供到实体的链接,以供进一步检查。
检索增强生成(RAG)已成为增强语言模型回答特定数据集问题能力的强大技术。虽然基准RAG在通过检索相关文本块回答特定查询方面表现出色,但在需要全面了解整个数据集的全局问题上却难以应对。为解决这一局限,微软推出了GraphRAG,一种创新方法,利用知识图谱和社区摘要来更有效地回答全局查询。
GraphRAG的索引过程涉及文档分块、提取实体和关系、构建图谱以及生成社区摘要。这种方法能够更细致和具有上下文意识地回应本地和全局查询。虽然GraphRAG在处理复杂的整个数据集问题方面具有显著优势,但需要注意的是,它带来了更高的计算成本,并需要仔细的提示工程来实现最佳结果。随着人工智能领域的不断发展,像GraphRAG这样的技术代表着迈向更全面和有洞察力的信息检索和生成系统的重要一步。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-07-01
提升 RAG 准确率全攻略 让你的 AI 知识库 真正靠谱起来!
2026-06-30
教程:如何用AutoRAG + Milvus避免RAG 与Agent 中出现串租问题
2026-06-30
知识库不是文件堆——我把RAG准确率从60%调到了92%
2026-06-30
本体论语义建设新思路,另类RAG来解决检索问题
2026-06-30
别把RAG当架构:Ontology(本体)才是Agent的业务世界
2026-06-29
PixelRAG:伯克利团队颠覆传统 RAG,用截图代替文本检索! 28 天狂揽 3000+ Star!
2026-06-29
腾讯WeKnora开源详解(三):检索引擎与生态集成
2026-06-29
腾讯开源WeKnora详解(二):知识库与对话核心能力
2026-04-06
2026-04-27
2026-04-23
2026-04-20
2026-04-09
2026-04-12
2026-04-22
2026-04-10
2026-05-14
2026-04-30
2026-06-23
2026-06-23
2026-06-15
2026-06-10
2026-06-10
2026-05-20
2026-05-18
2026-05-11
欢迎您使用【53AI 官方网站】(以下简称“本网站”或“我们”)。本《会员服务协议》(以下简称“本协议”)是您(以下简称“会员”或“用户”)与【深圳市博思协创网络科技有限公司】之间关于注册、登录及使用本网站会员服务所订立的法律协议。
在您注册或登录前,请务必审慎阅读、充分理解各条款内容,特别是免除或限制责任的条款、知识产权条款、争议解决条款等。此类条款将以加粗形式提示您注意。 当您通过微信公众号授权、手机验证码验证或其他方式成功登录本网站时,即视为您已完全理解并同意接受本协议的全部内容。
一、 定义
本网站:指由【深圳市博思协创网络科技有限公司】运营的,域名为【53ai.com】的网站及相关移动端页面。
会员服务:指本网站向注册会员提供的知识库文章查阅、内容检索及其他相关增值服务。
知识库内容:指本网站发布的包括但不限于文字、图表、数据、研究报告、行业分析等数字化内容资源。
二、 账号注册与登录
登录方式:本网站支持以下登录方式,您可根据实际情况选择:
微信公众号授权登录:您同意将您的微信OpenID信息授权给本网站,用于创建或关联会员账号。
手机验证码登录:您需提供真实有效的手机号码,并通过短信验证码完成身份验证与登录/注册。
账号安全:您的账号仅限您本人使用,禁止赠与、借用、租用、转让或售卖。因您保管不善导致的账号被盗、密码泄露等损失,由您自行承担。
实名认证:根据相关法律法规要求,我们可能要求您在特定功能下完成实名认证。如您拒绝提供,可能无法使用部分或全部服务。
未成年人保护:若您未满18周岁,请在法定监护人的陪同下阅读本协议,并在征得监护人同意后使用本服务。
三、 服务内容与规范
知识库查阅权限:会员登录后,有权按照其会员等级对应的权限范围,在线浏览、检索本网站知识库中的相关文章及内容。
服务变更:我们有权根据业务发展需要,调整、变更或终止部分服务内容,并将以网站公告、公众号消息等方式提前通知。
禁止行为:您在使用服务时不得实施以下行为:
利用技术手段批量爬取、下载、转存知识库内容;
将知识库内容用于商业目的或未经授权地向第三方传播;
干扰本网站正常运行或侵犯其他用户合法权益;
发布违法违规信息或从事违反公序良俗的活动。
四、 知识产权声明
权利归属:本网站知识库中的排版设计、软件代码等内容的知识产权均归【公司全称】或原权利人所有,受《中华人民共和国著作权法》等法律保护。
有限许可:本网站授予会员一项非独占、不可转让、不可转授权的普通许可,仅限于个人学习、研究之目的在线查阅知识库内容。
侵权追责:未经书面许可,任何单位或个人不得以任何形式复制、转载、摘编、镜像、汇编或以其他方式使用上述内容。一经发现,我们保留追究其法律责任的权利。
五、 个人信息保护
我们重视对您个人信息的保护。关于我们如何收集、使用、存储和保护您的个人信息,请单独阅读 《隐私政策》。
您通过微信公众号授权或手机号验证所提供的信息,我们将严格按照《个人信息保护法》的规定处理,仅用于身份识别、服务提供及安全验证等必要用途。
您可以随时通过网站设置或联系客服行使查阅、更正、删除个人信息及撤回授权同意的权利。
六、 免责声明
内容准确性:知识库内容仅供参考,不构成专业建议。我们不对其完整性、准确性、时效性作任何明示或暗示的保证,您应自行判断并承担使用风险。
不可抗力:因自然灾害、政策法规变化、网络故障、第三方平台接口异常(如微信接口维护、运营商短信通道故障)等不可抗力导致的服务中断或延迟,我们不承担违约责任。
第三方链接:本网站可能包含指向第三方网站的链接,该等网站的内容和服务不受我们控制,请您自行甄别风险。
七、 违约责任
如您违反本协议约定,我们有权视情节采取警告、限制功能、暂停服务、注销账号等措施,并保留要求赔偿损失的权利。
如因您的违约行为导致我们遭受行政处罚、第三方索赔或商誉损失,您应承担全部赔偿责任(包括但不限于罚款、赔偿金、律师费、公证费等)。
八、 法律适用与争议解决
本协议的订立、执行和解释均适用中华人民共和国大陆地区法律。
因本协议产生的或与本协议有关的任何争议,双方应友好协商解决;协商不成的,任何一方均可向【公司所在地】有管辖权的人民法院提起诉讼。
九、 其他
本协议构成双方就本服务达成的完整协议,取代此前任何口头或书面约定。
本协议任一条款被认定为无效或不可执行的,不影响其他条款的效力。
我们对本协议享有最终解释权,并在法律允许的范围内保留随时修改的权利。修改后的协议一经公布即生效,继续使用服务即视为同意修订内容。