zenaight commited on
Commit
2cce083
·
1 Parent(s): 46e8095

more updates for better ai huan

Browse files
Files changed (2) hide show
  1. ai_chat.py +12 -1
  2. persona_manager.py +9 -0
ai_chat.py CHANGED
@@ -218,6 +218,17 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
218
  extracted_persona = await extract_persona_from_message(user_message, persona)
219
  print(f"Extracted persona from message: {extracted_persona}")
220
 
 
 
 
 
 
 
 
 
 
 
 
221
  # Process with AI
222
  result = await chat_graph.ainvoke({
223
  "user_message": user_message,
@@ -226,7 +237,7 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
226
  "wa_id": wa_id,
227
  "wamid": wamid,
228
  "session_messages": session_messages,
229
- "persona": persona,
230
  "is_persona_question": is_persona_question,
231
  "current_field": current_field,
232
  "parsed_response": parsed_response,
 
218
  extracted_persona = await extract_persona_from_message(user_message, persona)
219
  print(f"Extracted persona from message: {extracted_persona}")
220
 
221
+ # Update persona with extracted information for persona_check calculation
222
+ updated_persona = persona.copy()
223
+ for field, value in extracted_persona.items():
224
+ if value is not None:
225
+ updated_persona[field] = value
226
+
227
+ # Recalculate persona check with updated persona (but only if we extracted something)
228
+ if extracted_persona:
229
+ conversation_context = " ".join([msg["content"] for msg in session_messages[-5:]]) + " " + user_message
230
+ should_ask, field_to_ask = await should_ask_persona_question(updated_persona, conversation_context)
231
+
232
  # Process with AI
233
  result = await chat_graph.ainvoke({
234
  "user_message": user_message,
 
237
  "wa_id": wa_id,
238
  "wamid": wamid,
239
  "session_messages": session_messages,
240
+ "persona": updated_persona, # Use updated persona
241
  "is_persona_question": is_persona_question,
242
  "current_field": current_field,
243
  "parsed_response": parsed_response,
persona_manager.py CHANGED
@@ -74,8 +74,10 @@ async def update_persona_field(wa_id: str, field: str, value) -> bool:
74
  "updated_at": datetime.utcnow().isoformat()
75
  }
76
  print(f"Updating persona field {field} with value {value} for user {wa_id}")
 
77
  response = supabase.table("user_personas").update(update_data).eq("wa_id", wa_id).execute()
78
  print(f"Persona update response: {response.data if response.data else 'No data returned'}")
 
79
  return True
80
  except Exception as e:
81
  print(f"Error updating persona field {field}: {e}")
@@ -416,9 +418,12 @@ Available persona fields:
416
  Return ONLY a JSON object with the extracted fields. Only include fields that are clearly mentioned or implied.
417
  If no relevant information is found for a field, don't include it in the response.
418
 
 
 
419
  Examples:
420
  - "I want a property in cpt" → {"location_preference": "cape town"}
421
  - "Looking for warehouse in pta around 500k" → {"location_preference": "pretoria", "budget": 500000}
 
422
  - "Need space for clothing business with office" → {"must_have": ["office"]}
423
  """
424
 
@@ -429,10 +434,13 @@ Examples:
429
 
430
  response = llm.invoke(messages)
431
 
 
 
432
  # Parse the JSON response
433
  import json
434
  try:
435
  extracted = json.loads(response.content)
 
436
 
437
  # Validate and clean the extracted data
438
  cleaned_extracted = {}
@@ -442,6 +450,7 @@ Examples:
442
  if field in PERSONA_FIELDS and not current_persona.get(field) and value is not None:
443
  cleaned_extracted[field] = value
444
 
 
445
  return cleaned_extracted
446
 
447
  except json.JSONDecodeError:
 
74
  "updated_at": datetime.utcnow().isoformat()
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}")
 
418
  Return ONLY a JSON object with the extracted fields. Only include fields that are clearly mentioned or implied.
419
  If no relevant information is found for a field, don't include it in the response.
420
 
421
+ IMPORTANT: 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.
422
+
423
  Examples:
424
  - "I want a property in cpt" → {"location_preference": "cape town"}
425
  - "Looking for warehouse in pta around 500k" → {"location_preference": "pretoria", "budget": 500000}
426
+ - "Looking for a property in pretoria for my clothing business" → {"location_preference": "pretoria"}
427
  - "Need space for clothing business with office" → {"must_have": ["office"]}
428
  """
429
 
 
434
 
435
  response = llm.invoke(messages)
436
 
437
+ print(f"AI extraction response: {response.content}") # Debug
438
+
439
  # Parse the JSON response
440
  import json
441
  try:
442
  extracted = json.loads(response.content)
443
+ print(f"Parsed extraction: {extracted}") # Debug
444
 
445
  # Validate and clean the extracted data
446
  cleaned_extracted = {}
 
450
  if field in PERSONA_FIELDS and not current_persona.get(field) and value is not None:
451
  cleaned_extracted[field] = value
452
 
453
+ print(f"Cleaned extraction: {cleaned_extracted}") # Debug
454
  return cleaned_extracted
455
 
456
  except json.JSONDecodeError: