mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-09 13:00:34 +00:00
community[patch]: add png support for vertexai._parse_chat_history_gemini() (#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.
This commit is contained in:
parent
f348ad4ba8
commit
529144649e
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Union, cast
|
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Union, cast
|
||||||
|
|
||||||
@ -105,8 +106,18 @@ def _parse_chat_history_gemini(
|
|||||||
path = part["image_url"]["url"]
|
path = part["image_url"]["url"]
|
||||||
if path.startswith("gs://"):
|
if path.startswith("gs://"):
|
||||||
image = load_image_from_gcs(path=path, project=project)
|
image = load_image_from_gcs(path=path, project=project)
|
||||||
elif path.startswith("data:image/jpeg;base64,"):
|
elif path.startswith("data:image/"):
|
||||||
image = Image.from_bytes(base64.b64decode(path[23:]))
|
# 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:
|
else:
|
||||||
image = Image.load_from_file(path)
|
image = Image.load_from_file(path)
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user