Zilliz 01月23日
Claude 3.5 +LlamaIndex+Milvus,六步教你搭建Agentic RAG
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文深入探讨了复合人工智能系统(CAIS)的演变,从最初的RAG到Agent的集成,旨在解决大型语言模型(LLM)的幻觉和知识更新问题。文章详细介绍了RAG和Agent的概念,以及它们在提升系统性能方面的作用。RAG通过向量数据库存储知识,而Agent则在pipeline中加入推理、工具使用等类人步骤。文章还介绍了如何在RAG pipeline中应用Agent,包括路由、查询规划、工具使用、ReAct和动态查询规划等方法。最后,通过一个使用Milvus向量数据库、LlamaIndex和Claude 3.5 Sonnet构建Agentic RAG的实例,展示了如何搭建一个更高效、更可靠的AI系统。

💡 **复合人工智能系统 (CAIS) 的演变**: CAIS 的提出旨在通过集成多种 AI 技术和模块,实现更高效、可靠和可解释的智能系统,它通过分解复杂系统为独立模块,提升了系统的灵活性、可扩展性和可复用性。

📚 **RAG 与 Agent 的概念**: RAG 通过向量数据库存储知识,并将其作为上下文与 LLM 结合,生成更准确的回答;而 Agent 则通过在 pipeline 中添加推理、工具使用等“类人”步骤,完成更复杂的操作。两者结合使用,可以有效解决LLM的局限性。

🛠️ **Agent 在 RAG Pipeline 中的应用**: Agent 在 RAG 中有多种应用方式,包括路由、查询规划、工具使用、ReAct 和动态查询规划。这些方法可以根据不同的需求,灵活地优化查询和响应过程,从而提高系统的整体性能。

🚀 **Agentic RAG 的构建实例**: 文章通过一个具体的实例,演示了如何使用 Milvus 向量数据库、LlamaIndex 和 Claude 3.5 Sonnet 构建一个 Agentic RAG 系统。这个实例展示了从数据加载、索引到查询引擎的完整流程,并对比了有无 Agent 的查询结果,突出了 Agent 的优势。

原创 和你一起进步的 2025-01-21 18:31 上海

手把手教你创建一个Agentic RAG。

过去三年中, OpenAI 的 ChatGPT为代表的基础模型的出现显著加速了 LLM 应用的发展,但是如果只使用 LLM 依赖其“自有”知识来回答问题,往往会出现大模型幻觉,或者知识更新不及时等问题。基于这一背景,使用多个 LLM,每个 LLM 针对不同类型的问题进行优化的解决思路应运而生。但这也会出现一定的局限,那就是让整体系统变得复杂且难以扩展

过去三年中, OpenAI 的 ChatGPT为代表的基础模型的出现显著加速了 LLM 应用的发展,但是如果只使用 LLM 依赖其“自有”知识来回答问题,往往会出现大模型幻觉,或者知识更新不及时等问题。基于这一背景,使用多个 LLM,每个 LLM 针对不同类型的问题进行优化的解决思路应运而生。

但过多的模型拼合,又会导致整体系统变得复杂且难以扩展。那么,要如何解决这个问题呢?

答案复合人工智能系统(CAIS)。接下来,我们将重点解读CAIS的历史演变以及搭建流程。

01.

CAIS的历史演变,从RAG到Agent

这一概念最早是2024年初加州大学伯克利分校的AI研究实验室网站上的一篇题为《从模型到复合人工智能系统的转变》的博客中被提及,它强调通过集成多种AI技术和模块来实现更高效、更可靠、更可解释的智能系统。

比如,我们可以通过在 LLM pipeline 中添加额外组件,进而提升系统性能。一个常见的代表是 RAG,RAG 通过将“知识库”或“上下文”存储在向量数据库(如 Milvus 或 Zilliz Cloud)中,并将其作为上下文与LLM结合,可以生成更准确、相关性更高的回答。

通常来说,构建 RAG 系统的基本步骤如下:

图 1:RAG 的基本步骤

不难发现,以上各个环节,主要基于向量相似性进来完成检索以及内容生成,可一旦分块不够精准,或者分块后的内容与答案相关性不高,就会导致最终生成的答案也会不尽如人意。

与此同时,RAG的另一大局限性在于,对特定模型的过分依赖。比如,我们做一个比较任务,但模型可能是为归纳任务训练的。

在此基础上,Agent应运而生。

LLM  Agent 是复杂的系统,它们通过在 pipeline 中添加了“类人”步骤,如推理、工具使用或规划,可以完成各种复杂操作。

比如,在下图中,LLM Agent 包含多个组件,它们可以在迭代过程中互动,不仅可以做相似性检索,还能完成包括规划、推理、工具使用和记忆等多种功能。

图 2:LLM Agents

目前各种 Agent 架构和框架中,最流行的是 ReAct(推理/行动)。其概念最早出自论文《ReAct: Synergizing Reasoning and Acting in Language Models》。

在ReAct中,主要包括四个步骤:规划/推理(Plan/Think)、行动(act/tools)、评估(observe)和答案生成(answer)。

比较值得一提的是评估环节,在这一环节中,如果模型没有找到答案,它会返回推理步骤或要求用户给出更多额外提示来继续生成答案。

图 3:ReAct 框架

那么,我们如何在 RAG pipeline 中使用这些 Agent 呢?以下是在 RAG pipeline 中五种常见的智能体构建方式:

图 4:LLM 编译器

现在,让我们搭建一个使用 Milvus 向量数据库的简单 Agentic pipeline。

02.

使用 Claude 3.5 Sonnet、LlamaIndex 和 Milvus 构建 Agentic RAG

以下内容是一个使用 LlamaIndex 作为 Agent 框架、Milvus 作为向量数据库、Claude 3.5 Sonnet 作为 LLM 的 Agentic RAG peipeline示例,我们将用其构建一个 Agentic RAG。

步骤 1:数据加载

我们使用 Milvus documentation 2.4.x 的 FAQ 页面作为 RAG 的私有知识库。

!pip install -qq llama-index pymilvus llama-index-vector-stores-milvus llama-index-llms-anthropic
!wget https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip 
!unzip -q /content/milvus_docs_2.4.x_en.zip -d /content/milvus_docs
from llama_index.core import SimpleDirectoryReader
# load documents
documents = SimpleDirectoryReader(
    input_files=["/content/milvus_docs/en/faq/operational_faq.md"]
).load_data()
print("Document ID:", documents[0].doc_id)

步骤 2:环境变量

我们需要导入两个 API 密钥:Anthropic 和 OpenAI。

import os
from google.colab import userdata
os.environ["ANTHROPIC_API_KEY"] = userdata.get('ANTHROPIC_API_KEY')
os.environ["OPENAI_API_KEY"] = userdata.get('OPENAI_API_KEY')

步骤 3:数据索引

使用 Milvus 向量数据库创建文档索引,作为我们的知识库。由于 OpenAI 是 LlamaIndex 中的默认 embedding 模型(可以更改),我们需要在 MilvusVectorStore 中定义相同的维度(dim = 1536)。运行以下代码后,将创建一个本地数据库,其中包含我们的知识库。

from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.milvus import MilvusVectorStore
vector_store = MilvusVectorStore(dim=1536)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)

步骤 4:简单查询引擎

首先测试没有 Agent 的查询引擎。它由 Claude 3.5 Sonnet 提供支持,并在我们的索引中搜索相关内容。

llm = Anthropic(model="claude-3-5-sonnet-20240620")
query_engine = index.as_query_engine(similarity_top_k=5, llm=llm)
res = query_engine.query("What is the maximum vector dimension supported in Milvus?")
print(res)
"""
Output:
Milvus supports vectors with up to 32,768 dimensions by default. However, if you need to work with vectors of even higher dimensionality, you have the option to increase the value of the 'Proxy.maxDimension' parameter. This allows Milvus to accommodate vectors with dimensions exceeding the default limit.
"""

步骤 5:Agent 查询引擎

现在,我们添加 QueryEngineTool,它将作为查询引擎的包装工具,供 Agent 使用。

from llama_index.core import VectorStoreIndex
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.llms.anthropic import Anthropic
llm = Anthropic(model="claude-3-5-sonnet-20240620")
query_engine = index.as_query_engine(similarity_top_k=5, llm=llm)
query_engine_tool = QueryEngineTool(
    query_engine=query_engine,
    metadata=ToolMetadata(
        name="knowledge_base",
        description=("Provides information about Milvus FAQ.""Use a detailed plain text question as input to the tool."
        ),
    ),
)

步骤 6:AI Agent 创建

本例中使用的 Agent 是 LlamaIndex 的 FunctionCallingAgentWorker,它在查询回复上部署 critic reflection,利用查询引擎工具生成改进的答案。

from llama_index.core.agent import FunctionCallingAgentWorker
agent_worker = FunctionCallingAgentWorker.from_tools(
    [query_engine_tool], llm=llm, verbose=True
)
agent = agent_worker.as_agent()
response = agent.chat("What is the maximum vector dimension supported in Milvus?")
print(str(response))
"""
Output:
Added user message to memory: What is the maximum vector dimension supported in Milvus?
=== LLM Response ===
To answer your question about the maximum vector dimension supported in Milvus, I'll need to consult the Milvus FAQ knowledge base. Let me do that for you.
=== Calling Function ===
Calling function: knowledge_base with args: {"
input": "What is the maximum vector dimension supported in Milvus?"}
=== Function Output ===
Milvus supports vectors with up to 32,768 dimensions by default. However, if you need to work with vectors of even higher dimensionality, you have the option to increase the value of the 'Proxy.maxDimension' parameter. This allows Milvus to accommodate vectors with dimensions exceeding the default limit.
=== LLM Response ===
Based on the information from the Milvus FAQ knowledge base, I can provide you with the following answer:
The maximum vector dimension supported in Milvus is 32,768 by default. This means that out of the box, Milvus can handle vectors with up to 32,768 dimensions, which is suitable for most applications.
However, it's important to note that Milvus offers flexibility for cases where you might need to work with even higher-dimensional vectors. If your use case requires vectors with dimensions exceeding 32,768, you have the option to increase this limit. This can be done by adjusting the 'Proxy.maxDimension' parameter in Milvus configuration.
So, to summarize:
1. Default maximum dimension: 32,768
2. Can be increased: Yes, by modifying the 'Proxy.maxDimension' parameter
This flexibility allows Milvus to accommodate a wide range of use cases, from typical machine learning and AI applications to more specialized scenarios that might require extremely high-dimensional vectors.
Based on the information from the Milvus FAQ knowledge base, I can provide you with the following answer:
The maximum vector dimension supported in Milvus is 32,768 by default. This means that out of the box, Milvus can handle vectors with up to 32,768 dimensions, which is suitable for most applications.
However, it's important to note that Milvus offers flexibility for cases where you might need to work with even higher-dimensional vectors. If your use case requires vectors with dimensions exceeding 32,768, you have the option to increase this limit. This can be done by adjusting the 'Proxy.maxDimension' parameter in Milvus configuration.
So, to summarize:
1. Default maximum dimension: 32,768
2. Can be increased: Yes, by modifying the 'Proxy.maxDimension' parameter
This flexibility allows Milvus to accommodate a wide range of use cases, from typical machine learning and AI applications to more specialized scenarios that might require extremely high-dimensional vectors.
"""

Agent 的输出提供了更详细的答案,包括信息来源、答案背后的推理过程以及一些与话题相关的额外建议。这帮助我们更好地理解 LLM 模型给出的答案。

03.

Agentic RAG 架构

我们刚刚构建的 Agentic RAG 的完整架构如下所示。

图 5:使用 Milvus、LlamaIndex 和 Claude 3.5 Sonnet 构建的 Agentic RAG 架构

04.

结论

Compund AI 的概念将复杂的人工智能系统分解为多个独立的模块,进而带来了更多的灵活性、可扩展性、可复用性,并降低了系统耦合度,极大提升了 pipeline 的整体效率。沿着这一理念, LLM 系统的演变,经历了从独立模型到结合 RAG 和 Agent 的更复杂架构的转变。

而在此过程中,Milvus为代表的向量数据库技术通过提供强大的数据管理、检索和融合能力,显著提升了系统的效率、准确性和可靠性。它不仅优化了 RAG 和 Agent 等模块的性能,还为多模态数据处理和实时决策提供了支持,并极大提升了系统的灵活性和可扩展性。

如对以上案例感兴趣,或想对milvus做进一步了解,欢迎扫描文末二维码交流进步。

本文作者:Benito Martin


推荐阅读


阅读原文

跳转微信打开

Fish AI Reader

Fish AI Reader

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

FishAI

FishAI

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

联系邮箱 441953276@qq.com

相关标签

Agentic RAG 复合人工智能 向量数据库 LLM Milvus
相关文章