Llama 3.2 Vision Function Calling
Using Llama 3.2 Vision for Function Calling
In our previous tutorial on multimodal image processing, we explored the capabilities of LLaVA (Large Language-and-Vision Assistant) by using it to analyze images of dogs and then write short childrens stories about them. In this tutorial we'll explore using Llama 3.2 Vision - a more powerful and functional multimodal model - to retrieve facts and information about dogs by calling the API Ninjas' Dogs API using features gathered from the dog image.
To do so we'll use Llama 3.2 Vision in conjunction with Groq API's JSON mode to extract specific information from the provided image, such as the breed of dog or its characteristics. By doing so, we'll show how to create a pipeline for image classification and data extraction, converting unstructured images into structured data that can be easily searched, filtered, and analyzed.
Setup
To complete this tutorial, you'll need a Groq API key. If you don't already have one, you can easily create a free account on GroqCloud by clicking here and generating your API key. You'll also need an API Key from API Ninjas, which you can also generate for free by signing up. Once you have your Groq API key, you'll need to set it as an environment variable called GROQ_API_KEY for the tutorial to work. We'll be working with the llama-3.2-11b-vision-preview model throughout this tutorial.
Basic Llama 3.2 Vision usage
For this tutorial, we'll be using the same set of dog images from Britannica that we utilized in the previous tutorial. You can access these images in the Groq API cookbook repository, specifically in the images folder. We'll be encoding a local image to base64 to integrate it with Llama 3.2 Vision. This is one of two methods to use an image with the vision model - the other approach involves providing the actual URL of the image.
To use locally saved images with Llama 3.2 Vision, we'll need to encode them to base64 first:
Analyzing an image with Llama 3.2 Vision using Groq API functions the same as with LLaVA. We'll make a request to the https://api.groq.com/openai/v1/chat/completions API endpoint, passing the image and prompt in a JSON structure that includes the image path:
The dog breed in the image is a Maltese.
However, Llama 3.2 Vision offers more functionality than LLaVA. It offers most of the functionality of any other chat model, including tool use and JSON mode (the only thing it can't do is process a system prompt). By leveraging function calling, we can utilize Llama 3.2 Vision to identify the breed of the dog in the image and use it to call the dogs API endpoint, which will return a set of facts for that breed. For a more detailed primer on function calling, check out this Function Calling 101 cookbook.
Function Calling
First, we'll define our function to be used by Llama 3.2 Vision. This function will take in the dog breed name as a parameter and use it to call the dogs API:
Next, we'll define our tools for Llama 3.2 Vision to use. Note that when defining your tool, the function description is used to determine which tool to call, and the parameter description is used to fill in the parameter of the function:
Now we can use the tool use functionality with Llama 3.2 Vision to extract the dog breed from the picture, and use that to call the get_dog_facts function. Note that the image itself is serving as the lone user message in this function call, with no additional user prompt:
{'image_link': 'https://api-ninjas.com/images/dogs/maltese.jpg', 'good_with_children': 3, 'good_with_other_dogs': 3, 'shedding': 1, 'grooming': 4, 'drooling': 1, 'coat_length': 1, 'good_with_strangers': 3, 'playfulness': 3, 'protectiveness': 4, 'trainability': 3, 'energy': 3, 'barking': 3, 'min_life_expectancy': 12.0, 'max_life_expectancy': 15.0, 'max_height_male': 9.0, 'max_height_female': 9.0, 'max_weight_male': 8.8, 'max_weight_female': 8.8, 'min_height_male': 7.0, 'min_height_female': 7.0, 'min_weight_male': 6.6, 'min_weight_female': 6.6, 'name': 'Maltese'}
Finally, we'll run the API output back through Llama 3.2 Vision, along with the field descriptions of the API output, so that it can provide a written assessment of the breed:
**Assessment of the Maltese Breed** The Maltese is a gentle, playful, and affectionate breed that makes an excellent companion for families. They are adaptable to living in small spaces, such as apartments, but require regular grooming to prevent matting and tangling of their coats. **Temperament** The Maltese has a friendly and outgoing personality, making them great with children and other pets. They are relatively low-maintenance and don't require excessive exercise, making them a perfect fit for busy owners. However, they do benefit from regular playtime to keep them entertained and stimulated. **Shedding and Grooming** As a low-shedding breed, the Maltese is hypoallergenic and suitable for owners with allergies. However, their long, silky coats require regular brushing and grooming to prevent matting and tangling. Expect to spend around 30 minutes a day on grooming, including brushing, bathing, and trimming. **Vocalization** The Maltese is a vocal breed, but they are not excessive barkers. They may alert their owners to potential strangers or noises, but they do not make a lot of noise. This makes them a great fit for owners who live in apartments or have noise restrictions. **Energy Level** The Maltese has a moderate energy level, requiring daily walks and playtime to keep them happy and healthy. However, they are not high-energy dogs and can adjust to a relatively sedentary lifestyle. **Protectiveness** The Maltese is not naturally protective of their owners or territory. They are friendly and outgoing, which makes them less likely to alert strangers. However, they can make great watchdogs if socialized properly and given the confidence to defend their owners. **Trainability** The Maltese can be a bit stubborn at times, but they are highly trainable. Consistent, positive reinforcement training and early socialization are key to developing good behavior and habits. **Health and Maintenance** The Maltese is generally a healthy breed, but they can be prone to certain health issues, such as dental problems, patellar luxation, and kidney problems. Regular veterinary check-ups, proper care, and attention to health issues can help prevent or manage these problems. **In Conclusion** The Maltese is a delightful, adaptable breed that makes an excellent companion for families and individuals alike. With proper care, attention, and training, they can thrive in a variety of living situations. Whether you're looking for a playful companion or a gentle lap dog, the Maltese is an excellent choice. **Recommendations** * Suitable for: Families with children, individuals, and retirees * Living Situation: Apartments, houses, and condos * Exercise: Daily walks and playtime, moderate energy level * Grooming: Regular brushing, bathing, and trimming * Training: Consistent, positive reinforcement training and socialization * Health: Regular veterinary check-ups, proper care, and attention to potential health issues
Let's try it with a few additional images:
**Assessment of the Golden Retriever Breed** **Overall Impression:** The Golden Retriever is a loving, loyal, and energetic breed that requires regular grooming and exercise. With its high trainability, affectionate nature, and protective instincts, it makes for a wonderful family pet. **Key Traits:** * **Energy:** 3/5 - Average energy level, requiring regular exercise but not excessive physical activity. * **Protectiveness:** 3/5 - Will alert strangers with a moderate level of protectiveness, making it a great breed for families. * **Trainability:** 5/5 - Exceptionally easy to train, making it a great breed for new dog owners. * **Barking:** 1/5 - Minimal barking, suitable for families who value quiet living. * **Shedding:** 4/5 - Moderate shedding, requiring regular grooming to prevent matting. **Health and Longevity:** * **Life Expectancy:** 10-12 years - Average to above-average lifespan for a medium-sized breed. * **Height and Weight:** The Golden Retriever stands between 23 and 24 inches tall at the shoulder, with a weight range of 65-75 pounds for males and 55-65 pounds for females. **Recommendations:** * **Families with Children:** The Golden Retriever is an excellent breed for families with children, offering a loving and playful companion. * **First-Time Dog Owners:** The breed's trainability and adaptability make it an ideal choice for new dog owners. * **Active Owners:** While moderate exercise is required, the Golden Retriever is not a high maintenance breed, allowing owners to balance their lifestyle with their love for the dog. **In Conclusion:** The Golden Retriever is a loyal, friendly, and energetic breed that excels as a family pet. Its average energy level, ease of training, and minimal barking make it a great choice for many families. Regular grooming and exercise are essential to maintaining their health and coat condition. With proper care and attention, the Golden Retriever will provide years of companionship and affection.
**American Bulldog Breed Assessment** The American Bulldog is a sturdy and affectionate breed that makes a great companion for families. Here's an overview of its characteristics: **Appearance:** The American Bulldog is a strong and muscular dog with a wide range of height and weight. Males can weigh up to 100 pounds and stand as tall as 28 inches, while females can weigh up to 80 pounds and stand up to 24 inches. With its short, easy-to-maintain coat and minimal shedding, it's an ideal choice for those who want a low-maintenance dog. **Temperament:** The American Bulldog is known for its friendly and outgoing nature. It's generally good with children and other dogs, although its shedding rating of 2 indicates that it may not be suitable for families with severe allergies. While it's not particularly protective, it will alert its owner to strangers. **Vocalization:** American Bulldogs are not excessively vocal dogs, with a barking rating of 1 out of 5. This makes them a great choice for those who want a quiet companion. **Playfulness and Energy:** This breed has a moderate level of energy, which means it needs regular exercise to stay happy and healthy. However, it's not an overly demanding dog and can adapt to a range of living situations. **Trainability:** American Bulldogs are known for their intelligence and trainability, with a trainability rating of 4 out of 5. They respond well to positive reinforcement and consistent training, making them a great choice for first-time dog owners. **Life Expectancy:** The American Bulldog has a minimum life expectancy of 10 years and a maximum life expectancy of 12 years, which is relatively long compared to other breeds. **Overall:** The American Bulldog is a loyal, loving, and adaptable breed that makes a great companion for many families. While it may require regular exercise and consistent training, its low-maintenance coat and minimal shedding make it an ideal choice for those who want a low-maintenance dog. With proper care and attention, the American Bulldog can live a long and happy life as a beloved member of your family.
Conclusion
Llama 3.2 Vision showcases the versatility and power of LLMs in Groq API, enabling developers to not just process an image but unlock new capabilities through function calling. In our demo, we demonstrated the ability to use Llama 3.2 Vision to identify dog breeds from images and call an API to retrieve additional information, and then write an assessment of that breed from the information extracted. This seamless integration highlights Llama 3.2 Vision's flexibility and power, making it a valuable tool for applications like image classification, object detection, and more. We're excited to see what you build with Llama 3.2 Vision on Groq API!