llamaindex实战-Agent-在Agent中使用RAG查询(本地部署)
概述
本文说明如何在Agent中加入RAG查询引擎,这样就可以在Agent中使用外部的知识库,从而让Agent可以进行外部知识库中数据的查询,让Agent更加强大。
这种模式在很多场景下都很有用,比如:我们很多时候需要先查询或计算某个指标后,若数据或指标符合某个条件时,再通过工具函数来进行处理。该文的实验是通过llamaindex自带的一个例子来完成,我们可以通过该例子结合自己的业务来进行拓展。
本文运行的代码完全是在x86机器上,并且都是在本地部署跑通,不依赖openai的任何接口,完全免费。
实现思路
为了演示如何在Agent中使用 RAG 引擎,我们将创建一个非常简单的 RAG 查询引擎。我们的源数据将是维基百科上关于 2023 年加拿大联邦预算的页面,并已将其打印为 PDF 格式。
通过rag和agent结合,可以先从文档数据中获取到对应的数据,让后再调用tools函数,来对文档中读取到的数据进行处理。
注意:这些步骤都是大模型根据语义的理解选取合适的工具,自动执行的。而我们只需要给我们的代码指令,告诉它,我们想要什么即可。
运行环境
我的环境是一台虚拟机,没有GPU,配置如下:
cpu类型: x86
核数:16核
内存:32G
实现步骤
准备好本地嵌入模式
从huggingface或其他地方下载bge-base-en-v1.5模型的所有文件到以下目录:
/opt/models/BAAI/bge-base-en-v1.5
准备本地大模型
直接可以通过ollama来获取大模型到本地,我这里使用的是gemm2。
ollama pull gemma2
把RAG查询引擎加入到Agent工具列表中
\# rag pipeline as a tool
budget\_tool \= QueryEngineTool.from\_defaults(
query\_engine,
name\="canadian\_budget\_2023",
description\="A RAG engine with some basic facts about the 2023 Canadian federal budget."
)
\# 把rag查询引擎作为agent的工具,加入到agent的工具列表中
agent \= ReActAgent.from\_tools(\[multiply\_tool, add\_tool, budget\_tool\], verbose\=True)
我们要查询的文档内容如下

把https://en.wikipedia.org/wiki/2023_Canadian_federal_budget保存成pdf文档,内容如上图所示,并保存到./agent_rag_data目录下。
代码实现
首先通过ollama下载大模型到本地,并下载并保存本地嵌入模型,我这里下载并使用的是:bge-base-en-v1.5模型,该模型所有的文件都保存在/opt/models/BAAI/bge-base-en-v1.5目录中。
from llama\_index.core.agent import ReActAgent
from llama\_index.llms.openai import OpenAI
from llama\_index.core.tools import FunctionTool
from llama\_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
from llama\_index.core.tools import QueryEngineTool
from llama\_index.llms.ollama import Ollama
from llama\_index.embeddings.huggingface import HuggingFaceEmbedding
from llama\_index.llms.ollama import Ollama
\# 创建本地大模型
#Settings.llm = Ollama(model="llama3.2", request\_timeout=360)
Settings.llm \= Ollama(model\="gemma2", request\_timeout\=360)
local\_model \= "/opt/models/BAAI/bge-base-en-v1.5"
\# 创建本地嵌入大模型
Settings.embed\_model \= HuggingFaceEmbedding(model\_name\=local\_model)
\# 创建其他数据处理的工具函数
def multiply(a: float, b: float) \-> float:
"""Multiply two numbers and returns the product"""
return a \* b
multiply\_tool \= FunctionTool.from\_defaults(fn\=multiply)
def add(a: float, b: float) \-> float:
"""Add two numbers and returns the sum"""
return a + b
add\_tool \= FunctionTool.from\_defaults(fn\=add)
\# 构建知识库查询引擎
documents \= SimpleDirectoryReader("./agent\_rag\_data").load\_data()
index \= VectorStoreIndex.from\_documents(documents,show\_progress\=True)
query\_engine \= index.as\_query\_engine()
\# test 2
\# rag pipeline as a tool
budget\_tool \= QueryEngineTool.from\_defaults(
query\_engine,
name\="canadian\_budget\_2023",
description\="A RAG engine with some basic facts about the 2023 Canadian federal budget."
)
agent \= ReActAgent.from\_tools(\[multiply\_tool, add\_tool, budget\_tool\], verbose\=True)
response \= agent.chat("What is the total amount of the 2023 Canadian federal budget multiplied by 3? Go step by step, using a tool to do any math."
)
print(response)
通过gemm2本地大模型,输出的结果如下:
python agent\_rag.py
Parsing nodes: 100%|█████████████████████████████████████████████████████████████████████████| 4/4 \[00:00<00:00, 1161.94it/s\]
Generating embeddings: 100%|███████████████████████████████████████████████████████████████████| 4/4 \[00:01<00:00, 2.92it/s\]
\> Running step a8beded3-6a08-4d4d-acec-189c63f0fbf3. Step input: What is the total amount of the 2023 Canadian federal budget multiplied by 3? Go step by step, using a tool to do any math.
Thought: The current language of the user is: English. I need to use a tool to help me answer the question.
Action: canadian\_budget\_2023
Action Input: {'input': 'What is the total amount of the 2023 Canadian federal budget?'}
Observation: $496.9 billion
\> Running step a783cc6d-ee83-4166-af2c-e2ea58333a9a. Step input: None
Thought: I need to multiply this by three.
Action: multiply
Action Input: {'a': 496.9, 'b': 3}
Observation: 1490.6999999999998
\> Running step 8dfa9d5c-c729-454d-bc97-7c59beba99b2. Step input: None
Thought: I can answer without using any more tools. I'll use the user's language to answer
Answer: The total amount of the 2023 Canadian federal budget multiplied by 3 is $1,490.7 billion.
The total amount of the 2023 Canadian federal budget multiplied by 3 is $1,490.7 billion.
从以上结果可以看到,gemm2结合嵌入模型,rag模式从文档中获取到了$496.9 billion这个数据,然后再对该数据乘以3,就得到了最终要求的结果。
注意:可以使用其他大模型试一下,比如:llama3,有时候效果并不是很好,不能得到想要的答案。所以,这个还需要进一步测试和优化,才能更好的使用。
参考资料
https://docs.llamaindex.ai/en/stable/understanding/agent/rag_agent/
文档位置:https://en.wikipedia.org/wiki/2023_Canadian_federal_budget
如何系统的去学习大模型LLM ?
大模型时代,火爆出圈的LLM大模型让程序员们开始重新评估自己的本领。 “AI会取代那些行业?”“谁的饭碗又将不保了?”等问题热议不断。
事实上,抢你饭碗的不是AI,而是会利用AI的人。
继科大讯飞、阿里、华为等巨头公司发布AI产品后,很多中小企业也陆续进场!超高年薪,挖掘AI大模型人才! 如今大厂老板们,也更倾向于会AI的人,普通程序员,还有应对的机会吗?
与其焦虑……
不如成为「掌握AI工具的技术人」,毕竟AI时代,谁先尝试,谁就能占得先机!
但是LLM相关的内容很多,现在网上的老课程老教材关于LLM又太少。所以现在小白入门就只能靠自学,学习成本和门槛很高。
针对所有自学遇到困难的同学们,我帮大家系统梳理大模型学习脉络,将这份 LLM大模型资料 分享出来:包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、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大模型学习路线、开源大模型学习教程等, 😝有需要的小伙伴,可以 扫描下方二维码领取🆓↓↓↓
👉👈
