zenaight commited on
Commit
b418dc7
·
1 Parent(s): a35f267

Enhance image request handling and intent classification in AI chat

Browse files

- Updated the `chat_with_session_memory` function to clarify the inclusion of property data without image URLs, improving user understanding of available information.
- Modified the `extract_and_update_intent` function to recognize requests for images, allowing the system to handle both property and image inquiries more effectively.
- Adjusted the `process_message` function to check for image requests based on user input, ensuring appropriate responses are generated when users ask for images or photos.
- These changes aim to improve user interaction by providing clearer communication regarding available listings and enhancing the system's ability to respond to image requests.

Files changed (2) hide show
  1. ai_chat.py +12 -10
  2. main.py +6 -1
ai_chat.py CHANGED
@@ -50,7 +50,7 @@ def chat_with_session_memory(state):
50
  if search_status:
51
  system_message += f"\n\n{search_status}"
52
 
53
- # Include property data if available
54
  if props:
55
  system_message += "\n\nAvailable listings:\n"
56
  for p in props[:5]:
@@ -58,7 +58,7 @@ def chat_with_session_memory(state):
58
  f"- {p.get('title')} in {p.get('location')}, {p.get('city')}: "
59
  f"{p.get('size_sqm')} sqm, {p.get('price')} ({p.get('price_type')})\n"
60
  )
61
- # Include all available data for the LLM to use (but not image URLs in listings)
62
  if p.get("listing_url"):
63
  system_message += f" URL: {p.get('listing_url')}\n"
64
  if p.get("features"):
@@ -67,7 +67,7 @@ def chat_with_session_memory(state):
67
  system_message += f" Floorplan: {p.get('floorplan_pdf')}\n"
68
  if p.get("video_url"):
69
  system_message += f" Video: {p.get('video_url')}\n"
70
- # Note: Images are available but not shown in listings - users must ask for them
71
 
72
  system_message += "\n\nIMPORTANT: You may only reference listings passed in state['properties']. If the user requests more detail, respond with whatever is in that listing dict (URL, images, features, etc.). If information is not available, respond with 'For this listing, I don't have [specific detail] available right now'. When users ask for images, photos, or pictures, let them know that images are available and will be sent separately."
73
 
@@ -269,12 +269,12 @@ async def extract_and_update_intent(state):
269
  missing = [f for f in intent_fields if state["intent"].get(f) is None]
270
  print(f"DEBUG - Missing intent fields: {missing}")
271
  if missing:
272
- # Check if user is asking for properties (using AI classification)
273
  classification = state.get("classification")
274
  print(f"DEBUG - Intent update classification check: '{classification}'")
275
- if classification == "search_listings":
276
- # User is asking for properties, don't interrupt with preference questions
277
- print("DEBUG - User asking for properties, skipping preference questions")
278
  return {"response": None}
279
 
280
  # User is setting preferences, ask for missing fields
@@ -300,7 +300,8 @@ async def classify_user_intent(state):
300
  prompt = f"""
301
  Classify the user's message into exactly one of:
302
  - search_listings (user wants to see property listings)
303
- - request_details (user wants more info on a shown listing)
 
304
  - other (anything else)
305
  Return only the tag.
306
  Message: {user_message}
@@ -319,7 +320,7 @@ async def extract_and_search_properties(state):
319
  # Only search when the LLM tagged this as a listings request
320
  classification = state.get("classification")
321
  print(f"DEBUG - Property search classification check: '{classification}'")
322
- if classification != "search_listings":
323
  print(f"DEBUG - Skipping property search, classification is '{classification}'")
324
  return {"response": None}
325
 
@@ -426,7 +427,8 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
426
 
427
  return {
428
  "response": result["response"],
429
- "properties": result.get("properties", [])
 
430
  }
431
 
432
  async def handle_image_request(state):
 
50
  if search_status:
51
  system_message += f"\n\n{search_status}"
52
 
53
+ # Include property data if available (without image URLs)
54
  if props:
55
  system_message += "\n\nAvailable listings:\n"
56
  for p in props[:5]:
 
58
  f"- {p.get('title')} in {p.get('location')}, {p.get('city')}: "
59
  f"{p.get('size_sqm')} sqm, {p.get('price')} ({p.get('price_type')})\n"
60
  )
61
+ # Include available data but NOT image URLs
62
  if p.get("listing_url"):
63
  system_message += f" URL: {p.get('listing_url')}\n"
64
  if p.get("features"):
 
67
  system_message += f" Floorplan: {p.get('floorplan_pdf')}\n"
68
  if p.get("video_url"):
69
  system_message += f" Video: {p.get('video_url')}\n"
70
+ # Images are available but not shown - users must ask for them specifically
71
 
72
  system_message += "\n\nIMPORTANT: You may only reference listings passed in state['properties']. If the user requests more detail, respond with whatever is in that listing dict (URL, images, features, etc.). If information is not available, respond with 'For this listing, I don't have [specific detail] available right now'. When users ask for images, photos, or pictures, let them know that images are available and will be sent separately."
73
 
 
269
  missing = [f for f in intent_fields if state["intent"].get(f) is None]
270
  print(f"DEBUG - Missing intent fields: {missing}")
271
  if missing:
272
+ # Check if user is asking for properties or images (using AI classification)
273
  classification = state.get("classification")
274
  print(f"DEBUG - Intent update classification check: '{classification}'")
275
+ if classification in ["search_listings", "request_images"]:
276
+ # User is asking for properties or images, don't interrupt with preference questions
277
+ print(f"DEBUG - User asking for {classification}, skipping preference questions")
278
  return {"response": None}
279
 
280
  # User is setting preferences, ask for missing fields
 
300
  prompt = f"""
301
  Classify the user's message into exactly one of:
302
  - search_listings (user wants to see property listings)
303
+ - request_images (user wants to see images/photos/pictures of a listing)
304
+ - request_details (user wants other info like features, floorplan, video)
305
  - other (anything else)
306
  Return only the tag.
307
  Message: {user_message}
 
320
  # Only search when the LLM tagged this as a listings request
321
  classification = state.get("classification")
322
  print(f"DEBUG - Property search classification check: '{classification}'")
323
+ if classification not in ["search_listings", "request_images"]:
324
  print(f"DEBUG - Skipping property search, classification is '{classification}'")
325
  return {"response": None}
326
 
 
427
 
428
  return {
429
  "response": result["response"],
430
+ "properties": result.get("properties", []),
431
+ "classification": result.get("classification", "")
432
  }
433
 
434
  async def handle_image_request(state):
main.py CHANGED
@@ -72,7 +72,12 @@ async def receive_message(req: Request):
72
  "properties": properties
73
  }
74
 
75
- image_messages = await handle_image_request(state)
 
 
 
 
 
76
 
77
  if image_messages:
78
  # Send images
 
72
  "properties": properties
73
  }
74
 
75
+ # Check if this is an image request
76
+ classification = ai_result.get("classification", "")
77
+ image_messages = None
78
+
79
+ if classification == "request_images" or "image" in user_message.lower() or "photo" in user_message.lower():
80
+ image_messages = await handle_image_request(state)
81
 
82
  if image_messages:
83
  # Send images