Zilliz 2024年11月07日
Neo4j×Milvus:手把手教你搭建GraphRAG Agent
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何结合Neo4j图数据库和Milvus向量数据库构建GraphRAG Agent,以提升检索增强生成(RAG)系统的准确性。传统RAG系统仅依赖向量数据库检索文档,而GraphRAG Agent通过引入Neo4j捕捉实体和概念间的关系,提供更细致的信息理解。该Agent遵循路由、回退和自我修正等原则,并包含检索、图增强、LLM集成等组件。此外,文章还详细介绍了Graph Generation和Composite Agent两个示例,展示了如何利用Neo4j和Milvus构建更强大的RAG系统,并提供了相关代码链接。

🤔**路由、回退和自我修正机制:** GraphRAG Agent遵循三个关键原则:路由机制决定使用向量数据库、知识图谱或两者结合;回退机制在初始检索不足时使用Tavily进行网络搜索;自我修正机制评估答案并尝试修正幻觉或错误。

🔎**检索和图增强:** Agent使用Milvus进行向量搜索,存储和检索文档片段;并使用Neo4j从检索到的文档构建知识图谱,丰富上下文信息。

🔗**LLM集成:** Agent集成Llama 3.1 8B作为本地LLM生成答案和评估信息,并使用GPT-4o生成Neo4j查询语言Cypher。

🧩**Graph Generation组件:** 该组件利用Neo4j的能力,通过GraphCypherQAChain允许LLM与图数据库交互,生成Cypher查询,并使用检索到的上下文生成答案。

🪄**Composite Agent组件:** 该组件结合Milvus和Neo4j的结果,利用向量搜索和图生成,提供更准确和细致的答案,充分发挥两种数据库的优势。

原创 和你一起进步的 2024-11-07 18:30 上海

结合图数据库和向量搜索的优势,大幅提高RAG准确率(提供完整代码)

文章最初发布于 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 图形数据库中的知识来回答问题。以下是它的工作原理:

    GraphCypherQAChain:允许 LLM 与 Neo4j 图数据库进行交互。它以两种方式使用 LLM:

    上下文检索:在 Neo4j 图上进行经过验证的查询以检索相关的上下文。

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


    ### Generate Cypher Query    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 = "agent memory"
    generation = graph_rag_chain.invoke({"query": question})

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

Composite Agent:图 + 向量 ?

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

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

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

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

    ### Composite Vector + Graph Generations    cypher_prompt = PromptTemplate(
        template="""You are an expert at generating Cypher queries for Neo4j.        Use the following schema to generate a Cypher query that answers the given question.        Make the query flexible by using case-insensitive matching and partial string matching where appropriate.        Focus on searching paper titles as they contain the most relevant information.                Schema:        {schema}                Question: {question}        
        Cypher Query:"""
,
        input_variables=["schema""question"],    )
    # QA prompt    qa_prompt = PromptTemplate(
        template="""You are an assistant for question-answering tasks.         Use the following Cypher query results to answer the question. If you don't know the answer, just say that you don't know.         Use three sentences maximum and keep the answer concise. If topic information is not available, focus on the paper titles.                Question: {question}         Cypher Query: {query}        Query Results: {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 进行图搜索:

    # Example input data
    question = "What paper talks about Multi-Agent?"
    generation = graph_rag_chain.invoke({"query": question})
    print(generation)
    > Entering new GraphCypherQAChain chain...    Generated Cypher:    cypher    MATCH (p:Paper)
    WHERE toLower(p.title) CONTAINS toLower("Multi-Agent")    RETURN p.title AS PaperTitle, p.summary AS Summary, p.url AS URL
    > Finished chain.
    {'query''What paper talks about Multi-Agent?''result': [{'PaperTitle''Collaborative Multi-Agent, Multi-Reasoning-Path (CoMM) Prompting Framework''Summary''In this work, we aim to push the upper bound of the reasoning capability of LLMs by proposing a collaborative multi-agent, multi-reasoning-path (CoMM) prompting framework. Specifically, we prompt LLMs to play different roles in a problem-solving team, and encourage different role-play agents to collaboratively solve the target task. In particular, we discover that applying different reasoning paths for different roles is an effective strategy to implement few-shot prompting approaches in the multi-agent scenarios. Empirical results demonstrate the effectiveness of the proposed methods on two college-level science problems over competitive baselines. Our further analysis shows the necessity of prompting LLMs to play different roles or experts independently.''URL''https://github.com/amazon-science/comm-prompt'}]

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

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

    # Example input data
    question = "What paper talks about Multi-Agent?"
    # Get vector + graph answers    docs = retriever.invoke(question)
    vector_context = rag_chain.invoke({"context": docs, "question": question})
    > The paper discusses "Adaptive In-conversation Team Building for Language Model Agents" and talks about Multi-Agent. It presents a new adaptive team-building paradigm that offers a flexible solution for building teams of LLM agents to solve complex tasks effectively. The approach, called Captain Agent, dynamically forms and manages teams for each step of the task-solving process, utilizing nested group conversations and reflection to ensure diverse expertise and prevent stereotypical outputs.

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

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

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

    composite_chain = prompt | llm | StrOutputParser()
    answer = composite_chain.invoke({"question": question, "context": vector_context, "graph_context": graph_context})
    print(answer)
  > The paper "Collaborative Multi-Agent, Multi-Reasoning-Path (CoMM) Prompting Framework" talks about Multi-Agent. It proposes a framework that prompts LLMs to play different roles in a problem-solving team and encourages different role-play agents to collaboratively solve the target task. The paper presents empirical results demonstrating the effectiveness of the proposed methods on two college-level science problems.

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

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

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

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

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

05.

总结

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

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

我们希望这篇文章对您有所帮助。同时,我们鼓励您在自己的项目中结合图数据库和向量搜索,探索更多可能性。

本文涉及的代码可以通过 GitHub 获取(https://github.com/milvus-io/bootcamp/blob/master/bootcamp/RAG/advanced_rag/langgraph-graphrag-agent-local.ipynb)

本文作者:

Jason Koo, Developer Advocate at Neo4j

Stephen Batifol, Developer Advocate at Zilliz



2024年非结构化数据峰会的报名通道现已开启!

作为备受瞩目的年度重磅盛会,本次大会以“数绘万象,智联八方”为主题,邀请广大AI产业伙伴,从生态构建、客户案例、技术前瞻、商业化落地等多个角度共同研讨智能化的新未来。

如果您对AI产业化落地充满热情,渴望加入这场智慧的盛宴,请移步至文章末尾,扫描二维码,或点击“阅读原文即刻报名参与!

阅读原文

跳转微信打开

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

RAG Neo4j Milvus 向量搜索 知识图谱
相关文章