Skip to content

Commit e066762

Browse files
committed
Enhance image selection logic and update response handling
- Added regex to extract numeric choice from verbose responses in the vision model. - Updated instructions for image matching to clarify response format. - Changed session.flush() to session.commit() for better database transaction handling.
1 parent 0787f5b commit e066762

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

app/tasks/catalog_image.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import logging
33
import os
4+
import re
45
import time
56

67
import anthropic
@@ -148,7 +149,9 @@ def _download_images(urls: list[str]) -> list[tuple[str, bytes, str]]:
148149
"- Have a clean, white, or neutral background\n"
149150
"- Show the complete product, not a close-up of a detail\n"
150151
"- Match the specific product described (correct brand, model, color if known)\n\n"
151-
"If NONE of the images are a good match for the product, respond with 0."
152+
"IMPORTANT: Your entire response must be a single integer and nothing else. "
153+
"No explanation, no reasoning, no text. Just the number.\n"
154+
"If NONE of the images are a good match, respond with: 0"
152155
)
153156

154157

@@ -222,8 +225,13 @@ def _select_best_image(
222225
try:
223226
choice = int(reply)
224227
except ValueError:
225-
logger.warning("Vision model returned non-numeric response: %s", reply)
226-
return None
228+
match = re.search(r'\b([0-5])\b', reply)
229+
if match:
230+
choice = int(match.group(1))
231+
logger.info("Extracted choice %d from verbose response", choice)
232+
else:
233+
logger.warning("Vision model returned non-numeric response: %s", reply)
234+
return None
227235

228236
if choice == 0:
229237
return None
@@ -291,7 +299,7 @@ def find_product_image(self, catalog_product_id: int):
291299

292300
if image_url:
293301
entry.image_url = image_url
294-
session.flush()
302+
session.commit()
295303
logger.info("Set image_url for %s: %s", label, image_url)
296304
else:
297305
logger.info("No suitable image found for %s", label)

0 commit comments

Comments
 (0)