멀티모달 모델

멀티모달 모델 (Multi-modal models)

LLM(대형 언어 모델)은 텍스트를 넣고 텍스트를 내보내는 형태예요. 멀티모달 모델(LMM, Large Multi-modal Model)은 여기서 더 나아가 텍스트 외의 여러 유형을 함께 다룰 수 있게 해 줘요. 예를 들어 GPT-4V 같은 모델은 이미지와 텍스트를 함께 입력받고 텍스트를 출력하죠.

LlamaIndex는 텍스트+이미지 모델을 위한 기본 MultiModalLLM 추상화를 제공해요. 참고: 이 이름은 추후 변경될 수 있어요!

출처: 문서

본문

사용 패턴

1. LMM(GPT-4V 등) 사용 시작하기

이미지 문서를 URL 또는 로컬 디렉토리에서 불러온 뒤, OpenAIMultiModal의 complete 메서드로 이미지 내용을 물어볼 수 있어요.

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
)

2. 멀티모달 Vector Store / Index 구축

텍스트 벡터 스토어와 이미지 벡터 스토어를 각각 만들고 MultiModalVectorStoreIndex로 인덱스를 만들어요.

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,
)

3. 멀티모달 Retriever와 Query Engine 사용

인덱스에서 리트리버로 관련 이미지/텍스트를 검색하고, SimpleMultiModalQueryEngine으로 질문에 답할 수 있어요.

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)

범례(Legend)

  • ✅ = 잘 동작해야 함
  • ⚠️ = 가끔 불안정함, 개선을 위해 튜닝이 더 필요할 수 있음
  • 🛑 = 현재는 사용 불가

End to End 멀티모달 워크플로

아래 표는 나만의 멀티모달 RAG(Retrieval Augmented Generation)를 구성하기 위해 LlamaIndex의 다양한 기능을 조합하는 초기 단계를 보여줘요. 여러 모듈/단계를 결합해 나만의 멀티모달 RAG 오케스트레이션을 만들 수 있어요.

Query Type Data Sources for MultiModal Vector Store/Index MultiModal Embedding Retriever Query Engine Output Data Type
Text ✅ Text ✅ Text ✅ Top-k retrieval ✅ Simple Fusion retrieval ✅ Simple Query Engine ✅ Retrieved Text ✅ Generated Text ✅
Image ✅ Image ✅ Image ✅ Image to Text Embedding ✅ Top-k retrieval ✅ Simple Fusion retrieval ✅ Simple Query Engine ✅ Retrieved Image ✅ Generated Image 🛑
Audio 🛑 Audio 🛑 Audio 🛑 🛑 🛑 Audio 🛑
Video 🛑 Video 🛑 Video 🛑 🛑 🛑 Video 🛑

멀티모달 LLM 모델

이 노트북들은 멀티모달 LLM 모델, 멀티모달 임베딩, 멀티모달 벡터 스토어, 리트리버, 쿼리 엔진을 조합해 멀티모달 RAG를 구축하는 예시예요.

Multi-Modal Vision Models 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 [To integrate] 🛑 🛑 ✅ 🛑 🛑
MiniGPT-4 ✅ 🛑 🛑 ✅ ⚠️
CogVLM ✅ 🛑 🛑 ✅ ⚠️
Qwen-VL [To integrate] ✅ 🛑 🛑 ✅ ⚠️

멀티모달 벡터 스토어

아래 표는 멀티모달 사용 사례를 지원하는 일부 벡터 스토어를 보여줘요. LlamaIndex 내장 MultiModalVectorStoreIndex는 이미지와 텍스트 임베딩용 벡터 스토어를 각각 별도로 만들 수 있게 해 주고, MultiModalRetriever와 SimpleMultiModalQueryEngine은 텍스트→텍스트/이미지 및 이미지→이미지 검색과 텍스트·이미지 검색 결과를 합치는 간단한 순위 퓨전 함수를 지원해요.

Multi-Modal Vector Stores Single Vector Store Multiple Vector Stores Text Embedding Image Embedding
LLamaIndex self-built MultiModal Index 🛑 ✅ Can be arbitrary text embedding (Default is GPT3.5) Can be arbitrary Image embedding (Default is CLIP)
Chroma ✅ 🛑 CLIP ✅ CLIP ✅
Weaviate [To integrate] ✅ 🛑 CLIP ✅ ImageBind ✅ CLIP ✅ ImageBind ✅

멀티모달 LLM 모듈

GPT4-V, Anthropic(Opus, Sonnet), Gemini(Google), CLIP(OpenAI), BLIP(Salesforce), Replicate(LLaVA, Fuyu-8B, MiniGPT-4, CogVLM) 등을 지원해요.

멀티모달 Retrieval Augmented Generation

다양한 멀티모달 LLM과 멀티모달 벡터 스토어로 멀티모달 RAG를 지원해요.

평가 (Evaluation)

멀티모달 LLM과 RAG에 대한 기본 평가를 지원해요.

더 알아보기 (Learn more)