Better prompt
This commit is contained in:
+19
-23
@@ -4,45 +4,44 @@ from APIs.aichat import generate
|
|||||||
import shared.config as config
|
import shared.config as config
|
||||||
|
|
||||||
|
|
||||||
# Output schema as a Pydantic model
|
# 1. Output schema with strict anti-hallucination descriptions
|
||||||
class DishSafetyResult(BaseModel):
|
class DishSafetyResult(BaseModel):
|
||||||
visible_objects: list[str] = Field(
|
visible_objects: list[str] = Field(
|
||||||
description="List all distinct physical objects visible in or around the dish (e.g., bowl, plastic tray, liquid, spoon, cover)."
|
description="Objective list of physical items visible (e.g., 'black plastic tray', 'stew', 'plastic fork'). Do NOT categorize them as safe or hazards here."
|
||||||
)
|
)
|
||||||
material_analysis: str = Field(
|
material_analysis: str = Field(
|
||||||
description="Analyze the physical material of each visible object (e.g., ceramic, polypropylene plastic, stainless steel, aluminum foil)."
|
description="Describe physical materials based strictly on visual cues. Note: Smooth, rigid black plastic meal trays are Polypropylene/CPET plastic (safe), NOT foam/styrofoam."
|
||||||
)
|
)
|
||||||
detected_hazards: list[str] = Field(
|
detected_hazards: list[str] = Field(
|
||||||
description=(
|
description=(
|
||||||
"List ONLY explicit, high-risk microwave hazards: metal items, cutlery, aluminum foil, "
|
"List ONLY explicit physical hazards: metal utensils/cutlery, aluminum foil/wrappers, "
|
||||||
"metallic trim/gilding, Styrofoam (expanded polystyrene), or tightly sealed/unvented foil lids. "
|
"metallic foil trim, spongy white foam/styrofoam containers, or airtight unvented plastic wrap. "
|
||||||
"Do NOT include standard plastic containers, TV dinner trays, polypropylene (#5), or Tupperware as hazards. Empty if none."
|
"Rigid black or clear plastic meal trays are NOT hazards. Empty if none."
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
is_safe: bool = Field(
|
is_safe: bool = Field(
|
||||||
description="Must be set to True if detected_hazards is empty. Otherwise False."
|
description="Set to True if detected_hazards is completely empty. Otherwise set to False."
|
||||||
)
|
)
|
||||||
warning_message: str = Field(
|
warning_message: str = Field(
|
||||||
description="One short sentence explaining the hazard if detected_hazards is not empty, otherwise an empty string."
|
description="One brief sentence warning if detected_hazards is non-empty, otherwise an empty string."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def check_dish_safety(image_path: str) -> dict:
|
def check_dish_safety(image_path: str) -> dict:
|
||||||
prompt = (
|
prompt = (
|
||||||
"Examine this photo of a meal intended for microwave heating.\n\n"
|
"Analyze this food image for critical microwave safety hazards.\n\n"
|
||||||
"Inspect all objects to determine if any CRITICAL microwave hazards exist.\n\n"
|
"MATERIAL IDENTIFICATION RULES:\n"
|
||||||
"STRICT HAZARDS TO DETECT:\n"
|
"- Black plastic meal trays / TV dinner containers = Smooth, rigid Polypropylene (PP) or CPET plastic. These are STANDARD MICROWAVE MEAL TRAYS and are SAFE.\n"
|
||||||
"- Metal utensils, cutlery, or metal objects.\n"
|
"- Styrofoam / EPS = White or light-colored spongy, cellular foam. (Do NOT misclassify smooth black plastic as styrofoam!).\n\n"
|
||||||
"- Aluminum foil, metallic packaging, or decorative metallic trim.\n"
|
"STRICT HAZARDS TO LOOK FOR:\n"
|
||||||
"- Expanded Polystyrene / Styrofoam containers.\n"
|
"1. Metal objects (metal forks, spoons, knives, metal bowls).\n"
|
||||||
"- Completely sealed non-vented foil or plastic film lids (explosion risk).\n\n"
|
"2. Aluminum foil or foil packaging.\n"
|
||||||
"SAFE MATERIALS (DO NOT FLAG AS HAZARDS):\n"
|
"3. Metallic gold/silver decorative trim on ceramic dishes.\n"
|
||||||
"- Standard microwavable food containers, plastic meal prep trays, black plastic TV dinner trays, and polypropylene (PP / #5) plastics.\n"
|
"4. White spongy foam / Styrofoam packaging.\n"
|
||||||
"- Glass, ceramic, or paper containers.\n\n"
|
"5. Tightly sealed, unvented plastic film or foil lids.\n\n"
|
||||||
"Only mark is_safe as False if a clear, high-risk hazard from the strictly dangerous list above is present."
|
"If the meal is simply inside a standard black plastic tray or ceramic/glass dish without any metal/foil/foam present, flag it as SAFE (is_safe = True)."
|
||||||
)
|
)
|
||||||
|
|
||||||
# 2. Pass the Pydantic class directly to generate()
|
|
||||||
response_raw = generate(
|
response_raw = generate(
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
images=[image_path],
|
images=[image_path],
|
||||||
@@ -51,14 +50,11 @@ def check_dish_safety(image_path: str) -> dict:
|
|||||||
)
|
)
|
||||||
print(f"[Debug] Raw response from AI generator: {response_raw}")
|
print(f"[Debug] Raw response from AI generator: {response_raw}")
|
||||||
|
|
||||||
# 3. Handle response parsing
|
|
||||||
if isinstance(response_raw, str):
|
if isinstance(response_raw, str):
|
||||||
# Parse and validate the JSON string into the Pydantic model, then return as a dict
|
|
||||||
try:
|
try:
|
||||||
validated_result = DishSafetyResult.model_validate_json(response_raw)
|
validated_result = DishSafetyResult.model_validate_json(response_raw)
|
||||||
return validated_result.model_dump()
|
return validated_result.model_dump()
|
||||||
except Exception:
|
except Exception:
|
||||||
# Fallback to standard json.loads if raw parsing is needed
|
|
||||||
return json.loads(response_raw)
|
return json.loads(response_raw)
|
||||||
|
|
||||||
if isinstance(response_raw, DishSafetyResult):
|
if isinstance(response_raw, DishSafetyResult):
|
||||||
|
|||||||
Reference in New Issue
Block a user