【多模态】20-基于OpenAI GPT4V和LanceDB的多模态视频RAG系统
案例目标本案例旨在构建一个多模态视频RAG检索增强生成系统能够处理视频内容并基于视频内容回答用户问题。系统使用OpenAI GPT4V多模态大语言模型结合CLIP模型生成多模态嵌入并使用LanceDB向量数据库进行高效的向量存储和检索。通过将视频转换为图像序列和文本转录系统可以基于视频内容进行智能问答。技术栈与核心依赖LlamaIndexLanceDBOpenAI GPT4VCLIPWhisperMoviePyPyTubeSpeechRecognitionpip install llama-index-vector-stores-lancedb pip install llama-index-multi-modal-llms-openai pip install llama-index-embeddings-clip pip install llama_index ftfy regex tqdm pip install -U openai-whisper pip install githttps://github.com/openai/CLIP.git pip install torch torchvision pip install matplotlib scikit-image pip install lancedb pip install moviepy pip install pytube pip install pydub pip install SpeechRecognition pip install ffmpeg-python pip install soundfile环境配置环境要求Python 3.8PyTorch 1.12OpenAI API密钥用于GPT4VLanceDB向量数据库import os OPENAI_API_KEY YOUR_OPENAI_API_KEY os.environ[OPENAI_API_KEY] OPENAI_API_KEY案例实现步骤1: 导入必要的库from moviepy.editor import VideoFileClip from pathlib import Path import speech_recognition as sr from pytube import YouTube from pprint import pprint from llama_index.core.indices import MultiModalVectorStoreIndex from llama_index.core import SimpleDirectoryReader, StorageContext from llama_index.vector_stores.lancedb import LanceDBVectorStore from llama_index.multi_modal_llms.openai import OpenAIMultiModal步骤2: 设置视频处理参数# 视频URL和输出路径配置 video_url https://www.youtube.com/watch?vd_qvLDhkg00 output_video_path ./video_data/ output_folder ./mixed_data/ output_audio_path ./mixed_data/output_audio.wav filepath output_video_path input_vid.mp4 Path(output_folder).mkdir(parentsTrue, exist_okTrue)步骤3: 定义视频处理函数def download_video(url, output_path): 从YouTube下载视频 yt YouTube(url) metadata {Author: yt.author, Title: yt.title, Views: yt.views} yt.streams.get_highest_resolution().download( output_pathoutput_path, filenameinput_vid.mp4 ) return metadata def video_to_images(video_path, output_folder): 将视频转换为图像序列 clip VideoFileClip(video_path) clip.write_images_sequence( os.path.join(output_folder, frame%04d.png), fps0.2 ) def video_to_audio(video_path, output_audio_path): 从视频中提取音频 clip VideoFileClip(video_path) audio clip.audio audio.write_audiofile(output_audio_path) def audio_to_text(audio_path): 使用Whisper将音频转换为文本 recognizer sr.Recognizer() audio sr.AudioFile(audio_path) with audio as source: audio_data recognizer.record(source) try: text recognizer.recognize_whisper(audio_data) except sr.UnknownValueError: print(Speech recognition could not understand the audio.) except sr.RequestError as e: print(fCould not request results from service; {e}) return text步骤4: 处理视频数据try: metadata_vid download_video(video_url, output_video_path) video_to_images(filepath, output_folder) video_to_audio(filepath, output_audio_path) text_data audio_to_text(output_audio_path) with open(output_folder output_text.txt, w) as file: file.write(text_data) print(Text data saved to file) file.close() os.remove(output_audio_path) print(Audio file removed) except Exception as e: raise e步骤5: 创建多模态向量存储# 创建文本和图像向量存储 text_store LanceDBVectorStore(urilancedb, table_nametext_collection) image_store LanceDBVectorStore(urilancedb, table_nameimage_collection) storage_context StorageContext.from_defaults( vector_storetext_store, image_storeimage_store ) # 创建多模态索引 documents SimpleDirectoryReader(output_folder).load_data() index MultiModalVectorStoreIndex.from_documents( documents, storage_contextstorage_context, )步骤6: 创建检索器retriever_engine index.as_retriever( similarity_top_k5, image_similarity_top_k5 )步骤7: 定义RAG提示模板import json metadata_str json.dumps(metadata_vid) qa_tmpl_str ( Given the provided information, including relevant images and retrieved context from the video, accurately and precisely answer the query without any additional prior knowledge.\n Please ensure honesty and responsibility, refraining from any racist or sexist remarks.\n ---------------------\n Context: {context_str}\n Metadata for video: {metadata_str} \n ---------------------\n Query: {query_str}\n Answer: )步骤8: 定义检索函数def retrieve(retriever_engine, query_str): retrieval_results retriever_engine.retrieve(query_str) retrieved_image [] retrieved_text [] for res_node in retrieval_results: if isinstance(res_node.node, ImageNode): retrieved_image.append(res_node.node.metadata[file_path]) else: display_source_node(res_node, source_length200) retrieved_text.append(res_node.text) return retrieved_image, retrieved_text步骤9: 执行查询并生成回答# 定义查询 query_str Using examples from video, explain all things covered in video regarding to gaussian function # 检索相关图像和文本 img, txt retrieve(retriever_engineretriever_engine, query_strquery_str) image_documents SimpleDirectoryReader( input_diroutput_folder, input_filesimg ).load_data() context_str .join(txt) # 显示检索到的图像 plot_images(img) # 使用GPT4V生成最终回答 openai_mm_llm OpenAIMultiModal( modelgpt-4o, api_keyOPENAI_API_KEY, max_new_tokens1500 ) response openai_mm_llm.complete( promptqa_tmpl_str.format( context_strcontext_str, query_strquery_str, metadata_strmetadata_str ), image_documentsimage_documents, ) pprint(response.text)案例效果查询结果示例当查询Using examples from video, explain all things covered in video regarding to gaussian function时系统返回了以下回答The video by 3Blue1Brown, titled A pretty reason why Gaussian Gaussian Gaussian, covers several aspects of Gaussian function, also known as the normal distribution. Heres a summary of key points discussed in the video: 1. **Central Limit Theorem**: The video begins by discussing central limit theorem, which states that the sum of multiple copies of a random variable tends to look like a normal distribution. As the number of variables increases, the approximation to a normal distribution becomes better. 2. **Convolution of Random Variables**: The process of adding two random variables is mathematically represented by a convolution of their respective distributions. The video explains concept of convolution and how it is used to find the distribution of sum of two random variables. 3. **Gaussian Function**: The Gaussian function is more complex than just \( e^{-x^2} \). The full formula includes a scaling factor to ensure the area under the curve is 1 (making it a valid probability distribution), a standard deviation parameter \( \sigma \) to describe the spread, and a mean parameter \( \mu \) to shift the center. However, the video focuses on centered distributions with \( \mu 0 \). 4. **Visualizing Convolution**: The video presents a visual method to understand the convolution of two Gaussian functions using diagonal slices on the xy-plane. This method involves looking at the probability density of landing on a point (x, y) as \( f(x) \times g(y) \), where f and g are the two distributions being convolved. 5. **Rotational Symmetry**: A key property of Gaussian function is its rotational symmetry, which is unique to bell curves. This symmetry is exploited in the video to simplify the calculation of convolution. By rotating the graph 45 degrees, the computation becomes easier because the integral only involves one variable. 6. **Result of Convolution**: The video demonstrates that the convolution of two Gaussian functions is another Gaussian function. This is a special property because convolutions typically result in a different kind of function. The standard deviation of the resulting Gaussian is \( \sqrt{2} \times \sigma \) if the original Gaussians had the same standard deviation. 7. **Proof of Central Limit Theorem**: The video explains that the convolution of two Gaussians being another Gaussian is a crucial step in proving the central limit theorem. It shows that the Gaussian function is a fixed point in the space of distributions, and since all distributions with finite variance tend towards a single universal shape, that shape must be the Gaussian. 8. **Connection to Pi**: The video also touches on the connection between the Gaussian function and the number Pi, which appears in the formula for the normal distribution. The video aims to provide an intuitive geometric argument for why the sum of two normally distributed random variables is also normally distributed, and how this relates to the central limit theorem and the special properties of the Gaussian function.系统成功检索到了与高斯函数相关的视频帧和文本内容并基于这些内容生成了详细的回答涵盖了视频中关于高斯函数的所有要点。案例实现思路本案例的实现思路基于以下核心概念视频多模态处理将视频转换为图像序列和文本转录实现视频内容的多模态表示。多模态嵌入使用CLIP模型生成图像嵌入使用文本嵌入模型生成文本嵌入将不同模态的数据映射到同一向量空间。向量存储与检索使用LanceDB向量数据库存储文本和图像的嵌入向量支持高效的相似性搜索。多模态RAG结合检索到的文本和图像内容使用GPT4V多模态大语言模型生成回答。提示工程设计专门的提示模板引导模型基于检索到的内容回答问题避免使用先验知识。系统架构如下图所示视频图像序列嵌入音频文本转录文本嵌入向量存储检索回答扩展建议功能扩展视频摘要生成自动生成视频内容的摘要便于用户快速了解视频主题时间戳定位基于查询内容定位视频中相关的时间戳实现精确的内容定位多视频检索支持同时检索多个视频内容实现跨视频的知识整合交互式查询开发交互式界面支持用户与视频内容进行更自然的交互多语言支持支持多语言视频内容的处理和查询性能优化增量索引实现视频内容的增量索引支持动态添加新视频分布式处理将视频处理和索引过程分布到多台机器提高处理效率缓存机制实现查询结果缓存减少重复查询的计算开销向量压缩使用产品量化等技术压缩向量减少存储空间总结本案例成功实现了一个基于OpenAI GPT4V和LanceDB的多模态视频RAG系统。该系统能够处理视频内容将其转换为图像序列和文本转录并基于这些内容回答用户问题。通过使用LlamaIndex框架和LanceDB向量数据库我们构建了一个高效、可扩展的多模态视频检索解决方案。该系统的核心优势包括支持视频内容的多模态处理和理解基于向量嵌入的高效相似性搜索结合文本和图像内容生成准确回答可扩展的架构易于添加新的视频内容未来该系统可以进一步扩展以支持更复杂的视频处理任务实现更精确的内容定位和更丰富的交互方式为用户提供更好的视频内容理解和检索体验。

相关新闻

经典配准算法一:ICP算法

经典配准算法一:ICP算法

1.定义:ICP算法的基本原理是:迭代最近点(Iterative Closest Point,ICP)算法是一种经典的三维点云配准算法,其核心思想是通过不断建立源点云与目标点云之间的对应点关系,并迭代求解两组点云之间的…

2026/10/18 9:04:36 阅读更多 →
Skills vs MCP:Agent能力扩展的双螺旋

Skills vs MCP:Agent能力扩展的双螺旋

摘要:Skills和MCP是Agent能力扩展的两大路径,各有优劣。本文深度对比Skills与MCP的架构差异、开发模式、适用场景,探讨两者融合使用的双螺旋模型。 Skills vs MCP Agent能力扩展的双螺旋 我在做一个数据分析Agent项目的时候,遇到…

2026/10/18 20:47:39 阅读更多 →
【关注可白嫖源码】--课程设计--毕业设计--springboot智能校园综合管理系统[编号:project68296](案件分析)

【关注可白嫖源码】--课程设计--毕业设计--springboot智能校园综合管理系统[编号:project68296](案件分析)

本文仅展示核心实现逻辑与部分代码片段,完整项目源码、配套文档、数据库脚本内容较多,篇幅有限无法全部放出。 有需要完整资源的同学,可以在评论区留言【资料或领源码】,我会一 一回复站内私信,发送完整文件 摘 要 校园…

2026/10/18 17:25:42 阅读更多 →

最新新闻

open-code-review:可审计、可干预的开源代码审查框架

open-code-review:可审计、可干预的开源代码审查框架

1. 这不是又一个“AI代码审查工具”,而是开发者自己掌控审查权的起点“open-code-review”这个项目名乍看平平无奇,甚至有点像某个被遗忘在GitHub角落的冷门仓库——没有炫酷的UI,不提“秒级修复”,也不承诺“零Bug交付”。但如果…

2026/10/19 0:31:03 阅读更多 →
Hermes Agent 中 Skills 与 Tools 的关系分析:从 TaoToken 统一 Key 通道看能力编排

Hermes Agent 中 Skills 与 Tools 的关系分析:从 TaoToken 统一 Key 通道看能力编排

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/19 0:31:03 阅读更多 →
【AI】codex 使用教程:从 AGENTS.md 到 skills 的完整配置指南

【AI】codex 使用教程:从 AGENTS.md 到 skills 的完整配置指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/19 0:29:02 阅读更多 →
DeepThink V1.1.0 开源发布:用 TaoToken 统一 Key 打造个人 AI 操作系统,全自主托管跑通超复杂长程任务

DeepThink V1.1.0 开源发布:用 TaoToken 统一 Key 打造个人 AI 操作系统,全自主托管跑通超复杂长程任务

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/19 0:28:02 阅读更多 →
优秀的大模型API网关系统Veloera:Docker部署与TaoToken统一Key接入实践

优秀的大模型API网关系统Veloera:Docker部署与TaoToken统一Key接入实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/19 0:28:02 阅读更多 →
Java批量重命名命令行工具:正则匹配与安全校验实战

Java批量重命名命令行工具:正则匹配与安全校验实战

简介:针对Java文件批量重命名的源码包,面向日常开发中需要整理大量文件的Java程序员,提供一套简单直接的解决方案;整个资源为一个压缩包,体积仅1KB,内含1个Java源文件,代码结构紧凑,…

2026/10/19 0:28:02 阅读更多 →

日新闻

频率跟踪型PWM电源设计:臭氧发生器谐振点漂移与软开关实现

频率跟踪型PWM电源设计:臭氧发生器谐振点漂移与软开关实现

简介:本资源为电源技术方向的学术研究文档,聚焦介质阻挡放电型臭氧发生器电源的频率跟踪型PWM控制策略,面向电力电子、高压电源及臭氧应用领域的科研人员与工程技术人员。针对负载呈容性、等效电容随外加电压升高而增大、谐振频率随之漂移的难…

2026/10/19 0:00:47 阅读更多 →
Vibe Coding 遇上 TaoToken:把开发者思维圣殿的灵感流接入统一 Key 通道

Vibe Coding 遇上 TaoToken:把开发者思维圣殿的灵感流接入统一 Key 通道

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/19 0:00:47 阅读更多 →
HundredCV-Chat话题建模实战教程:利用4,871个内置话题标签构建主题分析系统

HundredCV-Chat话题建模实战教程:利用4,871个内置话题标签构建主题分析系统

【免费下载链接】HundredCV-Chat 项目地址: https://ai.gitcode.com/hf_mirrors/Jax-dan/HundredCV-Chat 点击查看 免费下载 HundredCV-Chat 是一个由 100 位青年简历数据开发而来的中文多轮对话数据集,共包含 24,750 组日常闲聊对话和 4,871 个话题标签…

2026/10/19 0:00:47 阅读更多 →

周新闻

基于GMM的语音识别系统原理与MATLAB实现详解

基于GMM的语音识别系统原理与MATLAB实现详解

说到基于高斯混合模型(GMM)的语音识别系统,很多人第一反应就是一堆数学公式,觉得门槛很高。但如果你真的在MATLAB里把它落地跑通,会发现核心其实就是三件事:把声音变成特征向量,用GMM给每个类别…

2026/10/18 0:00:55 阅读更多 →
数字图像处理入门:反白、灰度化、调色板修改与导出txt实践

数字图像处理入门:反白、灰度化、调色板修改与导出txt实践

数字图像处理这四个字听起来挺唬人,但落到具体操作上,无非就是让计算机按你的想法“摆弄”像素。我在一个模拟项目X里用了一套特别经典的基础处理流程:对图像做反白、改调色板、彩色转灰度,最后把像素数据导出成txt文件。单看每一…

2026/10/18 0:00:55 阅读更多 →
机器人大脑技术盘点:具身智能的感知、决策与数据闭环

机器人大脑技术盘点:具身智能的感知、决策与数据闭环

如果你的信息流最近也天天刷到“机器人大脑”这个词,你会发现一个很有意思的现象:每个人说的“大脑”根本不是同一个东西。有人指的是大语言模型,有人指的是运动控制系统,还有人指的是能理解物理世界的端到端模型。在2026年这个时…

2026/10/18 0:00:55 阅读更多 →

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/18 0:55:02 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/17 17:22:31 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/18 12:16:16 阅读更多 →