zenaight commited on
Commit ·
aca24f6
1
Parent(s): a204bcd
Enhance intent field handling in AI chat
Browse files- Updated the `extract_and_update_intent` function to handle the "must_have" field as an array, converting string inputs into an appropriate array format for database storage.
- Improved logging to provide clarity on the conversion process and intent updates, ensuring better traceability during intent management.
- Refactored the `send_whatsapp_message` function to utilize an asynchronous HTTP client for sending messages, enhancing performance and reliability in message delivery.
- ai_chat.py +19 -2
- whatsapp.py +4 -3
ai_chat.py
CHANGED
|
@@ -194,8 +194,25 @@ async def extract_and_update_intent(state):
|
|
| 194 |
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
elif new_val is not None:
|
| 200 |
print(f"Intent field {field} unchanged: {new_val}")
|
| 201 |
else:
|
|
|
|
| 194 |
|
| 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:
|
| 206 |
+
must_have_array = [new_val.strip()]
|
| 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:
|
| 214 |
+
await update_user_intent(session_id, {field: new_val})
|
| 215 |
+
intent[field] = new_val
|
| 216 |
elif new_val is not None:
|
| 217 |
print(f"Intent field {field} unchanged: {new_val}")
|
| 218 |
else:
|
whatsapp.py
CHANGED
|
@@ -15,9 +15,10 @@ async def send_whatsapp_message(wa_id: str, message: str):
|
|
| 15 |
"text": {"body": message}
|
| 16 |
}
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
print(f"Failed to send message: {e}")
|
| 23 |
return False
|
|
|
|
| 15 |
"text": {"body": message}
|
| 16 |
}
|
| 17 |
try:
|
| 18 |
+
async with httpx.AsyncClient() as client:
|
| 19 |
+
resp = await client.post(url, headers=headers, json=payload)
|
| 20 |
+
print(f"Sent message → {resp.status_code}: {resp.text}")
|
| 21 |
+
return resp.status_code == 200
|
| 22 |
except Exception as e:
|
| 23 |
print(f"Failed to send message: {e}")
|
| 24 |
return False
|