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.
