zenaight commited on
Commit ·
0d86ec7
1
Parent(s): 2860609
Enhance intent extraction and debugging in AI chat
Browse files- Updated the normalization instructions for abbreviations and common terms to improve clarity and user understanding.
- Added debug print statements to log intent extraction results, errors, and classification checks, enhancing traceability during the intent processing.
- Refined the property search logic to utilize the extracted classification more effectively, ensuring that searches are only conducted when appropriate.
- ai_chat.py +15 -3
ai_chat.py
CHANGED
|
@@ -187,7 +187,11 @@ async def extract_and_update_intent(state):
|
|
| 187 |
- Must-haves: {intent.get('must_have', [])}
|
| 188 |
|
| 189 |
Instructions:
|
| 190 |
-
1. Normalize abbreviations
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
2. For must_have field: Determine if the user is ADDING new requirements, CHANGING their mind, or CLARIFYING existing ones.
|
| 192 |
- If adding: Include both existing and new items in the array
|
| 193 |
- If changing: Replace with new requirements
|
|
@@ -215,8 +219,10 @@ async def extract_and_update_intent(state):
|
|
| 215 |
return state
|
| 216 |
|
| 217 |
extracted = json.loads(content)
|
|
|
|
| 218 |
except Exception as e:
|
| 219 |
extracted = {}
|
|
|
|
| 220 |
|
| 221 |
# Check if this is a general area search (like "what do you have in [area]")
|
| 222 |
is_general_area_search = (
|
|
@@ -255,6 +261,7 @@ async def extract_and_update_intent(state):
|
|
| 255 |
intent[field] = new_val
|
| 256 |
|
| 257 |
state["intent"] = intent
|
|
|
|
| 258 |
|
| 259 |
missing = [f for f in intent_fields if state["intent"].get(f) is None]
|
| 260 |
if missing:
|
|
@@ -283,7 +290,9 @@ Return only the tag.
|
|
| 283 |
Message: {user_message}
|
| 284 |
"""
|
| 285 |
resp = await llm.ainvoke([{"role":"user","content":prompt}])
|
| 286 |
-
|
|
|
|
|
|
|
| 287 |
return {"response": None}
|
| 288 |
|
| 289 |
async def extract_and_search_properties(state):
|
|
@@ -291,7 +300,10 @@ async def extract_and_search_properties(state):
|
|
| 291 |
Search for properties based on user intent and store results in state.
|
| 292 |
"""
|
| 293 |
# Only search when the LLM tagged this as a listings request
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
| 295 |
return {"response": None}
|
| 296 |
|
| 297 |
intent = state.get("intent", {})
|
|
|
|
| 187 |
- Must-haves: {intent.get('must_have', [])}
|
| 188 |
|
| 189 |
Instructions:
|
| 190 |
+
1. Normalize abbreviations and common terms:
|
| 191 |
+
- 'JHB' or 'Jhb' → 'Johannesburg'
|
| 192 |
+
- 'CT' or 'Cape Town' → 'Cape Town'
|
| 193 |
+
- 'DBN' or 'Durban' → 'Durban'
|
| 194 |
+
- 'sqm' → 'square metres'
|
| 195 |
2. For must_have field: Determine if the user is ADDING new requirements, CHANGING their mind, or CLARIFYING existing ones.
|
| 196 |
- If adding: Include both existing and new items in the array
|
| 197 |
- If changing: Replace with new requirements
|
|
|
|
| 219 |
return state
|
| 220 |
|
| 221 |
extracted = json.loads(content)
|
| 222 |
+
print(f"DEBUG - Intent extraction result: {extracted}")
|
| 223 |
except Exception as e:
|
| 224 |
extracted = {}
|
| 225 |
+
print(f"DEBUG - Intent extraction error: {e}")
|
| 226 |
|
| 227 |
# Check if this is a general area search (like "what do you have in [area]")
|
| 228 |
is_general_area_search = (
|
|
|
|
| 261 |
intent[field] = new_val
|
| 262 |
|
| 263 |
state["intent"] = intent
|
| 264 |
+
print(f"DEBUG - Final intent state: {state['intent']}")
|
| 265 |
|
| 266 |
missing = [f for f in intent_fields if state["intent"].get(f) is None]
|
| 267 |
if missing:
|
|
|
|
| 290 |
Message: {user_message}
|
| 291 |
"""
|
| 292 |
resp = await llm.ainvoke([{"role":"user","content":prompt}])
|
| 293 |
+
classification = resp.content.strip()
|
| 294 |
+
state["classification"] = classification
|
| 295 |
+
print(f"DEBUG - Classification result: '{classification}' for message: '{user_message}'")
|
| 296 |
return {"response": None}
|
| 297 |
|
| 298 |
async def extract_and_search_properties(state):
|
|
|
|
| 300 |
Search for properties based on user intent and store results in state.
|
| 301 |
"""
|
| 302 |
# Only search when the LLM tagged this as a listings request
|
| 303 |
+
classification = state.get("classification")
|
| 304 |
+
print(f"DEBUG - Property search classification check: '{classification}'")
|
| 305 |
+
if classification != "search_listings":
|
| 306 |
+
print(f"DEBUG - Skipping property search, classification is '{classification}'")
|
| 307 |
return {"response": None}
|
| 308 |
|
| 309 |
intent = state.get("intent", {})
|