微信扫码
添加专属顾问
 
                        我要投稿
自 2023 年 8 月 NebulaGraph 和 LlamaIndex 共同推出 Graph RAG 以来,凭借着 LLM(大语言模型)和 RAG(检索增强生成)的发展势头,在业界掀起了一场革命。通过本文,我们将向你介绍什么是 Graph RAG,为什么它具有革命性,以及如何构建你的Graph RAG,来利用数据的上下文与 NebulaGraph 回答复杂的多部分问题。
微软在由高级首席数据架构师 Jonathan Larson 和首席项目经理 Steven Truitt 撰写的博客文章《GraphRAG: Unlocking LLM Discovery on Narrative Private Data》中强调了 Graph RAG 的重要性。他们通过将 Graph RAG 与基线 RAG 系统进行对比,展示了 Graph RAG 的有效性,得出结论认为 Graph RAG 方法使LLM能够在图谱中找到支点,从而提供包含原始支持文本的更优答案。而基线 RAG 在处理需要跨数据集汇总信息以形成答案的查询时则显得力不从心。
我们在上游发布之前已经探索了许多内部方法。我们相信 LlamaIndex 中Graph + RAG 的基本概念值得探索,因此我们将原始的工作坊和笔记本放在参考章节中。
设置你的 NebulaGraph 集群
通过 Google Colab 中的 NebulaGraph Lite 启动 NebulaGraph 集群
使用 Docker 扩展部署集群
实例化一个 NebulaGraphStore
import osfrom llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReaderfrom llama_index.core import StorageContextfrom llama_index.graph_stores.nebula import NebulaGraphStoreos.environ["NEBULA_USER"] = "root"os.environ["NEBULA_PASSWORD"] = "nebula"os.environ["NEBULA_ADDRESS"] = "127.0.0.1:9669"space_name = "paul_graham_essay"edge_types, rel_prop_names = ["relationship"], ["relationship"]# default, could be omit if create from an empty kgtags = ["entity"]# default, could be omit if create from an empty kggraph_store = NebulaGraphStore(space_name=space_name,edge_types=edge_types,rel_prop_names=rel_prop_names,tags=tags)storage_context = StorageContext.from_defaults(graph_store=graph_store)
依赖关系
Graph RAG 的实现需要以下依赖项:
pip install llama-indexpip install llama-index-llms-ollamapip install llama-index-graph-stores-nebulapip install ipython-ngqlpip install llama-index-embeddings-huggingface
KnowledgeGraphIndex 可从非结构化文本中自动构建知识图谱,也可基于实体的查询。在创建图 RAG 查询引擎之前,你需要设置 LlamaIndex 知识图谱索引。在这个示例中,我们将演示如何通过创建文档对象来从文本数据构建知识图谱,然后再创建索引。
from llama_index.core import (VectorStoreIndex,SimpleDirectoryReader,KnowledgeGraphIndex,)graph_store.query("SHOW HOSTS")Settings.chunk_size = 512documents = SimpleDirectoryReader("data").load_data()
kg_index = KnowledgeGraphIndex.from_documents(documents,storage_context=storage_context,max_triplets_per_chunk=10,space_name=space_name,edge_types=edge_types,rel_prop_names=rel_prop_names,tags=tags,max_knowledge_sequence=15,)
创建简单的图 RAG 检索器
最后一步是创建一个图 RAG 检索器,在此基础上将构建查询引擎,以便你可以使用自然语言问题进行查询。
from llama_index.core.query_engine import RetrieverQueryEnginefrom llama_index.core.retrievers import KnowledgeGraphRAGRetrievergraph_rag_retriever = KnowledgeGraphRAGRetriever(storage_context=storage_context,verbose=True,)query_engine = RetrieverQueryEngine.from_args(graph_rag_retriever,)response = query_engine.query("What's the relationship between Bob and Alice")
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-30
一篇文章讲清楚:Ontology为什么是企业落地Agent的关键
2025-10-21
DeepSeek V3.2 AI辅助-构建可视化多维知识立方体展示知识体系
2025-10-19
文档级知识图谱: RAKG(95.91%) VS GraphRAG(89.71%)
2025-10-13
用 AI 重塑阅读体验,将任何书籍转化为可交互的知识图谱
2025-09-29
Spring AI Alibaba Graph升级至1.0.0.4,流式输出演进说明
2025-09-20
AI赋能—大模型搭建知识库
2025-09-17
怎么使用Graph Maker 将文本转换为知识图谱
2025-09-03
向量检索快比LLM还贵?不支持S3的向量数据库,迟早要淘汰!
 
            2025-09-02
2025-08-28
2025-08-26
2025-08-24
2025-08-10
2025-08-30
2025-08-28
2025-08-25
2025-08-18
2025-09-17