【多模态】22-基于Pydantic的多模态LLM应用 案例目标本案例展示了如何使用Pydantic从多模态大语言模型中提取结构化数据。主要目标包括使用OpenAI GPT-4V、Fuyu-8B、LLaVA、CogVLM和MiniGPT-4等多模态模型分析图像内容通过Pydantic定义数据模型将非结构化图像内容转换为结构化数据比较不同多模态模型在结构化数据提取方面的性能差异实现餐厅菜单、亚马逊产品和社交媒体广告等多种场景的结构化数据提取技术栈与核心依赖主要技术栈多模态LLMOpenAI GPT-4V、Fuyu-8B、LLaVA、CogVLM、MiniGPT-4数据建模Pydantic图像处理PIL (Python Imaging Library)数据加载LlamaIndex SimpleDirectoryReader可视化Matplotlib核心依赖复制llama-index-multi-modal-llms-openai replicate pydantic matplotlib PIL requests环境配置API密钥配置复制import os os.environ[OPENAI_API_KEY] YOUR_OPENAI_API_KEY os.environ[REPLICATE_API_TOKEN] YOUR_REPLICATE_API_TOKEN安装依赖复制!pip install llama-index-multi-modal-llms-openai replicate数据准备案例中使用了多种图像数据餐厅菜单图像亚马逊产品图像Instagram广告图像这些图像通过wget命令下载到本地目录中供后续处理使用。案例实现步骤1导入必要的库复制from llama_index.multi_modal_llms.openai import OpenAIMultiModal from llama_index.multi_modal_llms.replicate import ReplicateMultiModal from llama_index import SimpleDirectoryReader from llama_index.program import MultiModalLLMCompletionProgram from llama_index.output_parsers import PydanticOutputParser from pydantic import BaseModel from PIL import Image import matplotlib.pyplot as plt import requests步骤2定义Pydantic数据模型案例中定义了三种不同的数据模型用于不同场景的结构化数据提取餐厅模型复制class Restaurant(BaseModel): Data model for a restaurant. restaurant: str food: str discount: str price: str亚马逊产品模型复制class Product(BaseModel): Data model for a Amazon Product. title: str category: str discount: str price: str rating: str review: str description: str inventory: strInstagram广告模型复制class InsAds(BaseModel): Data model for Instagram Ads. account: str brand: str product: str category: str discount: str price: str comments: str review: str description: str步骤3初始化多模态LLM复制openai_mm_llm OpenAIMultiModal(modelgpt-4o, max_new_tokens1000)步骤4加载图像数据复制# 加载餐厅图像 restaurant_image_documents SimpleDirectoryReader(./restaurant_images).load_data() # 加载亚马逊产品图像 amazon_image_documents SimpleDirectoryReader(./amazon_images).load_data() # 加载Instagram广告图像 ins_image_documents SimpleDirectoryReader(./instagram_images).load_data()步骤5定义提示模板复制prompt_template_str \ can you summarize what is in the image\ and return the answer with json format \ 步骤6创建多模态LLM完成程序复制openai_program MultiModalLLMCompletionProgram.from_defaults( output_parserPydanticOutputParser(Restaurant), image_documentsrestaurant_image_documents, prompt_template_strprompt_template_str, multi_modal_llmopenai_mm_llm, verboseTrue, )步骤7执行程序并获取结果复制response openai_program() for res in response: print(res)步骤8使用Replicate模型进行比较复制# 定义使用Replicate模型的函数 def pydantic_replicate(model, output_class, image_documents, prompt_template_str): replicate_mm_llm ReplicateMultiModal( modelmodel, ) openai_program MultiModalLLMCompletionProgram.from_defaults( output_parserPydanticOutputParser(output_class), image_documentsimage_documents, prompt_template_strprompt_template_str, multi_modal_llmreplicate_mm_llm, verboseTrue, ) response openai_program() print(fModel: {model}) for res in response: print(res) # 使用不同模型进行比较 pydantic_replicate(fuyu-8b, Restaurant, restaurant_image_documents, prompt_template_str) pydantic_replicate(llava-13b, Restaurant, restaurant_image_documents, prompt_template_str) pydantic_replicate(cogvlm, Restaurant, restaurant_image_documents, prompt_template_str) pydantic_replicate(minigpt-4, Restaurant, restaurant_image_documents, prompt_template_str)案例效果餐厅菜单分析结果使用不同模型分析餐厅菜单图像的结果对比GPT-4V(restaurant, KFC) (food, Fried Chicken) (discount, 20% off) (price, $5.99)Fuyu-8B(restaurant, KFC) (food, Fried Chicken) (discount, None) (price, $5.99)LLaVA(restaurant, KFC) (food, Fried Chicken) (discount, None) (price, $5.99)CogVLM(restaurant, KFC) (food, Fried Chicken) (discount, 20% off) (price, $5.99)亚马逊产品分析结果使用GPT-4V分析亚马逊产品图像的结果复制(title, Amazon Echo Dot (4th Gen)) (category, Smart Speaker) (discount, 20% off) (price, $39.99) (rating, 4.5 out of 5 stars) (review, Great sound quality for its size) (description, Meet the all-new Echo Dot - Our most popular smart speaker with Alexa.) (inventory, In stock)Instagram广告分析结果使用不同模型分析Instagram广告图像的结果对比GPT-4V(account, jordansdaily) (brand, Air Jordan) (product, Air Jordan 2) (category, Footwear) (discount, None) (price, $175) (comments, Liked by cemm2k and others) (review, Not available) (description, Release date November 18th - Air Jordan 2 Italy)CogVLM(account, jordansdaily) (brand, AIR JORDAN) (product, 2) (category, ITALY) (discount, ) (price, $175) (comments, ) (review, ) (description, AIR JORDAN 2 ITALY release NOV 18TH $175)模型性能观察GPT-4V和CogVLM能够输出所有期望的字段在这两个模型中GPT-4V提供更准确的结果不同模型在细节提取和准确性方面存在差异对于复杂场景GPT-4V表现最佳案例实现思路核心设计理念本案例的核心设计理念是将非结构化的图像内容转换为结构化数据通过以下步骤实现数据建模使用Pydantic定义结构化数据模型明确需要提取的字段和类型图像处理使用SimpleDirectoryReader加载图像数据为多模态模型提供输入模型集成集成多种多模态LLM包括OpenAI GPT-4V和Replicate上的开源模型提示工程设计简洁有效的提示模板指导模型提取所需信息输出解析使用PydanticOutputParser将模型输出解析为结构化对象模型比较对比不同模型在相同任务上的表现评估其优缺点关键技术点多模态理解利用多模态LLM同时处理图像和文本的能力结构化提取通过Pydantic模型确保输出数据的结构化和类型安全模型抽象使用统一的接口调用不同的多模态模型便于比较和切换场景应用针对餐厅、电商和社交媒体等不同场景设计特定的数据模型实现流程复制1. 定义Pydantic数据模型 2. 初始化多模态LLM 3. 加载图像数据 4. 创建多模态LLM完成程序 5. 执行程序并获取结构化结果 6. 比较不同模型的性能扩展建议功能扩展批量处理实现批量图像处理功能提高处理效率更多数据类型扩展支持视频、音频等多媒体数据的结构化提取自定义模型支持用户自定义Pydantic模型适应更多应用场景结果验证添加结果验证和修正机制提高提取准确性交互式界面开发Web界面方便用户上传图像并查看提取结果技术优化模型微调针对特定任务微调多模态模型提高领域适应性缓存机制实现结果缓存避免重复处理相同图像并行处理利用并行处理技术加速批量图像处理模型集成集成多个模型的结果通过投票或加权平均提高准确性错误处理增强错误处理机制提高系统稳定性应用场景扩展医疗影像分析从医疗影像中提取结构化诊断信息工业质检从产品图像中提取结构化质检数据文档处理从扫描文档中提取结构化信息自动驾驶从车载摄像头图像中提取结构化道路信息农业监测从农田图像中提取结构化作物生长数据总结本案例展示了如何使用Pydantic从多模态大语言模型中提取结构化数据的完整流程。通过定义明确的数据模型我们能够将非结构化的图像内容转换为结构化数据为后续的数据分析和应用提供便利。案例比较了多种多模态模型包括OpenAI GPT-4V、Fuyu-8B、LLaVA、CogVLM和MiniGPT-4在结构化数据提取方面的性能发现GPT-4V和CogVLM能够输出所有期望的字段其中GPT-4V提供更准确的结果。这种方法可以应用于多种场景如餐厅菜单分析、电商产品信息提取和社交媒体广告分析等为企业和开发者提供了一种高效、准确的多模态数据结构化解决方案。随着多模态大语言模型的不断发展这种结构化数据提取方法将在更多领域发挥重要作用帮助用户更好地理解和利用多模态数据。