1. 案例目标本案例演示如何使用Anthropic的多模态模型Claude 3包括Opus和Sonnet版本进行图像理解和推理。主要目标包括展示如何使用Anthropic多模态LLM类/抽象进行图像理解和推理演示Anthropic多模态LLM支持的多种功能complete、chat、stream complete和stream chat展示如何从图像中提取结构化信息如股票信息演示如何构建基于图像数据的RAG检索增强生成管道2. 技术栈与核心依赖本案例使用了以下技术栈和核心依赖多模态LLM: Anthropic Claude 3 (Opus和Sonnet) - 用于图像理解和推理向量数据库: Qdrant - 用于存储和检索图像描述文本嵌入模型: OpenAI Embedding - 用于将文本转换为向量表示核心框架: LlamaIndex - 提供多模态处理和向量检索功能结构化输出: Pydantic - 定义和验证结构化数据模型主要依赖包:llama-index-multi-modal-llms-anthropic llama-index-vector-stores-qdrant matplotlib3. 环境配置本案例需要配置以下环境变量ANTHROPIC_API_KEY - 用于访问Anthropic API的密钥注意: 需要获取Anthropic的API密钥并将其设置为环境变量ANTHROPIC_API_KEY。4. 案例实现1. 安装依赖和设置API密钥安装必要的依赖包并设置Anthropic API密钥!pip install llama-index-multi-modal-llms-anthropic !pip install llama-index-vector-stores-qdrant !pip install matplotlib import os os.environ[ANTHROPIC_API_KEY] # Your ANTHROPIC API key here2. 初始化Anthropic多模态LLM创建AnthropicMultiModal实例from llama_index.multi_modal_llms.anthropic import AnthropicMultiModal anthropic_mm_llm AnthropicMultiModal(max_tokens300)3. 加载图像数据使用SimpleDirectoryReader加载本地图像文件from llama_index.core import SimpleDirectoryReader # 加载本地图像 image_documents SimpleDirectoryReader(input_dir./images).load_data()4. 使用complete方法进行图像理解使用Anthropic多模态LLM的complete方法对图像进行描述response anthropic_mm_llm.complete( promptDescribe the images as an alternative text, image_documentsimage_documents, ) print(response)5. 定义结构化数据模型使用Pydantic定义股票信息的结构化模型from typing import List from pydantic import BaseModel class TickerInfo(BaseModel): 股票信息 direction: str ticker: str company: str shares_traded: int percent_of_total_etf: float class TickerList(BaseModel): 股票代码列表 fund: str tickers: List[TickerInfo]6. 使用MultiModalLLMCompletionProgram提取结构化信息创建多模态LLM完成程序从图像中提取结构化股票信息from llama_index.multi_modal_llms.anthropic import AnthropicMultiModal from llama_index.core.program import MultiModalLLMCompletionProgram from llama_index.core.output_parsers import PydanticOutputParser prompt_template_str \ Can you get the stock information in the image \ and return the answer? Pick just one fund. Make sure the answer is a JSON format corresponding to a Pydantic schema. The Pydantic schema is given below. # 初始化Anthropic多模态类 anthropic_mm_llm AnthropicMultiModal(max_tokens300) llm_program MultiModalLLMCompletionProgram.from_defaults( output_clsTickerList, image_documentsimage_documents, prompt_template_strprompt_template_str, multi_modal_llmanthropic_mm_llm, verboseTrue, ) # 执行程序 response llm_program() print(str(response))7. 构建图像RAG管道下载图像数据集并使用Claude 3提取图像描述然后构建向量索引# 下载图像数据集 !wget https://www.dropbox.com/scl/fi/c1ec6osn0r2ggnitijqhl/mixed_wiki_images_small.zip?rlkeyswwxc7h4qtwlnhmby5fsnderddl1 -O mixed_wiki_images_small.zip !unzip mixed_wiki_images_small.zip # 提取图像描述 from llama_index.core.schema import TextNode from pathlib import Path from llama_index.core import SimpleDirectoryReader nodes [] for img_file in Path(mixed_wiki_images_small).glob(*.png): print(img_file) # 加载本地图像 image_documents SimpleDirectoryReader(input_files[img_file]).load_data() response anthropic_mm_llm.complete( promptDescribe the images as an alternative text, image_documentsimage_documents, ) metadata {img_file: img_file} nodes.append(TextNode(textstr(response), metadatametadata))8. 创建向量索引使用Qdrant向量存储和OpenAI嵌入模型创建向量索引from llama_index.core import VectorStoreIndex, StorageContext from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.llms.anthropic import Anthropic from llama_index.vector_stores.qdrant import QdrantVectorStore from llama_index.core import Settings from llama_index.core import StorageContext import qdrant_client # 创建本地Qdrant向量存储 client qdrant_client.QdrantClient(pathqdrant_mixed_img) vector_store QdrantVectorStore(clientclient, collection_namecollection) # 使用OpenAI嵌入模型 embed_model OpenAIEmbedding() anthropic_mm_llm AnthropicMultiModal(max_tokens300) storage_context StorageContext.from_defaults(vector_storevector_store) index VectorStoreIndex( nodesnodes, storage_contextstorage_context, )9. 创建查询引擎并执行查询使用Anthropic LLM创建查询引擎并执行查询from llama_index.llms.anthropic import Anthropic query_engine index.as_query_engine(llmAnthropic()) response query_engine.query(Tell me more about porsche) print(str(response))10. 显示源节点显示查询结果的源节点包括相似度得分和文本内容from llama_index.core.response.notebook_utils import display_source_node for n in response.source_nodes: display_source_node(n, metadata_modeall)5. 案例效果本案例实现了以下效果图像理解: 成功使用Claude 3模型对图像进行描述和分析结构化信息提取: 从金融图表中提取结构化的股票信息包括股票代码、公司名称、交易数量等图像RAG: 构建了基于图像数据的检索增强生成系统能够根据文本查询检索相关图像并生成回答多模态查询: 支持基于文本的查询检索相关的图像描述信息6. 案例实现思路本案例的实现思路如下多模态处理: 利用Anthropic Claude 3的多模态能力理解图像内容并生成文本描述结构化输出: 使用Pydantic定义数据模型结合MultiModalLLMCompletionProgram从图像中提取结构化信息向量表示: 将图像描述文本转换为向量表示便于进行语义检索RAG架构: 构建检索增强生成系统结合向量检索和LLM生成能力混合模型: 结合Anthropic多模态LLM、OpenAI嵌入模型和Qdrant向量存储构建完整的多模态系统7. 扩展建议本案例可以进一步扩展和优化支持更多图像类型: 扩展到处理更多类型的图像如医学影像、卫星图像等多语言支持: 支持多语言图像描述和查询实时图像处理: 支持实时图像流处理和分析图像相似度搜索: 实现基于图像内容的相似度搜索而不仅仅是基于文本描述用户界面: 开发图形用户界面方便用户上传图像和执行查询性能优化: 优化向量存储和检索的性能支持大规模图像处理8. 总结本案例展示了如何使用Anthropic Claude 3多模态模型与LlamaIndex结合构建图像理解和检索系统。通过使用AnthropicMultiModal类我们能够对图像进行描述和分析提取结构化信息并构建基于图像数据的RAG管道。这种方法可以应用于各种需要图像理解和检索的场景如图像搜索、内容分析、文档处理等。核心价值: 本案例的核心价值在于展示了Anthropic Claude 3多模态模型与LlamaIndex的结合为构建智能图像理解和检索系统提供了实用方案。