面包屑图标 当前位置: 首页
AI资讯
热点详情

GraphRAG与普通RAG融合的高效混合检索增强生成框架

AI热点日报
AI热点日报时间:2026-05-30
热点解读

RAG与GraphRAG各有优劣,HybridRAG融合二者,同时执行向量检索与图分析,输出混合上下文,显著提升答案准确性与深度。该框架整合结构推理与灵活检索,增强实体关系理解,支持动态知识更新,通过LangChain实现可验证其优化效果。

RAG已在生成式AI领域占据重要地位,它使用户能够轻松地将个人文档、PDF、视频等资料与大型语言模型(LLMs)对接,实现交互式查询。而近期,RAG的进阶形态——GraphRAG也崭露头角,通过知识图谱与LLMs协作完成检索任务,其设计思路颇具巧思。

RAG与GraphRAG各具优势,但也存在各自的局限性。RAG擅长基于向量相似性进行快速检索,而GraphRAG则更依赖图分析与知识图谱来提供精准答案。那么问题来了:如果将两者结合,同时进行检索,能否产生意想不到的协同效应?

1 HybridRAG:融合RAG与GraphRAG的混合检索框架

HybridRAG正是这样一个融合了RAG和GraphRAG的高级框架。其目标十分明确:进一步提升信息检索的准确性与上下文相关性。简而言之,HybridRAG会同时从RAG和GraphRAG两个系统中获取上下文信息,最终输出两者的混合成果。这相当于汇总两位专家的意见,从而做出更为全面的判断。

2 HybridRAG的核心优势

这种组合策略带来的收益非常直观:

  • 准确性提升: 它将结构化推理能力与灵活检索能力融为一体,给出的答案自然比单独使用VectorRAG或GraphRAG更加精准。
  • 上下文理解更深入: 通过整合两个系统的信息,HybridRAG对实体之间的关系及其出现的具体语境有了更深刻的把握。这一点在应对复杂查询时尤为关键。
  • 具备动态推理能力: 知识图谱可不断更新,这意味着系统能够灵活适应新信息,持续优化自身知识库。

3 使用LangChain构建HybridRAG系统

为了让读者更直观地理解,我们借助LangChain演示如何构建这样一个HybridRAG系统。这里使用的测试文件名为“Moon.txt”,本质上是一则超级英雄的冒险故事。我们先看看故事内容:

In the bustling city of Lunaris, where the streets sparkled with neon lights and the moon hung low in the sky, lived an unassuming young man named Max. By day, he was a mild-mannered astronomer, spending his hours studying the stars and dreaming of adventures beyond Earth. But as the sun dipped below the horizon, Max transformed into something extraordinary—Moon Man, the guardian of the night sky.
...
From that day forward, Moon Man became a symbol of resilience and bra very. Max continued to protect Lunaris, knowing that as long as the moon shone brightly, he would always be there to guard the night sky. And so, the legend of Moon Man lived on, inspiring generations to look up at the stars and believe in the extraordinary.

接下来,导入必要的依赖包,并配置好LLM与嵌入模型(用于标准RAG)。

import os
from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_core.documents import Document
from langchain_community.graphs.networkx_graph import NetworkxEntityGraph
from langchain.chains import GraphQAChain
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import TextLoader
from langchain.chains import RetrievalQA
from langchain.vectorstores import Chroma
from langchain_google_genai import GoogleGenerativeAI,GoogleGenerativeAIEmbeddings

GOOGLE_API_KEY=''

embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001",google_api_key=GOOGLE_API_KEY)
llm = GoogleGenerativeAI(model="gemini-pro",google_api_key=GOOGLE_API_KEY)

随后,为GraphRAG的实现创建一个函数,专门处理“Moon.txt”这个文件。

def graphrag():
    with open('Moon.txt', 'r') as file:
        content = file.read()

    documents = [Document(page_content=content)]
    llm_transformer = LLMGraphTransformer(llm=llm)
    graph_documents = llm_transformer.convert_to_graph_documents(documents)

    graph = NetworkxEntityGraph()

    # 添加节点到图
    for node in graph_documents[0].nodes:
        graph.add_node(node.id)

    # 添加边到图
    for edge in graph_documents[0].relationships:
        graph._graph.add_edge(
                edge.source.id,
                edge.target.id,
                relation=edge.type,
            )

        graph._graph.add_edge(
                edge.target.id,
                edge.source.id,
                relation=edge.type+" by",
            )

    chain = GraphQAChain.from_llm(
        llm=llm, 
        graph=graph, 
        verbose=True
    )
        
    return chain

同样,也为同一个文件实现标准RAG的函数。

def rag():
    # 文档加载器
    loader = TextLoader('Moon.txt')
    data = loader.load()

    # 文档转换器
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
    texts = text_splitter.split_documents(data)

    # 向量数据库
    docsearch = Chroma.from_documents(texts, embeddings)

    # 需要知道的超参数
    retriever = docsearch.as_retriever(search_type='similarity_score_threshold',search_kwargs={"k": 7,"score_threshold":0.3})
    qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
        
    return qa

接着,分别为这两种RAG类型创建实例。

standard_rag = rag()
graph_rag = graphrag()

现在,终于轮到HybridRAG登场了。

def hybrid_rag(query,standard_rag,graph_rag):
    result1 = standard_rag.run(query)
    
    print("Standard RAG:",result1)
    result2 = graph_rag.run(query)
    
    
    print("Graph RAG:",result2)
    prompt = "Generate a final answer using the two context given : Context 1: {} n Context 2: {} n Question: {}".format(result1,result2,query)
    return llm(prompt)

query = "Some characteristics of Moon Man"
hybrid = hybrid_rag(query,standard_rag,graph_rag)
print("Hybrid:",hybrid)

可以看到,我们对同一查询分别独立执行了标准RAG和GraphRAG。当两个系统都给出回答后,就将它们的结果作为上下文,一起交由LLM来生成最终答案。

从输出结果来看,HybridRAG的表现确实亮眼。它成功地从两个检索结果中提取了有价值的上下文,并据此得出了更优质的答案。有些细节被标准RAG或GraphRAG各自遗漏,但最终在HybridRAG这里被完美整合,给出了一个几乎无懈可击的答案。

STANDARD RAG: 
 Here are some characteristics of Moon Man, based on the story:

* **Bra ve:** He confronts danger and fights villains like Umbra.
* **Powerful:** He has superhuman abilities granted by the amulet.
* **Protective:** He safeguards Lunaris and its citizens.
* **Determined:** He doesn't give up, even when facing powerful enemies.
* **Compassionate:** He helps those in need, like rescuing lost pets.
* **Humble:** Despite his powers, he remains grounded and dedicated to his city. 



> Entering new GraphQAChain chain...
Entities Extracted:
Moon Man

Full Context:
Moon Man PROTECTS night sky
Moon Man WEARS silver suit
Moon Man PROTECTED Lunaris
Moon Man CAPTURED thieves
Moon Man DEFEATED Umbra
Moon Man INSPIRES hope
Moon Man INSPIRES courage

> Finished chain.

 @@ 
 GRAPH RAG: 
 Helpful Answer: 
* Protective (protects night sky, protected Lunaris)
* Courageous and Inspiring (inspires hope, inspires courage)
* Strong (captured thieves, defeated Umbra) 


 @@ 
 HYBRID RAG: 
 Moon Man is the **protective** champion of Lunaris, using his **strength** and **courage** to defend its citizens and the night sky. He is **powerful** and **determined**, facing down villains like Umbra without giving up. Yet, despite his abilities, he remains **humble** and **compassionate**, always willing to help those in need. Moon Man is a true inspiration, reminding everyone that even in darkness, hope and heroism can shine through. 
热点追踪提示词
你是一名 AI 行业编辑,请围绕下面这条热点输出一份资讯解读:
热点:GraphRAG与普通RAG融合的高效混合检索增强生成框架要求:
1. 先用一句话解释这条热点在讲什么
2. 再总结它为什么重要
3. 说明会影响哪些 AI 产品或内容方向
4. 最后给出 3 个适合资讯站使用的标题
来源:https://www.53ai.com/news/RAG/2024123073165.html
ai 人工智能

游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。

相关热点
AI热点2026-07-14 19:48
面壁智能CTO谈端侧AI:从打字机到大模型的进化突围

面壁智能聚焦端侧AI,不拼参数大小,而是通过知识密度提升与模型风洞技术,将大模型压缩至手机、汽车等设备。其MiniCPM以2B参数超越同期8B对手。CTO曾国洋22岁主导训练中国首个大语言模型CPM-1。端侧AI追求“默契系统”,在用户开口前预判需求,已在吉利、上汽大众等车型落地应用。

AI热点2026-07-14 19:48
印度IT巨头HCL Tech投350亿卢比建50MW AI数据中心

印度IT巨头HCLTech投资最高350亿卢比建设AI数据中心,容量可扩展至50MW,提供从设计到运营的端到端服务,旨在满足政府及企业日益增长的算力需求,抢占印度快速增长的数据中心市场,并推动AI基础设施布局。

AI热点2026-07-14 19:48
小米具身智能机器人新工站双侧螺母上件成功率达98%

小米具身机器人在汽车工厂自攻螺母上件工站实现双侧作业成功率98%,接近人工水平。同时在新工站分别达到90%成功率,从单一操作拓展至多工站协同,验证了具身智能在复杂工业环境的落地能力。

AI热点2026-07-14 19:48
DeepSeek梁文锋身价360亿美元成AI新首富

全球AI行业正迎来新的财富格局,DeepSeek创始人梁文锋凭借其公司的迅猛发展,个人财富急剧膨胀,一举超越多位硅谷知名人物,成为全球AI公司领域的新首富。以下将详细解析其身价飙升背后的关键因素及公司发展历程。 一、身价飙升至360亿美元,超越多位AI大佬 根据最新彭博亿万富豪指数,DeepSeek

延伸阅读