链载Ai

标题: Neo4j×Milvus:手把手教你搭建GraphRAG Agent [打印本页]

作者: 链载Ai    时间: 昨天 11:55
标题: Neo4j×Milvus:手把手教你搭建GraphRAG Agent

文章最初发布于 Neo4j,已获得授权转载。

01.

概览

本文详细介绍了如何使用 Neo4j 图数据库和 Milvus 向量数据库搭建 GraphRAG Agent。这个 Agent 通过结合图数据库和向量搜索的强大功能,能够提供准确且与用户查询十分相关的答案。在本文示例中,我们将使用 LangGraph、Llama 3.1 8B 配合 Ollama 和 GPT-4o。

传统的检索增强生成(RAG)系统仅依赖向量数据库来检索相关文档。但我们进一步通过引入 Neo4j 来捕捉 Entity 和概念之间的关系,提供对信息更细致的理解。我们希望通过结合这两种技术,搭建一个更可靠、更富含信息量的 RAG 系统。

02.

搭建 RAG Agent

我们的 Agent 遵循三个关键概念:路由(routing)、回退机制(fallback)和自我修正(self-correction)。这些原则通过一系列 LangGraph 组件实现:

我们还有其他组件,例如:

03.

GraphRAG 架构

我们的 GraphRAG Agent 的架构可以被看作是一个具有多个相互连接的节点的工作流程:

04.

Agents 示例

为了展示 LLM Agent 的能力,让我们深入了解两个不同的组件:图生成 Graph Generation和复合代理 Composite Agent

本章节将帮助您更好理解这些 Agent 在 LangChain 框架中的工作原理。您可以在文末获取完整代码。

Graph Generation

这个组件旨在通过使用 Neo4j 的能力来改进问答过程。它通过利用 Neo4j 图形数据库中的知识来回答问题。以下是它的工作原理:

  1. GraphCypherQAChain:允许 LLM 与 Neo4j 图数据库进行交互。它以两种方式使用 LLM:
  1. 上下文检索:在 Neo4j 图上进行经过验证的查询以检索相关的上下文。

  2. 答案生成:语言模型使用检索到的上下文来生成用户问题的答案。


###GenerateCypherQuery

llm=ChatOllama(model=local_llm,temperature=0)

#Chain
graph_rag_chain=GraphCypherQAChain.from_llm(
cypher_llm=llm,
qa_llm=llm,
validate_cypher=True,
graph=graph,
verbose=True,
return_intermediate_steps=True,
return_direct=True,
)

#Run
question="agentmemory"
generation=graph_rag_chain.invoke({"query":question})

这个组建帮助 RAG 系统充分利用 Neo4j,从而提供更准确的答案。

Composite Agent:图 + 向量 ?

如同魔法一般,我们的 Agent 可以结合 Milvus 和 Neo4j 的结果,从而更好地理解信息,并返回更准确和细致的答案。以下是 Composite Agent 组件的工作原理:

  1. Prompt —— 我们定义了一个 Prompt,指导 LLM 使用来自 Milvus 和 Neo4j 的上下文来回答问题。

  2. 检索 —— Agent 从 Milvus(使用向量搜索)和 Neo4j(使用图生成)检索相关信息。

  3. 答案生成 —— Llama 3.1 8B 处理提示,并生成一个简洁的答案,利用来自向量和图数据库的复合链组合知识。

###CompositeVector+GraphGenerations

cypher_prompt=PromptTemplate(
template="""YouareanexpertatgeneratingCypherqueriesforNeo4j.
UsethefollowingschematogenerateaCypherquerythatanswersthegivenquestion.
Makethequeryflexiblebyusingcase-insensitivematchingandpartialstringmatchingwhereappropriate.
Focusonsearchingpapertitlesastheycontainthemostrelevantinformation.

Schema:
{schema}

Question:{question}

CypherQuery:""",
input_variables=["schema","question"],
)
#QAprompt
qa_prompt=PromptTemplate(
template="""Youareanassistantforquestion-answeringtasks.
UsethefollowingCypherqueryresultstoanswerthequestion.Ifyoudon'tknowtheanswer,justsaythatyoudon'tknow.
Usethreesentencesmaximumandkeeptheanswerconcise.Iftopicinformationisnotavailable,focusonthepapertitles.

Question:{question}
CypherQuery:{query}
QueryResults:{context}

Answer:""",
input_variables=["question","query","context"],
)


llm=ChatOpenAI(model="gpt-4o",temperature=0)
#Chain
graph_rag_chain=GraphCypherQAChain.from_llm(
cypher_llm=llm,
qa_llm=llm,
validate_cypher=True,
graph=graph,
verbose=True,
return_intermediate_steps=True,
return_direct=True,
cypher_prompt=cypher_prompt,
qa_prompt=qa_prompt,
)

让我们来看一下搜索结果,结合图数据库和向量数据库的优势来增强在研究论文中的发现。

我们首先使用 Neo4j 进行图搜索:

#Exampleinputdata
question="WhatpapertalksaboutMulti-Agent?"
generation=graph_rag_chain.invoke({"query":question})
print(generation)
>EnteringnewGraphCypherQAChainchain...
GeneratedCypher:
cypher
MATCH(paper)
WHEREtoLower(p.title)CONTAINStoLower("Multi-Agent")
RETURNp.titleASPaperTitle,p.summaryASSummary,p.urlASURL
>Finishedchain.
{'query':'WhatpapertalksaboutMulti-Agent?','result':[{'PaperTitle':'CollaborativeMulti-Agent,Multi-Reasoning-Path(CoMM)PromptingFramework','Summary':'Inthiswork,weaimtopushtheupperboundofthereasoningcapabilityofLLMsbyproposingacollaborativemulti-agent,multi-reasoning-path(CoMM)promptingframework.Specifically,wepromptLLMstoplaydifferentrolesinaproblem-solvingteam,andencouragedifferentrole-playagentstocollaborativelysolvethetargettask.Inparticular,wediscoverthatapplyingdifferentreasoningpathsfordifferentrolesisaneffectivestrategytoimplementfew-shotpromptingapproachesinthemulti-agentscenarios.Empiricalresultsdemonstratetheeffectivenessoftheproposedmethodsontwocollege-levelscienceproblemsovercompetitivebaselines.OurfurtheranalysisshowsthenecessityofpromptingLLMstoplaydifferentrolesorexpertsindependently.','URL':'https://github.com/amazon-science/comm-prompt'}]

图搜索在查找关系和元数据方面表现出色。它能够快速根据标题、作者或预定义的类别识别论文,提供数据的结构化视图。

下面,我们换个角度,看一下使用向量搜索的结果:

#Exampleinputdata
question="WhatpapertalksaboutMulti-Agent?"

#Getvector+graphanswers
docs=retriever.invoke(question)
vector_context=rag_chain.invoke({"context":docs,"question":question})
>Thepaperdiscusses"AdaptiveIn-conversationTeamBuildingforLanguageModelAgents"andtalksaboutMulti-Agent.Itpresentsanewadaptiveteam-buildingparadigmthatoffersaflexiblesolutionforbuildingteamsofLLMagentstosolvecomplextaskseffectively.Theapproach,calledCaptainAgent,dynamicallyformsandmanagesteamsforeachstepofthetask-solvingprocess,utilizingnestedgroupconversationsandreflectiontoensurediverseexpertiseandpreventstereotypicaloutputs.

向量搜索在理解上下文和语义相似性方面非常出色。它能够发现与查询概念相关的论文,即使这些论文没有明确包含搜索词。

最后,我们结合了两种搜索方法:

这是我们 RAG Agent 的一个关键部分,帮助我们同时发挥向量和图数据库的力量。

composite_chain=prompt|llm|StrOutputParser()
answer=composite_chain.invoke({"question":question,"context":vector_context,"graph_context":graph_context})

print(answer)
>Thepaper"CollaborativeMulti-Agent,Multi-Reasoning-Path(CoMM)PromptingFramework"talksaboutMulti-Agent.ItproposesaframeworkthatpromptsLLMstoplaydifferentrolesinaproblem-solvingteamandencouragesdifferentrole-playagentstocollaborativelysolvethetargettask.Thepaperpresentsempiricalresultsdemonstratingtheeffectivenessoftheproposedmethodsontwocollege-levelscienceproblems.

通过整合图搜索和向量搜索,我们发挥了这两种方法的优势。图搜索提供了精确度并展示了结构化的关系,而向量搜索通过语义理解增加了深度。

这种结合的方法提供了几个优势:

  1. 提高召回率:能够找到可能被单一方法遗漏的相关论文。

  2. 增强上下文:对论文之间关系提供了更细致的理解。

  3. 灵活性:可以适应不同类型的查询,从具体的关键词搜索到更广泛的概念探索。

05.

总结

本文展示了如何使用 Neo4j 和 Milvus 搭建一个 GraphRAG Agent。通过结合图数据库和向量搜索的优势,这个 Agent 能够为用户提供准确且相关的查询答案。

这个 RAG Agent 的架构中,配备了专门的路由、回退机制和自我修正能力,从而变得更可靠。Graph Generation 和 Composite Agent 组件的示例展示了这个 Agent 如何利用向量和图数据库来提供全面且细致的答案。






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5