重磅!用 Gemini 2.5 搭载 Cohere Embed v4,视觉RAG 终于不用 Markdown 绕路了!

在多模态AI快速发展的今天,企业在数字化转型中面临着如何高效处理和理解复杂图像信息的挑战。

传统的RAG(Retrieval-Augmented Generation)系统主要依赖于文本数据,对于包含图像、图表、幻灯片等视觉信息的文档处理能力有限,常常需要将图像转换为Markdown格式,导致信息丢失和处理效率低下。

如今,借助Google DeepMind的Gemini 2.5视觉大模型和Cohere最新发布的Embed v4多模态嵌入模型,企业可以构建真正的视觉RAG系统,直接处理和理解复杂图像,跳过繁琐的图像到Markdown转换步骤,保留丰富的视觉信息,实现更高效、准确的多模态信息检索和问答。

一、传统RAG的局限性

传统的RAG系统在处理包含图像的文档时,通常需要将图像转换为Markdown格式,以便进行文本检索和生成。

img

然而,这种方法存在以下问题:

  • 信息丢失:图像中的细节和布局在转换过程中可能无法完整保留,影响理解和生成的准确性。
  • 处理效率低:图像到Markdown的转换过程复杂,增加了系统的处理时间和资源消耗。
  • 适应性差:对于多样化的图像类型(如幻灯片、图表、信息图等),传统方法难以统一处理。

二、视觉RAG的优势

结合Gemini 2.5和Cohere Embed v4的视觉RAG系统,能够直接处理和理解图像信息。

img

视觉RAG系统具有以下优势:

  • 直接检索和理解复杂图像:无需转换,系统可以直接处理幻灯片、图表、图形、信息图等复杂图像,保留原始视觉信息。
  • 提高问答准确性:通过推理复杂图形的内容,提供更准确、详细的答案。
  • 增强多模态检索能力:Embed v4支持多语言和多模态数据的检索,适用于全球化企业的需求。
  • 提升处理效率:跳过图像到Markdown的步骤,简化处理流程,节省时间和资源。

三、技术实现

1. Cohere Embed v4

Cohere Embed v4是最新的多模态嵌入模型。

img

具备以下特性:

  • 多模态支持:能够处理文本、图像、表格、图表等多种数据类型,生成统一的向量表示。
  • 高准确性和效率:在复杂文档(如PDF报告、演示文稿)中实现快速、准确的搜索。
  • 多语言能力:支持100多种语言,包括阿拉伯语、日语、韩语和法语,满足全球企业的需求。
  • 增强的安全性:优化了对金融、医疗保健和制造等受监管行业数据的理解,支持虚拟专用云(VPC)和本地部署,确保数据安全。

img

2. Gemini 2.5 Flash

Gemini 2.5 Flash是Google DeepMind推出的视觉大模型。

img

Gemini 2.5 Flash专注于图像理解和推理,能够与Embed v4结合,实现视觉信息的检索和生成。

四、代码示例

到目前为止,RAG 主要在文本上完成。对于像 PDF 这样的丰富多媒体文件,通常需要复杂的 OCR 预处理步骤,并从您的 RAG 管道中删除相关图形。

以下是一个使用Cohere Embed v4和Gemini 2.5构建视觉RAG系统的简化示例。

img

前往 cohere.com 获取 API 密钥。同时安装我们的 SDK 以简化使用,并安装 pdf2image 和 poppler 将 PDF 转换为图片。

pip install -q cohere

前往谷歌 AI Studio 生成 Gemini 的 API 密钥。Gemini 还提供慷慨的免费层。然后安装谷歌 GenAI SDK。

pip install -q google-genai
from google import genaigemini_api_key = "<<YOUR_GEMINI_KEY>>"  #Replace with your Gemini API keyclient = genai.Client(api_key=gemini_api_key)
# Wrap long longs in this Notebookfrom IPython.display import HTML, displaydef set_css():  display(HTML('''  <style>    pre {        white-space: pre-wrap;    }  </style>  '''))get_ipython().events.register('pre_run_cell', set_css)

本节将加载来自 appeconomyinsights.com 的多个信息图表。对于每张图片,我们将调用 Cohere Embed v4 来获取嵌入。这个嵌入允许我们稍后执行搜索,以找到与我们的问题相关的图片。

import requestsimport osimport ioimport base64import PILimport tqdmimport timeimport numpy as np
# Some helper functions to resize images and to convert them to base64 formatmax_pixels = 1568*1568  #Max resolution for images# Resize too large imagesdef resize_image(pil_image):    org_width, org_height = pil_image.size    # Resize image if too large    if org_width * org_height > max_pixels:        scale_factor = (max_pixels / (org_width * org_height)) ** 0.5        new_width = int(org_width * scale_factor)        new_height = int(org_height * scale_factor)        pil_image.thumbnail((new_width, new_height))# Convert images to a base64 string before sending it to the APIdef base64_from_image(img_path):    pil_image = PIL.Image.open(img_path)    img_format = pil_image.format if pil_image.format else "PNG"    resize_image(pil_image)    with io.BytesIO() as img_buffer:        pil_image.save(img_buffer, format=img_format)        img_buffer.seek(0)        img_data = f"data:image/{img_format.lower()};base64,"+base64.b64encode(img_buffer.read()).decode("utf-8")    return img_data
# Several images from https://www.appeconomyinsights.com/images = {    "tesla.png": "https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef936e6-3efa-43b3-88d7-7ec620cdb33b_2744x1539.png",    "netflix.png": "https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bd84c9-5b62-4526-b467-3088e27e4193_2744x1539.png",    "nike.png": "https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5cd33ba-ae1a-42a8-a254-d85e690d9870_2741x1541.png",    "google.png": "https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F395dd3b9-b38e-4d1f-91bc-d37b642ee920_2741x1541.png",    "accenture.png": "https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b2227c-7dc8-49f7-b3c5-13cab5443ba6_2741x1541.png",    "tecent.png": "https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ec8448c-c4d1-4aab-a8e9-2ddebe0c95fd_2741x1541.png"}
# Download the images and compute an embedding for each imageimg_folder = "img"os.makedirs(img_folder, exist_ok=True)img_paths = []doc_embeddings = []for name, url in tqdm.tqdm(images.items()):    img_path = os.path.join(img_folder, name)    img_paths.append(img_path)    # Download the image    if not os.path.exists(img_path):        response = requests.get(url)        response.raise_for_status()        with open(img_path, "wb") as fOut:            fOut.write(response.content)    # Get the base64 representation of the image    api_input_document = {        "content": [            {"type": "image", "image": base64_from_image(img_path)},        ]    }    # Call the Embed v4.0 model with the image information    api_response = co.embed(        model="embed-v4.0",        input_type="search_document",        embedding_types=["float"],        inputs=[api_input_document],    )    # Append the embedding to our doc_embeddings list    emb = np.asarray(api_response.embeddings.float[0])    doc_embeddings.append(emb)doc_embeddings = np.vstack(doc_embeddings)print("\n\nEmbeddings shape:", doc_embeddings.shape)

以下展示了一个基于视觉的 RAG 的简单流程。

  1. 我们首先执行 search() - 我们计算问题的嵌入表示。然后我们可以使用这个嵌入表示来搜索我们之前嵌入的图像,找到最相关的图像。我们返回这张图像。

  2. 在 answer()函数中,我们将问题+图像发送到 Gemini,以获得我们问题的最终答案。

# Search allows us to find relevant images for a given question using Cohere Embed v4def search(question, max_img_size=800):    # Compute the embedding for the query    api_response = co.embed(        model="embed-v4.0",        input_type="search_query",        embedding_types=["float"],        texts=[question],    )    query_emb = np.asarray(api_response.embeddings.float[0])    # Compute cosine similarities    cos_sim_scores = np.dot(query_emb, doc_embeddings.T)    # Get the most relevant image    top_idx = np.argmax(cos_sim_scores)    # Show the images    print("Question:", question)    hit_img_path = img_paths[top_idx]    print("Most relevant image:", hit_img_path)    image = PIL.Image.open(hit_img_path)    max_size = (max_img_size, max_img_size)  # Adjust the size as needed    image.thumbnail(max_size)    display(image)    return hit_img_path# Answer the question based on the information from the image# Here we use Gemini 2.5 as powerful Vision-LLMdef answer(question, img_path):    prompt = [f"""Answer the question based on the following image.Don't use markdown.Please provide enough context for your answer.Question: {question}""", PIL.Image.open(img_path)]    response = client.models.generate_content(        model="gemini-2.5-flash-preview-04-17",        contents=prompt    )    answer = response.text    print("LLM Answer:", answer)
# Define the queryquestion = "What is the net profit for Nike?"# Search for the most relevant imagetop_image_path = search(question)# Use the image to answer the queryanswer(question, top_image_path)

img

# Define the queryquestion = "What are the 3 largest acquisitions from Google?"# Search for the most relevant imagetop_image_path = search(question)# Use the image to answer the queryanswer(question, top_image_path)

img

# Define the queryquestion = "What would be the net profit of Tesla without interest?"# Search for the most relevant imagetop_image_path = search(question)# Use the image to answer the queryanswer(question, top_image_path)

img

# Define the queryquestion = "Is GenAI a good business for consulting companies?"# Search for the most relevant imagetop_image_path = search(question)# Use the image to answer the queryanswer(question, top_image_path)

img

# Define the queryquestion = "In which region does Netflix generate the highest revenue?"# Search for the most relevant imagetop_image_path = search(question)# Use the image to answer the queryanswer(question, top_image_path)
# Define the queryquestion = "How much could tecent grow their revenue year-over-year for the last 5 years?"# Search for the most relevant imagetop_image_path = search(question)# Use the image to answer the queryanswer(question, top_image_path)

视觉RAG系统在多个行业中具有广泛的应用前景:

  • 金融行业:分析投资者演示文稿、年度财务报告、并购尽职调查文件等,提取关键数据和洞察。

  • 医疗保健:处理病历、流程图、临床试验报告等,辅助医生进行诊断和治疗决策。

  • 制造业:理解产品规格文档、维修指南、供应链计划等,优化生产和维护流程。

  • 教育领域:分析教学幻灯片、图表、信息图等,提升教学质量和学生理解能力。

通过将Gemini 2.5和Cohere Embed v4结合,构建的视觉RAG系统能够直接处理和理解复杂图像信息,跳过传统的图像到Markdown转换步骤,保留丰富的视觉信息,提高问答的准确性和效率,满足企业在多模态数据处理方面的需求。

随着多模态AI技术的不断发展,视觉RAG系统将在更多领域发挥重要作用,助力企业实现数字化转型,提升竞争力。

如何学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段(10天):初阶应用

该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。

  • 大模型 AI 能干什么?
  • 大模型是怎样获得「智能」的?
  • 用好 AI 的核心心法
  • 大模型应用业务架构
  • 大模型应用技术架构
  • 代码示例:向 GPT-3.5 灌入新知识
  • 提示工程的意义和核心思想
  • Prompt 典型构成
  • 指令调优方法论
  • 思维链和思维树
  • Prompt 攻击和防范

第二阶段(30天):高阶应用

该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。

  • 为什么要做 RAG
  • 搭建一个简单的 ChatPDF
  • 检索的基础概念
  • 什么是向量表示(Embeddings)
  • 向量数据库与向量检索
  • 基于向量检索的 RAG
  • 搭建 RAG 系统的扩展知识
  • 混合检索与 RAG-Fusion 简介
  • 向量模型本地部署

第三阶段(30天):模型训练

恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

  • 为什么要做 RAG
  • 什么是模型
  • 什么是模型训练
  • 求解器 & 损失函数简介
  • 小实验2:手写一个简单的神经网络并训练它
  • 什么是训练/预训练/微调/轻量化微调
  • Transformer结构简介
  • 轻量化微调
  • 实验数据集的构建

第四阶段(20天):商业闭环

对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。

  • 硬件选型
  • 带你了解全球大模型
  • 使用国产大模型服务
  • 搭建 OpenAI 代理
  • 热身:基于阿里云 PAI 部署 Stable Diffusion
  • 在本地计算机运行大模型
  • 大模型的私有化部署
  • 基于 vLLM 部署大模型
  • 案例:如何优雅地在阿里云私有部署开源大模型
  • 部署一套开源 LLM 项目
  • 内容安全
  • 互联网信息服务算法备案

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值