跳转到内容

多模态模型

大型语言模型(LLMs)是文本输入、文本输出的模型。大型多模态模型(LMMs)将这一概念推广到文本模态之外。例如,像GPT-4V这样的模型允许您同时输入图像和文本,并输出文本。

我们提供了一个基础的 MultiModalLLM 抽象层来支持文本+图像模型。注意:此命名可能会发生变更!

  1. 以下代码片段展示了如何开始使用大型多模态模型,例如结合 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
)
  1. 以下代码片段展示了如何构建多模态向量存储/索引。
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,
)
  1. 以下代码片段展示了如何使用多模态检索器和查询引擎。
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编排流程。

查询类型多模态向量存储/索引的
数据源
MultiModal
Embedding
检索器Query
Engine
Output
Data
Type
文本 ✅文本 ✅文本 ✅Top-k 检索 ✅
简单融合检索 ✅
简单查询引擎 ✅检索文本 ✅
生成文本 ✅
图像 ✅图像 ✅图像 ✅
图像转文本嵌入 ✅
Top-k 检索 ✅
简单融合检索 ✅
简单查询引擎 ✅已检索图像 ✅
已生成图像 🛑
音频 🛑音频 🛑音频 🛑🛑🛑音频 🛑
视频 🛑视频 🛑视频 🛑🛑🛑视频 🛑

这些笔记本展示了如何利用和集成多模态LLM模型、多模态嵌入、多模态向量存储、检索器、查询引擎来构建多模态检索增强生成(RAG)编排的示例。

多模态
视觉模型
Single
Image
Reasoning
Multiple
Images
Reasoning
Image
Embeddings
Simple
Query
Engine
Pydantic
Structured
Output
GPT4V
(OpenAI API)
🛑
GPT4V-Azure
(Azure API)
🛑
Gemini
(Google)
🛑
CLIP
(Local host)
🛑🛑🛑🛑
LLaVa
(replicate)
🛑🛑⚠️
Fuyu-8B
(replicate)
🛑🛑⚠️
ImageBind
[待集成]
🛑🛑🛑🛑
MiniGPT-4
🛑🛑⚠️
CogVLM
🛑🛑⚠️
Qwen-VL
[待集成]
🛑🛑⚠️

下表列出了一些支持多模态用例的向量存储。我们LlamaIndex内置的MultiModalVectorStoreIndex支持为图像和文本嵌入向量存储构建独立的向量存储。MultiModalRetrieverSimpleMultiModalQueryEngine支持文本到文本/图像以及图像到图像的检索,并提供用于合并文本和图像检索结果的简单排序融合功能。

多模态
向量存储
Single
Vector
Store
Multiple
Vector
Stores
Text
Embedding
Image
Embedding
LLamaIndex 自建
多模态索引
🛑可以是任意
文本嵌入
(默认为 GPT3.5)
可以是任意值
图像嵌入
(默认为CLIP)
Chroma🛑CLIP ✅CLIP ✅
Weaviate
[待集成]
🛑CLIP ✅
ImageBind ✅
CLIP ✅
ImageBind ✅

我们支持与 GPT4-V、Anthropic(Opus、Sonnet)、Gemini(谷歌)、CLIP(OpenAI)、BLIP(Salesforce)和 Replicate(LLaVA、Fuyu-8B、MiniGPT-4、CogVLM)等集成。

我们支持使用不同的多模态大语言模型与多模态向量存储库进行多模态检索增强生成。

我们支持多模态大语言模型和检索增强生成的基础评估。