Spaces:
Running
Running
Nguyễn Quốc Vỹ commited on
Commit ·
4f1a142
1
Parent(s): d33e9a6
Fix: Clear RAG source cache when admin uploads/updates/reindexes documents
Browse files- Add _clear_rag_cache() function to reset ChromaDB source cache
- Call cache clear in create_system_doc() after upload
- Call cache clear in update_system_doc() after modification
- Call cache clear in reindex_all() after reindexing
- Ensures new documents are searchable immediately without cache stale issues
- backend/admin_services.py +18 -0
backend/admin_services.py
CHANGED
|
@@ -21,6 +21,18 @@ from data_processing.dynamic_indexing import add_pdf_file
|
|
| 21 |
from data_processing.indexing import delete_chunks_by_source
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# ======================== UserService ========================
|
| 25 |
|
| 26 |
def list_users(limit: int = 500) -> list:
|
|
@@ -114,6 +126,8 @@ def create_system_doc(file_path: str, filename: str = None) -> tuple[bool, str]:
|
|
| 114 |
# Index ngay sau khi upload để hỏi đáp dùng được luôn.
|
| 115 |
chunks_added = add_pdf_file(dest)
|
| 116 |
schedule_pdf_upload(dest, filename)
|
|
|
|
|
|
|
| 117 |
return True, f"Đã thêm tài liệu và index {chunks_added} chunks."
|
| 118 |
except Exception as e:
|
| 119 |
return False, str(e)
|
|
@@ -145,6 +159,8 @@ def update_system_doc(ma_tai_lieu: str, file_path: str = None, ten_file: str = N
|
|
| 145 |
else:
|
| 146 |
msg = "Đã cập nhật metadata tài liệu."
|
| 147 |
schedule_pdf_upload(old_path, new_name)
|
|
|
|
|
|
|
| 148 |
return True, msg
|
| 149 |
|
| 150 |
|
|
@@ -206,6 +222,8 @@ def reindex_all() -> tuple[bool, str]:
|
|
| 206 |
skipped_docs += 1
|
| 207 |
|
| 208 |
schedule_vector_sync()
|
|
|
|
|
|
|
| 209 |
return True, (
|
| 210 |
f"Đồng bộ xong: thêm mới {indexed_docs} tài liệu / {indexed_chunks} chunks, "
|
| 211 |
f"bỏ qua {skipped_docs} tài liệu đã có."
|
|
|
|
| 21 |
from data_processing.indexing import delete_chunks_by_source
|
| 22 |
|
| 23 |
|
| 24 |
+
# ======================== RAG Cache Reset ========================
|
| 25 |
+
|
| 26 |
+
def _clear_rag_cache():
|
| 27 |
+
"""Reset cache RAG để cập nhật danh sách tài liệu mới sau khi thêm/sửa/xóa."""
|
| 28 |
+
try:
|
| 29 |
+
from backend import rag_chain_pg
|
| 30 |
+
rag_chain_pg._source_cache = None
|
| 31 |
+
print("[Admin] ✅ Đã clear RAG source cache")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"[Admin] ⚠️ Lỗi clear cache: {e}")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
# ======================== UserService ========================
|
| 37 |
|
| 38 |
def list_users(limit: int = 500) -> list:
|
|
|
|
| 126 |
# Index ngay sau khi upload để hỏi đáp dùng được luôn.
|
| 127 |
chunks_added = add_pdf_file(dest)
|
| 128 |
schedule_pdf_upload(dest, filename)
|
| 129 |
+
# Clear cache để tài liệu mới có thể được search ngay lập tức
|
| 130 |
+
_clear_rag_cache()
|
| 131 |
return True, f"Đã thêm tài liệu và index {chunks_added} chunks."
|
| 132 |
except Exception as e:
|
| 133 |
return False, str(e)
|
|
|
|
| 159 |
else:
|
| 160 |
msg = "Đã cập nhật metadata tài liệu."
|
| 161 |
schedule_pdf_upload(old_path, new_name)
|
| 162 |
+
# Clear cache để cập nhật được phản ánh ngay
|
| 163 |
+
_clear_rag_cache()
|
| 164 |
return True, msg
|
| 165 |
|
| 166 |
|
|
|
|
| 222 |
skipped_docs += 1
|
| 223 |
|
| 224 |
schedule_vector_sync()
|
| 225 |
+
# Clear cache sau khi reindex để search ngay được tài liệu mới
|
| 226 |
+
_clear_rag_cache()
|
| 227 |
return True, (
|
| 228 |
f"Đồng bộ xong: thêm mới {indexed_docs} tài liệu / {indexed_chunks} chunks, "
|
| 229 |
f"bỏ qua {skipped_docs} tài liệu đã có."
|