Skip to content

Commit

Permalink
community[patch]: add png support for vertexai._parse_chat_history_ge…
Browse files Browse the repository at this point in the history
…mini() (#14788)

- **Description:** Modify community chat model vertexai to handle png
and other image types encoded in base64
  - **Dependencies:** added `import re` but no new dependencies.

This addresses a problem where the vertexai method
_parse_chat_history_gemini() was only recognizing image uris in jpeg
format. I made a simple change to cover other extension types.
  • Loading branch information
nicsuzor committed Dec 20, 2023
1 parent f348ad4 commit 5291446
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions libs/community/langchain_community/chat_models/vertexai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
import logging
import re
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Union, cast

Expand Down Expand Up @@ -105,8 +106,18 @@ def _convert_to_prompt(part: Union[str, Dict]) -> Part:
path = part["image_url"]["url"]
if path.startswith("gs://"):
image = load_image_from_gcs(path=path, project=project)
elif path.startswith("data:image/jpeg;base64,"):
image = Image.from_bytes(base64.b64decode(path[23:]))
elif path.startswith("data:image/"):
# extract base64 component from image uri
try:
encoded = re.search(r"data:image/\w{2,4};base64,(.*)", path).group(
1
)
except AttributeError:
raise ValueError(
"Invalid image uri. It should be in the format "
"data:image/<image_type>;base64,<base64_encoded_image>."
)
image = Image.from_bytes(base64.b64decode(encoded))
else:
image = Image.load_from_file(path)
else:
Expand Down

0 comments on commit 5291446

Please sign in to comment.