zenaight commited on
Commit
954217a
·
1 Parent(s): 804b635

Implement enhanced greeting and property search handling in chat

Browse files

- Added logic to differentiate between property search requests and greetings, ensuring property searches are only triggered when explicitly asked.
- Introduced personalized greeting messages based on user persona, prompting users about their property search preferences.
- Implemented response handling for user replies to greeting questions, allowing for seamless transitions between continuing a search or starting fresh.
- Integrated database updates to reset user persona data when requested, improving user experience and interaction flow.

Files changed (1) hide show
  1. ai_chat.py +63 -3
ai_chat.py CHANGED
@@ -12,6 +12,8 @@ from persona_manager import (
12
  get_persona_summary,
13
  extract_persona_from_message
14
  )
 
 
15
  import re
16
 
17
  def chat_with_session_memory(state):
@@ -324,12 +326,17 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
324
  conversation_context = " ".join([msg["content"] for msg in session_messages[-5:]]) + " " + user_message
325
  should_ask, field_to_ask = await should_ask_persona_question(updated_persona, conversation_context)
326
 
327
- # Check for property search intent
328
  property_search_phrases = [
329
- "show me properties", "property in", "warehouse in", "industrial in", "listings in", "toon my eiendomme", "warehouse", "factory", "show me warehouses", "show me listings"
330
  ]
331
  is_property_search = any(phrase in user_message.lower() for phrase in property_search_phrases)
332
- if persona.get("location_preference") or is_property_search:
 
 
 
 
 
333
  property_msgs = await handle_property_search(user_message, updated_persona)
334
  for msg in property_msgs:
335
  # Send each property message (simulate WhatsApp message send)
@@ -337,6 +344,59 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
337
  # If property suggestions were sent, return the first as the AI response (rest will be sent as follow-ups)
338
  return property_msgs[0] if property_msgs else "Let me know if you'd like to see images or more details!"
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  # Process with AI
341
  result = await chat_graph.ainvoke({
342
  "user_message": user_message,
 
12
  get_persona_summary,
13
  extract_persona_from_message
14
  )
15
+ from config import supabase
16
+ from datetime import datetime
17
  import re
18
 
19
  def chat_with_session_memory(state):
 
326
  conversation_context = " ".join([msg["content"] for msg in session_messages[-5:]]) + " " + user_message
327
  should_ask, field_to_ask = await should_ask_persona_question(updated_persona, conversation_context)
328
 
329
+ # Check for property search intent - ONLY if user explicitly asks for properties
330
  property_search_phrases = [
331
+ "show me properties", "property in", "warehouse in", "industrial in", "listings in", "toon my eiendomme", "warehouse", "factory", "show me warehouses", "show me listings", "show me", "show"
332
  ]
333
  is_property_search = any(phrase in user_message.lower() for phrase in property_search_phrases)
334
+
335
+ # Only trigger property search if user explicitly asks for properties, NOT for greetings
336
+ greeting_phrases = ["hi", "hello", "hey", "good morning", "good afternoon", "good evening", "morning", "afternoon", "evening"]
337
+ is_greeting = any(phrase in user_message.lower() for phrase in greeting_phrases)
338
+
339
+ if is_property_search and not is_greeting:
340
  property_msgs = await handle_property_search(user_message, updated_persona)
341
  for msg in property_msgs:
342
  # Send each property message (simulate WhatsApp message send)
 
344
  # If property suggestions were sent, return the first as the AI response (rest will be sent as follow-ups)
345
  return property_msgs[0] if property_msgs else "Let me know if you'd like to see images or more details!"
346
 
347
+ # Handle greetings with persona context
348
+ if is_greeting and updated_persona.get("location_preference"):
349
+ # User has a persona, ask if they're still looking
350
+ persona_summary = get_persona_summary(updated_persona)
351
+ greeting_msg = f"Hi {user_info.get('name', 'there')}! 👋\n\nI see you were previously looking for properties in {updated_persona['location_preference'].title()}"
352
+
353
+ if updated_persona.get("size_preference_sqm"):
354
+ greeting_msg += f" around {updated_persona['size_preference_sqm']} sqm"
355
+
356
+ if updated_persona.get("budget"):
357
+ greeting_msg += f" with a budget of R{int(updated_persona['budget']):,} per month"
358
+
359
+ greeting_msg += ".\n\nAre you still looking for properties with these requirements, or would you like to start fresh?"
360
+
361
+ return greeting_msg
362
+
363
+ # Handle responses to the greeting question
364
+ if session_messages and session_messages[-1]["role"] == "assistant":
365
+ last_ai_message = session_messages[-1]["content"]
366
+ if "Are you still looking for properties with these requirements" in last_ai_message:
367
+ user_response_lower = user_message.lower()
368
+
369
+ # Check for affirmative responses
370
+ affirmative_words = ["yes", "yeah", "yep", "sure", "ok", "okay", "correct", "right", "that's right", "exactly", "still looking", "still searching"]
371
+ negative_words = ["no", "nope", "not", "start fresh", "new search", "different", "change", "reset"]
372
+
373
+ if any(word in user_response_lower for word in affirmative_words):
374
+ # User wants to continue with current persona, search for properties
375
+ property_msgs = await handle_property_search(user_message, updated_persona)
376
+ for msg in property_msgs:
377
+ print(f"Property suggestion: {msg}")
378
+ return property_msgs[0] if property_msgs else "Let me know if you'd like to see images or more details!"
379
+
380
+ elif any(word in user_response_lower for word in negative_words):
381
+ # User wants to start fresh, reset persona
382
+ if wa_id:
383
+ # Reset persona fields
384
+ reset_data = {
385
+ "location_preference": None,
386
+ "size_preference_sqm": None,
387
+ "budget": None,
388
+ "must_have": None,
389
+ "intent": None,
390
+ "updated_at": datetime.utcnow().isoformat()
391
+ }
392
+ try:
393
+ supabase.table("user_personas").update(reset_data).eq("wa_id", wa_id).execute()
394
+ print(f"Reset persona for user {wa_id}")
395
+ except Exception as e:
396
+ print(f"Error resetting persona: {e}")
397
+
398
+ return f"Perfect! Let's start fresh. Hi {user_info.get('name', 'there')}! 👋\n\nHow can I help you find the perfect industrial property today?"
399
+
400
  # Process with AI
401
  result = await chat_graph.ainvoke({
402
  "user_message": user_message,