zenaight commited on
Commit
fcb25fc
·
1 Parent(s): b77c962

Enhance AI chat functionality with property details retrieval

Browse files

- Added logic in `chat_with_session_memory` to respond to user queries about images, features, floorplans, and video tours based on the provided properties.
- Implemented fallback responses for each property detail type to ensure clear communication when specific information is unavailable.
- Updated the system message to inform users about available options for additional property details, improving user interaction and experience.

Files changed (1) hide show
  1. ai_chat.py +45 -0
ai_chat.py CHANGED
@@ -12,6 +12,31 @@ def chat_with_session_memory(state):
12
  wa_id = state.get("wa_id")
13
  wamid = state.get("wamid")
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Get conversation history from database
16
  session_messages = []
17
  if session_id:
@@ -56,6 +81,26 @@ def chat_with_session_memory(state):
56
  f"- {title} in {loc}, {city}: {size} sqm, "
57
  f"{price} ({ptype}); features: {features}\n"
58
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  # Build messages array with history
61
  messages = [{"role": "system", "content": system_message}]
 
12
  wa_id = state.get("wa_id")
13
  wamid = state.get("wamid")
14
 
15
+ msg_lower = state["user_message"].lower()
16
+ props = state.get("properties", [])
17
+ if props:
18
+ first = props[0]
19
+ # a) Images
20
+ if "image" in msg_lower or "images" in msg_lower:
21
+ imgs = first.get("images") or []
22
+ response = "\n".join(imgs) if imgs else "There aren't any images for that listing."
23
+ return {"response": response}
24
+ # b) Features
25
+ if "feature" in msg_lower:
26
+ feats = first.get("features") or []
27
+ response = "Features:\n" + "\n".join(f"• {f}" for f in feats) if feats else "No feature list available."
28
+ return {"response": response}
29
+ # c) Floorplan
30
+ if "floorplan" in msg_lower:
31
+ fp = first.get("floorplan_pdf")
32
+ response = fp if fp else "There isn't a floorplan PDF for that listing."
33
+ return {"response": response}
34
+ # d) Video
35
+ if "video" in msg_lower or "tour" in msg_lower:
36
+ vid = first.get("video_url")
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:
 
81
  f"- {title} in {loc}, {city}: {size} sqm, "
82
  f"{price} ({ptype}); features: {features}\n"
83
  )
84
+ url = p.get("listing_url")
85
+ if url:
86
+ system_message += f" Link: {url}\n"
87
+
88
+ # After the listings loop, before building messages[]
89
+ extras = set()
90
+ for p in props[:5]:
91
+ if p.get("images"):
92
+ extras.add("images")
93
+ if p.get("features"):
94
+ extras.add("feature list")
95
+ if p.get("floorplan_pdf"):
96
+ extras.add("floorplan PDF")
97
+ if p.get("video_url"):
98
+ extras.add("video tour")
99
+ if extras:
100
+ opts = ", ".join(sorted(extras))
101
+ system_message += (
102
+ f"\nI can also share {opts} for any listing—just let me know what you'd like."
103
+ )
104
 
105
  # Build messages array with history
106
  messages = [{"role": "system", "content": system_message}]