Nano-graphrag: 輕量級(jí)、靈活的 GraphRAG 實(shí)現(xiàn)
微軟提出的GraphRAG 很有效,但是官方實(shí)現(xiàn)和使用都很復(fù)雜,不易修改和定制。近日,網(wǎng)絡(luò)上出現(xiàn)一個(gè)國(guó)人開發(fā)的更簡(jiǎn)潔、易用且高度可定制的版本實(shí)現(xiàn)——nano-graphrag,它保留了核心功能,同時(shí)提供了更友好的用戶體驗(yàn)。值得一提的是之前介紹的fast-graphrag也受到了該項(xiàng)目的啟發(fā)。
nano-graphrag 核心特點(diǎn)是其簡(jiǎn)潔性、易用性和可定制性。代碼量?jī)H為 1100 行(不包括測(cè)試和提示),是官方實(shí)現(xiàn)的緊湊高效替代品。它設(shè)計(jì)為輕量級(jí)、異步和完全類型化,是希望將 GraphRAG 集成到項(xiàng)目中而不增加復(fù)雜性的開發(fā)者的理想選擇。
安裝 nano-graphrag 非常簡(jiǎn)單,可以直接從 PyPi 安裝,或從源代碼安裝以獲取最新功能。安裝后,您可以通過設(shè)置 OpenAI API 密鑰并下載文本文件開始工作。
from nano_graphrag import GraphRAG, QueryParam
graph_func = GraphRAG(working_dir="./dickens")
with open("./book.txt") as f:
graph_func.insert(f.read())
# Perform global graphrag search
print(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")))
# Batch Insert
graph_func.insert(["TEXT1", "TEXT2",...])
# Incremental Insert
with open("./book.txt") as f:
book = f.read()
half_len = len(book) // 2
graph_func.insert(book[:half_len])
graph_func.insert(book[half_len:])
上面提供的 Python 代碼片段展示了圖的構(gòu)建以及如何將文本插入圖中并執(zhí)行全局和局部 GraphRAG 搜索。nano-graphrag 的一個(gè)關(guān)鍵特性是支持批量插入和增量插入,這使得在知識(shí)圖中進(jìn)行高效更新成為可能,而無需冗余計(jì)算。它還支持簡(jiǎn)單的 RAG 操作,以應(yīng)對(duì)更簡(jiǎn)單的用例。
對(duì)于高級(jí)用戶,nano-graphrag 提供了一系列自定義選項(xiàng)。您可以替換默認(rèn)組件,例如 LLM 函數(shù)、嵌入函數(shù)和存儲(chǔ)組件。這種靈活性使您能夠根據(jù)特定需求定制系統(tǒng),并將其無縫集成到您的項(xiàng)目中。
nano-graphrag 非常適合對(duì) GraphRAG 感興趣的人學(xué)習(xí)和使用,它的簡(jiǎn)潔性、易用性和可定制性使其成為開發(fā)人員選擇GraphRAG的平替選擇。
