zenaight commited on
Commit
1a45c02
Β·
1 Parent(s): ae927ca
Files changed (2) hide show
  1. ai_chat.py +2 -2
  2. persona_manager.py +22 -4
ai_chat.py CHANGED
@@ -218,8 +218,8 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
218
 
219
  # Proactive persona extraction from any message
220
  extracted_persona = {}
221
- if not is_persona_question and wa_id:
222
- # Try to extract persona fields from the current message
223
  extracted_persona = await extract_persona_from_message(user_message, persona)
224
  print(f"Extracted persona from message: {extracted_persona}")
225
 
 
218
 
219
  # Proactive persona extraction from any message
220
  extracted_persona = {}
221
+ if wa_id:
222
+ # Try to extract persona fields from the current message (ALWAYS)
223
  extracted_persona = await extract_persona_from_message(user_message, persona)
224
  print(f"Extracted persona from message: {extracted_persona}")
225
 
persona_manager.py CHANGED
@@ -75,9 +75,19 @@ async def update_persona_field(wa_id: str, field: str, value) -> bool:
75
  }
76
  print(f"Updating persona field {field} with value {value} for user {wa_id}")
77
  print(f"Update data: {update_data}")
 
 
 
 
 
78
  response = supabase.table("user_personas").update(update_data).eq("wa_id", wa_id).execute()
79
  print(f"Persona update response: {response.data if response.data else 'No data returned'}")
80
  print(f"Response status: {response.status_code if hasattr(response, 'status_code') else 'No status'}")
 
 
 
 
 
81
  return True
82
  except Exception as e:
83
  print(f"Error updating persona field {field}: {e}")
@@ -414,18 +424,21 @@ async def extract_persona_from_message(user_message: str, current_persona: Dict)
414
  return {}
415
 
416
  try:
 
417
  # Create a comprehensive prompt for AI extraction
418
  system_prompt = """You are a property agent assistant. Extract any persona information from the user's message.
419
 
420
  Available persona fields:
421
  - intent: "buy" or "lease" (extract from words like buy, purchase, own, lease, rent)
422
- - location_preference: Extract location names, including abbreviations (e.g., "cpt" = "cape town", "jhb" = "johannesburg", "pta" = "pretoria")
423
  - budget: Extract numeric values with currency/budget indicators (e.g., "500k" = 500000, "$1m" = 1000000)
424
  - size_preference_sqm: Extract size in square meters (convert from sq ft if needed)
425
  - must_have: Extract features as array (e.g., ["truck access", "office space"])
 
426
 
427
  IMPORTANT: You MUST return a valid JSON object. If no information is found, return {} (empty object).
428
- For location_preference, be very generous in extraction. If the user mentions ANY location (including "pretoria", "pta", "johannesburg", "jhb", "cape town", "cpt", etc.), extract it.
 
429
 
430
  Examples:
431
  - "I want a property in cpt" β†’ {"location_preference": "cape town"}
@@ -433,6 +446,8 @@ Examples:
433
  - "Looking for a property in pretoria for my clothing business" β†’ {"location_preference": "pretoria"}
434
  - "Need space for clothing business with office" β†’ {"must_have": ["office"]}
435
  - "I want one big enough for a phara manufacturing plant" β†’ {"size_preference_sqm": 5000, "must_have": ["manufacturing"]}
 
 
436
 
437
  Return ONLY the JSON object, no other text."""
438
 
@@ -441,9 +456,12 @@ Return ONLY the JSON object, no other text."""
441
  {"role": "user", "content": user_message}
442
  ]
443
 
 
444
  response = llm.invoke(messages)
 
445
 
446
  print(f"AI extraction response: {response.content}") # Debug
 
447
 
448
  # Parse the JSON response
449
  import json
@@ -454,9 +472,9 @@ Return ONLY the JSON object, no other text."""
454
  # Validate and clean the extracted data
455
  cleaned_extracted = {}
456
 
457
- # Only include fields that aren't already set in current_persona
458
  for field, value in extracted.items():
459
- if field in PERSONA_FIELDS and not current_persona.get(field) and value is not None:
460
  cleaned_extracted[field] = value
461
 
462
  print(f"Cleaned extraction: {cleaned_extracted}") # Debug
 
75
  }
76
  print(f"Updating persona field {field} with value {value} for user {wa_id}")
77
  print(f"Update data: {update_data}")
78
+
79
+ # First check if the persona exists
80
+ check_response = supabase.table("user_personas").select("*").eq("wa_id", wa_id).execute()
81
+ print(f"Current persona data: {check_response.data}")
82
+
83
  response = supabase.table("user_personas").update(update_data).eq("wa_id", wa_id).execute()
84
  print(f"Persona update response: {response.data if response.data else 'No data returned'}")
85
  print(f"Response status: {response.status_code if hasattr(response, 'status_code') else 'No status'}")
86
+
87
+ # Verify the update
88
+ verify_response = supabase.table("user_personas").select("*").eq("wa_id", wa_id).execute()
89
+ print(f"Updated persona data: {verify_response.data}")
90
+
91
  return True
92
  except Exception as e:
93
  print(f"Error updating persona field {field}: {e}")
 
424
  return {}
425
 
426
  try:
427
+ print(f"Starting AI extraction for message: '{user_message}'")
428
  # Create a comprehensive prompt for AI extraction
429
  system_prompt = """You are a property agent assistant. Extract any persona information from the user's message.
430
 
431
  Available persona fields:
432
  - intent: "buy" or "lease" (extract from words like buy, purchase, own, lease, rent)
433
+ - location_preference: Extract location names, ALWAYS use full names (e.g., "cpt" = "cape town", "jhb" = "johannesburg", "pta" = "pretoria")
434
  - budget: Extract numeric values with currency/budget indicators (e.g., "500k" = 500000, "$1m" = 1000000)
435
  - size_preference_sqm: Extract size in square meters (convert from sq ft if needed)
436
  - must_have: Extract features as array (e.g., ["truck access", "office space"])
437
+ - language: Extract language preference (e.g., "afrikaans", "english", "afrikaans praat", "speak afrikaans")
438
 
439
  IMPORTANT: You MUST return a valid JSON object. If no information is found, return {} (empty object).
440
+ For location_preference, be very generous in extraction and ALWAYS use full city names, not abbreviations.
441
+ For language, extract if user mentions language preference (e.g., "praat afrikaans", "speak english", etc.)
442
 
443
  Examples:
444
  - "I want a property in cpt" β†’ {"location_preference": "cape town"}
 
446
  - "Looking for a property in pretoria for my clothing business" β†’ {"location_preference": "pretoria"}
447
  - "Need space for clothing business with office" β†’ {"must_have": ["office"]}
448
  - "I want one big enough for a phara manufacturing plant" β†’ {"size_preference_sqm": 5000, "must_have": ["manufacturing"]}
449
+ - "Praat van nou af net afrikaans met my" β†’ {"language": "afrikaans"}
450
+ - "Speak English from now on" β†’ {"language": "english"}
451
 
452
  Return ONLY the JSON object, no other text."""
453
 
 
456
  {"role": "user", "content": user_message}
457
  ]
458
 
459
+ print(f"Calling LLM with messages: {messages}")
460
  response = llm.invoke(messages)
461
+ print(f"LLM response received: {type(response)}")
462
 
463
  print(f"AI extraction response: {response.content}") # Debug
464
+ print(f"User message being extracted: {user_message}") # Debug
465
 
466
  # Parse the JSON response
467
  import json
 
472
  # Validate and clean the extracted data
473
  cleaned_extracted = {}
474
 
475
+ # Include fields that can be extracted, even if they're already set (allow updates)
476
  for field, value in extracted.items():
477
+ if field in PERSONA_FIELDS and value is not None:
478
  cleaned_extracted[field] = value
479
 
480
  print(f"Cleaned extraction: {cleaned_extracted}") # Debug