AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


GraphRAG门槛高,试试更小、更快、更干净的 GraphRAG
发布日期:2024-08-22 12:14:10 浏览次数: 1865


微软开源的GraphRAG一直是被寄予厚望的下一代RAG技术,但是其使用门槛过高的问题也一直被人所诟病。

今天我们分享的开源项目,它是一个简化版实现的GraphRAG,可以让用户更简单并且低成本的使用GraphRAG的能力,它就是:nano-GraphRAG



GraphRAG



Graph RAG(Graph Retrieval-Augmented Generation**)**是一种基于图的知识检索增强技术,它结合了知识图谱的广泛知识表示能力和大语言模型(LLM)的生成能力。不同于使用纯文本片段的简单语义搜索方法。GraphRAG 流程包括从原始文本中提取知识图谱、构建社区层次结构、为这些社区生成摘要,然后在执行基于 RAG 的任务时利用这些结构。

微软在今年4月份的时候提出了GraphRAG的概念,发布了GraphRAG的论文《From Local to Global: A Graph RAG Approach to Query-Focused Summarization》。在这篇论文中,微软的研究人员提出了一种从文本构建并增强知识图的方法,来解决 Baseline RAG 系统在全局理解上的缺陷。

有兴趣的同学也可以直接访问对应的代码仓库和文档,自己动手试一试。

  • 项目仓库:https://github.com/microsoft/graphrag

  • 项目文档:microsoft.github.io/graphrag/





nano-GraphRAG 是什么



nano-GraphRAG是一款简洁且易于定制的 GraphRAG 实现。微软开源的GraphRAG 在功能上确实非常强大,但是官方版本中对于阅读与定制修改都非常的不友好。nano-GraphRAG项目的目的就是为您呈现了一个更为精简、高效、清晰的 GraphRAG 版本,同时保留了其核心特性。如何不考虑测试的代码,那么nano-GraphRAG 的代码量大约只有 800行。并且它短小精悍,易于扩展,支持异步操作,且完全采用类型注解。

 




安装nano-GraphRAG



安装nano-GraphRAG的方式非常简单,你可以选择从pip来安装或者自己从原来来安装。

从 PIP 安装

pip install nano-graphrag

从源代码安装

# clone this repo firstcd nano-graphragpip install -e .

 




上手使用



配置LLM

首先需要在你的环境中配置LLM,在nano-graphrag中,我们需要两种类型的 LLM,一种是优秀的,一种是便宜的。前者用于规划和响应,后者用于总结。默认情况下,优秀的是gpt-4o,便宜的是gpt-4o-mini

如果使用默认OpenAI,这需要设置penAI API 密钥:export OPENAI_API_KEY="sk-..."

您可以实现自己的LLM函数(参考_llm.gpt_4o_complete):


async def my_llm_complete(prompt, system_prompt=None, history_messages=[], **kwargs) -> str:# pop cache KV database if anyhashing_kv: BaseKVStorage = kwargs.pop("hashing_kv", None)# the rest kwargs are for calling LLM, for example, `max_tokens=xxx`...# YOUR LLM callingresponse = await call_your_LLM(messages, **kwargs)  return response
将默认值替换为:

# Adjust the max token size or the max async requests if neededGraphRAG(best_model_func=my_llm_complete, best_model_max_token_size=..., best_model_max_async=...)GraphRAG(cheap_model_func=my_llm_complete, cheap_model_max_token_size=..., cheap_model_max_async=...)
下载资源物料

这里下载下载查尔斯·狄更斯的《圣诞颂歌》


curl <https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt> > ./book.txt
通过Python来使用nano-GraphRAG

from nano_graphrag import GraphRAG, QueryParamgraph_func = GraphRAG(working_dir="./dickens")
with open("./book.txt") as f:graph_func.insert(f.read())
# Perform global graphrag searchprint(graph_func.query("What are the top themes in this story?"))
# Perform local graphrag search (I think is better and more scalable one)print(graph_func.query("What are the top themes in this story?", param=QueryParam(mode="local")))
增量更新

nano-graphrag支持增量插入,不会添加重复的计算或数据。nano-graphrag使用内容的 md5-hash 作为密钥,因此没有重复的块。但是,每次插入时,图的社区都会重新计算,并且社区报告也会重新生成

with open("./book.txt") as f:book = f.read()half_len = len(book) // 2graph_func.insert(book[:half_len])graph_func.insert(book[half_len:])

异步

对于每个方法NAME(...),都有一个对应的异步方法aNAME(...)

await graph_func.ainsert(...)await graph_func.aquery(...)...

可用参数

GraphRAGQueryParamdataclassPython 中。使用help(GraphRAG)help(QueryParam)查看所有可用参数! 




自定义扩展



在使用中给大家看了可以通过自定义LLM来,使用非OpenAI的LLM,这里除了LLM以外,nano-GraphRAG支持的扩展还包括:

  • Prompt:nano-graphrag使用来自 dict 对象的提示nano_graphrag.prompt.PROMPTS。你可以使用它并替换里面的任何提示。

  • Embedding:您可以用任何实例替换默认嵌入函数_utils.EmbedddingFunc

  • 存储:你可以将所有存储相关的组件替换为你自己的实现,nano-graphrag主要使用三种存储:base.BaseKVStorage用于存储 key-json 对数据,base.BaseVectorStorage用于索引嵌入,base.BaseGraphStorage用于存储知识图谱。 




    Benchmark



     

首先我们先说明对比的基础条件,我们使用三国演义来作为benchmark的基准语料库,GraphRAG和nano-graphrag都使用OpenAI的Embedding和gpt-4o,并且都不是不使用缓存,两者在同一设备和网络连接上。

Index对比 

相对都比较慢,都超过了 10 分钟。

Local Search对比


问题:大乔和曹操的关系

GraphRAG

nano-graphrag

Global Search对比

问题:"大乔与曹操的关系”

GraphRAG

nano-graph





总结



nano-graphrag的尝试对于行业来说是非常有益的,GraphRAG的能力是非常不错的,因此如果能在GraphRAG的基础上实现出更加轻量级、更加易于上手和入门的产品,那对于有RAG需要的用户来说,必然是非常大的福音



53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询