微信扫码
添加专属顾问
我要投稿
自 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 os
from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader
from llama_index.core import StorageContext
from llama_index.graph_stores.nebula import NebulaGraphStore
os.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 kg
tags = ["entity"]# default, could be omit if create from an empty kg
graph_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 = 512
documents = 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 RetrieverQueryEngine
from llama_index.core.retrievers import KnowledgeGraphRAGRetriever
graph_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-03-05
低成本+高性能+超灵活!Deepseek 671B+Milvus重新定义知识库搭建
2025-03-05
LlamaIndex+Phi-3:知识图谱生成的黄金组合
2025-03-04
一键发布知识图谱:Obsidian 与 Quartz 的高效协作
2025-03-04
构建智能知识库 - 知识获取:Obsidian Web Clipper 的 AI 自动化流程
2025-03-02
大模型时代的知识工程:企业级智能知识库构建与增强指南
2025-03-02
从 0 到 3000 节点: 我用 DeepSeek + NebulaGraph 构建农业知识图谱
2025-02-26
将知识图谱与大模型 (LLM) 协同化:实现语义增强智能的途径
2025-02-23
DeepSeek+dify知识库,查询数据库的两种方式(api+直连)
2025-01-02
2024-07-17
2025-01-03
2024-08-13
2024-07-11
2024-06-24
2024-08-27
2024-07-13
2024-06-10
2024-07-12
2025-02-13
2025-01-14
2025-01-10
2025-01-06
2025-01-02
2024-12-16
2024-12-10
2024-12-04