自 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-index
pip install llama-index-llms-ollama
pip install llama-index-graph-stores-nebula
pip install ipython-ngql
pip 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")
复制
最后欢迎大家能加入到我们的 RAG 讨论,并享你的 Graph RAG 探索情况。
参考:
https://bit.ly/graph-rag-workshop
https://siwei.io/graph-enabled-llama-index/kg_and_vector_RAG.html
https://siwei.io/demo-dumps/graph-rag/GraphRAG.html
NebulaGraph 用户案例集
案例推荐:
知识图谱案例:
苏宁基于 NebulaGraph 构建知识图谱的大规模告警收敛和根因定位实践
金融风控案例:
图数据库 Nebula Graph 在 BOSS 直聘的应用
360数科:基于 NebulaGraph 打造智能化的金融反欺诈系统
NebulaGraph 助力金蝶征信产业图谱深挖企业关系链,实现银行批量获客
智能运维案例:
58 同城基于 NebulaGraph 一键部署运维架构的实践
苏宁基于 NebulaGraph 构建知识图谱的大规模告警收敛和根因定位实践
大数据/图平台:
OPPO:通过 NebulaGraph 建设全局图数据库平台
数据治理:
微众银行:利用 NebulaGraph 进行全局数据血缘治理的实践
安全:
对图数据库 NebulaGraph 感兴趣?欢迎前往 GitHub ✨ 查看源码:https://github.com/vesoft-inc/nebula