AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


实战精选 | 使用 OpenVINO™ 和 LlamaIndex 构建 Agentic-RAG 系统
发布日期:2024-07-22 20:47:56 浏览次数: 1711


背景


RAG 系统的全称是 Retrieval-augmented Generation,本质上是 Prompt Engineering,通过在 Prompt 中注入检索得到的外部数据,可以有效地解决大语言模型在知识时效性和专业性上的不足。但同时传统的 RAG 系统也有它的缺陷,例如灵活性较差,由于 RAG 会过分依赖于向量数据库的检索结果,导致其在解决一些复杂问题的时候,只是一味地 “搬运” 检索结果,无法通过推理找到更优的解决途径,此外随着向量数据库的规模增大,传统 RAG 也无法高效对输入请求进行分类和过滤,导致检索过程犹如“大海捞针”,费时费力。

图:Agentic-RAG系统示例


而基于 AI 智能体的 RAG 系统(以下简称 Agentic-RAG )恰好可以解决传统 RAG 在灵活性上的不足,它通过将多个不同类别的 RAG 检测器,以工具的形式集成在 AI 智能体中,让 AI 智能体根据用户的请求,判断是否需要调用 RAG 搜索上下文,以及调用哪个 RAG 工具进行检索,例如在回答一个历史相关的问题时,Agentic-RAG 就会优先在历史类的 RAG 检索器中搜索答案,又或是在回答一个涉及数学计算的问题时,Agentic-RAG 则不会使用 RAG,而是调用数据计算相关的工具,甚至如果 LLM 本身具备一定的数据运算能力话,则完全不需要调用外部工具,直接输出答案。当然我们也可以将 RAG 和其他外部工具结合起来,协同解决更复杂的问题,如上图所示,在这个过程中,AI智能 体会将任务拆解后,在每个步骤中分别调用不同的工具,或是 RAG 组件来输出最终答案。接下来我们就一起看下如何利用 OpenVINO™ 和 LlamaIndex 工具来构建一个 Agentic-RAG 系统。

完整示例:

https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-rag-llamaindex/llm-rag-llamaindex.ipynb

第一步:模型转换与量化


LLM 和 Embedding 模型是 RAG系统 中必要的组件,这里我们可以通过 Optimum-intel CLI 分别把他们转化为 OpenVINO™ 的 IR 格式,并进行量化压缩。


  • 安装方法:

pip install optimum[openvino]


  • LLM:

optimum-cli export openvino --model {llm_model_id} --task text-generation-with-past --trust-remote-code --weight-format int4 {llm_model_path}


  • Embedding:

optimum-cli export openvino --model {embedding_model_id} --task feature-extraction {embedding_model_path}


第二步:模型任务初始化


目前基于 OpenVINO™ 的 LLM,Embedding 以及 Reranker 任务均已被集成在 LlamaIndex 框架中,开发者可以非常方便地利用导出的 LLM 和 Embedding 模型,将这两类任务在 LlamaIndex 中进行初始化。


  • 安装方法:

pip install llama-index llama-index-llms-openvino llama-index-embeddings-openvino


  • LLM:

from llama_index.llms.openvino import OpenVINOLLM

llm = OpenVINOLLM(    model_name=str(llm_model_path),    tokenizer_name=str(llm_model_path),    context_window=3900,    max_new_tokens=1000,    model_kwargs={"ov_config": ov_config},    device_map=llm_device.value,    completion_to_prompt=completion_to_prompt,)


  • Embedding:

from llama_index.embeddings.huggingface_openvino import OpenVINOEmbedding
embedding = OpenVINOEmbedding(folder_name=embedding_model_path, device=embedding_device.value)


第三步:构建RAG工具


接下来我们可以利用初始化后的 LLM 以及 Embedding 组件来构建 RAG 工具。第一步需要在 LlamaIndex 创建一个标准的 RAG 检索引擎,为了方便演示,该检索器仅使用默认的向量相似度搜索方式进行上下文过滤,如果想了解更完整的 RAG 搭建方法,可以参考 OpenVINO™ notebooks 仓库中的另一个示例:

https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-rag-llamaindex


from llama_index.readers.file import PyMuPDFReaderfrom llama_index.core import VectorStoreIndex, Settingsfrom llama_index.core.tools import FunctionTool
Settings.embed_model = embeddingSettings.llm = llmloader = PyMuPDFReader()documents = loader.load(file_path=text_example_en_path)index = VectorStoreIndex.from_documents(documents)query_engine = index.as_query_engine(similarity_top_k=2)


在完成 RAG 检索引擎创建后,可以直接调用 LlamaIndex 的接口将它包装为一个 Agent 的工具,如下所示,同时需要添加对该工具的描述,以便 LLM 判断在什么时候调用什么工具。

from llama_index.core.tools import QueryEngineTool
budget_tool = QueryEngineTool.from_defaults(    query_engine,    name="Xeon6",    description="A RAG engine with some basic facts about Intel Xeon 6 processors with E-cores",)


此外,为了演示 Agentic-RAG 对于复杂任务的拆解与多工具间的路由能力,我们还可以再准备两个单独的数学运算工具,供 LLM 选择。

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)


第四步:构建 Agent 任务流水线


因为该示例中用到的 Llama3 还不支持 Function-call,所以这里我们可以创建了一个基于 ReAct 的 Agent 。在 LlamaIndex中搭建 Agent 流水线只需要一行代码,通过 ReAct Agent.from_tools 接口可以创建一个基础的 ReAct Agent ,并将刚才定义好的工具及 LLM 组件绑定到该 Agent 中。

agent = ReActAgent.from_tools([multiply_tool, add_tool, budget_tool], llm=llm, verbose=True)


接下来可以测试下效果,我们向 Agent 咨询了关于“4颗第六代 Xeon  CPU 最大线程数“的问题,可以看到 Agent 首先会调用 Xeon 6 的 RAG 系统查询单颗 CPU 支持的最大线程数,然后再调用数学运算工具将获得的线程数乘以4,最后将得到的数字反馈给用户。

response = agent.chat("What's the maximum number of cores in an Intel Xeon 6 processor server with 4 sockets ? 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: Xeon6

Action Input: {'input': 'maximum cores in a single socket'}

Observation:

 

According to the provided context information, the maximum cores in a single socket is 144.

Thought: The current language of the user is English. I need to use a tool to help me answer the question.

Action: multiply

Action Input: {'a': 144, 'b': 4}

Observation: 576

Thought: The current language of the user is English. I can answer without using any more tools. I'll use the user's language to answer

Answer: The maximum number of cores in an Intel Xeon 6 processor server with 4 sockets is 576.


OpenVINO™


总结和展望


通过将 Agent 和 RAG 进行结合,我们直接提升 LLM 在解决复杂任务时的能力,相较于传统的 RAG,Agentic-RAG 更具产业落地价值。同时随着多智能体方法的引入,基于 Agent 的 RAG 将逐步取代传统 RAG 系统,实现更灵活,更精确的大语言模型应用业务体系。


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询