11import base64
22import io
33import json
4+ import sys
45
56from PIL import Image
67
@@ -102,16 +103,10 @@ def convert_to_openai_messages(
102103 new_message ["role" ] = "user"
103104 new_message ["content" ] = content
104105 elif interpreter .code_output_sender == "assistant" :
105- if "@@@SEND_MESSAGE_AS_USER@@@" in message ["content" ]:
106- new_message ["role" ] = "user"
107- new_message ["content" ] = message ["content" ].replace (
108- "@@@SEND_MESSAGE_AS_USER@@@" , ""
109- )
110- else :
111- new_message ["role" ] = "assistant"
112- new_message ["content" ] = (
113- "\n ```output\n " + message ["content" ] + "\n ```"
114- )
106+ new_message ["role" ] = "assistant"
107+ new_message ["content" ] = (
108+ "\n ```output\n " + message ["content" ] + "\n ```"
109+ )
115110
116111 elif message ["type" ] == "image" :
117112 if message .get ("format" ) == "description" :
@@ -129,95 +124,18 @@ def convert_to_openai_messages(
129124 else :
130125 extension = "png"
131126
132- # Construct the content string
133- content = f"data:image/{ extension } ;base64,{ message ['content' ]} "
134-
135- if shrink_images :
136- try :
137- # Decode the base64 image
138- img_data = base64 .b64decode (message ["content" ])
139- img = Image .open (io .BytesIO (img_data ))
140-
141- # Resize the image if it's width is more than 1024
142- if img .width > 1024 :
143- new_height = int (img .height * 1024 / img .width )
144- img = img .resize ((1024 , new_height ))
145-
146- # Convert the image back to base64
147- buffered = io .BytesIO ()
148- img .save (buffered , format = extension )
149- img_str = base64 .b64encode (buffered .getvalue ()).decode (
150- "utf-8"
151- )
152- content = f"data:image/{ extension } ;base64,{ img_str } "
153- except :
154- # This should be non blocking. It's not required
155- # print("Failed to shrink image. Proceeding with original image size.")
156- pass
157-
158- # Must be less than 5mb
159- # Calculate the size of the original binary data in bytes
160- content_size_bytes = len (message ["content" ]) * 3 / 4
161-
162- # Convert the size to MB
163- content_size_mb = content_size_bytes / (1024 * 1024 )
164-
165- # If the content size is greater than 5 MB, resize the image
166- if content_size_mb > 5 :
167- try :
168- # Decode the base64 image
169- img_data = base64 .b64decode (message ["content" ])
170- img = Image .open (io .BytesIO (img_data ))
171-
172- # Calculate the size of the original binary data in bytes
173- content_size_bytes = len (img_data )
174-
175- # Convert the size to MB
176- content_size_mb = content_size_bytes / (1024 * 1024 )
177-
178- # Run in a loop to make SURE it's less than 5mb
179- while content_size_mb > 5 :
180- # Calculate the scale factor needed to reduce the image size to 5 MB
181- scale_factor = (5 / content_size_mb ) ** 0.5
182-
183- # Calculate the new dimensions
184- new_width = int (img .width * scale_factor )
185- new_height = int (img .height * scale_factor )
186-
187- # Resize the image
188- img = img .resize ((new_width , new_height ))
189-
190- # Convert the image back to base64
191- buffered = io .BytesIO ()
192- img .save (buffered , format = extension )
193- img_str = base64 .b64encode (buffered .getvalue ()).decode (
194- "utf-8"
195- )
196-
197- # Set the content
198- content = f"data:image/{ extension } ;base64,{ img_str } "
199-
200- # Recalculate the size of the content in bytes
201- content_size_bytes = len (content ) * 3 / 4
202-
203- # Convert the size to MB
204- content_size_mb = content_size_bytes / (1024 * 1024 )
205- except :
206- # This should be non blocking. It's not required
207- # print("Failed to shrink image. Proceeding with original image size.")
208- pass
127+ encoded_string = message ["content" ]
209128
210129 elif message ["format" ] == "path" :
211130 # Convert to base64
212131 image_path = message ["content" ]
213- file_extension = image_path .split ("." )[- 1 ]
132+ extension = image_path .split ("." )[- 1 ]
214133
215134 with open (image_path , "rb" ) as image_file :
216135 encoded_string = base64 .b64encode (image_file .read ()).decode (
217136 "utf-8"
218137 )
219138
220- content = f"data:image/{ file_extension } ;base64,{ encoded_string } "
221139 else :
222140 # Probably would be better to move this to a validation pass
223141 # Near core, through the whole messages object
@@ -228,17 +146,57 @@ def convert_to_openai_messages(
228146 f"Unrecognized image format: { message ['format' ]} "
229147 )
230148
231- # Calculate the size of the original binary data in bytes
232- content_size_bytes = len (content ) * 3 / 4
149+ content = f"data:image/{ extension } ;base64,{ encoded_string } "
150+
151+ if shrink_images :
152+ # Shrink to less than 5mb
153+
154+ # Calculate size
155+ content_size_bytes = sys .getsizeof (str (content ))
156+
157+ # Convert the size to MB
158+ content_size_mb = content_size_bytes / (1024 * 1024 )
159+
160+ # If the content size is greater than 5 MB, resize the image
161+ if content_size_mb > 5 :
162+ # Decode the base64 image
163+ img_data = base64 .b64decode (encoded_string )
164+ img = Image .open (io .BytesIO (img_data ))
165+
166+ # Run in a loop to make SURE it's less than 5mb
167+ for _ in range (10 ):
168+ # Calculate the scale factor needed to reduce the image size to 4.9 MB
169+ scale_factor = (4.9 / content_size_mb ) ** 0.5
170+
171+ # Calculate the new dimensions
172+ new_width = int (img .width * scale_factor )
173+ new_height = int (img .height * scale_factor )
174+
175+ # Resize the image
176+ img = img .resize ((new_width , new_height ))
177+
178+ # Convert the image back to base64
179+ buffered = io .BytesIO ()
180+ img .save (buffered , format = extension )
181+ encoded_string = base64 .b64encode (
182+ buffered .getvalue ()
183+ ).decode ("utf-8" )
184+
185+ # Set the content
186+ content = f"data:image/{ extension } ;base64,{ encoded_string } "
233187
234- # Convert the size to MB
235- content_size_mb = content_size_bytes / ( 1024 * 1024 )
188+ # Recalculate the size of the content in bytes
189+ content_size_bytes = sys . getsizeof ( str ( content ) )
236190
237- # Print the size of the content in MB
238- # print(f"File size: { content_size_mb} MB" )
191+ # Convert the size to MB
192+ content_size_mb = content_size_bytes / ( 1024 * 1024 )
239193
240- # Assert that the content size is under 20 MB
241- assert content_size_mb < 20 , "Content size exceeds 20 MB"
194+ if content_size_mb < 5 :
195+ break
196+ else :
197+ print (
198+ "Attempted to shrink the image but failed. Sending to the LLM anyway."
199+ )
242200
243201 new_message = {
244202 "role" : "user" ,
0 commit comments