Spaces:
Sleeping
Sleeping
Upload chatbot.py
Browse files- chatbot.py +30 -36
chatbot.py
CHANGED
|
@@ -221,57 +221,51 @@ YOUR RESPONSE:"""
|
|
| 221 |
else:
|
| 222 |
self.sql_validator.set_allowed_tables(schema.table_names)
|
| 223 |
|
| 224 |
-
# Check for memory commands
|
| 225 |
-
# Check for memory commands
|
| 226 |
# Check for memory commands using regex for flexibility
|
| 227 |
import re
|
| 228 |
-
|
| 229 |
-
|
|
|
|
| 230 |
|
| 231 |
-
#
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
)
|
| 236 |
|
| 237 |
if is_command and memory:
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
# If
|
| 241 |
if content_to_save:
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
if success:
|
| 245 |
return ChatResponse(answer=f"💾 I've saved to your permanent memory: '{content_to_save}'", query_type="memory")
|
| 246 |
else:
|
| 247 |
-
return ChatResponse(answer="❌ Failed to save to permanent memory
|
| 248 |
|
| 249 |
# If no content (e.g. "Save this"), save the previous conversation turn
|
| 250 |
elif len(memory.messages) >= 2:
|
| 251 |
-
#
|
| 252 |
-
|
| 253 |
-
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
if len(memory.messages) >= 3:
|
| 260 |
-
msg_user = memory.messages[-3]
|
| 261 |
-
msg_ai = memory.messages[-2]
|
| 262 |
-
|
| 263 |
-
# Verify roles to ensure we are saving a Q&A pair
|
| 264 |
-
if msg_user.role == "user" and msg_ai.role == "assistant":
|
| 265 |
-
msgs_to_save = [msg_user, msg_ai]
|
| 266 |
-
|
| 267 |
-
if msgs_to_save:
|
| 268 |
-
# Format: "User: ... | Assistant: ..."
|
| 269 |
-
context_str = f"User: {msgs_to_save[0].content} | Assistant: {msgs_to_save[1].content}"
|
| 270 |
-
success = memory.save_permanent_context(context_str)
|
| 271 |
-
if success:
|
| 272 |
return ChatResponse(answer="💾 I've saved our last exchange to your permanent memory.", query_type="memory")
|
| 273 |
else:
|
| 274 |
-
return ChatResponse(answer="❌ Failed to save to permanent memory
|
| 275 |
else:
|
| 276 |
return ChatResponse(answer="⚠️ I couldn't find a clear previous exchange to save. Try saying 'Remember that [fact]'.", query_type="memory")
|
| 277 |
else:
|
|
|
|
| 221 |
else:
|
| 222 |
self.sql_validator.set_allowed_tables(schema.table_names)
|
| 223 |
|
|
|
|
|
|
|
| 224 |
# Check for memory commands using regex for flexibility
|
| 225 |
import re
|
| 226 |
+
# This regex captures patterns like "save this", "remember that my size is 7", "please memorize my name"
|
| 227 |
+
save_pattern = re.compile(r"(?:please\s+)?(?:save|remember|memorize|record|store)\s+(?:this|that|to\s+(?:main\s+)?memory)?\s*(?:that)?\s*:?\s*(.*)", re.IGNORECASE)
|
| 228 |
+
match = save_pattern.search(query.strip())
|
| 229 |
|
| 230 |
+
# Additional check for colloquial "save to memory" or "memory: X" phrasings
|
| 231 |
+
is_memory_phrase = any(phrase in query.lower() for phrase in ["save to memory", "remember this", "memorize this", "save my", "remember my"])
|
| 232 |
+
|
| 233 |
+
is_command = bool(match) or is_memory_phrase
|
|
|
|
| 234 |
|
| 235 |
if is_command and memory:
|
| 236 |
+
# Prioritize explicit content from the regex match
|
| 237 |
+
content_to_save = match.group(1).strip() if (match and match.group(1)) else ""
|
| 238 |
+
|
| 239 |
+
# Special case enhancement
|
| 240 |
+
if not content_to_save:
|
| 241 |
+
# Try to extract content if regex was too strict but is_memory_phrase matched
|
| 242 |
+
# e.g. "my shoe size is 7, save to memory"
|
| 243 |
+
if "save" in query.lower():
|
| 244 |
+
content_to_save = query.lower().split("save")[0].strip().strip(",").strip()
|
| 245 |
+
elif "remember" in query.lower():
|
| 246 |
+
content_to_save = query.lower().split("remember")[1].strip()
|
| 247 |
|
| 248 |
+
# If we have content, save it
|
| 249 |
if content_to_save:
|
| 250 |
+
is_ok, msg = memory.save_permanent_context(content_to_save)
|
| 251 |
+
if is_ok:
|
|
|
|
| 252 |
return ChatResponse(answer=f"💾 I've saved to your permanent memory: '{content_to_save}'", query_type="memory")
|
| 253 |
else:
|
| 254 |
+
return ChatResponse(answer=f"❌ Failed to save to permanent memory: {msg}", query_type="memory")
|
| 255 |
|
| 256 |
# If no content (e.g. "Save this"), save the previous conversation turn
|
| 257 |
elif len(memory.messages) >= 2:
|
| 258 |
+
# We try to grab the last Assistant Response
|
| 259 |
+
last_ai_msg = next((m for m in reversed(memory.messages[:-1]) if m.role == "assistant"), None)
|
| 260 |
+
last_user_msg = next((m for m in reversed(memory.messages[:-1]) if m.role == "user"), None)
|
| 261 |
|
| 262 |
+
if last_ai_msg and last_user_msg:
|
| 263 |
+
context_str = f"User: {last_user_msg.content} | AI: {last_ai_msg.content}"
|
| 264 |
+
is_ok, msg = memory.save_permanent_context(context_str)
|
| 265 |
+
if is_ok:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
return ChatResponse(answer="💾 I've saved our last exchange to your permanent memory.", query_type="memory")
|
| 267 |
else:
|
| 268 |
+
return ChatResponse(answer=f"❌ Failed to save to permanent memory: {msg}", query_type="memory")
|
| 269 |
else:
|
| 270 |
return ChatResponse(answer="⚠️ I couldn't find a clear previous exchange to save. Try saying 'Remember that [fact]'.", query_type="memory")
|
| 271 |
else:
|