前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >整合LlamaIndex与LangChain构建高级的查询处理系统

整合LlamaIndex与LangChain构建高级的查询处理系统

作者头像
deephub
发布2024-05-22 10:11:48
1430
发布2024-05-22 10:11:48
举报
文章被收录于专栏:DeepHub IMBADeepHub IMBA

点击上方“Deephub Imba”,关注公众号,好文章不错过 !

构建大型语言模型应用程序可能会颇具挑战,尤其是当我们在不同的框架(如Langchain和LlamaIndex)之间进行选择时。LlamaIndex在智能搜索和数据检索方面的性能令人瞩目,而LangChain则作为一个更加通用的应用程序框架,提供了更好的与各种平台的兼容性。

本篇文章将介绍如何将LlamaIndex和LangChain整合使用,创建一个既可扩展又可定制的代理RAG(Retrieval-Augmented Generation)应用程序,利用两种技术的强大功能,开发出能够处理复杂查询并提供精准答案的高效应用程序。

在我们继续实施之前,需要简单的介绍代理RAG的一些知识:

代理RAG是一种基于代理的RAG实现方式。与传统的一般RAG方法相比,代理RAG在自主性和决策能力方面有了显著提升。我们通过授权大型语言模型(LLM)访问多个RAG查询引擎来创建一个复杂的推理循环。每个查询引擎都作为一种工具,能根据需要被LLM调用。这种结构不仅使得执行复杂决策成为可能,还扩展了系统回答各种查询的能力,并能够更好地为用户提供最适宜的响应。

通过这种方式,代理RAG能够在提供答案的同时,考虑到信息的来源多样性和质量,从而在提供答案时实现更高的准确性和相关性。这种模型的实现,为处理复杂问题和提供创新解决方案提供了更强大的工具。

首先我们定义基本LLM和嵌入模型

代码语言:javascript
复制
 # LLM 
 llm = ChatOpenAI(model_name="gpt-4-1106-preview", temperature=0, streaming=True)
 
 # Embedding Model
 embed_model = OpenAIEmbedding(
     model="text-embedding-3-small", embed_batch_size=100
 )
 
 # Set Llamaindex Configs
 Settings.llm = llm 
 Settings.embed_model = embed_model

然后利用LlamaIndex的索引和检索功能为文档定义单独的查询引擎。

代码语言:javascript
复制
 #Building Indexes for each of the Documents
 try:
     storage_context = StorageContext.from_defaults(
         persist_dir="./storage/lyft"
     )
     lyft_index = load_index_from_storage(storage_context)
 
     storage_context = StorageContext.from_defaults(
         persist_dir="./storage/uber"
     )
     uber_index = load_index_from_storage(storage_context)
 
     index_loaded = True
     print("Index was already created. We just loaded it from the local storage.")
 
 except:
 
     index_loaded = False
     print("Index is not present. We need it to create it again.")
 
 if not index_loaded:
 
     print("Creating Index..")
 
     # load data
     lyft_docs = SimpleDirectoryReader(
         input_files=["./data/10k/lyft_2021.pdf"]
     ).load_data()
     uber_docs = SimpleDirectoryReader(
         input_files=["./data/10k/uber_2021.pdf"]
     ).load_data()
 
     # build index
     lyft_index = VectorStoreIndex.from_documents(lyft_docs)
     uber_index = VectorStoreIndex.from_documents(uber_docs)
 
     # persist index
     lyft_index.storage_context.persist(persist_dir="./storage/lyft")
     uber_index.storage_context.persist(persist_dir="./storage/uber")
     
     index_loaded = True
 
 #Creating Query engines on top of the indexes
 lyft_engine = lyft_index.as_query_engine(similarity_top_k=3)
 uber_engine = uber_index.as_query_engine(similarity_top_k=3)
 
 print("LlamaIndex Query Engines created successfully.")

然后使用LlamaIndex的QueryEngineTool抽象类将查询引擎转换为工具,这些工具将稍后提供给LLM使用。

代码语言:javascript
复制
 #creating tools for each of our query engines
 query_engine_tools = [
 
     QueryEngineTool(
         query_engine=lyft_engine,
         metadata=ToolMetadata(
             name="lyft_10k",
             description=(
                 "Provides information about Lyft financials for year 2021. "
                 "Use a detailed plain text question as input to the tool."
             ),
         ),
     ),
     QueryEngineTool(
         query_engine=uber_engine,
         metadata=ToolMetadata(
             name="uber_10k",
             description=(
                 "Provides information about Uber financials for year 2021. "
                 "Use a detailed plain text question as input to the tool."
             ),
         ),
     ),
 ]

然后我们将LlamaIndex工具转换为与Langchain代理兼容的格式,这样就可以和Langchain 进行对接了。

代码语言:javascript
复制
 llamaindex_to_langchain_converted_tools = [t.to_langchain_tool() for t in query_engine_tools]

除此以外我们还定义了一个附加的带有Web搜索功能的Langchain工具。这样可以进行页面搜索

代码语言:javascript
复制
 search = DuckDuckGoSearchRun()
 
 duckduckgo_tool = Tool(
         name='DuckDuckGoSearch',
         func= search.run,
         description='Use for when you need to perform an internet search to find information that another tool can not provide.'
 )   
 
 langchain_tools = [duckduckgo_tool]
 
 
 #Combine to create final list of tools
 tools = llamaindex_to_langchain_converted_tools + langchain_tools

下面就是Langchain的工作了,初始化调用代理。

代码语言:javascript
复制
 system_context = "You are a stock market expert.\
 You will answer questions about Uber and Lyft companies as in the persona of a veteran stock market investor."
 
 prompt = ChatPromptTemplate.from_messages(
     [
         (
             "system",
             system_context,
         ),
         ("placeholder", "{chat_history}"),
         ("human", "{input}"),
         ("placeholder", "{agent_scratchpad}"),
     ]
 )
 
 # Construct the Tools agent
 agent = create_tool_calling_agent(llm, tools, prompt,)
 
 # Create an agent executor by passing in the agent and tools
 agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, return_intermediate_steps=True, handle_parsing_errors=True, max_iterations=10)

然就就可以进行测试了。

测试1:

代码语言:javascript
复制
 question =  "What was Lyft's revenue growth in 2021?"
 
 response = agent_executor.invoke({"input": question})
 print("\nFinal Response:", response['output'])

代理正确地调用了lyft_10k查询引擎工具。

测试2:

代码语言:javascript
复制
 question =  "Is Uber profitable?"
 
 response = agent_executor.invoke({"input": question})
 print("\nFinal Response:", response['output'])

代理正确调用了uber_10k查询引擎工具。

测试3:

代码语言:javascript
复制
 question =  "List me the names of Uber's board of directors."
 
 response = agent_executor.invoke({"input": question})
 print("\nFinal Response:", response['output'])

我们这个信息超出了任何检索工具的范围,所以代理决定调用外部搜索工具,然后返回结果。

可以看到,我们的例子完美的结合了2者的优势,通过引入多个代理可以进一步提高系统的效率和精准度。每个代理可以专门处理同一领域内的不同文档子集,使得信息检索更为精细和专业。

我们可以设定一名代理来担任这些代理的协调者或主管。这名负责监控和调节各个代理的活动,确保信息流动的协调一致,并对整体查询过程进行优化。这种层次化的管理结构不仅优化了数据处理流程,也提高了响应速度和准确性,使得整个系统在处理复杂查询时更加高效和可靠。

通过这种方法,我们可以实现一个更加动态和适应性强的RAG系统,能够更好地满足不断变化的用户需求和应对多样化的信息挑战。希望本文能帮助你了解如何有效地整合LlamaIndex和LangChain,以构建一个高效、可扩展的代理RAG应用程序。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DeepHub IMBA 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档