LLama Multimodal Prompting

Working with text

from utils import llama32, stream_response, disp_image, img2base64
from fastcore.all import *
import pathlib
messages = [
  {"role": "user",
    "content": "Who wrote the book Charlotte's Web?"}
]

resp = stream_response(llama32(messages), from_gen=None)

<<Streaming response>>

The book “Charlotte’s Web” was written by E.B. White (Elwyn Brooks White). It was published in 1952 and has since become a beloved children’s classic, known for its timeless story of friendship and loyalty between a pig named Wilbur and a spider named Charlotte. E.B. White is also famous for his other works, such as “Stuart Little” and “The Trumpet of the Swan”.

messages = [
  {"role": "user",
    "content": "Who wrote the book Charlotte's Web?"},
      {"role": "assistant",
    "content": resp},
      {"role": "user",
    "content": "3 of the best quotes"}
]

resp2 = stream_response(llama32(messages, stream=True))

<<Streaming response>>

Here are three of the most iconic and memorable quotes from “Charlotte’s Web”:

  1. “Some pig.” - This is one of the most famous lines in the book, spoken by Charlotte as she begins to weave her web with words that will save Wilbur’s life.

  2. “It doesn’t matter where you come from; all that matters is where you go.” - This quote highlights the themes of hope and determination that run throughout the story.

  3. “I am not just a spider, I’m Charlotte A. Cavatica, the greatest spider the world has ever known!” - This quote showcases Charlotte’s confident and spirited personality, as well as her cleverness in coming up with words to save Wilbur.

Working with Images

image_url = ("https://raw.githubusercontent.com/meta-llama/"
            "llama-models/refs/heads/main/Llama_Repo.jpeg"); disp_image(image_url)
messages = [
  {"role": "user",
    "content": [
      {"type": "text",
        "text": "Describe the image in one sentence."
      },
      {"type": "image_url",
        "image_url": {"url": image_url}
      }
    ]
  },
]
pathlib.Path("images").ls()
(#23) [Path('images/fridge-2.jpg'),Path('images/001.jpeg'),Path('images/llama32mm.png'),Path('images/fridge.jpg'),Path('images/math_hw2.jpg'),Path('images/Llama_Repo.jpeg'),Path('images/drinks.png'),Path('images/receipt-2.jpg'),Path('images/merged_image_horizontal.jpg'),Path('images/tree.jpg')...]
image_url = 'images/drinks.png'; disp_image(image_url)
messages = [
  {"role": "user",
    "content": [
      {"type": "text",
        "text": "Describe the image in one sentence."
      },
      {"type": "image_url",
        "image_url": {"url": image_url}
      }
    ]
  },
]

resp3 = stream_response(llama32(messages, stream=True))

<<Streaming response>>

image_url = 'images/001.jpeg'; disp_image(image_url)
base64_image=img2base64(image_url)

messages = [
  {"role": "user",
    "content": [
      {"type": "text",
        "text": "Describe the image in one sentence."
      },
      {"type": "image_url",
        "image_url": {"url": f"data:image/jpg;base64,{base64_image}"}
      }
    ]
  },
]
resp4 = stream_response(llama32(messages, stream=True))

<<Streaming response>>

The image depicts a living room featuring a sleek, modern design with a fireplace, couch, and coffee table situated on an area rug over hardwood floors, exuding a sense of warmth and coziness despite its minimalist aesthetic.

## Follow up question on image
messages = [
  {"role": "user",
    "content": [
      {"type": "text",
        "text": "Describe the image in one sentence."
      },
      {"type": "image_url",
        "image_url": {"url": f"data:image/jpg;base64,{base64_image}"}
      }
    ]
  },
 {"role": "assistant", "content": resp4},
 {"role": "user", "content": "how many of them are spherical in shape? Describe them all."}
]

resp5 = stream_response(llama32(messages, stream=True))

<<Streaming response>>

There are two objects that are spherical in shape.

The first object is located to the right of the coffee table and appears to be a decorative piece, possibly made of white ceramic or glass material with numerous small holes on its surface.

The second object is situated near the fireplace and resembles a large, spherical vase or sculpture. It has a smooth texture and lacks any visible openings or holes.

def llama32pi(prompt, image_url, stream=True):
    messages = [
                {"role": "user",
                    "content": [
                        {"type": "text",
                            "text": prompt
                        },
                        {"type": "image_url",
                            "image_url": {"url": image_url}
                        }
                    ]
                }]
    if stream: result = stream_response(llama32(messages, stream=stream))
    else: result = llama32(messages)
    return result
llama32pi("describe the image in one sentence",
                "https://raw.githubusercontent.com/meta-llama/"
                "llama-models/refs/heads/main/Llama_Repo.jpeg", stream=False)
'The image depicts three alpacas, each with distinct characteristics: the leftmost alpaca is white and has a spiky haircut, the middle alpaca is purple with yellow string wrapped around its neck and body, and the rightmost alpaca wears a blue party hat.'

Plant recognition

question = ("What kind of plant is this in my garden?"
            "Describe it in a short paragraph.")
image_url = "images/tree.jpg"; disp_image(image_url)
llama32pi(question, image_url);

<<Streaming response>>

The plant in your garden is a pomegranate tree, scientifically known as Punica granatum. It’s an evergreen shrub or small tree native to the Mediterranean region and Asia. The tree produces showy, red-orange flowers that bloom from spring to summer, followed by fruiting structures called capsules, which contain the edible seeds of pomegranates. The leaves are dark green, elliptical, and pointed at both ends. Pomegranate trees thrive in warm climates with well-drained soil and full sun exposure. They’re relatively low-maintenance plants that can grow up to 12 feet tall and live for many years with proper care.

Dog breed recognition

question = (("What dog breed is this? Describe in one paragraph,"
             "and 3-5 short bullet points"))
image_url = "images/ww1.jpg"; disp_image(image_url)
llama32pi(question, image_url);

<<Streaming response>>

Based on the image, it appears to be a Labrador Retriever puppy. The dog’s short, dense coat and broad head are characteristic of the breed.

Here are some key features of Labradors:

Coat: Short, dense, and smooth • Head Shape: Broad with well-defined stops (the area where the muzzle meets the forehead) • Ears: Long and hanging • Size: Medium-sized dog, typically weighing between 55-80 pounds (25-36 kg) and standing between 21.5-24.5 inches (55-62 cm) tall at the shoulder

Overall, the dog’s appearance is consistent with the breed standard for Labradors.

question = (("What dog breed is this? Describe in one paragraph,"
             "and 3-5 short bullet points"))
image_url = "images/ww2.png"; disp_image(image_url)
llama32pi(question, image_url);

<<Streaming response>>

Based on the image, it appears to be a Labrador Retriever puppy. The breed is known for its distinctive physical characteristics and friendly temperament.

Key Features:

  • Short, dense coat that can vary in color from black to yellow to chocolate
  • Broad head with a black nose and floppy ears
  • Stocky build with webbed feet, suitable for swimming

Personality Traits:

  • Known for being highly intelligent, loyal, and outgoing
  • Generally easy-going and adaptable to new environments
  • Often used as family pets or working dogs due to their versatility

Pressure Warning

question = (("What's the problem this is about?"
             " What should be good numbers?"))
image_url = "images/tire_pressure.png"; disp_image(image_url)
llama32pi(question, image_url);

<<Streaming response>>

This image shows a car’s dashboard displaying an error message related to tire pressure. The “Tire Pressure” display on the right side of the screen indicates that the driver needs to check the air pressure in their tires.

Typical Tire Pressure Range:

  • 32-33 PSI for this vehicle (as indicated by the number displayed).

Possible Causes of Low Air Pressure:

  • A puncture or leak in one of the tires
  • Overloading the vehicle, which can cause excessive wear on the tires and affect their performance

Important Safety Note:

It is crucial to address this issue promptly to ensure safe driving conditions.