自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 的实现需要以下依赖项:
pipinstallllama-indexpipinstallllama-index-llms-ollamapipinstallllama-index-graph-stores-nebulapipinstallipython-ngqlpipinstallllama-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'stherelationshipbetweenBobandAlice")
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |