zenaight commited on
Commit
b7f1d5f
·
1 Parent(s): c62a5af

Refactor property selection logic and enhance conversation context handling

Browse files

- Removed the previous check for existing properties in the `extract_and_search_properties` function to streamline property request handling.
- Introduced a new mechanism in the `handle_image_request` function to analyze recent user messages for specific property selections, improving the AI's ability to respond to user preferences.
- Added debug statements to track user-selected options and property mentions, enhancing traceability during property inquiries.
- These changes aim to improve the responsiveness and accuracy of property selections based on user interactions, ultimately enhancing the overall user experience.

Files changed (2) hide show
  1. ai_chat.py +34 -11
  2. database.py +2 -2
ai_chat.py CHANGED
@@ -371,17 +371,6 @@ async def extract_and_search_properties(state):
371
  classification = state.get("classification")
372
  print(f"DEBUG - Property search classification check: '{classification}'")
373
 
374
- # Check if this is a detail request for existing properties in state
375
- existing_properties = state.get("properties", [])
376
- if (classification.startswith("request_images") or
377
- classification == "request_address" or
378
- classification == "request_details") and existing_properties:
379
- print(f"DEBUG - Using existing properties from state, count: {len(existing_properties)}")
380
- return {
381
- "properties": existing_properties,
382
- "classification": classification
383
- }
384
-
385
  # Check if classification matches our search categories
386
  is_search_request = (
387
  classification == "search_listings" or
@@ -586,6 +575,40 @@ async def handle_image_request(state):
586
  best_match_score = score
587
  selected_property = prop
588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589
  # Fallback: Use conversation context to find which property user was discussing
590
  if not selected_property and len(props) > 1:
591
  # Check conversation history for property context
 
371
  classification = state.get("classification")
372
  print(f"DEBUG - Property search classification check: '{classification}'")
373
 
 
 
 
 
 
 
 
 
 
 
 
374
  # Check if classification matches our search categories
375
  is_search_request = (
376
  classification == "search_listings" or
 
575
  best_match_score = score
576
  selected_property = prop
577
 
578
+ # Look for specific property selections in conversation
579
+ session_messages = state.get("session_messages", [])
580
+ recent_messages = session_messages[-20:] # Look at more messages
581
+
582
+ # Look for patterns like "option 3", "the warehouse", "this property"
583
+ selected_property_context = None
584
+
585
+ for msg in recent_messages:
586
+ if msg.get("role") == "user":
587
+ content = msg.get("content", "").lower()
588
+ # Look for option selections
589
+ if "option" in content:
590
+ import re
591
+ option_match = re.search(r'option\s+(\d+)', content)
592
+ if option_match:
593
+ option_num = int(option_match.group(1))
594
+ if 1 <= option_num <= len(props):
595
+ selected_property_context = props[option_num - 1]
596
+ print(f"DEBUG - Found user selected option {option_num}: {selected_property_context.get('title')}")
597
+ break
598
+
599
+ # Look for property type mentions that user specifically asked about
600
+ for i, prop in enumerate(props):
601
+ title_words = prop.get("title", "").lower().split()
602
+ for word in ["warehouse", "office", "space", "unit"]:
603
+ if word in content and word in title_words:
604
+ selected_property_context = prop
605
+ print(f"DEBUG - Found user interest in {word}: {prop.get('title')}")
606
+ break
607
+
608
+ # Use the property the user specifically selected/discussed
609
+ if selected_property_context:
610
+ selected_property = selected_property_context
611
+
612
  # Fallback: Use conversation context to find which property user was discussing
613
  if not selected_property and len(props) > 1:
614
  # Check conversation history for property context
database.py CHANGED
@@ -329,8 +329,8 @@ async def search_properties(filters: dict) -> list:
329
  # Note: Must-have features are removed from database search
330
  # They will be handled by the LLM in the chat response
331
 
332
- # e. Order & limit - prioritize recent activity, then featured, then price
333
- query = query.order("updated_at", desc=True).order("is_featured", desc=True).order("price").limit(5)
334
 
335
  # f. Execute and return
336
  resp = query.execute()
 
329
  # Note: Must-have features are removed from database search
330
  # They will be handled by the LLM in the chat response
331
 
332
+ # e. Order & limit
333
+ query = query.order("is_featured", desc=True).order("price").limit(5)
334
 
335
  # f. Execute and return
336
  resp = query.execute()