zenaight commited on
Commit
ba64859
·
1 Parent(s): aca24f6

Enhance intent extraction logic in AI chat

Browse files

- Updated the `extract_and_update_intent` function to provide clearer instructions for handling the "must_have" field, specifying how to manage additions, changes, and clarifications of requirements.
- Improved logging to reflect the LLM's decision-making process regarding the "must_have" field, enhancing traceability during intent updates.
- Added detailed context for the current intent state, improving the clarity of intent extraction and user interaction.

Files changed (1) hide show
  1. ai_chat.py +17 -7
ai_chat.py CHANGED
@@ -147,9 +147,20 @@ async def extract_and_update_intent(state):
147
  Extract and normalize the user's current property search intent from this message:
148
  {user_message}
149
 
150
- Normalize abbreviations (e.g., 'sqm' → 'square metres') and correct any spelling mistakes.
151
- If the user is asking a definition or clarification (e.g. 'What does square metre mean?'), answer that question fully and do not update the intent. After answering, wait for the next user message to extract values.
152
- Return only a JSON object with keys {intent_fields}, using null for unknown.
 
 
 
 
 
 
 
 
 
 
 
153
  """
154
 
155
  response = await llm.ainvoke([{"role":"user","content":extraction_prompt}])
@@ -195,11 +206,10 @@ async def extract_and_update_intent(state):
195
  if new_val is not None and new_val != old_val:
196
  print(f"Updating intent field {field}: {old_val} -> {new_val}")
197
 
198
- # Handle must_have field as array
199
  if field == "must_have" and new_val:
200
- # Convert string to array format for database
201
  if isinstance(new_val, str):
202
- # Split by comma if multiple items, otherwise single item
203
  if "," in new_val:
204
  must_have_array = [item.strip() for item in new_val.split(",")]
205
  else:
@@ -207,7 +217,7 @@ async def extract_and_update_intent(state):
207
  else:
208
  must_have_array = new_val if isinstance(new_val, list) else [str(new_val)]
209
 
210
- print(f"Converting must_have to array: {must_have_array}")
211
  await update_user_intent(session_id, {field: must_have_array})
212
  intent[field] = must_have_array
213
  else:
 
147
  Extract and normalize the user's current property search intent from this message:
148
  {user_message}
149
 
150
+ Current intent state:
151
+ - Location: {intent.get('location_preference', 'Not set')}
152
+ - Budget: {intent.get('budget', 'Not set')}
153
+ - Size: {intent.get('size_preference_sqm', 'Not set')} sqm
154
+ - Must-haves: {intent.get('must_have', [])}
155
+
156
+ Instructions:
157
+ 1. Normalize abbreviations (e.g., 'sqm' → 'square metres') and correct any spelling mistakes.
158
+ 2. For must_have field: Determine if the user is ADDING new requirements, CHANGING their mind, or CLARIFYING existing ones.
159
+ - If adding: Include both existing and new items in the array
160
+ - If changing: Replace with new requirements
161
+ - If clarifying: Update with more specific versions
162
+ 3. If the user is asking a definition or clarification (e.g. 'What does square metre mean?'), answer that question fully and do not update the intent.
163
+ 4. Return only a JSON object with keys {intent_fields}, using null for unknown.
164
  """
165
 
166
  response = await llm.ainvoke([{"role":"user","content":extraction_prompt}])
 
206
  if new_val is not None and new_val != old_val:
207
  print(f"Updating intent field {field}: {old_val} -> {new_val}")
208
 
209
+ # Handle must_have field as array (LLM decides the logic)
210
  if field == "must_have" and new_val:
211
+ # Convert to array format for database
212
  if isinstance(new_val, str):
 
213
  if "," in new_val:
214
  must_have_array = [item.strip() for item in new_val.split(",")]
215
  else:
 
217
  else:
218
  must_have_array = new_val if isinstance(new_val, list) else [str(new_val)]
219
 
220
+ print(f"LLM decided must_have: {must_have_array}")
221
  await update_user_intent(session_id, {field: must_have_array})
222
  intent[field] = must_have_array
223
  else: