使用Llama index构建多代理 RAG

检索增强生成(RAG)已成为增强大型语言模型(LLM)能力的一种强大技术。通过从知识来源中检索相关信息并将其纳入提示,RAG为LLM提供了有用的上下文,以产生基于事实的输出。

但是现有的单代理RAG系统面临着检索效率低下、高延迟和次优提示的挑战。这些问题在限制了真实世界的RAG性能。多代理体系结构提供了一个理想的框架来克服这些挑战并释放RAG的全部潜力。通过划分职责,多代理系统允许专门的角色、并行执行和优化协作。

在这里插入图片描述

单代理RAG

当前的RAG系统使用单个代理来处理完整的工作流程——查询分析、段落检索、排序、摘要和提示增强。

这种单一的方法提供了一个简单的一体化解决方案。但是对每个任务依赖一个代理会导致瓶颈。代理会浪费时间从大量语料库中检索无关紧要的段落。长上下文的总结很糟糕,并且提示无法以最佳方式集成原始问题和检索到的信息。

这些低效率严重限制了实时应用程序的RAG的可伸缩性和速度。

多代理RAG

多代理体系结构可以克服单代理的限制。通过将RAG划分为并发执行的模块化角色可以实现:

检索:专用检索代理专注于使用优化的搜索技术进行有效的通道检索。这将最小化延迟。

搜索:通过排除检索因素,搜索可以在检索代理之间并行化,以减少等待时间。

排名:单独的排名代理评估检索的丰富度,特异性和其他相关信号的传代。这将过滤最大的相关性。

总结:将冗长的上下文总结成简洁的片段,只包含最重要的事实。

优化提示:动态调整原始提示和检索信息的集成。

灵活的体系:可以替换和添加代理来定制系统。可视化工具代理可以提供对工作流的洞察。

通过将RAG划分为专门的协作角色,多代理系统增强了相关性,减少了延迟,并优化了提示。这将解锁可伸缩的高性能RAG。

划分职责允许检索代理结合互补技术,如向量相似性、知识图谱和互联网抓取。这种多信号方法允许检索捕获相关性不同方面的不同内容。

通过在代理之间协作分解检索和排序,可以从不同的角度优化相关性。结合阅读和编排代理,它支持可伸缩的多角度RAG。

模块化架构允许工程师跨专门代理组合不同的检索技术。

Llama index的多代理 RAG

Llama index概述了使用多代理RAG的具体示例:

文档代理——在单个文档中执行QA和摘要。

向量索引——为每个文档代理启用语义搜索。

摘要索引——允许对每个文档代理进行摘要。

高阶(TOP-LEVEL)代理——编排文档代理以使用工具检索回答跨文档的问题。

对于多文档QA,比单代理RAG基线显示出真正的优势。由顶级代理协调的专门文档代理提供基于特定文档的更集中、更相关的响应。

下面我们看看Llama index是如何实现的:

我们将下载关于不同城市的Wikipedia文章。每篇文章都是单独存储的。我们只找了18个城市,虽然不是很大,但是这已经可以很好的演示高级文档检索的功能。

代码语言:javascript

 from llama_index import (
     VectorStoreIndex,
     SummaryIndex,
     SimpleKeywordTableIndex,
     SimpleDirectoryReader,
     ServiceContext,
 )
 from llama_index.schema import IndexNode
 from llama_index.tools import QueryEngineTool, ToolMetadata
 from llama_index.llms import OpenAI

下面是城市的列表:

代码语言:javascript

 wiki_titles = [
     "Toronto",
     "Seattle",
     "Chicago",
     "Boston",
     "Houston",
     "Tokyo",
     "Berlin",
     "Lisbon",
     "Paris",
     "London",
     "Atlanta",
     "Munich",
     "Shanghai",
     "Beijing",
     "Copenhagen",
     "Moscow",
     "Cairo",
     "Karachi",
 ]

下面是下载每个城市文档代码:

代码语言:javascript

 from pathlib import Path
 
 import requests
 
 for title in wiki_titles:
     response = requests.get(
         "https://en.wikipedia.org/w/api.php",
         params={
             "action": "query",
             "format": "json",
             "titles": title,
             "prop": "extracts",
             # 'exintro': True,
             "explaintext": True,
         },
     ).json()
     page = next(iter(response["query"]["pages"].values()))
     wiki_text = page["extract"]
 
     data_path = Path("data")
     if not data_path.exists():
         Path.mkdir(data_path)
 
     with open(data_path / f"{title}.txt", "w") as fp:
         fp.write(wiki_text)

加载下载的文档

代码语言:javascript

 # Load all wiki documents
 city_docs = {}
 for wiki_title in wiki_titles:
     city_docs[wiki_title] = SimpleDirectoryReader(
         input_files=[f"data/{wiki_title}.txt"]
     ).load_data()

定义LLM +上下文+回调管理器

代码语言:javascript

 llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
 service_context = ServiceContext.from_defaults(llm=llm)

我们为每个文档定义“文档代理”:为每个文档定义向量索引(用于语义搜索)和摘要索引(用于摘要)。然后将这两个查询引擎转换为传递给OpenAI函数调用工具。

文档代理可以动态选择在给定文档中执行语义搜索或摘要。我们为每个城市创建一个单独的文档代理。

代码语言:javascript

 from llama_index.agent import OpenAIAgent
 from llama_index import load_index_from_storage, StorageContext
 from llama_index.node_parser import SimpleNodeParser
 import os
 
 node_parser = SimpleNodeParser.from_defaults()
 
 # Build agents dictionary
 agents = {}
 query_engines = {}
 
 # this is for the baseline
 all_nodes = []
 
 for idx, wiki_title in enumerate(wiki_titles):
     nodes = node_parser.get_nodes_from_documents(city_docs[wiki_title])
     all_nodes.extend(nodes)
 
     if not os.path.exists(f"./data/{wiki_title}"):
         # build vector index
         vector_index = VectorStoreIndex(nodes, service_context=service_context)
         vector_index.storage_context.persist(
             persist_dir=f"./data/{wiki_title}"
         )
     else:
         vector_index = load_index_from_storage(
             StorageContext.from_defaults(persist_dir=f"./data/{wiki_title}"),
             service_context=service_context,
         )
 
     # build summary index
     summary_index = SummaryIndex(nodes, service_context=service_context)
     # define query engines
     vector_query_engine = vector_index.as_query_engine()
     summary_query_engine = summary_index.as_query_engine()
 
     # define tools
     query_engine_tools = [
         QueryEngineTool(
             query_engine=vector_query_engine,
             metadata=ToolMetadata(
                 name="vector_tool",
                 description=(
                     "Useful for questions related to specific aspects of"
                     f" {wiki_title} (e.g. the history, arts and culture,"
                     " sports, demographics, or more)."
                 ),
             ),
         ),
         QueryEngineTool(
             query_engine=summary_query_engine,
             metadata=ToolMetadata(
                 name="summary_tool",
                 description=(
                     "Useful for any requests that require a holistic summary"
                     f" of EVERYTHING about {wiki_title}. For questions about"
                     " more specific sections, please use the vector_tool."
                 ),
             ),
         ),
     ]
 
     # build agent
     function_llm = OpenAI(model="gpt-4")
     agent = OpenAIAgent.from_tools(
         query_engine_tools,
         llm=function_llm,
         verbose=True,
         system_prompt=f"""\
 You are a specialized agent designed to answer queries about {wiki_title}.
 You must ALWAYS use at least one of the tools provided when answering a question; do NOT rely on prior knowledge.\
 """,
     )
 
     agents[wiki_title] = agent
     query_engines[wiki_title] = vector_index.as_query_engine(
         similarity_top_k=2
     )

下面就是高阶代理,它可以跨不同的文档代理进行编排,回答任何用户查询。

高阶代理可以将所有文档代理作为工具,执行检索。这里我们使用top-k检索器,但最好的方法是根据我们的需求进行自定义检索。

代码语言:javascript

 # define tool for each document agent
 all_tools = []
 for wiki_title in wiki_titles:
     wiki_summary = (
         f"This content contains Wikipedia articles about {wiki_title}. Use"
         f" this tool if you want to answer any questions about {wiki_title}.\n"
     )
     doc_tool = QueryEngineTool(
         query_engine=agents[wiki_title],
         metadata=ToolMetadata(
             name=f"tool_{wiki_title}",
             description=wiki_summary,
         ),
     )
     all_tools.append(doc_tool)
     
 # define an "object" index and retriever over these tools
 from llama_index import VectorStoreIndex
 from llama_index.objects import ObjectIndex, SimpleToolNodeMapping
 
 tool_mapping = SimpleToolNodeMapping.from_objects(all_tools)
 obj_index = ObjectIndex.from_objects(
     all_tools,
     tool_mapping,
     VectorStoreIndex,
 )
 
 from llama_index.agent import FnRetrieverOpenAIAgent
 
 top_agent = FnRetrieverOpenAIAgent.from_retriever(
     obj_index.as_retriever(similarity_top_k=3),
     system_prompt=""" \
 You are an agent designed to answer queries about a set of given cities.
 Please always use the tools provided to answer a question. Do not rely on prior knowledge.\
 
 """,
     verbose=True,
 )

作为比较,我们定义了一个“简单”的RAG管道,它将所有文档转储到单个矢量索引集合中。设置top_k = 4

代码语言:javascript

 base_index = VectorStoreIndex(all_nodes)
 base_query_engine = base_index.as_query_engine(similarity_top_k=4)

让我们运行一些示例查询,对比单个文档的QA /摘要到多个文档的QA /摘要。

代码语言:javascript

 response = top_agent.query("Tell me about the arts and culture in Boston")

结果如下:

代码语言:javascript

 === Calling Function ===
 Calling function: tool_Boston with args: {
   "input": "arts and culture"
 }
 === Calling Function ===
 Calling function: vector_tool with args: {
   "input": "arts and culture"
 }
 Got output: Boston is known for its vibrant arts and culture scene. The city is home to a number of performing arts organizations, including the Boston Ballet, Boston Lyric Opera Company, Opera Boston, Boston Baroque, and the Handel and Haydn Society. There are also several theaters in or near the Theater District, such as the Cutler Majestic Theatre, Citi Performing Arts Center, the Colonial Theater, and the Orpheum Theatre. Boston is a center for contemporary classical music, with groups like the Boston Modern Orchestra Project and Boston Musica Viva. The city also hosts major annual events, such as First Night, the Boston Early Music Festival, and the Boston Arts Festival. In addition, Boston has several art museums and galleries, including the Museum of Fine Arts, the Isabella Stewart Gardner Museum, and the Institute of Contemporary Art.
 ========================
 Got output: Boston is renowned for its vibrant arts and culture scene. It is home to numerous performing arts organizations, including the Boston Ballet, Boston Lyric Opera Company, Opera Boston, Boston Baroque, and the Handel and Haydn Society. The city's Theater District houses several theaters, such as the Cutler Majestic Theatre, Citi Performing Arts Center, the Colonial Theater, and the Orpheum Theatre.
 
 Boston is also a hub for contemporary classical music, with groups like the Boston Modern Orchestra Project and Boston Musica Viva. The city hosts major annual events, such as First Night, the Boston Early Music Festival, and the Boston Arts Festival, which contribute to its cultural richness.
 
 In terms of visual arts, Boston boasts several art museums and galleries. The Museum of Fine Arts, the Isabella Stewart Gardner Museum, and the Institute of Contemporary Art are among the most notable. These institutions offer a wide range of art collections, from ancient to contemporary, attracting art enthusiasts from around the world.
 ========================

下面我们看看上面的简单RAG管道的结果

代码语言:javascript

 # baseline
 response = base_query_engine.query(
     "Tell me about the arts and culture in Boston"
 )
 print(str(response))
 
 Boston has a rich arts and culture scene. The city is home to a variety of performing arts organizations, such as the Boston Ballet, Boston Lyric Opera Company, Opera Boston, Boston Baroque, and the Handel and Haydn Society. Additionally, there are numerous contemporary classical music groups associated with the city's conservatories and universities, like the Boston Modern Orchestra Project and Boston Musica Viva. The Theater District in Boston is a hub for theater, with notable venues including the Cutler Majestic Theatre, Citi Performing Arts Center, the Colonial Theater, and the Orpheum Theatre. Boston also hosts several significant annual events, including First Night, the Boston Early Music Festival, the Boston Arts Festival, and the Boston gay pride parade and festival. The city is renowned for its historic sites connected to the American Revolution, as well as its art museums and galleries, such as the Museum of Fine Arts, Isabella Stewart Gardner Museum, and the Institute of Contemporary Art.

可以看到我们构建的多代理系统的结果要好的多。

总结

RAG系统必须发展多代理体系结构以实现企业级性能。正如这个例子所说明的,划分职责可以在相关性、速度、摘要质量和及时优化方面获得收益。通过将RAG分解为专门的协作角色,多代理系统可以克服单代理的限制,并启用可扩展的高性能RAG。

如何系统的去学习大模型LLM ?

大模型时代,火爆出圈的LLM大模型让程序员们开始重新评估自己的本领。 “AI会取代那些行业?”“谁的饭碗又将不保了?”等问题热议不断。

事实上,抢你饭碗的不是AI,而是会利用AI的人。

科大讯飞、阿里、华为等巨头公司发布AI产品后,很多中小企业也陆续进场!超高年薪,挖掘AI大模型人才! 如今大厂老板们,也更倾向于会AI的人,普通程序员,还有应对的机会吗?

与其焦虑……

不如成为「掌握AI工具的技术人」,毕竟AI时代,谁先尝试,谁就能占得先机!

但是LLM相关的内容很多,现在网上的老课程老教材关于LLM又太少。所以现在小白入门就只能靠自学,学习成本和门槛很高。

针对所有自学遇到困难的同学们,我帮大家系统梳理大模型学习脉络,将这份 LLM大模型资料 分享出来:包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、LLM大模型学习路线、开源大模型学习教程等, 😝有需要的小伙伴,可以 扫描下方二维码领取🆓↓↓↓

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

一、LLM大模型经典书籍

AI大模型已经成为了当今科技领域的一大热点,那以下这些大模型书籍就是非常不错的学习资源。

在这里插入图片描述

二、640套LLM大模型报告合集

这套包含640份报告的合集,涵盖了大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。(几乎涵盖所有行业)

在这里插入图片描述

三、LLM大模型系列视频教程

在这里插入图片描述

四、LLM大模型开源教程(LLaLA/Meta/chatglm/chatgpt)

在这里插入图片描述

LLM大模型学习路线

阶段1:AI大模型时代的基础理解

  • 目标:了解AI大模型的基本概念、发展历程和核心原理。

  • 内容

    • L1.1 人工智能简述与大模型起源
    • L1.2 大模型与通用人工智能
    • L1.3 GPT模型的发展历程
    • L1.4 模型工程
    • L1.4.1 知识大模型
    • L1.4.2 生产大模型
    • L1.4.3 模型工程方法论
    • L1.4.4 模型工程实践
    • L1.5 GPT应用案例

阶段2:AI大模型API应用开发工程

  • 目标:掌握AI大模型API的使用和开发,以及相关的编程技能。

  • 内容

    • L2.1 API接口
    • L2.1.1 OpenAI API接口
    • L2.1.2 Python接口接入
    • L2.1.3 BOT工具类框架
    • L2.1.4 代码示例
    • L2.2 Prompt框架
    • L2.3 流水线工程
    • L2.4 总结与展望

阶段3:AI大模型应用架构实践

  • 目标:深入理解AI大模型的应用架构,并能够进行私有化部署。

  • 内容

    • L3.1 Agent模型框架
    • L3.2 MetaGPT
    • L3.3 ChatGLM
    • L3.4 LLAMA
    • L3.5 其他大模型介绍

阶段4:AI大模型私有化部署

  • 目标:掌握多种AI大模型的私有化部署,包括多模态和特定领域模型。

  • 内容

    • L4.1 模型私有化部署概述
    • L4.2 模型私有化部署的关键技术
    • L4.3 模型私有化部署的实施步骤
    • L4.4 模型私有化部署的应用场景

这份 LLM大模型资料 包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、LLM大模型学习路线、开源大模型学习教程等, 😝有需要的小伙伴,可以 扫描下方二维码领取🆓↓↓↓

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值