zenaight commited on
Commit
383dff9
·
1 Parent(s): 42bd84b

Refactor property listing summary in chat_with_session_memory

Browse files

- Introduced a direct summary response for property listings, bypassing the LLM when properties are available, to enhance user experience.
- Consolidated the logic for building the response text, including search status and property details, improving readability and maintainability.
- Removed redundant code related to listing details in the system message, ensuring clarity in the response structure.
- Updated instructions for handling unavailable property details to improve user communication.

Files changed (1) hide show
  1. ai_chat.py +32 -40
ai_chat.py CHANGED
@@ -37,6 +37,37 @@ def chat_with_session_memory(state):
37
  response = vid if vid else "There isn't a video tour for that listing."
38
  return {"response": response}
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Get conversation history from database
41
  session_messages = []
42
  if session_id:
@@ -66,46 +97,7 @@ def chat_with_session_memory(state):
66
  f"and must-haves: {', '.join(must_have_list) if must_have_list else '[none]'}. "
67
  )
68
 
69
- props = state.get("properties", [])
70
- search_status = state.get("search_status_message", "")
71
-
72
- if search_status:
73
- system_message += f"\n{search_status}\n"
74
-
75
- if props:
76
- system_message += "\nHere are some listings I found:\n"
77
- for p in props[:5]:
78
- title = p.get("title", "[no title]")
79
- loc = p.get("location", "")
80
- city = p.get("city", "")
81
- size = p.get("size_sqm", "")
82
- price = p.get("price", "")
83
- ptype = p.get("price_type", "")
84
- features = ", ".join(p.get("features", [])) or "[no features]"
85
- system_message += (
86
- f"- {title} in {loc}, {city}: {size} sqm, "
87
- f"{price} ({ptype}); features: {features}\n"
88
- )
89
- url = p.get("listing_url")
90
- if url:
91
- system_message += f" Link: {url}\n"
92
-
93
- # After the listings loop, before building messages[]
94
- extras = set()
95
- for p in props[:5]:
96
- if p.get("images"):
97
- extras.add("images")
98
- if p.get("features"):
99
- extras.add("feature list")
100
- if p.get("floorplan_pdf"):
101
- extras.add("floorplan PDF")
102
- if p.get("video_url"):
103
- extras.add("video tour")
104
- if extras:
105
- opts = ", ".join(sorted(extras))
106
- system_message += (
107
- f"\nI can also share {opts} for any listing—just let me know what you'd like."
108
- )
109
 
110
  system_message += "\n\nIMPORTANT: When users ask about specific details for a listing, check if that information is available in the property data. If not available, respond with 'For this listing, I don't have [specific detail] available right now' instead of saying you can't provide URLs or external information."
111
 
 
37
  response = vid if vid else "There isn't a video tour for that listing."
38
  return {"response": response}
39
 
40
+ # Direct listing summary (bypass LLM when properties are found)
41
+ props = state.get("properties", [])
42
+ if props:
43
+ # a) Build a direct summary string
44
+ response_text = ""
45
+ # include any search status
46
+ status = state.get("search_status_message")
47
+ if status:
48
+ response_text += status + "\n\n"
49
+ response_text += "Here are some listings I found:\n"
50
+ for p in props[:5]:
51
+ response_text += (
52
+ f"- {p.get('title')} in {p.get('location')}, {p.get('city')}: "
53
+ f"{p.get('size_sqm')} sqm, {p.get('price')} ({p.get('price_type')})\n"
54
+ )
55
+ url = p.get("listing_url")
56
+ if url:
57
+ response_text += f" Link: {url}\n"
58
+ # b) Suggest extras based on available fields
59
+ extras = []
60
+ if any(p.get("images") for p in props): extras.append("images")
61
+ if any(p.get("features") for p in props): extras.append("feature list")
62
+ if any(p.get("floorplan_pdf") for p in props): extras.append("floorplan PDF")
63
+ if any(p.get("video_url") for p in props): extras.append("video tour")
64
+ if extras:
65
+ response_text += (
66
+ "\nI can also share " + ", ".join(extras) +
67
+ " for any listing—just let me know what you'd like."
68
+ )
69
+ return {"response": response_text}
70
+
71
  # Get conversation history from database
72
  session_messages = []
73
  if session_id:
 
97
  f"and must-haves: {', '.join(must_have_list) if must_have_list else '[none]'}. "
98
  )
99
 
100
+ # Note: Listing details are now handled by direct summary above, not included in system_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  system_message += "\n\nIMPORTANT: When users ask about specific details for a listing, check if that information is available in the property data. If not available, respond with 'For this listing, I don't have [specific detail] available right now' instead of saying you can't provide URLs or external information."
103