背景
RAG 系統(tǒng)的全稱(chēng)是 Retrieval-augmented Generation,本質(zhì)上是 Prompt Engineering,通過(guò)在 Prompt 中注入檢索得到的外部數(shù)據(jù),可以有效地解決大語(yǔ)言模型在知識(shí)時(shí)效性和專(zhuān)業(yè)性上的不足。但同時(shí)傳統(tǒng)的 RAG 系統(tǒng)也有它的缺陷,例如靈活性較差,由于 RAG 會(huì)過(guò)分依賴(lài)于向量數(shù)據(jù)庫(kù)的檢索結(jié)果,導(dǎo)致其在解決一些復(fù)雜問(wèn)題的時(shí)候,只是一味地 “搬運(yùn)” 檢索結(jié)果,無(wú)法通過(guò)推理找到更優(yōu)的解決途徑,此外隨著向量數(shù)據(jù)庫(kù)的規(guī)模增大,傳統(tǒng) RAG 也無(wú)法高效對(duì)輸入請(qǐng)求進(jìn)行分類(lèi)和過(guò)濾,導(dǎo)致檢索過(guò)程猶如“大海撈針”,費(fèi)時(shí)費(fèi)力。
圖:Agentic-RAG系統(tǒng)示例
而基于 AI 智能體的 RAG 系統(tǒng)(以下簡(jiǎn)稱(chēng) Agentic-RAG )恰好可以解決傳統(tǒng) RAG 在靈活性上的不足,它通過(guò)將多個(gè)不同類(lèi)別的 RAG 檢測(cè)器,以工具的形式集成在 AI 智能體中,讓 AI 智能體根據(jù)用戶的請(qǐng)求,判斷是否需要調(diào)用 RAG 搜索上下文,以及調(diào)用哪個(gè) RAG 工具進(jìn)行檢索,例如在回答一個(gè)歷史相關(guān)的問(wèn)題時(shí),Agentic-RAG 就會(huì)優(yōu)先在歷史類(lèi)的 RAG 檢索器中搜索答案,又或是在回答一個(gè)涉及數(shù)學(xué)計(jì)算的問(wèn)題時(shí),Agentic-RAG 則不會(huì)使用 RAG,而是調(diào)用數(shù)據(jù)計(jì)算相關(guān)的工具,甚至如果 LLM 本身具備一定的數(shù)據(jù)運(yùn)算能力話,則完全不需要調(diào)用外部工具,直接輸出答案。當(dāng)然我們也可以將 RAG 和其他外部工具結(jié)合起來(lái),協(xié)同解決更復(fù)雜的問(wèn)題,如上圖所示,在這個(gè)過(guò)程中,AI智能 體會(huì)將任務(wù)拆解后,在每個(gè)步驟中分別調(diào)用不同的工具,或是 RAG 組件來(lái)輸出最終答案。接下來(lái)我們就一起看下如何利用 OpenVINO 和 LlamaIndex 工具來(lái)構(gòu)建一個(gè) Agentic-RAG 系統(tǒng)。
完整示例:
https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-rag-llamaindex/llm-rag-llamaindex.ipynb
第一步模型轉(zhuǎn)換與量化
LLM 和 Embedding 模型是 RAG系統(tǒng)中必要的組件,這里我們可以通過(guò) Optimum-intel CLI 分別把他們轉(zhuǎn)化為 OpenVINO 的 IR 格式,并進(jìn)行量化壓縮。
安裝方法:
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:
pip install optimum[openvino]
第二步 模型任務(wù)初始化
目前基于 OpenVINO 的 LLM,Embedding 以及 Reranker 任務(wù)均已被集成在 LlamaIndex 框架中,開(kāi)發(fā)者可以非常方便地利用導(dǎo)出的 LLM 和 Embedding 模型,將這兩類(lèi)任務(wù)在 LlamaIndex 中進(jìn)行初始化。
安裝方法:
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)
第三步 構(gòu)建RAG工具
接下來(lái)我們可以利用初始化后的 LLM 以及 Embedding 組件來(lái)構(gòu)建 RAG 工具。第一步需要在 LlamaIndex 創(chuàng)建一個(gè)標(biāo)準(zhǔn)的 RAG 檢索引擎,為了方便演示,該檢索器僅使用默認(rèn)的向量相似度搜索方式進(jìn)行上下文過(guò)濾,如果想了解更完整的 RAG 搭建方法,可以參考 OpenVINO notebooks 倉(cāng)庫(kù)中的另一個(gè)示例:
https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-rag-llamaindex
from llama_index.readers.file import PyMuPDFReader from llama_index.core import VectorStoreIndex, Settings from llama_index.core.tools import FunctionTool Settings.embed_model = embedding Settings.llm = llm loader = 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 檢索引擎創(chuàng)建后,可以直接調(diào)用 LlamaIndex 的接口將它包裝為一個(gè) Agent 的工具,如下所示,同時(shí)需要添加對(duì)該工具的描述,以便 LLM 判斷在什么時(shí)候調(diào)用什么工具。
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 對(duì)于復(fù)雜任務(wù)的拆解與多工具間的路由能力,我們還可以再準(zhǔn)備兩個(gè)單獨(dú)的數(shù)學(xué)運(yùn)算工具,供 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)
第四步 構(gòu)建 Agent 任務(wù)流水線
因?yàn)樵撌纠杏玫降?Llama3 還不支持 Function-call,所以這里我們可以創(chuàng)建了一個(gè)基于 ReAct 的 Agent 。在 LlamaIndex中搭建 Agent 流水線只需要一行代碼,通過(guò) ReAct Agent.from_tools 接口可以創(chuàng)建一個(gè)基礎(chǔ)的 ReAct Agent ,并將剛才定義好的工具及 LLM 組件綁定到該 Agent 中。
agent = ReActAgent.from_tools([multiply_tool, add_tool, budget_tool], llm=llm, verbose=True)
接下來(lái)可以測(cè)試下效果,我們向 Agent 咨詢(xún)了關(guān)于“4顆第六代 Xeon CPU 最大線程數(shù)“的問(wèn)題,可以看到 Agent 首先會(huì)調(diào)用 Xeon 6 的 RAG 系統(tǒng)查詢(xún)單顆 CPU 支持的最大線程數(shù),然后再調(diào)用數(shù)學(xué)運(yùn)算工具將獲得的線程數(shù)乘以4,最后將得到的數(shù)字反饋給用戶。
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.
總結(jié)和展望
通過(guò)將 Agent 和 RAG 進(jìn)行結(jié)合,我們直接提升 LLM 在解決復(fù)雜任務(wù)時(shí)的能力,相較于傳統(tǒng)的 RAG,Agentic-RAG 更具產(chǎn)業(yè)落地價(jià)值。同時(shí)隨著多智能體方法的引入,基于 Agent 的 RAG 將逐步取代傳統(tǒng) RAG 系統(tǒng),實(shí)現(xiàn)更靈活,更精確的大語(yǔ)言模型應(yīng)用業(yè)務(wù)體系。
-
AI
+關(guān)注
關(guān)注
87文章
31513瀏覽量
270323 -
模型
+關(guān)注
關(guān)注
1文章
3305瀏覽量
49217 -
智能體
+關(guān)注
關(guān)注
1文章
166瀏覽量
10613 -
OpenVINO
+關(guān)注
關(guān)注
0文章
95瀏覽量
225
原文標(biāo)題:使用 OpenVINO? 和 LlamaIndex 構(gòu)建 Agentic-RAG 系統(tǒng)|開(kāi)發(fā)者實(shí)戰(zhàn)
文章出處:【微信號(hào):英特爾物聯(lián)網(wǎng),微信公眾號(hào):英特爾物聯(lián)網(wǎng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論