Nguyễn Quốc Vỹ commited on
Commit
0700453
·
1 Parent(s): 47738d8

Tối ưu đồng bộ chỉ mục: chỉ index tài liệu mới, bỏ reindex toàn bộ

Browse files
Files changed (2) hide show
  1. backend/admin_services.py +34 -13
  2. frontend/app.py +4 -3
backend/admin_services.py CHANGED
@@ -173,22 +173,43 @@ def reindex_doc(ma_tai_lieu: str) -> tuple[bool, str]:
173
 
174
 
175
  def reindex_all() -> tuple[bool, str]:
176
- """Tái lập chỉ mục Vector DB: chạy pipeline đọc PDF → chunk → ChromaDB."""
 
 
 
 
 
177
  try:
178
- from data_processing.loader import load_all_documents
179
- from data_processing.chunking import chunk_documents
180
- from data_processing.indexing import create_vector_database, delete_collection
181
- documents = load_all_documents(PDF_DIR)
182
- if not documents:
183
  return False, f"Không có tài liệu PDF trong {PDF_DIR}"
184
- chunks = chunk_documents(documents)
185
- try:
186
- delete_collection()
187
- except Exception:
188
- pass
189
- create_vector_database(chunks)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  schedule_vector_sync()
191
- return True, f"Đã tái lập chỉ mục: {len(documents)} tài liệu, {len(chunks)} chunks."
 
 
 
192
  except Exception as e:
193
  return False, f"Lỗi: {str(e)}"
194
 
 
173
 
174
 
175
  def reindex_all() -> tuple[bool, str]:
176
+ """
177
+ Đồng bộ chỉ mục theo kiểu tăng dần (incremental):
178
+ - Quét thư mục PDF runtime
179
+ - Chỉ index tài liệu CHƯA có trong ChromaDB
180
+ Tránh reindex toàn bộ gây chậm khi số tài liệu lớn.
181
+ """
182
  try:
183
+ from data_processing.dynamic_indexing import add_pdf_file
184
+
185
+ if not os.path.isdir(PDF_DIR):
 
 
186
  return False, f"Không có tài liệu PDF trong {PDF_DIR}"
187
+
188
+ pdf_files = [
189
+ os.path.join(PDF_DIR, f)
190
+ for f in sorted(os.listdir(PDF_DIR))
191
+ if f.lower().endswith(".pdf")
192
+ ]
193
+ if not pdf_files:
194
+ return False, f"Không có tài liệu PDF trong {PDF_DIR}"
195
+
196
+ indexed_docs = 0
197
+ indexed_chunks = 0
198
+ skipped_docs = 0
199
+
200
+ for pdf_path in pdf_files:
201
+ added = add_pdf_file(pdf_path)
202
+ if added > 0:
203
+ indexed_docs += 1
204
+ indexed_chunks += added
205
+ else:
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ó."
212
+ )
213
  except Exception as e:
214
  return False, f"Lỗi: {str(e)}"
215
 
frontend/app.py CHANGED
@@ -1011,15 +1011,16 @@ def show_admin_page():
1011
  st.error(msg)
1012
 
1013
  with tab5:
1014
- st.subheader("Thống kê RAG & Tái lập chỉ mục")
1015
  stats = get_rag_stats()
1016
  if stats.get("error"):
1017
  st.warning(stats["error"])
1018
  else:
1019
  st.metric("Tổng số chunks", stats.get("total_chunks", 0))
1020
  st.caption(f"Collection: {stats.get('collection_name')} | Thư mục: {stats.get('persist_dir', '')}")
1021
- if st.button("Tái lập chỉ mục Vector DB (ChromaDB)", type="primary"):
1022
- with st.spinner("Đang đọc PDF, chia đoạn index..."):
 
1023
  ok, msg = reindex_all()
1024
  if ok:
1025
  st.success(msg)
 
1011
  st.error(msg)
1012
 
1013
  with tab5:
1014
+ st.subheader("Thống kê RAG & Đồng bộ chỉ mục")
1015
  stats = get_rag_stats()
1016
  if stats.get("error"):
1017
  st.warning(stats["error"])
1018
  else:
1019
  st.metric("Tổng số chunks", stats.get("total_chunks", 0))
1020
  st.caption(f"Collection: {stats.get('collection_name')} | Thư mục: {stats.get('persist_dir', '')}")
1021
+ st.caption("Nút bên dưới chỉ index tài liệu mới, bỏ qua tài liệu/chunks đã có để chạy nhanh hơn.")
1022
+ if st.button("Đồng bộ chỉ mục (chỉ tài liệu mới)", type="primary"):
1023
+ with st.spinner("Đang đồng bộ tài liệu mới vào ChromaDB..."):
1024
  ok, msg = reindex_all()
1025
  if ok:
1026
  st.success(msg)