SVashishta1
commited on
Commit
·
264c011
1
Parent(s):
98c1d89
Error Fix
Browse files- app.py +5 -1
- backend/main.py +8 -16
- backend/vector_db.py +23 -6
app.py
CHANGED
|
@@ -596,7 +596,11 @@ def flush_databases():
|
|
| 596 |
if success:
|
| 597 |
result.append("✅ ChromaDB cleared successfully")
|
| 598 |
else:
|
| 599 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
except Exception as e:
|
| 601 |
result.append(f"❌ Error clearing ChromaDB: {str(e)}")
|
| 602 |
|
|
|
|
| 596 |
if success:
|
| 597 |
result.append("✅ ChromaDB cleared successfully")
|
| 598 |
else:
|
| 599 |
+
# Even if reset fails, we can still reinitialize the document assistant
|
| 600 |
+
# This is a workaround that creates a fresh instance
|
| 601 |
+
global document_assistant
|
| 602 |
+
document_assistant = DocumentAssistant()
|
| 603 |
+
result.append("⚠️ ChromaDB reset partially completed - created new instance")
|
| 604 |
except Exception as e:
|
| 605 |
result.append(f"❌ Error clearing ChromaDB: {str(e)}")
|
| 606 |
|
backend/main.py
CHANGED
|
@@ -66,26 +66,18 @@ class DocumentAssistant:
|
|
| 66 |
try:
|
| 67 |
# Reset the vector database
|
| 68 |
if hasattr(self, 'vector_db') and self.vector_db is not None:
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Delete the collection if it exists
|
| 73 |
-
try:
|
| 74 |
-
client.delete_collection("documents")
|
| 75 |
-
print("Collection 'documents' deleted successfully")
|
| 76 |
-
except Exception as e:
|
| 77 |
-
print(f"Error deleting collection: {str(e)}")
|
| 78 |
-
|
| 79 |
-
# Recreate the collection
|
| 80 |
-
self.vector_db.collection = client.get_or_create_collection("documents")
|
| 81 |
-
print("Collection 'documents' recreated successfully")
|
| 82 |
|
| 83 |
# Also clear the SimpleDB
|
| 84 |
if hasattr(self, 'db') and self.db is not None:
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
return
|
| 89 |
else:
|
| 90 |
print("Vector database not initialized")
|
| 91 |
return False
|
|
|
|
| 66 |
try:
|
| 67 |
# Reset the vector database
|
| 68 |
if hasattr(self, 'vector_db') and self.vector_db is not None:
|
| 69 |
+
# Try to reset the collection
|
| 70 |
+
success = self.vector_db.reset_collection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Also clear the SimpleDB
|
| 73 |
if hasattr(self, 'db') and self.db is not None:
|
| 74 |
+
try:
|
| 75 |
+
self.db.clear_all()
|
| 76 |
+
print("SimpleDB cleared successfully")
|
| 77 |
+
except Exception as db_error:
|
| 78 |
+
print(f"Error clearing SimpleDB: {str(db_error)}")
|
| 79 |
|
| 80 |
+
return success
|
| 81 |
else:
|
| 82 |
print("Vector database not initialized")
|
| 83 |
return False
|
backend/vector_db.py
CHANGED
|
@@ -52,13 +52,30 @@ class ChromaVectorDB:
|
|
| 52 |
self.collection.delete(ids=results['ids'])
|
| 53 |
|
| 54 |
def reset_collection(self):
|
| 55 |
-
"""Reset the collection by
|
| 56 |
try:
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
print(f"Error resetting collection: {str(e)}")
|
| 64 |
return False
|
|
|
|
| 52 |
self.collection.delete(ids=results['ids'])
|
| 53 |
|
| 54 |
def reset_collection(self):
|
| 55 |
+
"""Reset the collection by clearing all documents"""
|
| 56 |
try:
|
| 57 |
+
# Get all document IDs
|
| 58 |
+
try:
|
| 59 |
+
all_ids = self.collection.get()["ids"]
|
| 60 |
+
if all_ids:
|
| 61 |
+
# Delete all documents
|
| 62 |
+
self.collection.delete(ids=all_ids)
|
| 63 |
+
print(f"Deleted {len(all_ids)} documents from collection")
|
| 64 |
+
else:
|
| 65 |
+
print("Collection is already empty")
|
| 66 |
+
return True
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error getting or deleting documents: {str(e)}")
|
| 69 |
+
|
| 70 |
+
# Try recreating the collection as a fallback
|
| 71 |
+
try:
|
| 72 |
+
self.client.delete_collection("documents")
|
| 73 |
+
self.collection = self.client.get_or_create_collection("documents")
|
| 74 |
+
print("Collection recreated successfully")
|
| 75 |
+
return True
|
| 76 |
+
except Exception as e2:
|
| 77 |
+
print(f"Error recreating collection: {str(e2)}")
|
| 78 |
+
return False
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error resetting collection: {str(e)}")
|
| 81 |
return False
|