來源:OpenVINO 中文社區(qū)
介紹
Retrieval-Augmented Generation (RAG) 系統(tǒng)可以通過從知識庫中過濾關(guān)鍵信息來優(yōu)化 LLM 任務(wù)的內(nèi)存占用及推理性能。歸功于文本解析、索引和檢索等成熟工具的應(yīng)用,為文本內(nèi)容構(gòu)建 RAG 流水線已經(jīng)相對成熟。然而為視頻內(nèi)容構(gòu)建 RAG 流水線則困難得多。由于視頻結(jié)合了圖像,音頻和文本元素,因此需要更多和更復(fù)雜的數(shù)據(jù)處理能力。本文將介紹如何利用 OpenVINO 和 LlamaIndex 工具構(gòu)建應(yīng)用于視頻理解任務(wù)的RAG流水線。
要構(gòu)建真正的多模態(tài)視頻理解RAG,需要處理視頻中不同模態(tài)的數(shù)據(jù),例如語音內(nèi)容、視覺內(nèi)容等。在這個例子中,我們展示了專為視頻分析而設(shè)計的多模態(tài) RAG 流水線。它利用 Whisper 模型將視頻中的語音內(nèi)容轉(zhuǎn)換為文本內(nèi)容,利用 CLIP 模型生成多模態(tài)嵌入式向量,利用視覺語言模型(VLM)處理檢索到的圖像和文本消息以及用戶請求。下圖詳細(xì)說明了該流水線的工作原理。
圖:視頻理解 RAG 工作原理
源碼地址:
https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/multimodal-rag
環(huán)境準(zhǔn)備
該示例基于 Jupyter Notebook 編寫,因此我們需要準(zhǔn)備好相對應(yīng)的 Python 環(huán)境。基礎(chǔ)環(huán)境可以參考以下鏈接安裝,并根據(jù)自己的操作系統(tǒng)進(jìn)行選擇具體步驟。
https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-getting-started
圖:基礎(chǔ)環(huán)境安裝導(dǎo)航頁面
此外本示例將依賴 OpenVINO 和 LlamaIndex 的集成組件,因此我們需要單獨在環(huán)境中對他們進(jìn)行安裝,分別是用于為圖像和文本生成多模態(tài)向量的llama-index-embeddings-openvino庫,以及視覺多模態(tài)推理llama-index-multi-modal-llms-openvino庫。
模型下載和轉(zhuǎn)換
完成環(huán)境搭建后,我們需要逐一下載流水線中用到的語音識別 ASR 模型,多模型向量化模型 CLIP,以及視覺語言模型模型 VLM。
考慮到精度對模型準(zhǔn)確性的影響,在這個示例中我們直接從 OpenVINO HuggingFace 倉庫中,下載轉(zhuǎn)換以后的 ASR int8 模型。
import huggingface_hub as hf_hub asr_model_id = "OpenVINO/distil-whisper-large-v3-int8-ov" asr_model_path = asr_model_id.split("/")[-1] if not Path(asr_model_path).exists(): hf_hub.snapshot_download(asr_model_id, local_dir=asr_model_path)
而 CLIP 及 VLM 模型則采用 Optimum-intel 的命令行工具,通過下載原始模型對它們進(jìn)行轉(zhuǎn)換和量化。
from cmd_helper import optimum_cli clip_model_id = "laion/CLIP-ViT-B-32-laion2B-s34B-b79K" clip_model_path = clip_model_id.split("/")[-1] if not Path(clip_model_path).exists(): optimum_cli(clip_model_id, clip_model_path)
視頻數(shù)據(jù)提取與處理
接下來我們需要使用第三方工具提取視頻文件中的音頻和圖片,并利用 ASR 模型將音頻轉(zhuǎn)化為文本,便于后續(xù)的向量化操作。在這一步中我們選擇了一個關(guān)于高斯分布的科普視頻作為示例(https://www.youtube.com/watch?v=d_qvLDhkg00)。可以參考以下代碼片段,完成對 ASR 模型的初始化以及音頻內(nèi)容識別。識別結(jié)果將被以 .txt 文件格式保存在本地。
from optimum.intel import OVModelForSpeechSeq2Seq from transformers import AutoProcessor, pipeline asr_model = OVModelForSpeechSeq2Seq.from_pretrained(asr_model_path, device=asr_device.value) asr_processor = AutoProcessor.from_pretrained(asr_model_path) pipe = pipeline("automatic-speech-recognition", model=asr_model, tokenizer=asr_processor.tokenizer, feature_extractor=asr_processor.feature_extractor) result = pipe(en_raw_speech, return_timestamps=True)
創(chuàng)建多模態(tài)向量索引
這也是整個 RAG 鏈路中最關(guān)鍵的一步,將視頻文件中獲取的文本和圖像轉(zhuǎn)換為向量數(shù)據(jù),存入向量數(shù)據(jù)庫。這些向量數(shù)據(jù)的質(zhì)量也直接影響后續(xù)檢索任務(wù)中的召回準(zhǔn)確性。這里我們首先需要對 CLIP 模型進(jìn)行初始化,利用 OpenVINO 和 LlamaIndex 集成后的庫可以輕松實現(xiàn)這一點。
from llama_index.embeddings.huggingface_openvino import OpenVINOClipEmbedding clip_model = OpenVINOClipEmbedding(model_id_or_path=clip_model_path, device=clip_device.value)
然后可以直接調(diào)用 LlamaIndex 提供的向量數(shù)據(jù)庫組件快速完成建庫過程,并對檢索引擎進(jìn)行初始化。
from llama_index.core.indices import MultiModalVectorStoreIndex from llama_index.vector_stores.qdrant import QdrantVectorStore from llama_index.core import StorageContext, Settings from llama_index.core.node_parser import SentenceSplitter Settings.embed_model = clip_model index = MultiModalVectorStoreIndex.from_documents( documents, storage_context=storage_context, image_embed_model=Settings.embed_model, transformations=[SentenceSplitter(chunk_size=300, chunk_overlap=30)] ) retriever_engine = index.as_retriever(similarity_top_k=2, image_similarity_top_k=5)
多模態(tài)向量檢索
傳統(tǒng)的文本 RAG 通過檢索文本相似度來召喚向量數(shù)據(jù)庫中關(guān)鍵的文本內(nèi)容,而多模態(tài) RAG 則需要額外對圖片向量進(jìn)行檢索,用以返回與輸入問題相關(guān)性最高的關(guān)鍵幀,供 VLM 進(jìn)一步理解。這里我們會將用戶的提問文本向量化后,通過向量引擎檢索得到與該問題相似度最高的若干個文本片段,以及視頻幀。LlamaIndex 為我們提供了強大的工具組件,通過調(diào)用函數(shù)的方式可以輕松實現(xiàn)以上步驟。
from llama_index.core import SimpleDirectoryReader query_str = "tell me more about gaussian function" img, txt = retrieve(retriever_engine=retriever_engine, query_str=query_str) image_documents = SimpleDirectoryReader(input_dir=output_folder, input_files=img).load_data()
代碼運行后,我們可以看到檢索得到的文本段和關(guān)鍵幀。
圖:檢索返回的關(guān)鍵幀和相關(guān)文本片段
答案生成
多模態(tài) RAG 流水線的最后一步是要將用戶問題,以及檢索到相關(guān)文本及圖像內(nèi)容送入 VLM 模型進(jìn)行答案生成。這里我們選擇微軟的 Phi-3.5-vision-instruct 多模態(tài)模型,以及 OpenVINO 和 LlamaIndex 集后的多模態(tài)模任務(wù)組件,完成圖片及文本內(nèi)容理解。值得注意的是由于檢索返回的關(guān)鍵幀往往包含多張圖片,因此這里需要選擇支持多圖輸入的多模態(tài)視覺模型。以下代碼為 VLM 模型初始化方法。
from llama_index.multi_modal_llms.openvino import OpenVINOMultiModal vlm = OpenVINOMultiModal( model_id_or_path=vlm_int4_model_path, device=vlm_device.value, messages_to_prompt=messages_to_prompt, trust_remote_code=True, generate_kwargs={"do_sample": False, "eos_token_id": processor.tokenizer.eos_token_id}, )
完成 VLM 模型對象初始化后,我們需要將上下文信息與圖片送入 VLM 模型,生成最終答案。此外在這個示例中還構(gòu)建了基于 Gradio 的交互式 demo,供大家參考。
response = vlm.stream_complete( prompt=qa_tmpl_str.format(context_str=context_str, query_str=query_str), image_documents=image_documents, ) for r in response: print(r.delta, end="")
運行結(jié)果如下:
“A Gaussian function, also known as a normal distribution, is a type of probability distribution that is symmetric and bell-shaped. It is characterized by its mean and standard deviation, which determine the center and spread of the distribution, respectively. The Gaussian function is widely used in statistics and probability theory due to its unique properties and applications in various fields such as physics, engineering, and finance. The function is defined by the equation e to the negative x squared, where x represents the input variable. The graph of a Gaussian function is a smooth curve that approaches the x-axis as it moves away from the center, creating a bell-like shape. The function is also known for its property of being able to describe the distribution of random variables, making it a fundamental concept in probability theory and statistics.”
總結(jié)
在視頻內(nèi)容理解任務(wù)中,如果將全部的視頻幀一并送入 VLM 進(jìn)行理解和識別,會對 VLM 性能和資源占用帶來非常大的挑戰(zhàn)。通過多模態(tài) RAG 技術(shù),我們可以首先對關(guān)鍵幀進(jìn)行檢索,從而壓縮在視頻理解任務(wù)中 VLM 的輸入數(shù)據(jù)量,提高整套系統(tǒng)的識別效率和準(zhǔn)確性。而 OpenVINO 與 LlamaIndex 集成后的組件則可以提供完整方案的同時,在本地 PC 端流暢運行流水線中的各個模型。
-
英特爾
+關(guān)注
關(guān)注
61文章
10121瀏覽量
173449 -
流水線
+關(guān)注
關(guān)注
0文章
123瀏覽量
26392 -
模型
+關(guān)注
關(guān)注
1文章
3455瀏覽量
49737 -
OpenVINO
+關(guān)注
關(guān)注
0文章
111瀏覽量
371
原文標(biāo)題:開發(fā)者實戰(zhàn)|如何利用 OpenVINO? 在本地構(gòu)建多模態(tài) RAG 應(yīng)用
文章出處:【微信號:英特爾物聯(lián)網(wǎng),微信公眾號:英特爾物聯(lián)網(wǎng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
【「基于大模型的RAG應(yīng)用開發(fā)與優(yōu)化」閱讀體驗】+第一章初體驗
【「基于大模型的RAG應(yīng)用開發(fā)與優(yōu)化」閱讀體驗】RAG基本概念
為什么無法在RedHat中構(gòu)建OpenVINO? 2022.2?
構(gòu)建開源OpenVINO?工具包后,使用MYRIAD插件成功運行演示時報錯怎么解決?
構(gòu)建開源OpenVINO?工具套件后,模型優(yōu)化器位于何處呢?
從源代碼構(gòu)建OpenVINO?后,無法獲得Open Model Zoo工具怎么解決?
如何為Raspbian Bullseye構(gòu)建開源OpenVINO??

從源代碼構(gòu)建OpenVINO工具套件時報錯怎么解決?
如何使用交叉編譯方法為Raspbian 32位操作系統(tǒng)構(gòu)建OpenVINO工具套件的開源分發(fā)
如何使用Python包裝器正確構(gòu)建OpenVINO工具套件
如何利用LLM做多模態(tài)任務(wù)?

使用OpenVINO和LlamaIndex構(gòu)建Agentic-RAG系統(tǒng)

評論