Skip to content

Commit

Permalink
fix: limit image size, fixes exception when viewing large images (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 9, 2024
1 parent f258602 commit 9a4d2f6
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions gptme/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def replace(self, **kwargs) -> Self:
def _content_files_list(
self, openai: bool = False, anthropic: bool = False
) -> list[dict[str, Any]]:
# only these providers support files in the content
if not openai and not anthropic:
raise ValueError("Provider does not support files in the content")

# combines a content message with a list of files
content: list[dict[str, Any]] = (
self.content
Expand All @@ -99,14 +103,32 @@ def _content_files_list(
"text": f"![{f.name}]({f.name}):",
}
)

# read file
data_bytes = f.read_bytes()
data = base64.b64encode(data_bytes).decode("utf-8")

# check that the file is not too large
# anthropic limit is 5MB
# TODO: use compression to reduce file size
# print(f"{len(data)=}")
if len(data) > 5_000_000:
content.append(
{
"type": "text",
"text": "Image size exceeds 5MB. Please upload a smaller image.",
}
)
continue

if anthropic:
content.append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": media_type,
"data": base64.b64encode(f.read_bytes()).decode("utf-8"),
"data": data,
},
}
)
Expand All @@ -115,9 +137,7 @@ def _content_files_list(
content.append(
{
"type": "image_url",
"image_url": {
"url": f"data:{media_type};base64,{base64.b64encode(f.read_bytes()).decode('utf-8')}"
},
"image_url": {"url": f"data:{media_type};base64,{data}"},
}
)
else:
Expand Down

0 comments on commit 9a4d2f6

Please sign in to comment.