Update utility.py
Browse files- utility.py +27 -2
utility.py
CHANGED
|
@@ -1185,12 +1185,35 @@ def read_datalake(user_phone: str, query: str) -> str:
|
|
| 1185 |
|
| 1186 |
response = lake.chat(pandasai_prompt)
|
| 1187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1188 |
if isinstance(response, dict) and 'value' in response:
|
| 1189 |
-
|
|
|
|
|
|
|
|
|
|
| 1190 |
elif isinstance(response, str):
|
|
|
|
|
|
|
| 1191 |
return response
|
| 1192 |
else:
|
| 1193 |
-
|
|
|
|
|
|
|
|
|
|
| 1194 |
|
| 1195 |
except Exception as e:
|
| 1196 |
# Unified handler: catch PandasAI edge-cases by CLASS NAME or MESSAGE,
|
|
@@ -1202,6 +1225,7 @@ def read_datalake(user_phone: str, query: str) -> str:
|
|
| 1202 |
or "No code found" in err_msg
|
| 1203 |
or "NoCodeFoundError" in err_name
|
| 1204 |
or "MaliciousQueryError" in err_name
|
|
|
|
| 1205 |
)
|
| 1206 |
|
| 1207 |
if is_pandasai_no_code:
|
|
@@ -1293,6 +1317,7 @@ def read_datalake(user_phone: str, query: str) -> str:
|
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
|
|
|
| 1296 |
def _find_document_by_details(user_phone: str, collection_name: str, details: Dict) -> Optional[Any]:
|
| 1297 |
col_ref = db.collection("users").document(user_phone).collection(collection_name)
|
| 1298 |
if 'transaction_id' in details and details['transaction_id']:
|
|
|
|
| 1185 |
|
| 1186 |
response = lake.chat(pandasai_prompt)
|
| 1187 |
|
| 1188 |
+
# --- Sanitize response so error text never reaches the user ---
|
| 1189 |
+
def _looks_like_error_text(s: str) -> bool:
|
| 1190 |
+
if not s:
|
| 1191 |
+
return False
|
| 1192 |
+
s_low = s.lower()
|
| 1193 |
+
return any(tok in s_low for tok in [
|
| 1194 |
+
"traceback",
|
| 1195 |
+
"no code found",
|
| 1196 |
+
"nocodefounderror",
|
| 1197 |
+
"maliciousqueryerror",
|
| 1198 |
+
"pandasai.exceptions",
|
| 1199 |
+
"pipeline failed",
|
| 1200 |
+
"error:"
|
| 1201 |
+
])
|
| 1202 |
+
|
| 1203 |
if isinstance(response, dict) and 'value' in response:
|
| 1204 |
+
text = str(response['value'])
|
| 1205 |
+
if _looks_like_error_text(text):
|
| 1206 |
+
raise RuntimeError("Unsafe PandasAI response text contained error markers")
|
| 1207 |
+
return text
|
| 1208 |
elif isinstance(response, str):
|
| 1209 |
+
if _looks_like_error_text(response):
|
| 1210 |
+
raise RuntimeError("Unsafe PandasAI response text contained error markers")
|
| 1211 |
return response
|
| 1212 |
else:
|
| 1213 |
+
text = str(response)
|
| 1214 |
+
if _looks_like_error_text(text):
|
| 1215 |
+
raise RuntimeError("Unsafe PandasAI response text contained error markers")
|
| 1216 |
+
return text
|
| 1217 |
|
| 1218 |
except Exception as e:
|
| 1219 |
# Unified handler: catch PandasAI edge-cases by CLASS NAME or MESSAGE,
|
|
|
|
| 1225 |
or "No code found" in err_msg
|
| 1226 |
or "NoCodeFoundError" in err_name
|
| 1227 |
or "MaliciousQueryError" in err_name
|
| 1228 |
+
or "pandasai.exceptions.NoCodeFoundError" in err_msg
|
| 1229 |
)
|
| 1230 |
|
| 1231 |
if is_pandasai_no_code:
|
|
|
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
+
|
| 1321 |
def _find_document_by_details(user_phone: str, collection_name: str, details: Dict) -> Optional[Any]:
|
| 1322 |
col_ref = db.collection("users").document(user_phone).collection(collection_name)
|
| 1323 |
if 'transaction_id' in details and details['transaction_id']:
|