Skip to content

Commit

Permalink
Merge pull request #3711 from hmcp22/add_gemini_base64_image_support
Browse files Browse the repository at this point in the history
Adding decoding of base64 image data for gemini pro 1.5
  • Loading branch information
ishaan-jaff authored May 20, 2024
2 parents 55c3e41 + 32e25cd commit 622e241
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions litellm/llms/prompt_templates/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,15 +1501,30 @@ def _gemini_vision_convert_messages(messages: list):
# Case 1: Image from URL
image = _load_image_from_url(img)
processed_images.append(image)

else:
try:
from PIL import Image
except:
raise Exception(
"gemini image conversion failed please run `pip install Pillow`"
)
# Case 2: Image filepath (e.g. temp.jpeg) given
image = Image.open(img)

if "base64" in img:
# Case 2: Base64 image data
import base64
import io
# Extract the base64 image data
base64_data = img.split("base64,")[1]

# Decode the base64 image data
image_data = base64.b64decode(base64_data)

# Load the image from the decoded data
image = Image.open(io.BytesIO(image_data))
else:
# Case 3: Image filepath (e.g. temp.jpeg) given
image = Image.open(img)
processed_images.append(image)
content = [prompt] + processed_images
return content
Expand Down

0 comments on commit 622e241

Please sign in to comment.