Spaces:
Runtime error
Runtime error
Julien Simon Claude Opus 4.5 commited on
Commit Β·
35ad3d5
1
Parent(s): 1f6e10e
fix: Batch document additions to avoid ChromaDB size limit
Browse filesChromaDB has a max batch size of 5461 documents. When adding large
numbers of chunks (e.g., 59336), the upsert fails. Added batching
function to split documents into chunks of 5000.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- vectorstore.py +17 -2
vectorstore.py
CHANGED
|
@@ -116,6 +116,21 @@ def handle_existing_vectorstore(embeddings):
|
|
| 116 |
return vectorstore
|
| 117 |
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
def update_vectorstore(vectorstore, new_pdfs, processed_files):
|
| 120 |
"""Update existing vectorstore with new documents.
|
| 121 |
|
|
@@ -133,8 +148,8 @@ def update_vectorstore(vectorstore, new_pdfs, processed_files):
|
|
| 133 |
|
| 134 |
filtered_chunks = process_documents(new_documents, get_text_splitter())
|
| 135 |
if filtered_chunks:
|
| 136 |
-
print("Adding new
|
| 137 |
-
|
| 138 |
print("Database updated successfully!")
|
| 139 |
|
| 140 |
|
|
|
|
| 116 |
return vectorstore
|
| 117 |
|
| 118 |
|
| 119 |
+
def add_documents_in_batches(vectorstore, documents, batch_size=5000):
|
| 120 |
+
"""Add documents to vectorstore in batches to avoid exceeding ChromaDB limits.
|
| 121 |
+
|
| 122 |
+
Args:
|
| 123 |
+
vectorstore: Chroma vectorstore instance
|
| 124 |
+
documents: List of documents to add
|
| 125 |
+
batch_size: Maximum documents per batch (ChromaDB limit is 5461)
|
| 126 |
+
"""
|
| 127 |
+
total = len(documents)
|
| 128 |
+
for i in range(0, total, batch_size):
|
| 129 |
+
batch = documents[i : i + batch_size]
|
| 130 |
+
print(f"Adding batch {i // batch_size + 1}/{(total + batch_size - 1) // batch_size} ({len(batch)} documents)...")
|
| 131 |
+
vectorstore.add_documents(batch)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
def update_vectorstore(vectorstore, new_pdfs, processed_files):
|
| 135 |
"""Update existing vectorstore with new documents.
|
| 136 |
|
|
|
|
| 148 |
|
| 149 |
filtered_chunks = process_documents(new_documents, get_text_splitter())
|
| 150 |
if filtered_chunks:
|
| 151 |
+
print(f"Adding {len(filtered_chunks)} new document chunks to existing database...")
|
| 152 |
+
add_documents_in_batches(vectorstore, filtered_chunks)
|
| 153 |
print("Database updated successfully!")
|
| 154 |
|
| 155 |
|