ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">检索增强生成 (RAG) 方法已迅速成为构建将大型语言模型 ( LLMs ) 与外部数据的优势相结合的智能系统的重要工具。该领域的最新突破之一是LightRAG——一种先进、经济高效的 RAG 框架,利用知识图和基于向量的检索来改进文档交互。在本文中,我们将深入探讨LightRAG、它与GraphRAG等方法的比较,以及如何在您的计算机上设置它。 ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin-top: calc(2.33333em);margin-bottom: calc(1.16667em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">什么是 LightRAG?
ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">LightRAG 是一个简化的 RAG 框架,旨在通过使用知识图和嵌入来检索相关知识块来生成响应。传统的 RAG 系统通常将文档分解为独立的块,但 LightRAG 更进一步 — 它构建了连接文本中各个概念的实体关系对。
ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">如果您听说过 Microsoft 的GraphRAG,它是一个类似的想法,但有所不同:LightRAG更快、更实惠,并且允许对图形进行增量更新,而无需完全重新生成。 ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin-top: calc(1.90909em);margin-bottom: calc(1.27273em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">为什么 LightRAG 优于传统 RAG 系统?
ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">RAG 系统在设计上将文档分成多个片段以供检索。然而,这种方法忽略了这些片段之间的上下文关系。如果含义或上下文跨越多个块,则很难准确回答复杂的问题。 LightRAG 通过生成知识图来解决这个问题 - 绘制数据中实体之间的关系。 ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin-top: calc(1.90909em);margin-bottom: calc(1.27273em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">GraphRAG 的局限性
ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">GraphRAG 虽然具有创新性,但却是资源密集型的。它需要数百次 API 调用,通常使用 GPT-4o 等昂贵的模型。每次更新数据时,GraphRAG 都必须重建整个图,从而增加成本。另一方面,LightRAG: ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin-top: calc(1.90909em);margin-bottom: calc(1.27273em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">掌握新信息
ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);">在技术或新闻等快速变化的领域,过时的信息可能是一个问题。 LightRAG 通过增量更新系统解决了这个问题,这意味着每当有新内容出现时,它不必重建整个知识库。相反,它会快速动态添加新数据,因此即使在不断变化的环境中,答案也能保持相关性。使用图表进行更快、更智能的检索 通过将图形与基于矢量的搜索(一种快速找到相关项目的奇特方式)相结合,LightRAG 确保响应不仅准确而且快速。该系统有效地组织相关的想法,其重复数据删除功能可以删除重复的信息——确保用户只得到最重要的信息。
为什么 LightRAG 很重要? 测试表明,与旧的 RAG 模型相比,LightRAG 显着提高了准确性和速度。它还可以优雅地处理新信息,这意味着您每次都能获得最新且上下文相关的答案。这使得它成为聊天机器人、个人助理和动态搜索工具等以快速、相关响应为关键的应用程序的游戏规则改变者。
LightRAG 的工作原理:工作流程分解 LightRAG 的工作流程分为两个主要阶段- 索引和检索。
1. 索引过程:创建知识图 以下是在 LightRAG 中索引数据时会发生的情况:
分块:您的文档被分成更小的块。
实体识别:LLM识别每个块中的实体(如人、地点或概念)。
关系提取:模型确定这些实体之间的关系,生成实体关系键值对。
知识图构建:这些对组合成一个图结构。删除任何重复的节点或冗余关系以优化图形。
嵌入存储:将描述和关系嵌入到向量中并存储在向量数据库(例如,纳米向量)中。
Nano Vector是 LightRAG 使用的默认数据库。 2. 双层检索:涵盖细节和大局 LightRAG 使用双层检索——本地和全局查询的组合:
这种两层搜索可确保系统提供全面的答案,包括事实及其背后的背景。
如何使用 openai 和本地设置 LightRAG:分步指南 对于 Openai 型号:
第 1 步:克隆存储库 首先,克隆 LightRAG GitHub 存储库:
git clone https://github.com/HKUDS/LightRAG.git cd LightRAG # Or Install from PyPI pip install lightrag-hku第2步:设置虚拟环境 建议使用conda或virtualenv来隔离项目依赖项:
conda create -n lightrag python=3.12 conda activate lightrag第三步:安装依赖项 使用以下命令安装所需的软件包:
pipinstall-e. 第 4 步:对数据建立索引并运行查询 对于此演示,我们将索引查尔斯·狄更斯的“圣诞颂歌”:
import os from lightrag import LightRAG, QueryParam from lightrag.llm import gpt_4o_mini_complete, gpt_4o_complete ######### # Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert() # import nest_asyncio # nest_asyncio.apply() ######### WORKING_DIR = "./dickens" if not os.path.exists(WORKING_DIR): os.mkdir(WORKING_DIR) rag = LightRAG( working_dir=WORKING_DIR, llm_model_func=gpt_4o_mini_complete# Use gpt_4o_mini_complete LLM model # llm_model_func=gpt_4o_complete# Optionally, use a stronger model ) with open("./book.txt") as f: rag.insert(f.read()) # Perform naive search print(rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))) # Perform local search print(rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))) # Perform global search print(rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))) # Perform hybrid search print(rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid")))模式:
naive:标准 RAG
local:基于实体邻域的检索
global:更广泛的全球实体关系
hybrid:结合本地和全局模式
我想使用免费和私有模型运行,该怎么做?
您可以使用 ollama 模型来实现此目的,如下面的代码所示。
import os from lightrag import LightRAG, QueryParam from lightrag.llm import ollama_model_complete, ollama_embedding from lightrag.utils import EmbeddingFunc WORKING_DIR = "./dickens" if not os.path.exists(WORKING_DIR): os.mkdir(WORKING_DIR) rag = LightRAG( working_dir=WORKING_DIR, llm_model_func=ollama_model_complete, llm_model_name="your_model_name", embedding_func=EmbeddingFunc( embedding_dim=768, max_token_size=8192, func=lambda texts: ollama_embedding(texts, embed_model="nomic-embed-text"), ), ) with open("./book.txt", "r", encoding="utf-8") as f: rag.insert(f.read()) # Perform naive search print( rag.query("What are the top themes in this story?", param=QueryParam(mode="naive")) ) # Perform local search print( rag.query("What are the top themes in this story?", param=QueryParam(mode="local")) ) # Perform global search print( rag.query("What are the top themes in this story?", param=QueryParam(mode="global")) ) # Perform hybrid search print( rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid")) )LightRAG 的关键用例 LightRAG 在处理复杂文档或需要基于实体的分析的行业中特别有用。以下是一些示例:
法律研究:提取法律、案例和先例之间的关系。
医疗保健:分析患者数据、症状和治疗,以揭示医学见解。
农业:组织和检索有关农作物、土壤类型和天气模式的信息。
结论:为什么选择 LightRAG? LightRAG 代表了检索增强生成的下一步,以轻量级、经济实惠的方式提供知识图集成和基于向量的检索。凭借增量更新、双层检索以及使用本地或更小模型的能力,它在实用性和成本效益方面优于 GraphRAG。无论您是处理大型数据集还是需要生成智能的、上下文感知的响应,LightRAG 都能提供强大的开源解决方案。