Spaces:
Running
Running
sunheycho
commited on
Commit
·
ce3f373
1
Parent(s):
c6d454e
Improve LLM prompts for better JSON generation
Browse files- Add explicit JSON format requirements to specification extraction prompts
- Include concrete JSON examples for each product type (car, phone, laptop, generic)
- Emphasize 'ONLY valid JSON' requirement to reduce fallback parsing
- Should reduce malformed JSON responses like 'car"' and incomplete arrays
- product_comparison.py +109 -46
product_comparison.py
CHANGED
@@ -341,8 +341,21 @@ class ImageProcessingAgent(BaseAgent):
|
|
341 |
if ':' in line:
|
342 |
key, value = line.split(':', 1)
|
343 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
344 |
-
value
|
345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
extracted[key] = value
|
347 |
return extracted
|
348 |
except Exception as e:
|
@@ -418,50 +431,62 @@ class FeatureExtractionAgent(BaseAgent):
|
|
418 |
prompt = f"""Based on this product information, generate a detailed list of specifications.
|
419 |
Product information: {json.dumps(product_info)}
|
420 |
|
421 |
-
For a {product_type}, extract or infer the following specifications
|
|
|
|
|
|
|
|
|
422 |
"""
|
423 |
|
424 |
# Add product-specific specification types based on product type
|
425 |
if "car" in product_type.lower() or "vehicle" in product_type.lower():
|
426 |
-
prompt += """
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
|
|
|
|
436 |
elif "phone" in product_type.lower() or "smartphone" in product_type.lower():
|
437 |
-
prompt += """
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
|
|
|
|
446 |
elif "laptop" in product_type.lower() or "computer" in product_type.lower():
|
447 |
-
prompt += """
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
|
|
|
|
455 |
else:
|
456 |
# Generic product specifications
|
457 |
-
prompt += """
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
|
|
|
|
465 |
|
466 |
try:
|
467 |
# Use LangChain's invoke method
|
@@ -485,8 +510,21 @@ class FeatureExtractionAgent(BaseAgent):
|
|
485 |
if ':' in line:
|
486 |
key, value = line.split(':', 1)
|
487 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
488 |
-
value
|
489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
490 |
specs[key] = value
|
491 |
return specs
|
492 |
except Exception as e:
|
@@ -628,12 +666,24 @@ class ComparisonAgent(BaseAgent):
|
|
628 |
comparison = {}
|
629 |
|
630 |
for line in lines:
|
631 |
-
|
632 |
-
if ':' in line and not line.startswith(' '):
|
633 |
key, value = line.split(':', 1)
|
634 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
635 |
-
value
|
636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
637 |
comparison[key] = value
|
638 |
|
639 |
return comparison
|
@@ -823,8 +873,21 @@ class RecommendationAgent(BaseAgent):
|
|
823 |
if ':' in line:
|
824 |
key, value = line.split(':', 1)
|
825 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
826 |
-
value
|
827 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
828 |
recommendation[key] = value
|
829 |
|
830 |
return recommendation
|
|
|
341 |
if ':' in line:
|
342 |
key, value = line.split(':', 1)
|
343 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
344 |
+
# Clean up value more thoroughly
|
345 |
+
value = value.strip().rstrip(',').rstrip(';')
|
346 |
+
# Remove mismatched quotes
|
347 |
+
if value.startswith('"') and not value.endswith('"'):
|
348 |
+
value = value[1:]
|
349 |
+
elif value.endswith('"') and not value.startswith('"'):
|
350 |
+
value = value[:-1]
|
351 |
+
if value.startswith("'") and not value.endswith("'"):
|
352 |
+
value = value[1:]
|
353 |
+
elif value.endswith("'") and not value.startswith("'"):
|
354 |
+
value = value[:-1]
|
355 |
+
# Clean up array-like strings
|
356 |
+
if value.startswith('[') and not value.endswith(']'):
|
357 |
+
value = value + ']'
|
358 |
+
if key and value and value not in ['null', 'None', '']:
|
359 |
extracted[key] = value
|
360 |
return extracted
|
361 |
except Exception as e:
|
|
|
431 |
prompt = f"""Based on this product information, generate a detailed list of specifications.
|
432 |
Product information: {json.dumps(product_info)}
|
433 |
|
434 |
+
For a {product_type}, extract or infer the following specifications.
|
435 |
+
|
436 |
+
IMPORTANT: Return ONLY valid JSON format. Do not include any explanatory text before or after the JSON.
|
437 |
+
|
438 |
+
Required JSON structure for a {product_type}:
|
439 |
"""
|
440 |
|
441 |
# Add product-specific specification types based on product type
|
442 |
if "car" in product_type.lower() or "vehicle" in product_type.lower():
|
443 |
+
prompt += """{
|
444 |
+
"make": "The manufacturer of the car",
|
445 |
+
"model": "The model name",
|
446 |
+
"year": "Estimated year of manufacture",
|
447 |
+
"body_type": "The body style (sedan, SUV, etc.)",
|
448 |
+
"fuel_type": "The fuel type if identifiable",
|
449 |
+
"engine": "Engine specifications if identifiable",
|
450 |
+
"color": "Exterior color",
|
451 |
+
"features": ["List", "of", "visible", "features"]
|
452 |
+
}
|
453 |
+
|
454 |
+
Example: {"make": "Toyota", "model": "Camry", "year": "2020", "body_type": "Sedan", "fuel_type": "Gasoline", "engine": "2.5L", "color": "Blue", "features": ["LED headlights", "Alloy wheels"]}"""
|
455 |
elif "phone" in product_type.lower() or "smartphone" in product_type.lower():
|
456 |
+
prompt += """{
|
457 |
+
"brand": "The manufacturer",
|
458 |
+
"model": "The phone model",
|
459 |
+
"screen_size": "Estimated screen size",
|
460 |
+
"camera": "Visible camera specifications",
|
461 |
+
"color": "Device color",
|
462 |
+
"generation": "Device generation if identifiable",
|
463 |
+
"features": ["List", "of", "visible", "features"]
|
464 |
+
}
|
465 |
+
|
466 |
+
Example: {"brand": "Apple", "model": "iPhone 14", "screen_size": "6.1 inches", "camera": "Dual camera", "color": "Blue", "generation": "14th", "features": ["Face ID", "Wireless charging"]}"""
|
467 |
elif "laptop" in product_type.lower() or "computer" in product_type.lower():
|
468 |
+
prompt += """{
|
469 |
+
"brand": "The manufacturer",
|
470 |
+
"model": "The computer model",
|
471 |
+
"screen_size": "Estimated screen size",
|
472 |
+
"form_factor": "Laptop, desktop, all-in-one, etc.",
|
473 |
+
"color": "Device color",
|
474 |
+
"features": ["List", "of", "visible", "features"]
|
475 |
+
}
|
476 |
+
|
477 |
+
Example: {"brand": "Dell", "model": "XPS 13", "screen_size": "13.3 inches", "form_factor": "Laptop", "color": "Silver", "features": ["Touchscreen", "Backlit keyboard"]}"""
|
478 |
else:
|
479 |
# Generic product specifications
|
480 |
+
prompt += """{
|
481 |
+
"brand": "The manufacturer if identifiable",
|
482 |
+
"model": "The product model if identifiable",
|
483 |
+
"color": "Product color",
|
484 |
+
"dimensions": "Estimated dimensions",
|
485 |
+
"features": ["List", "of", "visible", "features"],
|
486 |
+
"materials": "Visible materials used"
|
487 |
+
}
|
488 |
+
|
489 |
+
Example: {"brand": "Unknown", "model": "Unknown", "color": "Black", "dimensions": "Medium", "features": ["Modern design"], "materials": "Plastic"}"""
|
490 |
|
491 |
try:
|
492 |
# Use LangChain's invoke method
|
|
|
510 |
if ':' in line:
|
511 |
key, value = line.split(':', 1)
|
512 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
513 |
+
# Clean up value more thoroughly
|
514 |
+
value = value.strip().rstrip(',').rstrip(';')
|
515 |
+
# Remove mismatched quotes
|
516 |
+
if value.startswith('"') and not value.endswith('"'):
|
517 |
+
value = value[1:]
|
518 |
+
elif value.endswith('"') and not value.startswith('"'):
|
519 |
+
value = value[:-1]
|
520 |
+
if value.startswith("'") and not value.endswith("'"):
|
521 |
+
value = value[1:]
|
522 |
+
elif value.endswith("'") and not value.startswith("'"):
|
523 |
+
value = value[:-1]
|
524 |
+
# Clean up array-like strings
|
525 |
+
if value.startswith('[') and not value.endswith(']'):
|
526 |
+
value = value + ']'
|
527 |
+
if key and value and value not in ['null', 'None', '']:
|
528 |
specs[key] = value
|
529 |
return specs
|
530 |
except Exception as e:
|
|
|
666 |
comparison = {}
|
667 |
|
668 |
for line in lines:
|
669 |
+
if ':' in line:
|
|
|
670 |
key, value = line.split(':', 1)
|
671 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
672 |
+
# Clean up value more thoroughly
|
673 |
+
value = value.strip().rstrip(',').rstrip(';')
|
674 |
+
# Remove mismatched quotes
|
675 |
+
if value.startswith('"') and not value.endswith('"'):
|
676 |
+
value = value[1:]
|
677 |
+
elif value.endswith('"') and not value.startswith('"'):
|
678 |
+
value = value[:-1]
|
679 |
+
if value.startswith("'") and not value.endswith("'"):
|
680 |
+
value = value[1:]
|
681 |
+
elif value.endswith("'") and not value.startswith("'"):
|
682 |
+
value = value[:-1]
|
683 |
+
# Clean up array-like strings
|
684 |
+
if value.startswith('[') and not value.endswith(']'):
|
685 |
+
value = value + ']'
|
686 |
+
if key and value and value not in ['null', 'None', '']:
|
687 |
comparison[key] = value
|
688 |
|
689 |
return comparison
|
|
|
873 |
if ':' in line:
|
874 |
key, value = line.split(':', 1)
|
875 |
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
876 |
+
# Clean up value more thoroughly
|
877 |
+
value = value.strip().rstrip(',').rstrip(';')
|
878 |
+
# Remove mismatched quotes
|
879 |
+
if value.startswith('"') and not value.endswith('"'):
|
880 |
+
value = value[1:]
|
881 |
+
elif value.endswith('"') and not value.startswith('"'):
|
882 |
+
value = value[:-1]
|
883 |
+
if value.startswith("'") and not value.endswith("'"):
|
884 |
+
value = value[1:]
|
885 |
+
elif value.endswith("'") and not value.startswith("'"):
|
886 |
+
value = value[:-1]
|
887 |
+
# Clean up array-like strings
|
888 |
+
if value.startswith('[') and not value.endswith(']'):
|
889 |
+
value = value + ']'
|
890 |
+
if key and value and value not in ['null', 'None', '']:
|
891 |
recommendation[key] = value
|
892 |
|
893 |
return recommendation
|