Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: limit image size, fixes exception for large images (fixes #185) #188

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
ErikBjare marked this conversation as resolved.
Show resolved Hide resolved
# TODO: use compression to reduce file size
# print(f"{len(data)=}")
if len(data) > 5_000_000:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the size of data (base64 encoded) is incorrect for file size validation. Base64 encoding increases the size by approximately 33%. Instead, check len(data_bytes) to ensure the original file size is within limits.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ellipsis-dev I did that originally, but it didn't match the numbers returned by the API (roughly the 33% difference), maybe add a comment about this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ErikBjare, I have addressed your comments in pull request #189


You can configure Ellipsis to address comments with a direct commit or a side PR, see docs.

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
Loading