返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

KG2-RAG:利用图谱来增强LLM生成效果

[复制链接]
链载Ai 显示全部楼层 发表于 4 小时前 |阅读模式 打印 上一主题 下一主题

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin: 0px 0px calc(1.16667em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">1、前言

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">目前在RAG方向,存在两个主要的方向,一个是检索文档+rank方式来提升LLM,一个是利用图谱(知识库)来提升LLM,形式如下:

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">两种方式存在的优缺点是:

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">(1)第一种就是采用检索+rank方式来获取相关片段来增强大模型生成,优点就是简单有效;缺点是存在检索到很多与query无关的片段,增加噪声输入,与query关联的信息比较稀疏,需要LLM本身去提炼或挖掘利用;且一般是直接将检索的片段(chunks)放入指令中,忽略了片段中的内在关联信息或者query中的核心实体与片段的关联信息;

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">(2)第二种是利用query去检索图谱的信息,然后过滤有效的信息输入LLM;该方式优点就是检索到的信息是以实体及实体关联的形成呈现,信息密度比较高,噪声小;缺点就是:对图谱的信息质量要求比较高;此外,提供的信息比较单一,覆盖率比较小。

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">若按检索来理解,第一种方式是召回率高,准确率低些;第二种是准确率高,但召回率低些。为此,今天讲述一个新的框架——KG2RAG,其主要将上述两种方式融合起来用。当然,这种想法并不新奇,也有类似的工作,今天主要看看KG2RAG具体是如何实现的。

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-optical-sizing: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;margin: calc(2.33333em) 0px calc(1.16667em);clear: left;color: rgb(25, 27, 31);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">2、模型

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">

ingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">上图为KG2RAG的整体流程图,下面详细来说下

2.1 Document Offline Processing

首先,对语料库文档进行离线处理:1)将每个文档按一定规则(固定长度)方式切成片段(chunk);2)对每个片段做下增强处理,包括抽取相关元信息(标题、摘要等)、以及该片段涉及到的相关query信息等;3)得到一个片段集合

其次,为了获取每个片段中包含的实体之间的信息,利用LLM从每个片段中抽取三元组信息,形成图谱。具体形式为:

其中h,r,t分别表示头实体,关系,尾实体,c为对应的片段。该抽取的过程与query无关,所以该过程是可以离线来操作。三元组抽取的指令形式如下:

2.2 KG-enhanced Chunk Retrieval

有了上述的语料离线处理,接下来就是如何来利用这些信息来检索增强llm。包含两个步骤:1)Semantic-based Retrieval;2)Graph-guided Expansion。

1)Semantic-based Retrieval

该过程就是熟知的检索+rank过程,即给定一个query,计算它与片段集合D中相关的片段的相似度:

其中,s(.) 为相似度计算函数。通过相似度对检索的片段rank,选择top-k个片段作为检索信息,记为作为语义检索的结果。

2)Graph-guided Expansion

利用离线构建好的图谱信息和检索的片段,可以得到一个与query相关的子图信息,即:

按近邻m-hop的搜索方式对上述的子图进行扩充,得到扩充后的子图,接着讲扩充后的子图所关联的片段形成一个新的集合,作为利用图谱后扩充的片段信息,即:

2.3 KG-based Context Organization

在利用上述2.2步骤扩充后的片段集合后,可能存在片段数量过多,超过最大输入长度等问题,为了解决这些问题,需要进一步进行过滤与重组。

1)Serving as a Filter

首先将扩充后的图变成一个无向带权重的图,计算方式如下:

其中代表无向边,rel,src,weight作为属性信息,权重计算来自所在片段与query的相似度。

接着根据图的链接性,可以将其分成几个独立的子图(如figure2中色块所示),表示为,其中i代表数量。为了限制某个子图过于庞大,可以按maximum spanning tree(MST)的方式对每个子图进行裁剪过滤,形成过滤后的子图:

2)Serving as an Arranger

接着对上述的子图再进行重排,计算方式如下:

其中C(.)代表cross-encoder reranking function重排函数,conc(.)是代表某个子图中包含的所有三元组进行拼接。可以看出重排的时候,就没有用片段的表征向量,而是用了三元组的信息,目的是三元组的信息更为准确。

最后利用重排的分数,选择top-k相关的片段进行拼接,作为最终的检索信息,输入LLM中进行解码生成。

3、实验

上图是主实验结果,在4个数据集,对比了4种不同的RAG方式,验证了回答质量和检索质量两个维度,显示:

(1)在4个数据集上,论文提出的KG2-RAG在reponse quality上都有非常明显的提升;

(2)与KG2-RAG效果像接近的是Hybrid RAG和Semantic RAG+Rerank;

(3)比较热门的GraphRAG在四个数据集上表现的很差,这个挺让人意外的。

上图是显示对Graph-guided Expansion与KG-based Context Organization的消融实验,显示:

(1)在reponse quality上,Expansion方式对影响的更大;

(2)在retrieval quality,Organization方式影响的更大,而没有Expansion方式,检索的质量更高,说明利用KG可以提高检索质量。

4、结语

本次分享一个KG2-RAG框架,其主要思路利用片段构建图信息,进一步增强片段信息,其中核心是利用检索的图扩充片段,以及利用图对检索片段进行重排。整体看来,相比正常的RAG流程,其在线推理成本没有增加太多,构建图谱的环节可以离线实现,觉得某些应用场景可值得试试。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ