Nano-graphrag: 輕量級、靈活的 GraphRAG 實現
微軟提出的GraphRAG 很有效,但是官方實現和使用都很復雜,不易修改和定制。近日,網絡上出現一個國人開發的更簡潔、易用且高度可定制的版本實現——nano-graphrag,它保留了核心功能,同時提供了更友好的用戶體驗。值得一提的是之前介紹的fast-graphrag也受到了該項目的啟發。
nano-graphrag 核心特點是其簡潔性、易用性和可定制性。代碼量僅為 1100 行(不包括測試和提示),是官方實現的緊湊高效替代品。它設計為輕量級、異步和完全類型化,是希望將 GraphRAG 集成到項目中而不增加復雜性的開發者的理想選擇。
安裝 nano-graphrag 非常簡單,可以直接從 PyPi 安裝,或從源代碼安裝以獲取最新功能。安裝后,您可以通過設置 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 代碼片段展示了圖的構建以及如何將文本插入圖中并執行全局和局部 GraphRAG 搜索。nano-graphrag 的一個關鍵特性是支持批量插入和增量插入,這使得在知識圖中進行高效更新成為可能,而無需冗余計算。它還支持簡單的 RAG 操作,以應對更簡單的用例。
對于高級用戶,nano-graphrag 提供了一系列自定義選項。您可以替換默認組件,例如 LLM 函數、嵌入函數和存儲組件。這種靈活性使您能夠根據特定需求定制系統,并將其無縫集成到您的項目中。
nano-graphrag 非常適合對 GraphRAG 感興趣的人學習和使用,它的簡潔性、易用性和可定制性使其成為開發人員選擇GraphRAG的平替選擇。
贊
收藏
回復
分享
微博
QQ
微信
舉報

回復
相關推薦