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
This commit is contained in:
parent
ff9d8011a6
commit
305375d9e6
1 changed files with 21 additions and 0 deletions
21
app.py
21
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})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue