Spaces:
Sleeping
Sleeping
Update app/policy_vector_db.py
Browse files- app/policy_vector_db.py +6 -3
app/policy_vector_db.py
CHANGED
|
@@ -42,7 +42,7 @@ class PolicyVectorDB:
|
|
| 42 |
except Exception as e:
|
| 43 |
logger.warning(f"Could not retrieve existing IDs from ChromaDB: {e}. Assuming no existing IDs for now.")
|
| 44 |
|
| 45 |
-
new_chunks = [chunk for chunk in chunks if chunk.get('id') and chunk
|
| 46 |
|
| 47 |
if not new_chunks:
|
| 48 |
logger.info("No new chunks to add.")
|
|
@@ -53,7 +53,8 @@ class PolicyVectorDB:
|
|
| 53 |
for i in range(0, len(new_chunks), batch_size):
|
| 54 |
batch = new_chunks[i:i + batch_size]
|
| 55 |
texts = [chunk['text'] for chunk in batch]
|
| 56 |
-
|
|
|
|
| 57 |
metadatas = [self._flatten_metadata(chunk['metadata']) if chunk.get('metadata') else {} for chunk in batch]
|
| 58 |
embeddings = self.embedding_model.encode(texts, show_progress_bar=False).tolist()
|
| 59 |
collection.add(ids=ids, embeddings=embeddings, documents=texts, metadatas=metadatas)
|
|
@@ -89,8 +90,10 @@ def ensure_db_populated(db_instance: PolicyVectorDB, chunks_file_path: str):
|
|
| 89 |
logger.error(f"Chunks file not found at {chunks_file_path}. Cannot populate DB.")
|
| 90 |
return False
|
| 91 |
|
|
|
|
| 92 |
with open(chunks_file_path, 'r', encoding='utf-8') as f:
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
if not chunks_to_add:
|
| 96 |
logger.warning(f"Chunks file at {chunks_file_path} is empty. No data to add to DB.")
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
logger.warning(f"Could not retrieve existing IDs from ChromaDB: {e}. Assuming no existing IDs for now.")
|
| 44 |
|
| 45 |
+
new_chunks = [chunk for chunk in chunks if chunk.get('id') and str(chunk.get('id')) not in existing_ids]
|
| 46 |
|
| 47 |
if not new_chunks:
|
| 48 |
logger.info("No new chunks to add.")
|
|
|
|
| 53 |
for i in range(0, len(new_chunks), batch_size):
|
| 54 |
batch = new_chunks[i:i + batch_size]
|
| 55 |
texts = [chunk['text'] for chunk in batch]
|
| 56 |
+
# --- FIX: Ensure all IDs are strings before adding to ChromaDB ---
|
| 57 |
+
ids = [str(chunk['id']) for chunk in batch]
|
| 58 |
metadatas = [self._flatten_metadata(chunk['metadata']) if chunk.get('metadata') else {} for chunk in batch]
|
| 59 |
embeddings = self.embedding_model.encode(texts, show_progress_bar=False).tolist()
|
| 60 |
collection.add(ids=ids, embeddings=embeddings, documents=texts, metadatas=metadatas)
|
|
|
|
| 90 |
logger.error(f"Chunks file not found at {chunks_file_path}. Cannot populate DB.")
|
| 91 |
return False
|
| 92 |
|
| 93 |
+
chunks_to_add = []
|
| 94 |
with open(chunks_file_path, 'r', encoding='utf-8') as f:
|
| 95 |
+
for line in f:
|
| 96 |
+
chunks_to_add.append(json.loads(line))
|
| 97 |
|
| 98 |
if not chunks_to_add:
|
| 99 |
logger.warning(f"Chunks file at {chunks_file_path} is empty. No data to add to DB.")
|