zenaight commited on
Commit ·
c62a5af
1
Parent(s): f692b71
Enhance property request handling and conversation context in AI chat
Browse files- Updated the `extract_and_search_properties` function to utilize existing properties in the state for specific classification requests, improving efficiency in handling user inquiries.
- Enhanced the `handle_image_request` function to analyze recent conversation history for property mentions, allowing for more contextually relevant property selections when multiple options are available.
- Added debug statements to track property mentions and selections, improving traceability and user experience during property inquiries.
- These changes aim to streamline user interactions by leveraging existing data and conversation context, ensuring more accurate and responsive AI behavior.
- ai_chat.py +50 -1
- database.py +2 -2
ai_chat.py
CHANGED
|
@@ -371,6 +371,17 @@ 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 classification matches our search categories
|
| 375 |
is_search_request = (
|
| 376 |
classification == "search_listings" or
|
|
@@ -575,7 +586,45 @@ async def handle_image_request(state):
|
|
| 575 |
best_match_score = score
|
| 576 |
selected_property = prop
|
| 577 |
|
| 578 |
-
# Fallback:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
if not selected_property:
|
| 580 |
selected_property = props[0]
|
| 581 |
|
|
|
|
| 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 |
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
|
| 592 |
+
session_messages = state.get("session_messages", [])
|
| 593 |
+
print(f"DEBUG - Checking {len(session_messages)} session messages for property context")
|
| 594 |
+
|
| 595 |
+
# Look for property mentions in recent conversation
|
| 596 |
+
recent_messages = session_messages[-10:] # Last 10 messages
|
| 597 |
+
property_mentions = {}
|
| 598 |
+
|
| 599 |
+
for msg in recent_messages:
|
| 600 |
+
if msg.get("role") == "assistant":
|
| 601 |
+
content = msg.get("content", "").lower()
|
| 602 |
+
for i, prop in enumerate(props):
|
| 603 |
+
title = prop.get("title", "").lower()
|
| 604 |
+
location = prop.get("location", "").lower()
|
| 605 |
+
# Check if this property was mentioned in AI response
|
| 606 |
+
title_words = title.split()
|
| 607 |
+
if len(title_words) >= 2: # Use first 2 words for matching
|
| 608 |
+
key_phrase = " ".join(title_words[:2])
|
| 609 |
+
if key_phrase in content or location in content:
|
| 610 |
+
property_mentions[i] = property_mentions.get(i, 0) + 1
|
| 611 |
+
print(f"DEBUG - Found mention of property {i}: {title}")
|
| 612 |
+
|
| 613 |
+
# Use most mentioned property from conversation
|
| 614 |
+
if property_mentions:
|
| 615 |
+
most_mentioned = max(property_mentions.items(), key=lambda x: x[1])
|
| 616 |
+
selected_property = props[most_mentioned[0]]
|
| 617 |
+
print(f"DEBUG - Selected property from conversation context: {selected_property.get('title')}")
|
| 618 |
+
else:
|
| 619 |
+
# Multiple properties available - ask user to specify
|
| 620 |
+
prop_options = []
|
| 621 |
+
for i, prop in enumerate(props[:3], 1): # Show first 3 options
|
| 622 |
+
prop_options.append(f"Option {i}: {prop.get('title')}")
|
| 623 |
+
|
| 624 |
+
options_text = "\n".join(prop_options)
|
| 625 |
+
return [f"I have multiple properties available. Which one would you like to see images of?\n\n{options_text}\n\nPlease let me know which option you'd like images for."]
|
| 626 |
+
|
| 627 |
+
# Final fallback: use first property if only one or no context found
|
| 628 |
if not selected_property:
|
| 629 |
selected_property = props[0]
|
| 630 |
|
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
|
| 333 |
-
query = query.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 - 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()
|