From 305375d9e69a20a52cd5649db9d3cc72a960309d Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 7 Dec 2025 11:42:53 -0500 Subject: [PATCH] Limit vision model to 2 most recent images to prevent slow/hanging requests Multiple large base64 images can overwhelm the vision model. Now we: 1. First pass: identify the 2 most recent image messages 2. Second pass: build chat history, skipping older images --- app.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app.py b/app.py index f5b41e4..1b055bb 100644 --- a/app.py +++ b/app.py @@ -1881,11 +1881,32 @@ def chat_gpt(username, room_name, model_name="gpt-4o-mini"): ) chat_history = [] + max_images = 2 # Limit images to prevent slow/hanging requests + + # First pass: identify image messages to include (most recent N) + # last_messages is newest-first, so iterate in that order to find recent images + image_msg_ids_to_include = set() + if vision_enabled: + for msg in last_messages: # newest first + is_image_msg = msg.is_base64_image() or extract_external_image_url(msg.content) + if is_image_msg: + if len(image_msg_ids_to_include) < max_images: + image_msg_ids_to_include.add(msg.id) + # Continue to count all images for logging + if len(image_msg_ids_to_include) > 0: + print(f"Vision: including {len(image_msg_ids_to_include)} most recent images") + + # Second pass: build chat history (oldest first) for msg in reversed(last_messages): # Skip images for non-vision models if msg.is_base64_image() and not vision_enabled: continue + # For vision models, skip old images not in our include set + is_image_msg = msg.is_base64_image() or extract_external_image_url(msg.content) + if vision_enabled and is_image_msg and msg.id not in image_msg_ids_to_include: + continue + role = "assistant" if msg.username in SYSTEM_USERS else "user" content = build_message_content(msg, vision_enabled, room_id=room_id) chat_history.append({"role": role, "content": content})