AI Hints
Home›LangChain Tutorial›Images and Long Documents
LangChain · Lesson 109 of 120

Multimodal Input

Images too.

⏱ 2 min read▶ Video at 2:04:08🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Many models can read images, not just text. A customer could send a photo of a book cover, and ask, do you have this? In LangChain, a message can hold text and images together.

Multimodal Input
From the video: Multimodal Input
  • Models can see
  • A photo of a cover
  • Text and images together

Reading a book cover

To keep things self contained, we draw a simple book cover with the Pillow library, and turn it into base sixty four text. The message content is now a list of blocks. A text block with the question, and an image block with the picture. Run it. The model reads the title and the price, straight from the image.

image_input.pypython
import base64, io
from PIL import Image, ImageDraw, ImageFont
from langchain.chat_models import init_chat_model

cover = Image.new("RGB", (300, 420), "#1d3557")
draw, font = ImageDraw.Draw(cover), ImageFont.load_default(size=36)
draw.text((30, 80), "THE SILENT\nRIVER", fill="white", font=font)
draw.text((30, 330), "£8.99", fill="#ffd166", font=font)
buffer = io.BytesIO()
cover.save(buffer, format="PNG")
data = base64.b64encode(buffer.getvalue()).decode()

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
message = {"role": "user", "content": [
    {"type": "text", "text": "What are the title and price on this book cover? One line."},
    {"type": "image", "base64": data, "mime_type": "image/png"},
]}
print(model.invoke([message]).text)
Output
The title is "THE SILENT RIVER" and the price is 8.99.

Ideas with images

Reading images opens up new features. Identify a book from a photo of its cover. Read a receipt for a return. Check a photo of a damaged parcel. Or describe a picture for a customer who can't see it. Images also cost tokens, so resize big photos first.

keyvalue
Coversidentify books
Receiptsread details
Damagecheck photos
Costresize big images

Summary

Messages can hold text and image blocks together, so the model can answer questions about pictures. Next, summarising documents too long for one prompt.

Key takeaways
  • Content as a list
  • Text block + image block
  • base64 data
Next up: Long Documents — summarising in parts