AlgoMaster Logo

Exercise: Multimodal RAG

3 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Exercise 1: Caption and Retrieve over Images

Make a set of images searchable by text. Use a vision model to describe each image, embed those descriptions into the same vector space as a text query, and retrieve the image whose description best matches the query.

This is the most common way to bring images into a RAG pipeline. Rather than building a separate image search system, you turn each image into a text description with a vision model, then reuse the text embedding and retrieval machinery you already have. The retrieval quality depends entirely on how well the descriptions capture what a user might search for.

Requirements

  • For each image URL, ask a vision model to write a short factual description.
  • Embed every description with text-embedding-3-small.
  • Embed the text query and retrieve the image whose description is most similar.
  • Print each description and the URL of the best-matching image.
main.py
Loading...

Expected Output

Each image gets a description, and the landmark query retrieves the tower image even though the query never mentions a tower.

Solution

main.py
Loading...

The vision model converts each image into a sentence that lives in the same space as text, so a single embedding model can compare the query against all of them. Everything after the describe step is ordinary text retrieval, which is exactly the appeal: one pipeline handles both modalities, and the only multimodal-specific piece is the captioning call. The cost is that anything the description omits becomes invisible to search, so the prompt you give the vision model directly shapes what users can find.

Exercise 2: Caption, Retrieve, then Answer

Extend image retrieval into a full multimodal RAG answer: caption the images, retrieve the one most relevant to a question, then answer the question using that image's description as context. The model never sees the pixels at answer time, only the caption that retrieval selected.

Requirements

  • Caption each image with a vision model.
  • Embed the captions and the question, and retrieve the most relevant caption.
  • Answer the question using only that caption as context, and print the chosen image and answer.
main.py
Loading...

Expected Output

The landmark question retrieves the Eiffel Tower image, and the answer comes from its caption.

Solution

main.py
Loading...

This is the complete multimodal RAG path: a vision model turns images into text, retrieval picks the relevant one, and a text model answers from it. Separating captioning from answering means the expensive vision call happens once at index time, while answering reuses ordinary text retrieval and generation. The answer is only as good as the caption, which is why the captioning prompt (what details to include) shapes what the system can later answer. This exercise needs a vision-capable model and network access to the images.