主题
概念
大语言模型(LLM)是文本进、文本出的。大型多模态模型(LMM)把这一能力扩展到文本之外的模态。例如 GPT-4V 这类模型允许你同时输入图像与文本,并输出文本。
我们提供了一个基础的 MultiModalLLM 抽象以支持文本+图像模型。注意:这个命名后续还可能调整!
使用方式
- 下面这段代码演示如何开始使用 LMM,例如配合 GPT-4V。
from llama_index.multi_modal_llms.openai import OpenAIMultiModal
from llama_index.core.multi_modal_llms.generic_utils import load_image_urls
from llama_index.core import SimpleDirectoryReader
# load image documents from urls
image_documents = load_image_urls(image_urls)
# load image documents from local directory
image_documents = SimpleDirectoryReader(local_directory).load_data()
# non-streaming
openai_mm_llm = OpenAIMultiModal(
model="gpt-4-vision-preview", api_key=OPENAI_API_KEY, max_new_tokens=300
)
response = openai_mm_llm.complete(
prompt="what is in the image?", image_documents=image_documents
)- 下面这段代码演示如何构建多模态向量库/索引。
from llama_index.core.indices import MultiModalVectorStoreIndex
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.core import SimpleDirectoryReader, StorageContext
import qdrant_client
from llama_index.core import SimpleDirectoryReader
# Create a local Qdrant vector store
client = qdrant_client.QdrantClient(path="qdrant_mm_db")
# if you only need image_store for image retrieval,
# you can remove text_store
text_store = QdrantVectorStore(
client=client, collection_name="text_collection"
)
image_store = QdrantVectorStore(
client=client, collection_name="image_collection"
)
storage_context = StorageContext.from_defaults(
vector_store=text_store, image_store=image_store
)
# Load text and image documents from local folder
documents = SimpleDirectoryReader("./data_folder/").load_data()
# Create the MultiModal index
index = MultiModalVectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
)- 下面这段代码演示如何使用多模态检索器与查询引擎。
from llama_index.multi_modal_llms.openai import OpenAIMultiModal
from llama_index.core import PromptTemplate
from llama_index.core.query_engine import SimpleMultiModalQueryEngine
retriever_engine = index.as_retriever(
similarity_top_k=3, image_similarity_top_k=3
)
# retrieve more information from the GPT4V response
retrieval_results = retriever_engine.retrieve(response)
# if you only need image retrieval without text retrieval
# you can use `text_to_image_retrieve`
# retrieval_results = retriever_engine.text_to_image_retrieve(response)
qa_tmpl_str = (
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the query.\n"
"Query: {query_str}\n"
"Answer: "
)
qa_tmpl = PromptTemplate(qa_tmpl_str)
query_engine = index.as_query_engine(
multi_modal_llm=openai_mm_llm, text_qa_template=qa_tmpl
)
query_str = "Tell me more about the Porsche"
response = query_engine.query(query_str)图例
- ✅ = 可以正常工作
- ⚠️ = 有时不可靠,可能需要更多调优来改善
- 🛑 = 暂不可用。
端到端多模态工作流
下面的表格尝试展示用各种 LlamaIndex 特性构建你自己的多模态 RAG(检索增强生成)的初始步骤。你可以把不同的模块/步骤组合起来,编排出自己的多模态 RAG 流程。
| 查询类型 | 多模态向量库/索引的数据源 | 多模态嵌入 | 检索器 | 查询引擎 | 输出数据类型 |
|---|---|---|---|---|---|
| 文本 ✅ | 文本 ✅ | 文本 ✅ | Top-k 检索 ✅ 简单融合检索 ✅ | 简单查询引擎 ✅ | 检索到的文本 ✅ 生成的文本 ✅ |
| 图像 ✅ | 图像 ✅ | 图像 ✅ 图像转文本嵌入 ✅ | Top-k 检索 ✅ 简单融合检索 ✅ | 简单查询引擎 ✅ | Retrieved Image ✅ Generated Image 🛑 |
| 音频 🛑 | 音频 🛑 | 音频 🛑 | 🛑 | 🛑 | 音频 🛑 |
| 视频 🛑 | 视频 🛑 | 视频 🛑 | 🛑 | 🛑 | 视频 🛑 |
多模态 LLM 模型
这些 notebook 演示如何借助并集成多模态 LLM 模型、多模态嵌入、多模态向量库、检索器与查询引擎,以编排多模态检索增强生成(RAG)。
| 多模态视觉模型 | 单图推理 | 多图推理 | 图像嵌入 | 简单查询引擎 | Pydantic 结构化输出 |
|---|---|---|---|---|---|
| GPT4V (OpenAI API) | ✅ | ✅ | 🛑 | ✅ | ✅ |
| GPT4V-Azure (Azure API) | ✅ | ✅ | 🛑 | ✅ | ✅ |
| Gemini (Google) | ✅ | ✅ | 🛑 | ✅ | ✅ |
| CLIP (本地部署) | 🛑 | 🛑 | ✅ | 🛑 | 🛑 |
| LLaVa (replicate) | ✅ | 🛑 | 🛑 | ✅ | ⚠️ |
| Fuyu-8B (replicate) | ✅ | 🛑 | 🛑 | ✅ | ⚠️ |
| ImageBind [待集成] | 🛑 | 🛑 | ✅ | 🛑 | 🛑 |
| MiniGPT-4 | ✅ | 🛑 | 🛑 | ✅ | ⚠️ |
| CogVLM | ✅ | 🛑 | 🛑 | ✅ | ⚠️ |
| Qwen-VL [待集成] | ✅ | 🛑 | 🛑 | ✅ | ⚠️ |
多模态向量库
下表列出了一些支持多模态场景的向量库。我们内置的 MultiModalVectorStoreIndex 支持为图像与文本分别构建向量库。MultiModalRetriever 与 SimpleMultiModalQueryEngine 支持文本到文本/图像、图像到图像的检索,并提供简单的排序融合函数来合并文本与图像的检索结果。
| 多模态向量库 | 单向量库 | 多向量库 | 文本嵌入 | 图像嵌入 |
|---|---|---|---|---|
| LlamaIndex 自建多模态索引 | 🛑 | ✅ | 可为任意文本嵌入(默认 GPT3.5) | 可为任意图像嵌入(默认 CLIP) |
| Chroma | ✅ | 🛑 | CLIP ✅ | CLIP ✅ |
| Weaviate [待集成] | ✅ | 🛑 | CLIP ✅ ImageBind ✅ | CLIP ✅ ImageBind ✅ |
多模态 LLM 模块
我们支持与 GPT4-V、Anthropic(Opus、Sonnet)、Gemini(Google)、CLIP(OpenAI)、BLIP(Salesforce)以及 Replicate(LLaVA、Fuyu-8B、MiniGPT-4、CogVLM)等的集成。
多模态检索增强生成
我们支持将不同的多模态 LLM 与多模态向量库搭配,实现多模态检索增强生成。
评估
我们支持对多模态 LLM 与检索增强生成做基础评估。